⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileintf_js.cpp

📁 电信的97接口程序,用于把话单入库。这里是采用FTP方式采集话单
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//////////////////////////////////////////////////////////////////
// History:
//	2001.10.24	Start it. (shencan@263.net)
// History:
//  2001.11.07    
//  add the directory of Ftp getting. (zoohoo@163.com)
//	add the code for ignoring error of ftp.(try{}catch(...)...)
// History:
//	2002.05.20
//	Unit the file to the Interface97. (zoohoo@163.com)
//////////////////////////////////////////////////////////////////

#if (_MSC_VER >= 1200)  
#pragma warning( disable : 4800 ) // one performance warning off
#pragma warning( disable : 4786 ) // warning about too long debug symbol off
#endif

#include "gk.h"
#include "fstream.h"
#include "fileIntf_JS.h"

#ifdef ACE_WIN32
#include <process.h>
#else
#include <unistd.h>
#endif /* ACE_WIN32 */

#define FTP_DISCONNECT_TIME 60000 //ms
SOCKET vRxSock[20];
int SaveDB(UserInterface * userInterface);
int GetValueFromShareDir();
void Code2Msg(int retCode, char * msg);
CFileIntf_ChD  fileIntf_ChD;
CSheet_ChD sheet_ChD;

int GetValueFromFTP_JS()
{
	RTRACE(2, "Begin ftp loop");
	if ( CGlobal::Instance()->m_isShutdown == 1 )
		return -1;
	
	PFTPClient ftp;
    PTimeInterval   timeInterval(FTP_DISCONNECT_TIME);
	CFileIntf_JS fileIntf;
	fstream fdSuccess, fdFailure;
	PString	filePrefix;
	int j = 0;
    bool    canDelete = true;
	
	RTRACE(2, "FtpSrv NUMBER: " << CGlobal::Instance()->m_ftpNum);
    ftp.SetReadTimeout(timeInterval);
    ftp.SetWriteTimeout(timeInterval);
	
	for(int i=0;i<CGlobal::Instance()->m_ftpNum;i++)
	{
		try
		{
			if ( ftp.Connect(PIPSocket::Address(CGlobal::Instance()->m_negGkList[i].m_ip), 
				CGlobal::Instance()->m_negGkList[i].m_port) == false )
			{
				RTRACE(2, "Connect fail");
				continue;
				//return 0;
			}
			
			if ( ftp.LogIn(CGlobal::Instance()->m_username[i], CGlobal::Instance()->m_password[i]) == false )
			{
				RTRACE(2, "Login fail");
				continue;
				//return 0;
			}
			else
				RTRACE(4, 
				CGlobal::Instance()->m_username[i] << ":" <<
				CGlobal::Instance()->m_password[i] <<
				" login successfully");
			
			
			if( !ftp.ChangeDirectory(CGlobal::Instance()->m_getFilePath))
			{
				RTRACE(2, "Can't change to the directory " 
					<< CGlobal::Instance()->m_getFilePath );
				continue;
			}
			else
				RTRACE(4, "Change to the directory "
				<< CGlobal::Instance()->m_getFilePath );
			
			PStringArray fileArray = ftp.GetDirectoryNames(PFTP::ShortNames);	//, PFTP::NormalPort);
			RTRACE(5, "Files: " << fileArray);
			
			// treat those files.
			for ( j=0; j<fileArray.GetSize(); j++) {
				// begin to get every file, and save it to local disk
				
				filePrefix = fileArray[j].Mid(0,2);
				if (CGlobal::Instance()->m_areacode == 4) //JangShu 97
				{
					if (strcmp(filePrefix, "SM") != 0 )
						continue;
				} //过滤非工单的文件
				
				PTCPSocket * pSock;
				pSock = ftp.GetFile(fileArray[j]);		//, PFTP::NormalPort);
				if ( pSock == NULL ) 
				{
					RTRACE(2, "Read file: " << fileArray[j] << " fail!");
					continue;
				}
				
				PString str;
				int count = 0;
				
				while(pSock->Read(str.GetPointer(count+1000)+count, 1000))
					count += pSock->GetLastReadCount();
				str.SetSize(count+1);
				
				delete pSock;
				RTRACE(5, "Response:" << ftp.ReadResponse());
				
				/*PFile save;
				PString pfileName;
				pfileName = CGlobal::Instance()->m_localDir;
				pfileName += "\\";
				pfileName += fileArray[j];
				RTRACE(5, pfileName);
				save.Open(pfileName, PFile::WriteOnly);
				save.WriteString(str);
				save.Close();*/  //hide by zjl 2002-11-4
				
                //back up ftp files  add by zjl 2002-11-04 for JiangShu
				if( !ftp.ChangeDirectory(CGlobal::Instance()->m_backupFilePath))
				{
					RTRACE(2, "Can't change to the directory " 
						<< CGlobal::Instance()->m_backupFilePath );
					continue;
				}
				else
					RTRACE(4, "Change to the ftp backup file directory "
					<< CGlobal::Instance()->m_backupFilePath );
				
                pSock = ftp.PutFile(fileArray[j]);
                if (pSock == NULL)
                    RTRACE(2, "PutFile failed, file nane: " << fileArray[j]);
                else
                {
					count = 0;
					pSock->Write((const char *)str, str.GetLength());
					
					//					while(pSock->Write(sFailure.GetPointer(count+1000)+count, 1000))
					//						count += pSock->GetLastWriteCount();
					//					sFailure.SetSize(count+1);
					
					delete pSock;
					RTRACE(5, "Response:" << ftp.ReadResponse());
					RTRACE(5, "Put back ftp file successfully");
                }
				
				RTRACE(4, "Read size:" << count+1);
				RTRACE(6, ", context:\n" << str);
				
                ftp.ChangeDirectory("..");
                RTRACE(5, "return ftp root directory");
                if( !ftp.ChangeDirectory(CGlobal::Instance()->m_getFilePath))
				{
					RTRACE(2, "Can't change to the directory " 
						<< CGlobal::Instance()->m_getFilePath );
					continue;
				}
				else
					RTRACE(4, "Change to the directory "
					<< CGlobal::Instance()->m_getFilePath );
				
				
				// open temporary file for success & failure
				PString fSuccess = fileArray[j];
				PString fFailure = fileArray[j];
				fileIntf.SetBackFileName(&fSuccess, "_s");
				fileIntf.SetBackFileName(&fFailure, "_f");
				
				//				fSuccess = CGlobal::Instance()->m_localDir + fSuccess;
				//				fFailure = CGlobal::Instance()->m_localDir + fFailure;
				
				RTRACE(4, "write the file " << fSuccess);
				RTRACE(4, "write the file " << fFailure);
				
				// open back file
				fdSuccess.open(CGlobal::Instance()->m_localDir 
					+ fSuccess, ios::out);
				if(fdSuccess.bad() || !fdSuccess.good())
				{
					RTRACE(2, "Can't open file " << fSuccess);
					continue;
				}
				
				// open back file
				fdFailure.open(CGlobal::Instance()->m_localDir 
					+ fFailure, ios::out);
				if(fdFailure.bad() || !fdFailure.good())
				{
					RTRACE(2, "Can't open file " << fSuccess);
					fdSuccess.close();
					continue;
				}
				
				// get the line from sheet file
				PStringArray line = str.Lines();
				PString sSuccess="", sFailure="";
				int iSuccessNum, iFailNum;
				
				if (strcmp(filePrefix, "SM") == 0) //正常工单
				{
					fileIntf.userInterface.isBackSheet = 0;
					iSuccessNum = 0;
					iFailNum = 0;
                    int iEndNumber = 0;
                    iEndNumber = line.GetSize() - 1;
					for(int k=0;k<line.GetSize();k++)
					{
					/*if ( k == 0 )
					{
					char	buf1[61], buf2[61];
					char *	pDest;
					::sscanf(line[k], "%s%s", buf1, buf2);
					pDest = strstr(buf2, "-");
					*pDest = 0;
					strcat(buf2, pDest+1);
					strncpy(fileIntf.sheet.sendDate, buf2, sizeof(fileIntf.sheet.sendDate)-1);
					continue;
					}*/
						// fetch the value to classs UserInterface
						if(line[k].Trim() == "")
							continue;
							/*if (k == line.GetSize() -1 )//过滤最后一行的汉字
							{
							continue;
					}*/
						
                        canDelete = true;
                        if (k == 0)
                        {
                            if (!strncmp(line[k], "Started at:", 11))
                            {
                                if (strncmp(line[iEndNumber], "end of file", 11))
                                {
                                    --iEndNumber;
                                    if (( iEndNumber < 0 ) || strncmp(line[iEndNumber], "end of file", 11))
                                    {
                                        canDelete = false;
                                        RTRACE(2, "File has not 100% upload ,\
                                            or file format is not valid, discard it. filename = " << fileArray[j]);
                                        sFailure = "File has not 100% upload, or file format is not valid";
                                        break;
                                    }
                                    else
                                        continue;
                                } 
                                else
                                    continue;
                            }
                        }
						
                        if (k == iEndNumber)
                        {
                            if (!strncmp(line[k], "end of file", 11))
                                //continue;
                                break;
                        }
						
						fileIntf.GetValueFromLine(line[k]);
						
						if (fileIntf.SheetToUserInterface() != 0) //该行数据不合法
						{
							sFailure = sFailure + line[k];
							sFailure = sFailure + "\tretCode= line inavlid\n";
							RTRACE(2, "Get sheet item failed! line invalid. Order = " << ++iFailNum );
							RTRACE(5, line[k]);
							continue;
						}
						
						// operate database
						int iRes = SaveDB(&fileIntf.userInterface);
						if ( iRes == 0 )
						{
							// write the right feeback to temp file
							sSuccess = sSuccess + line[k];
							sSuccess = sSuccess + "\n";
							RTRACE(2, "Get Sheet item successfully! Order = " << ++iSuccessNum);
							RTRACE(5, line[k]);
						}
						else
						{
							// write the wrong feeback to temp file
							char temp[4], msg[80];
							sprintf(temp, "%d", iRes);
                            Code2Msg(iRes, msg);
							sFailure = sFailure + line[k];
							sFailure = sFailure + "\tretCode=" + (PString)temp + (PString)msg + "\n";
							RTRACE(2, "Get sheet item failed!Order = " << ++iFailNum << "\tretCode = " 
                                << iRes << msg);
							RTRACE(5, line[k]);
						}
					} //end of read file for loop
				}
				
				// write file
				char temp[6];
				RTRACE(6, "Write to success file " << sSuccess);
				sprintf(temp, "%d", iSuccessNum);
				sSuccess = sSuccess + "\n\nTotal process suceeded record number: " + (PString)temp;
				fdSuccess << sSuccess;
				RTRACE(6, "Write to failure file " << sFailure);
				sprintf(temp, "%d", iFailNum);
				sFailure = sFailure + "\n\nTotal process failed record number: " + (PString)temp;
				fdFailure << sFailure;
				fdSuccess.close();
				fdFailure.close();
				
				// delete this file.
                if (canDelete) //若应为ftp文件没有100unload,则跳过该文件(不删除)
                {
					if ( ftp.ExecuteCommand(PFTP::DELE, fileArray[j])/100 == 2 ) 
					{
						RTRACE(5, "Ftp Del success: " << fileArray[j]);
					}
					else 
					{
						RTRACE(2, "Ftp Del fail: " << fileArray[j]);
					}
                }                
				
				/*pSock = ftp.PutFile(fSuccess);
				if(pSock == NULL)
				RTRACE(2, "Put back success file failed");
				else
				{
				count = 0;
				pSock->Write((const char *)sSuccess, sSuccess.GetLength());
				//					while(pSock->Write(sSuccess.GetPointer(count+1000)+count, 1000))
				//						count += pSock->GetLastWriteCount();
				//					sSuccess.SetSize(count+1);
				
				  delete pSock;
				  RTRACE(5, "Response:" << ftp.ReadResponse());
				  
					RTRACE(5, "Put back file successfully");
					
					  //#ifdef ACE_WIN32
					  //					_unlink(fSuccess);
					  //#else
					  //					unlink(fSuccess);
					  //#endif
					  //					RTRACE(2, "Delete local file " << fSuccess);
					  
				}*/  //hide by zjl 2002-11-04
				
				// put the backfeed file
                if (iFailNum > 0)  //有出错信息才写出错文件
                {
					if( !ftp.ChangeDirectory(CGlobal::Instance()->m_putFilePath))
					{
						RTRACE(2, "Can't change to the directory " 
							<< CGlobal::Instance()->m_putFilePath );
						continue;
					}
					else
						RTRACE(4, "Change to the directory " 
						<< CGlobal::Instance()->m_putFilePath);
					
					pSock = ftp.PutFile(fFailure);
					if(pSock == NULL)
						RTRACE(2, "Put back failure file failed");
					else
					{
						count = 0;
						pSock->Write((const char *)sFailure, sFailure.GetLength());
						
						delete pSock;
						RTRACE(5, "Response:" << ftp.ReadResponse());
						RTRACE(5, "Put back failure file successfully");
					}
					
                    ftp.ChangeDirectory("..");
					if( !ftp.ChangeDirectory(CGlobal::Instance()->m_getFilePath))
					{
						RTRACE(2, "Can't change to the directory " 
							<< CGlobal::Instance()->m_getFilePath );
						continue;
					}
					else
						RTRACE(4, "Change to the directory "
						<< CGlobal::Instance()->m_getFilePath );
                }
				
			}
			ftp.Close();
		}
		catch(...)
		{
			RTRACE(2, "Connect to Ftp Server " << PIPSocket::Address(CGlobal::Instance()->m_negGkList[i].m_ip) << " error");
			RTRACE(2, "Unknown Error!");
		}
	}
	return 0;
}

// Realize interface97 through the option of shared direcotry
int GetValueFromShareDir_JS()
{
	CFileIntf_JS fileIntf;
	RTRACE(2, "Begin loop from getting files from share dir");
	if ( CGlobal::Instance()->m_isShutdown == 1 )
		return -1;
	
	RTRACE(2, "Share dir : " << CGlobal::Instance()->m_shareDir);
	PDirectory * direc = new PDirectory();
	if(!direc->Exists(CGlobal::Instance()->m_shareDir))
	{
		RTRACE(1, "Warning! The share diretory " << CGlobal::Instance()->m_shareDir
			<< " does not exist! ");
		delete direc;
		return 0;
	}
	
	/*
	if(CGlobal::Instance()->m_changeDir == 0)
	{
	RTRACE(3, "Change to " << CGlobal::Instance()->m_shareDir);
	direc->Change(CGlobal::Instance()->m_shareDir);
	CGlobal::Instance()->m_changeDir = 1;
	} 
	else
	{
	*/
	RTRACE(3, "Open files in shared directory");
	if(direc->Open())
	{
		if(!direc->IsSubDir())
		{
			RTRACE(5, direc->GetEntryName());
			fileIntf.SaveToFile(direc->GetEntryName());
		}
	}
	while(direc->Next())
	{
		if(!direc->IsSubDir())
		{
			RTRACE(5, direc->GetEntryName());
			fileIntf.SaveToFile(direc->GetEntryName());
		}
	}
	direc->Close();
	//	}
	delete direc;
	
	return 0;
	
}

///////////////////////////////////////////////////////
//Realize the class of CFileIntf
///////////////////////////////////////////////////////
int CFileIntf_JS::SaveToFile(PString fileName)
{
/*	PString newFile, oldFile;
PStringArray str;
int len;
char tmp[BUFSIZE];

  newFile = CGlobal::Instance()->m_localDir;
  len = newFile.GetLength();
  if(newFile[len-1] != '\\')
		newFile += "\\";
		newFile += fileName;
		
		  oldFile = CGlobal::Instance()->m_shareDir;
		  len = oldFile.GetLength();
		  if(oldFile[len-1] != '\\')
		  oldFile += "\\";
		  oldFile += fileName;
		  
			RTRACE(3, "Read from " << oldFile);
			RTRACE(3, "Write to " << newFile);
			
			  ::fstream fold(oldFile, ios::in  | ios::nocreate );
			  ::fstream fnew(newFile, ios::out | ios::app );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -