DownloadTimerCallback.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "DownloadTimerCallback.h"
  2. #include "Poco/File.h"
  3. #include "Poco/Exception.h"
  4. #include <vector>
  5. #include "Util.h"
  6. #include "PhmsLogger.h"
  7. #include "ErrorCode.h"
  8. using namespace std;
  9. using Poco::File;
  10. using Poco::Exception;
  11. CDownloadTimerCallback::CDownloadTimerCallback(pProgressAndSpeedCallback pCallback, unsigned long nFileSize, string stringLocalFilePath,
  12. string stringTempDirectory, unsigned long nAlreadyDownloadFileSize, int nThreadCount, string stringServerFilePath)
  13. :m_pCallback(pCallback), m_nFileSize(nFileSize), m_stringLocalFilePath(stringLocalFilePath),
  14. m_stringTempDirectory(stringTempDirectory), m_nCurrentDownloadFileSize(nAlreadyDownloadFileSize),
  15. m_nPreDownloadFileSize(nAlreadyDownloadFileSize),m_nThreadCount(nThreadCount), m_stringServerFilePath(stringServerFilePath), m_nSeconds(0)
  16. {
  17. }
  18. CDownloadTimerCallback::~CDownloadTimerCallback(void)
  19. {
  20. }
  21. void CDownloadTimerCallback::onTimer(Timer& timer)
  22. {
  23. m_nSeconds++;
  24. File fileLocalFile(m_stringLocalFilePath, "gbk");
  25. try
  26. {
  27. fileLocalFile.createFile();
  28. m_nCurrentDownloadFileSize = (unsigned long)fileLocalFile.getSize();
  29. }
  30. catch(const Exception& e)
  31. {
  32. CPhmsLogger::GetPhmsLogger()->WriteLog(e, __FUNCTION__, __FILE__, __LINE__);
  33. CPhmsLogger::GetPhmsLogger()->WriteLog(CUtil::GetErrorMsg(COMMON_FILE_OPEN_FAIL), __FUNCTION__, __FILE__, __LINE__);
  34. m_nCurrentDownloadFileSize = 0;
  35. }
  36. File fileTempDirectory(m_stringTempDirectory, "gbk");
  37. vector<File> vTempFile;
  38. vector<File>::const_iterator iter;
  39. try
  40. {
  41. fileTempDirectory.list(vTempFile);
  42. }
  43. catch(const Exception&)
  44. {
  45. vTempFile.clear();
  46. }
  47. for(int nThreadNum=0; nThreadNum<m_nThreadCount; nThreadNum++)
  48. {
  49. for(iter=vTempFile.begin(); iter!=vTempFile.end(); iter++)
  50. {
  51. if(iter->path().find("PHMS_TEMP_"+CUtil::UIntToString(nThreadNum)) != string::npos)
  52. {
  53. try
  54. {
  55. m_nCurrentDownloadFileSize += (unsigned long)iter->getSize();
  56. }
  57. catch(const Exception&)
  58. {
  59. m_nCurrentDownloadFileSize += 0;
  60. }
  61. break;
  62. }
  63. }
  64. }
  65. //¼ÆËã½ø¶È
  66. double dProgress = 0.0;
  67. if(m_nCurrentDownloadFileSize <= m_nFileSize)
  68. {
  69. dProgress = ((double)(m_nCurrentDownloadFileSize))/m_nFileSize*100;
  70. }
  71. else
  72. {
  73. dProgress = 100.0;
  74. }
  75. //¼ÆËãËÙ¶È
  76. long nInterval = 1000;
  77. // if(timer.getPeriodicInterval() == 0)
  78. // {
  79. // nInterval = 1000;
  80. // }
  81. // else
  82. // {
  83. // nInterval = timer.getPeriodicInterval();
  84. // }
  85. unsigned long nTimeCount = m_nSeconds*nInterval/1000;
  86. double dSpeed = 0.0;
  87. if(nTimeCount != 0)
  88. {
  89. if(m_nCurrentDownloadFileSize<m_nPreDownloadFileSize)
  90. {
  91. dSpeed = 0.0;
  92. }
  93. else
  94. {
  95. dSpeed = (m_nCurrentDownloadFileSize-m_nPreDownloadFileSize)/nTimeCount;
  96. }
  97. m_nPreDownloadFileSize = m_nCurrentDownloadFileSize;
  98. }
  99. if(m_pCallback != NULL)
  100. {
  101. (*m_pCallback)(dProgress, dSpeed, m_stringServerFilePath.c_str(), m_stringLocalFilePath.c_str());
  102. }
  103. m_nSeconds = 0;
  104. return;
  105. }