FtpWorker.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:9k
源码类别:

网格计算

开发平台:

Visual C++

  1. #include "stdAfx.h"
  2. #include "FtpWorker.h"
  3. CFtpWorker::CFtpWorker()
  4. {
  5. m_hMsgWnd = NULL;
  6. m_bIsConnected = FALSE;
  7. m_pFtpConnection = NULL;
  8. m_nConnectionTimeout = 10000;
  9. m_hEventKill = CreateEvent(NULL, FALSE, FALSE, NULL);
  10. m_poInternetSession = new CInternetSession("trfFtpWorker/1.0");
  11. m_poInternetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, m_nConnectionTimeout);
  12. m_poInternetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, m_nConnectionTimeout);
  13. m_poInternetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, m_nConnectionTimeout);
  14. }
  15. CFtpWorker::CFtpWorker(CFtpSite &oFtpSite)
  16. {
  17. m_hMsgWnd = NULL;
  18. m_oFptSite = oFtpSite;
  19. m_bIsConnected = FALSE;
  20. m_pFtpConnection = NULL;
  21. m_nConnectionTimeout = 10000;
  22. m_hEventKill = CreateEvent(NULL, FALSE, FALSE, NULL);
  23. m_poInternetSession = new CInternetSession("trfFtpWorker/1.0");
  24. m_poInternetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, m_nConnectionTimeout);
  25. m_poInternetSession->SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, m_nConnectionTimeout);
  26. m_poInternetSession->SetOption(INTERNET_OPTION_SEND_TIMEOUT, m_nConnectionTimeout);
  27. }
  28. CFtpWorker::~CFtpWorker()
  29. {
  30. CloseHandle(this->m_hEventKill);
  31. if(NULL != m_poInternetSession)
  32. {
  33. delete m_poInternetSession;
  34. }
  35. this->Disconnect();
  36. }
  37. BOOL CFtpWorker::IsConnected(void)
  38. {
  39. return m_bIsConnected;
  40. }
  41. CFtpSite &CFtpWorker::GetFtpSiteInfo(void) const
  42. {
  43. return (CFtpSite&)m_oFptSite;
  44. }
  45. void CFtpWorker::SetFtpSiteInfo(CFtpSite &oFtpSite)
  46. {
  47. this->m_oFptSite = oFtpSite;
  48. }
  49. BOOL CFtpWorker::Connect(void)
  50. {
  51. try
  52. {
  53. m_pFtpConnection = m_poInternetSession->GetFtpConnection(m_oFptSite.m_sHost, 
  54. m_oFptSite.m_sUser, m_oFptSite.m_sPassword, 
  55. m_oFptSite.m_nPort, m_oFptSite.m_bPASVMode);
  56. return (m_bIsConnected = (NULL != m_pFtpConnection));
  57. }
  58. catch (CInternetException *ex)
  59. {
  60. ex->Delete();
  61. m_pFtpConnection = NULL;
  62. m_bIsConnected = FALSE;
  63. return FALSE;
  64. }
  65. return FALSE;
  66. }
  67. void CFtpWorker::Disconnect(void)
  68. {
  69. if(NULL != m_pFtpConnection)
  70. {
  71. m_pFtpConnection->Close();
  72. delete m_pFtpConnection;
  73. m_pFtpConnection = NULL;
  74. m_bIsConnected = FALSE;
  75. }
  76. }
  77. BOOL CFtpWorker::GetCurrentDir(CString strDirName)
  78. {
  79. try
  80. {
  81. strDirName.Empty();
  82. return m_pFtpConnection->GetCurrentDirectory(strDirName);
  83. }
  84. catch (CInternetException *ex)
  85. {
  86. ex->Delete();
  87. return FALSE;
  88. }
  89. return FALSE;
  90. }
  91. BOOL CFtpWorker::ChangeCurrentDir(LPCSTR lpszToDir)
  92. {
  93. try
  94. {
  95. if(NULL == lpszToDir) return FALSE;
  96. return m_pFtpConnection->SetCurrentDirectory(lpszToDir);
  97. }
  98. catch (CInternetException *ex)
  99. {
  100. ex->Delete();
  101. return FALSE;
  102. }
  103. return FALSE;
  104. }
  105. BOOL CFtpWorker::GetCurSubDirectorys(CStringList &oSubDirNames, LPCSTR lpszSubDirMask)
  106. {
  107. CFtpFileFind oFtpFileFinder(this->m_pFtpConnection);
  108. try
  109. {
  110. CString strFindMask = (NULL == lpszSubDirMask) ? "*" : lpszSubDirMask;
  111. BOOL bContinue = oFtpFileFinder.FindFile(strFindMask.GetBuffer(0));
  112. for(oSubDirNames.RemoveAll(); bContinue; )
  113. {
  114. bContinue = oFtpFileFinder.FindNextFile();
  115. if(oFtpFileFinder.IsDots()) continue;
  116. if(!oFtpFileFinder.IsDirectory()) continue;
  117. oSubDirNames.AddTail(oFtpFileFinder.GetFileName());
  118. }
  119. oFtpFileFinder.Close();
  120. return TRUE;
  121. }
  122. catch(CInternetException *ex)
  123. {
  124. ex->Delete();
  125. oFtpFileFinder.Close();
  126. return FALSE;
  127. }
  128. return FALSE;
  129. }
  130. BOOL CFtpWorker::GetFileNamesOfCurDir(CList<LPREMOTEFILEINFO,LPREMOTEFILEINFO&> &oFileInfos, LPCSTR lpszFileNameMask)
  131. {
  132. CFtpFileFind oFtpFileFinder(this->m_pFtpConnection);
  133. try
  134. {
  135. CString strFindMask = (NULL == lpszFileNameMask) ? "*" : lpszFileNameMask;
  136. BOOL bContinue = oFtpFileFinder.FindFile(strFindMask.GetBuffer(0));
  137. for(; oFileInfos.GetCount() > 0; )
  138. {
  139. REMOTEFILEINFO *pitem = (REMOTEFILEINFO*)oFileInfos.GetHead();
  140. if(NULL != pitem) delete pitem;
  141. oFileInfos.RemoveHead();
  142. }
  143. for(; bContinue; )
  144. {
  145. bContinue = oFtpFileFinder.FindNextFile();
  146. if(oFtpFileFinder.IsDots()) continue;
  147. if(oFtpFileFinder.IsDirectory()) continue;
  148. REMOTEFILEINFO *lpRemoteFile = new REMOTEFILEINFO;
  149. memset(lpRemoteFile, 0x0, sizeof(REMOTEFILEINFO));
  150. lpRemoteFile->dwFilesize = oFtpFileFinder.GetLength();
  151. _snprintf(lpRemoteFile->szRemoteFile, MAX_PATH, "%s", oFtpFileFinder.GetFileName());
  152. oFileInfos.AddTail(lpRemoteFile);
  153. }
  154. oFtpFileFinder.Close();
  155. return TRUE;
  156. }
  157. catch(CInternetException *ex)
  158. {
  159. ex->Delete();
  160. oFtpFileFinder.Close();
  161. return FALSE;
  162. }
  163. return FALSE;
  164. }
  165. void CFtpWorker::SetMsgWnd(HWND hwnd)
  166. {
  167. this->m_hMsgWnd = hwnd;
  168. }
  169. BOOL CFtpWorker::DownloadFile(LPREMOTEFILEINFO lpRemoteFile, LPCSTR lpszNewFilename)
  170. {
  171. DWORD dwExitCode = 0x0000;
  172. LPDOWNLOADARGS lpParameters = new DOWNLOADARGS;
  173. lpParameters->lpAddition = this;
  174. memcpy(&(lpParameters->rfRemoteFile), lpRemoteFile, sizeof(REMOTEFILEINFO));
  175. _snprintf(lpParameters->szLocalFilename, MAX_PATH, "%s", lpszNewFilename);
  176. // create dummy window as parent for the progress dialog
  177. if (!m_wndDummy.CreateEx(0, AfxRegisterWndClass(0), "trfAgent Download Dummy Window",
  178.  WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL))
  179. {
  180. delete lpParameters;
  181. return FALSE;
  182. }
  183. // Create the progress dialog box.
  184. if (!m_ProgressDlg.Create(&m_wndDummy, m_hEventKill))
  185. {
  186. delete lpParameters;
  187. return FALSE;
  188. }
  189. WaitForProgressDialog();
  190. this->SetMsgWnd(m_ProgressDlg.GetSafeHwnd());
  191. m_ProgressDlg.m_oFromToHint.Format("%s -> %s", lpRemoteFile->szRemoteFile, lpszNewFilename);
  192. m_ProgressDlg.UpdateData(FALSE);
  193. DWORD dwthreadid = 0x0000;
  194. HANDLE hDownThread = CreateThread(NULL, 0, Routine_Download, lpParameters, 0, &dwthreadid);
  195. DWORD dwWaitRet = WaitForSingleObject(hDownThread, 0);
  196. for(; TRUE;)
  197. {
  198. dwWaitRet = WaitForSingleObject(hDownThread, 0);
  199. if(dwWaitRet == WAIT_OBJECT_0) break;
  200. DoEvents();
  201. Sleep(10);
  202. }
  203. GetExitCodeThread(hDownThread, &dwExitCode);
  204. CloseHandle(hDownThread);
  205. delete lpParameters;
  206. m_ProgressDlg.DestroyWindow();
  207. m_wndDummy.DestroyWindow();
  208. return (dwExitCode == 0);
  209. }
  210. DWORD CFtpWorker::Routine_Download(LPVOID lpParameters)
  211. {
  212. char  lpBuffer[MAX_BUFSIZE];
  213. DWORD dwLeftSize = 0x0000, dwReaded = 0x0000, dwWriten = 0x0000;
  214. HANDLE hLocalFile = INVALID_HANDLE_VALUE;
  215. CInternetFile *pfile = NULL;
  216. //get parameters.
  217. LPDOWNLOADARGS lpDownParams = (LPDOWNLOADARGS)lpParameters;
  218. if(NULL == lpDownParams) return 1;
  219. CFtpWorker *pthis = (CFtpWorker*)lpDownParams->lpAddition;
  220. if(NULL == pthis) return 1;
  221. try
  222. {
  223. //open remote file.
  224. for(; (WaitForSingleObject(pthis->m_hEventKill, 0) == WAIT_TIMEOUT); )
  225. {
  226. dwLeftSize = lpDownParams->rfRemoteFile.dwFilesize;
  227. pfile = pthis->m_pFtpConnection->OpenFile(
  228. lpDownParams->rfRemoteFile.szRemoteFile);
  229. if(NULL == pfile) return 1;
  230. //create local file.
  231. hLocalFile = CreateFile(lpDownParams->szLocalFilename, GENERIC_READ | GENERIC_WRITE, 
  232. FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  233. if(INVALID_HANDLE_VALUE == hLocalFile)
  234. {
  235. //close remote file.
  236. pfile->Close();
  237. return 1;
  238. }
  239. //start read and write actions.
  240. if(pthis->m_hMsgWnd)
  241. {
  242. SendMessage(pthis->m_hMsgWnd, UWM_SETPPROGRESS, (WPARAM)0x0000, dwLeftSize);
  243. }
  244. BOOL bReadFinished = FALSE;
  245. for(; (WaitForSingleObject(pthis->m_hEventKill, 0) == WAIT_TIMEOUT) && dwLeftSize > 0; )
  246. {
  247. //read contents from remote file.
  248. memset(lpBuffer, 0x0, sizeof(lpBuffer));
  249. dwReaded = pfile->Read(lpBuffer, MAX_BUFSIZE);
  250. //write remote file's contents into local file.
  251. WriteFile(hLocalFile, lpBuffer, dwReaded, &dwWriten, NULL);
  252. if(pthis->m_hMsgWnd)
  253. {
  254. PostMessage(pthis->m_hMsgWnd, UWM_SETPPROGRESS, (WPARAM)0x0001, dwReaded);
  255. }
  256. dwLeftSize -= dwReaded;
  257. //if arrived end of file...
  258. if(dwReaded < MAX_BUFSIZE)
  259. {
  260. bReadFinished = TRUE;
  261. break;
  262. }
  263. }
  264. CloseHandle(hLocalFile);
  265. pfile->Close();
  266. if(pthis->m_hMsgWnd)
  267. {
  268. SendMessage(pthis->m_hMsgWnd, UWM_SETPPROGRESS, (WPARAM)0x0002, 0);
  269. }
  270. return bReadFinished ? 0 : 1;
  271. }
  272. }
  273. catch (CInternetException *ex)
  274. {
  275. ex->Delete();
  276. if(NULL != pfile) pfile->Close();
  277. return 1;
  278. }
  279. return 0L;
  280. }
  281. /********************************************************************/
  282. /* */
  283. /* Function name : WaitForProgressDialog */
  284. /* Description   : If the thread has just started, it may not have */
  285. /*    had time yet to initialize the dialog window. */
  286. /* */
  287. /********************************************************************/
  288. void CFtpWorker::WaitForProgressDialog(void)
  289. {
  290. if(m_ProgressDlg.m_hWnd == NULL)
  291. {
  292. while(m_ProgressDlg.m_hWnd == NULL)
  293. {
  294. DoEvents();
  295. }
  296. }
  297. if(!::IsWindow(m_ProgressDlg.m_hWnd))
  298. {
  299. while(!::IsWindow(m_ProgressDlg.m_hWnd))
  300. {
  301. DoEvents();
  302. }
  303. }
  304. }