DownloaderFeedback.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:15k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // DownloaderFeedback.cpp: implementation of the CDownloaderFeedback class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "testbt.h"
  6. #include "DownloaderFeedback.h"
  7. #include "Connector.h"
  8. #include "Connection.h"
  9. #include "singledownload.h"
  10. #include "upload.h"
  11. #include "Downloader.h"
  12. #include "RateMeasure.h"
  13. #include "Download.h"
  14. #include "EncryptedConnection.h"
  15. #ifdef _DEBUG
  16. #undef THIS_FILE
  17. static char THIS_FILE[]=__FILE__;
  18. #define new DEBUG_NEW
  19. #endif
  20. bool operator < (const CAddrPort& a, const CAddrPort& b)
  21. {
  22. return a.m_lAddr < b.m_lAddr;
  23. }
  24. //////////////////////////////////////////////////////////////////////
  25. // Construction/Destruction
  26. //////////////////////////////////////////////////////////////////////
  27. CDownloaderFeedback::CDownloaderFeedback()
  28. {
  29. m_bBad = false;
  30. m_pDownload = 0;
  31. time(&m_tStart);
  32. m_tKeepSeedTimeHour = 1;
  33. m_tCompleted = 0;
  34. m_hFeedbackwnd = 0;
  35. m_curItem = 0;
  36. m_lParam = 0;
  37. m_lPort = 0;
  38. m_lSystemMsgMaxCount = 200;
  39. }
  40. CDownloaderFeedback::~CDownloaderFeedback()
  41. {
  42. }
  43. void CDownloaderFeedback::SetDownload(CDownload* pDownload, HTREEITEM curItem, 
  44.   HWND hFeedbackwnd, time_t tKeepSeedTimeHour, long lParam)
  45. {
  46. m_pDownload = pDownload;
  47. m_curItem = curItem;
  48. m_hFeedbackwnd = hFeedbackwnd;
  49. m_lParam = lParam;
  50. m_tKeepSeedTimeHour = tKeepSeedTimeHour;
  51. // m_bCompleteKeepSeed = bCompleteKeepSeed; 
  52. }
  53. void CDownloaderFeedback::Create(CConnector* pConnector, long lPutDataTimeSpan)
  54. {
  55. assert(m_pDownload);
  56. m_pConnector = pConnector;
  57. m_lPutDataTimeSpan = lPutDataTimeSpan;
  58. // time(&m_lLast);
  59. m_lLast = clock();
  60. }
  61. void CDownloaderFeedback::SetCompleteKeepSeedHour(long lCompleteKeepSeedHour)
  62. {
  63. CSingleLock singleLock(&m_criticalSection, true);
  64. assert(singleLock.IsLocked());
  65. m_tKeepSeedTimeHour = lCompleteKeepSeedHour;
  66. }
  67. //////////////////////////////////////////////////////////////
  68. //
  69. // Completed
  70. //
  71. //////////////////////////////////////////////////////////////
  72. void CDownloaderFeedback::Completed()
  73. {
  74. assert(!m_tCompleted);
  75. // if (m_bCompleteKeepSeed)
  76. time(&m_tCompleted);
  77. assert(m_pDownload);
  78. m_pDownload->Completed();
  79. }
  80. void CDownloaderFeedback::PartCompleted()
  81. {
  82. assert(m_pDownload);
  83. m_pDownload->PartCompleted();
  84. }
  85. void CDownloaderFeedback::data_flunked(long lAmount)
  86. {
  87. assert(m_pDownload);
  88. m_pDownload->data_flunked(lAmount);
  89. errorFunc("**********data_flunked****************");
  90. }
  91. //////////////////////////////////////////////////////////////
  92. //
  93. // Data
  94. //
  95. //////////////////////////////////////////////////////////////
  96. void CDownloaderFeedback::TrackerConnected()
  97. {
  98. CSingleLock singleLock(&m_criticalSection, true);
  99. assert(singleLock.IsLocked());
  100. m_statics.m_bTrackerConnected = true;
  101. }
  102. void CDownloaderFeedback::SetStorageAllocating(bool bAllocating, float fAllocatingPercent)
  103. {
  104. CSingleLock singleLock(&m_criticalSection, true);
  105. assert(singleLock.IsLocked());
  106. m_statics.m_bAllocating = bAllocating;
  107. m_statics.m_fAllocatingPercent = fAllocatingPercent;
  108. }
  109. bool CDownloaderFeedback::IsStorageChecking()
  110. {
  111. CSingleLock singleLock(&m_criticalSection, true);
  112. return m_statics.m_bStorageChecking;
  113. }
  114. void CDownloaderFeedback::SetStorageChecking(bool bStorageChecking, float fCheckingPercent)
  115. {
  116. CSingleLock singleLock(&m_criticalSection, true);
  117. assert(singleLock.IsLocked());
  118. m_statics.m_bStorageChecking = bStorageChecking;
  119. m_statics.m_fCheckingPercent = fCheckingPercent;
  120. }
  121. void CDownloaderFeedback::SetCheckingDone(float fCompleted, float fPartCompletedPercent)
  122. {
  123. CSingleLock singleLock(&m_criticalSection, true);
  124. assert(singleLock.IsLocked());
  125. m_statics.m_bCheckingDone = true;
  126. //
  127. // completed percent.
  128. //
  129. m_statics.m_fCompleted = fCompleted;
  130. m_statics.m_fCompletedPart = fPartCompletedPercent;
  131. }
  132. void CDownloaderFeedback::GetFractions(vector<long>& vFractions)
  133. {
  134. CSingleLock singleLock(&m_criticalSection, true);
  135. vFractions = m_vFractions;
  136. }
  137. void CDownloaderFeedback::PutFractionsState(long lFractionInx, long lState)
  138. {
  139. CSingleLock singleLock(&m_criticalSection, true);
  140. assert(lState>=0 && lState<=2);
  141. assert(lFractionInx < m_vFractions.size() &&  lFractionInx>=0);
  142. m_vFractions[lFractionInx] = lState;
  143. }
  144. void CDownloaderFeedback::PutFractions(const vector<bool>& vHaves)
  145. {
  146. CSingleLock singleLock(&m_criticalSection, true);
  147. if (!m_vFractions.empty())
  148. {
  149. assert(false);
  150. m_vFractions.clear();
  151. }
  152. for (int i=0; i<vHaves.size();i++)
  153. {
  154. m_vFractions.push_back(vHaves[i]);
  155. }
  156. }
  157. void CDownloaderFeedback::PutRunParameter(long lPort)
  158. {
  159. m_lPort = lPort;
  160. }
  161. void CDownloaderFeedback::GetRunParameter(long& lPort)
  162. {
  163. lPort = m_lPort;
  164. }
  165. void CDownloaderFeedback::PutDataTime()
  166. {
  167. clock_t t = clock();
  168. clock_t tspan = t - m_lLast;
  169. assert(tspan >= 0);
  170. if (tspan < 0)
  171. m_lLast = t;
  172. if (tspan < m_lPutDataTimeSpan)
  173. return ;
  174. CSingleLock singleLock(&m_criticalSection, true);
  175. PutData();
  176. PutStatics();
  177. m_lLast = clock();
  178. if (m_tCompleted)
  179. {
  180. time_t tNow, tSpan;
  181. time(&tNow);
  182. tSpan = tNow - m_tCompleted;
  183. if (tspan < 0 || tspan >= (m_tKeepSeedTimeHour *60 * 60))
  184. {
  185. char strText[1024] = {0};
  186. sprintf(strText, "下载已经完成, 并作种(%d)小时", m_tKeepSeedTimeHour);
  187. SetCompleteMsg(strText);
  188. }
  189. }
  190. }
  191. void CDownloaderFeedback::PutStatics()
  192. {
  193. assert(m_pConnector);
  194. const CDownloader* pDownload = m_pConnector->GetDownloader();
  195. CMeasure* const pUpTotalMeasure = (CMeasure*)m_pConnector->GetUpTotalMeasure();
  196. m_statics.m_lDownRate = pDownload->m_pDownMeasure->get_rate();
  197. m_statics.m_lUpRate = pUpTotalMeasure->get_rate();
  198. m_statics.m_tTimeLeft = pDownload->m_pRateMeasure->get_time_left();
  199. // time 
  200. time_t tnow;
  201. time(&tnow);
  202. if (tnow < m_tStart)
  203. {
  204. assert(false);
  205. m_tStart = tnow;
  206. }
  207. m_statics.m_tTimeRunned = tnow - m_tStart;
  208. //
  209. // completed percent.
  210. //
  211. m_statics.m_fCompleted = 0;
  212. BLONG lTotalLength = pDownload->m_pstrorage->get_total_legth();
  213. if (lTotalLength > 0)
  214. {
  215. BLONG lAmountLeft = pDownload->m_pstrorage->get_amount_left();
  216. m_statics.m_fCompleted = (float)(lTotalLength - lAmountLeft) / lTotalLength;
  217. // m_statics.m_fCompleted = ((float)(lTotalLength - pDownload->m_pstrorage->get_amount_left()))/lTotalLength;
  218. }
  219. m_statics.m_fCompletedPart = pDownload->m_pstrorage->GetPartCompletedPercent();
  220. //
  221. // other info.
  222. //
  223. m_statics.m_lPeersCount = m_pConnector->m_connections.size();
  224. m_statics.m_lSeedsCount = 0;
  225. m_statics.m_bFired = true;
  226. for (int i=0; i<m_pConnector->m_connections.size(); i++)
  227. {
  228. CConnection* pCon = m_pConnector->m_connections[i];
  229. if (pCon->m_pDownload->GetHaveCountPercent() == 1)
  230. m_statics.m_lSeedsCount ++;
  231. if (!pCon->is_locally_initiated())
  232. m_statics.m_bFired = false;
  233. }
  234. }
  235. CStatics& CDownloaderFeedback::GetStatics()
  236. {
  237. CSingleLock singleLock(&m_criticalSection, true);
  238. return m_statics;
  239. }
  240. void CDownloaderFeedback::PutData()
  241. {
  242. assert(m_pConnector);
  243. // m_vConnectionDatas.clear();
  244. vector<long> vToAdd;
  245. long lInx = 0;
  246. bool bEnd = m_vConnectionDatas.size() <= 0;
  247. for (int i=0; i<m_pConnector->m_connections.size(); i++)
  248. {
  249. CConnection* pCon = m_pConnector->m_connections[i];
  250. long lAddr = 0;
  251. short sPort = 0;
  252. pCon->GetIP(lAddr, sPort);
  253. if (!bEnd)
  254. {
  255. bool bFind = false;
  256. for (; lInx< m_vConnectionDatas.size(); )
  257. {
  258. if (m_vConnectionDatas[lInx].m_lAddr == lAddr &&  
  259. m_vConnectionDatas[lInx].m_sPort == sPort)
  260. {
  261. CConncetionData& connectionData = m_vConnectionDatas[lInx];
  262. pCon->GetRate(connectionData.m_lDownRate, connectionData.m_lUpRate);
  263. memcpy(connectionData.m_lValueDown, pCon->m_lValueDown, CConnection::eCount*sizeof(long));
  264. memcpy(connectionData.m_lValueUp, pCon->m_lValueUp, CConnection::eCount*sizeof(long));
  265. connectionData.m_lIndex = pCon->m_lIndex;
  266. connectionData.m_lDownCount = pCon->m_pDownload->get_total();
  267. connectionData.m_lUpCount = pCon->m_pUpload->get_total();
  268. connectionData.m_bInterested = pCon->m_pUpload->is_interested();
  269. connectionData.m_bInteresting = pCon->m_pDownload->is_interested();
  270. connectionData.m_bChoked = pCon->m_pUpload->is_choked();
  271. connectionData.m_bChoking = pCon->m_pDownload->is_choked();
  272. connectionData.m_bPaused = pCon->IsPause();
  273. connectionData.m_fPeerHavePercent= pCon->m_pDownload->GetHaveCountPercent();
  274. pCon->m_pDownload->get_new_have(connectionData.m_vHave);
  275. bFind = true;
  276. lInx ++;
  277. break;
  278. }
  279. else
  280. {
  281. m_vConnectionDatas.erase(m_vConnectionDatas.begin() + lInx);
  282. }
  283. }
  284. if (!bFind)
  285. bEnd = true;
  286. }
  287. if (bEnd)
  288. {
  289. CConncetionData connectionData;
  290. connectionData.m_lAddr = lAddr;
  291. connectionData.m_sPort = sPort;
  292. connectionData.m_bLocallyInitiated = pCon->is_locally_initiated();
  293. // connectionData.m_lUpRate = pCon->m_pUpload->get_rate();
  294. // connectionData.m_lDownRate = pCon->m_pDownload->get_rate();
  295. pCon->GetRate(connectionData.m_lDownRate, connectionData.m_lUpRate);
  296. memcpy(connectionData.m_lValueDown, pCon->m_lValueDown, CConnection::eCount*sizeof(long));
  297. memcpy(connectionData.m_lValueUp, pCon->m_lValueUp, CConnection::eCount*sizeof(long));
  298. connectionData.m_lIndex = pCon->m_lIndex;
  299. connectionData.m_lDownCount = pCon->m_pDownload->get_total();
  300. connectionData.m_lUpCount = pCon->m_pUpload->get_total();
  301. connectionData.m_bInterested = pCon->m_pUpload->is_interested();
  302. connectionData.m_bInteresting = pCon->m_pDownload->is_interested();
  303. connectionData.m_bChoked = pCon->m_pUpload->is_choked();
  304. connectionData.m_bChoking = pCon->m_pDownload->is_choked();
  305. connectionData.m_bPaused = pCon->IsPause();
  306. connectionData.m_strPeerId = pCon->m_pEConnection->GetInfohashString();
  307. // if (pCon->m_pDownload->GetHaveCountPercent() != connectionData.m_fPeerHavePercent ||
  308. // connectionData.m_vHave.size() != pCon->m_pDownload->get_have_count())
  309. {
  310. connectionData.m_fPeerHavePercent= pCon->m_pDownload->GetHaveCountPercent();
  311. pCon->m_pDownload->get_have(connectionData.m_vHave);
  312. }
  313. m_vConnectionDatas.push_back(connectionData);
  314. }
  315. } // end for
  316. if (m_vConnectionDatas.size() > m_pConnector->m_connections.size())
  317. {
  318. m_vConnectionDatas.erase(m_vConnectionDatas.begin() + m_pConnector->m_connections.size(), m_vConnectionDatas.end());
  319. }
  320. #ifdef _DEBUG
  321. int ia = m_vConnectionDatas.size();
  322. int ib = m_pConnector->m_connections.size();
  323. assert(m_vConnectionDatas.size() == m_pConnector->m_connections.size());
  324. for (i=0; i<m_vConnectionDatas.size();i++)
  325. {
  326. CConnection* pCon = m_pConnector->m_connections[i];
  327. long lAddr = 0;
  328. short sPort = 0;
  329. pCon->GetIP(lAddr, sPort);
  330. assert(m_vConnectionDatas[i].m_lAddr == lAddr &&
  331. m_vConnectionDatas[i].m_sPort == sPort);
  332. }
  333. #endif
  334. }
  335. vector<CConncetionData>& CDownloaderFeedback::GetData()
  336. {
  337. CSingleLock singleLock(&m_criticalSection, true);
  338. // assert(m_pConnector);
  339. return m_vConnectionDatas;
  340. }
  341. //////////////////////////////////////////////////////////////
  342. //
  343. // Socket Message
  344. //
  345. //////////////////////////////////////////////////////////////
  346. void CDownloaderFeedback::ShowSocketMessage(long lAddr, short sPort, string strMsg)
  347. {
  348. //*****************
  349. return;
  350. CAddrPort addr(lAddr, sPort);
  351. map<CAddrPort, vector<string> >::iterator iter = m_mapMsg.find(addr);
  352. if (iter == m_mapMsg.end())
  353. {
  354. vector<string> vMsg;
  355. vMsg.push_back(strMsg);
  356. m_mapMsg[addr] = vMsg;
  357. }
  358. else
  359. {
  360. m_mapMsg[addr].push_back(strMsg);
  361. }
  362. }
  363. string CDownloaderFeedback::GetSocketMessage(long lAddr, short sPort)
  364. {
  365. CAddrPort addr(lAddr, sPort);
  366. map<CAddrPort, vector<string> >::iterator iter = m_mapMsg.find(addr);
  367. if (iter == m_mapMsg.end())
  368. {
  369. return "Connecting...";
  370. }
  371. else
  372. {
  373. string strRet;
  374. vector<string> vMsg = (*iter).second;
  375. for (int i=0; i<vMsg.size(); i++)
  376. {
  377. strRet += vMsg[i] + "rn";
  378. }
  379. return strRet;
  380. }
  381. return "";
  382. }
  383. string CDownloaderFeedback::GetSocketMessageAll()
  384. {
  385. string strRet;
  386. for (map<CAddrPort, vector<string> >::iterator iter = m_mapMsg.begin(); 
  387. iter != m_mapMsg.end(); iter++)
  388. {
  389. vector<string> vMsg = (*iter).second;
  390. for (int i=0; i<vMsg.size(); i++)
  391. {
  392. strRet += vMsg[i] + "rn";
  393. }
  394. strRet += "rn**************************rn";
  395. }
  396. return strRet;
  397. }
  398. //////////////////////////////////////////////////////////////
  399. //
  400. // SystemMessage
  401. //
  402. //////////////////////////////////////////////////////////////
  403. void CDownloaderFeedback::ShowSystemMessage(string strMsg, CSystemMsg::eMSG_TYPE eMsgType)
  404. {
  405. CSingleLock singleLock(&m_criticalSection, true);
  406. if (m_vSystemMsg.size() >= m_lSystemMsgMaxCount)
  407. m_vSystemMsg.erase(m_vSystemMsg.begin());
  408. time_t t;
  409. time(&t);
  410. m_vSystemMsg.push_back(CSystemMsg(t, strMsg, eMsgType));
  411. }
  412. void CDownloaderFeedback::ClearSystemMessage()
  413. {
  414. CSingleLock singleLock(&m_criticalSection, true);
  415. m_vSystemMsg.clear();
  416. }
  417. void CDownloaderFeedback::GetSystemMessage(vector<CSystemMsg>& vMsg)
  418. {
  419. CSingleLock singleLock(&m_criticalSection, true);
  420. vMsg = m_vSystemMsg;
  421. }
  422. //////////////////////////////////////////////////////////////
  423. //
  424. // EncryptedMessage
  425. //
  426. //////////////////////////////////////////////////////////////
  427. void CDownloaderFeedback::ShowEncryptedMessage(string strMsg)
  428. {
  429. ;
  430. }
  431. void CDownloaderFeedback::errorFunc(string e)
  432. {
  433. ShowSystemMessage("******** error : " + e, CSystemMsg::eBad);
  434. }
  435. void CDownloaderFeedback::SetCompleteMsg(string strMsg)
  436. {
  437. CSingleLock singleLock(&m_criticalSection, true);
  438. time_t t; time(&t);
  439. m_vSystemMsg.push_back(CSystemMsg(t, strMsg, CSystemMsg::eCmd));
  440. if (!IsEventSet(m_pDownload->m_hevDone))
  441. SetEvent(m_pDownload->m_hevDone);
  442. }
  443. void CDownloaderFeedback::SetBadMsg(string strBad, bool bSendMessage)
  444. {
  445. CSingleLock singleLock(&m_criticalSection, true);
  446. assert (!m_bBad);
  447. m_bBad = true;
  448. m_strBad += strBad + "rn";
  449. time_t t; time(&t);
  450. m_vSystemMsg.push_back(CSystemMsg(t, strBad, CSystemMsg::eBad));
  451. // Stop downloading.
  452. if (!IsEventSet(m_pDownload->m_hevDone))
  453. SetEvent(m_pDownload->m_hevDone);
  454. }
  455. string CDownloaderFeedback::GetBadMsg()
  456. {
  457. CSingleLock singleLock(&m_criticalSection, true);
  458. return m_strBad;
  459. }
  460. bool CDownloaderFeedback::IsBad()
  461. {
  462. CSingleLock singleLock(&m_criticalSection, true);
  463. return m_bBad;
  464. }
  465. void CDownloaderFeedback::StorageCheckComplete()
  466. {
  467. CSingleLock singleLock(&m_criticalSection, true);
  468. assert(m_hFeedbackwnd);
  469. int iRet = ::PostMessage(m_hFeedbackwnd, STORAGE_CHECK_COMPLETE, (long)m_curItem, (long)m_lParam);
  470. }
  471. void CDownloaderFeedback::QuitDownload()
  472. {
  473. assert(m_hFeedbackwnd);
  474. int iRet = ::PostMessage(m_hFeedbackwnd, DOWNLOAD_TERMINATED, (long)m_curItem, (long)m_lParam);
  475. }