DownloaderFeedback.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:15k
源码类别:
P2P编程
开发平台:
Visual C++
- // DownloaderFeedback.cpp: implementation of the CDownloaderFeedback class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "DownloaderFeedback.h"
- #include "Connector.h"
- #include "Connection.h"
- #include "singledownload.h"
- #include "upload.h"
- #include "Downloader.h"
- #include "RateMeasure.h"
- #include "Download.h"
- #include "EncryptedConnection.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- bool operator < (const CAddrPort& a, const CAddrPort& b)
- {
- return a.m_lAddr < b.m_lAddr;
- }
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CDownloaderFeedback::CDownloaderFeedback()
- {
- m_bBad = false;
- m_pDownload = 0;
- time(&m_tStart);
- m_tKeepSeedTimeHour = 1;
- m_tCompleted = 0;
- m_hFeedbackwnd = 0;
- m_curItem = 0;
- m_lParam = 0;
- m_lPort = 0;
- m_lSystemMsgMaxCount = 200;
- }
- CDownloaderFeedback::~CDownloaderFeedback()
- {
- }
- void CDownloaderFeedback::SetDownload(CDownload* pDownload, HTREEITEM curItem,
- HWND hFeedbackwnd, time_t tKeepSeedTimeHour, long lParam)
- {
- m_pDownload = pDownload;
- m_curItem = curItem;
- m_hFeedbackwnd = hFeedbackwnd;
- m_lParam = lParam;
- m_tKeepSeedTimeHour = tKeepSeedTimeHour;
- // m_bCompleteKeepSeed = bCompleteKeepSeed;
- }
- void CDownloaderFeedback::Create(CConnector* pConnector, long lPutDataTimeSpan)
- {
- assert(m_pDownload);
- m_pConnector = pConnector;
- m_lPutDataTimeSpan = lPutDataTimeSpan;
- // time(&m_lLast);
- m_lLast = clock();
- }
- void CDownloaderFeedback::SetCompleteKeepSeedHour(long lCompleteKeepSeedHour)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(singleLock.IsLocked());
- m_tKeepSeedTimeHour = lCompleteKeepSeedHour;
- }
- //////////////////////////////////////////////////////////////
- //
- // Completed
- //
- //////////////////////////////////////////////////////////////
- void CDownloaderFeedback::Completed()
- {
- assert(!m_tCompleted);
- // if (m_bCompleteKeepSeed)
- time(&m_tCompleted);
- assert(m_pDownload);
- m_pDownload->Completed();
- }
- void CDownloaderFeedback::PartCompleted()
- {
- assert(m_pDownload);
- m_pDownload->PartCompleted();
- }
- void CDownloaderFeedback::data_flunked(long lAmount)
- {
- assert(m_pDownload);
- m_pDownload->data_flunked(lAmount);
- errorFunc("**********data_flunked****************");
- }
- //////////////////////////////////////////////////////////////
- //
- // Data
- //
- //////////////////////////////////////////////////////////////
- void CDownloaderFeedback::TrackerConnected()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(singleLock.IsLocked());
- m_statics.m_bTrackerConnected = true;
- }
- void CDownloaderFeedback::SetStorageAllocating(bool bAllocating, float fAllocatingPercent)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(singleLock.IsLocked());
- m_statics.m_bAllocating = bAllocating;
- m_statics.m_fAllocatingPercent = fAllocatingPercent;
- }
- bool CDownloaderFeedback::IsStorageChecking()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- return m_statics.m_bStorageChecking;
- }
- void CDownloaderFeedback::SetStorageChecking(bool bStorageChecking, float fCheckingPercent)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(singleLock.IsLocked());
- m_statics.m_bStorageChecking = bStorageChecking;
- m_statics.m_fCheckingPercent = fCheckingPercent;
- }
- void CDownloaderFeedback::SetCheckingDone(float fCompleted, float fPartCompletedPercent)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(singleLock.IsLocked());
- m_statics.m_bCheckingDone = true;
- //
- // completed percent.
- //
- m_statics.m_fCompleted = fCompleted;
- m_statics.m_fCompletedPart = fPartCompletedPercent;
- }
- void CDownloaderFeedback::GetFractions(vector<long>& vFractions)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- vFractions = m_vFractions;
- }
- void CDownloaderFeedback::PutFractionsState(long lFractionInx, long lState)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(lState>=0 && lState<=2);
- assert(lFractionInx < m_vFractions.size() && lFractionInx>=0);
- m_vFractions[lFractionInx] = lState;
- }
- void CDownloaderFeedback::PutFractions(const vector<bool>& vHaves)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- if (!m_vFractions.empty())
- {
- assert(false);
- m_vFractions.clear();
- }
- for (int i=0; i<vHaves.size();i++)
- {
- m_vFractions.push_back(vHaves[i]);
- }
- }
- void CDownloaderFeedback::PutRunParameter(long lPort)
- {
- m_lPort = lPort;
- }
- void CDownloaderFeedback::GetRunParameter(long& lPort)
- {
- lPort = m_lPort;
- }
- void CDownloaderFeedback::PutDataTime()
- {
- clock_t t = clock();
- clock_t tspan = t - m_lLast;
- assert(tspan >= 0);
- if (tspan < 0)
- m_lLast = t;
- if (tspan < m_lPutDataTimeSpan)
- return ;
- CSingleLock singleLock(&m_criticalSection, true);
- PutData();
- PutStatics();
- m_lLast = clock();
- if (m_tCompleted)
- {
- time_t tNow, tSpan;
- time(&tNow);
- tSpan = tNow - m_tCompleted;
- if (tspan < 0 || tspan >= (m_tKeepSeedTimeHour *60 * 60))
- {
- char strText[1024] = {0};
- sprintf(strText, "下载已经完成, 并作种(%d)小时", m_tKeepSeedTimeHour);
- SetCompleteMsg(strText);
- }
- }
- }
- void CDownloaderFeedback::PutStatics()
- {
- assert(m_pConnector);
- const CDownloader* pDownload = m_pConnector->GetDownloader();
- CMeasure* const pUpTotalMeasure = (CMeasure*)m_pConnector->GetUpTotalMeasure();
- m_statics.m_lDownRate = pDownload->m_pDownMeasure->get_rate();
- m_statics.m_lUpRate = pUpTotalMeasure->get_rate();
- m_statics.m_tTimeLeft = pDownload->m_pRateMeasure->get_time_left();
- // time
- time_t tnow;
- time(&tnow);
- if (tnow < m_tStart)
- {
- assert(false);
- m_tStart = tnow;
- }
- m_statics.m_tTimeRunned = tnow - m_tStart;
- //
- // completed percent.
- //
- m_statics.m_fCompleted = 0;
- BLONG lTotalLength = pDownload->m_pstrorage->get_total_legth();
- if (lTotalLength > 0)
- {
- BLONG lAmountLeft = pDownload->m_pstrorage->get_amount_left();
- m_statics.m_fCompleted = (float)(lTotalLength - lAmountLeft) / lTotalLength;
- // m_statics.m_fCompleted = ((float)(lTotalLength - pDownload->m_pstrorage->get_amount_left()))/lTotalLength;
- }
- m_statics.m_fCompletedPart = pDownload->m_pstrorage->GetPartCompletedPercent();
- //
- // other info.
- //
- m_statics.m_lPeersCount = m_pConnector->m_connections.size();
- m_statics.m_lSeedsCount = 0;
- m_statics.m_bFired = true;
- for (int i=0; i<m_pConnector->m_connections.size(); i++)
- {
- CConnection* pCon = m_pConnector->m_connections[i];
- if (pCon->m_pDownload->GetHaveCountPercent() == 1)
- m_statics.m_lSeedsCount ++;
- if (!pCon->is_locally_initiated())
- m_statics.m_bFired = false;
- }
- }
- CStatics& CDownloaderFeedback::GetStatics()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- return m_statics;
- }
- void CDownloaderFeedback::PutData()
- {
- assert(m_pConnector);
- // m_vConnectionDatas.clear();
- vector<long> vToAdd;
- long lInx = 0;
- bool bEnd = m_vConnectionDatas.size() <= 0;
- for (int i=0; i<m_pConnector->m_connections.size(); i++)
- {
- CConnection* pCon = m_pConnector->m_connections[i];
- long lAddr = 0;
- short sPort = 0;
- pCon->GetIP(lAddr, sPort);
- if (!bEnd)
- {
- bool bFind = false;
- for (; lInx< m_vConnectionDatas.size(); )
- {
- if (m_vConnectionDatas[lInx].m_lAddr == lAddr &&
- m_vConnectionDatas[lInx].m_sPort == sPort)
- {
- CConncetionData& connectionData = m_vConnectionDatas[lInx];
- pCon->GetRate(connectionData.m_lDownRate, connectionData.m_lUpRate);
- memcpy(connectionData.m_lValueDown, pCon->m_lValueDown, CConnection::eCount*sizeof(long));
- memcpy(connectionData.m_lValueUp, pCon->m_lValueUp, CConnection::eCount*sizeof(long));
- connectionData.m_lIndex = pCon->m_lIndex;
- connectionData.m_lDownCount = pCon->m_pDownload->get_total();
- connectionData.m_lUpCount = pCon->m_pUpload->get_total();
- connectionData.m_bInterested = pCon->m_pUpload->is_interested();
- connectionData.m_bInteresting = pCon->m_pDownload->is_interested();
- connectionData.m_bChoked = pCon->m_pUpload->is_choked();
- connectionData.m_bChoking = pCon->m_pDownload->is_choked();
- connectionData.m_bPaused = pCon->IsPause();
- connectionData.m_fPeerHavePercent= pCon->m_pDownload->GetHaveCountPercent();
- pCon->m_pDownload->get_new_have(connectionData.m_vHave);
- bFind = true;
- lInx ++;
- break;
- }
- else
- {
- m_vConnectionDatas.erase(m_vConnectionDatas.begin() + lInx);
- }
- }
- if (!bFind)
- bEnd = true;
- }
- if (bEnd)
- {
- CConncetionData connectionData;
- connectionData.m_lAddr = lAddr;
- connectionData.m_sPort = sPort;
- connectionData.m_bLocallyInitiated = pCon->is_locally_initiated();
- // connectionData.m_lUpRate = pCon->m_pUpload->get_rate();
- // connectionData.m_lDownRate = pCon->m_pDownload->get_rate();
- pCon->GetRate(connectionData.m_lDownRate, connectionData.m_lUpRate);
- memcpy(connectionData.m_lValueDown, pCon->m_lValueDown, CConnection::eCount*sizeof(long));
- memcpy(connectionData.m_lValueUp, pCon->m_lValueUp, CConnection::eCount*sizeof(long));
- connectionData.m_lIndex = pCon->m_lIndex;
- connectionData.m_lDownCount = pCon->m_pDownload->get_total();
- connectionData.m_lUpCount = pCon->m_pUpload->get_total();
- connectionData.m_bInterested = pCon->m_pUpload->is_interested();
- connectionData.m_bInteresting = pCon->m_pDownload->is_interested();
- connectionData.m_bChoked = pCon->m_pUpload->is_choked();
- connectionData.m_bChoking = pCon->m_pDownload->is_choked();
- connectionData.m_bPaused = pCon->IsPause();
- connectionData.m_strPeerId = pCon->m_pEConnection->GetInfohashString();
- // if (pCon->m_pDownload->GetHaveCountPercent() != connectionData.m_fPeerHavePercent ||
- // connectionData.m_vHave.size() != pCon->m_pDownload->get_have_count())
- {
- connectionData.m_fPeerHavePercent= pCon->m_pDownload->GetHaveCountPercent();
- pCon->m_pDownload->get_have(connectionData.m_vHave);
- }
- m_vConnectionDatas.push_back(connectionData);
- }
- } // end for
- if (m_vConnectionDatas.size() > m_pConnector->m_connections.size())
- {
- m_vConnectionDatas.erase(m_vConnectionDatas.begin() + m_pConnector->m_connections.size(), m_vConnectionDatas.end());
- }
- #ifdef _DEBUG
- int ia = m_vConnectionDatas.size();
- int ib = m_pConnector->m_connections.size();
- assert(m_vConnectionDatas.size() == m_pConnector->m_connections.size());
- for (i=0; i<m_vConnectionDatas.size();i++)
- {
- CConnection* pCon = m_pConnector->m_connections[i];
- long lAddr = 0;
- short sPort = 0;
- pCon->GetIP(lAddr, sPort);
- assert(m_vConnectionDatas[i].m_lAddr == lAddr &&
- m_vConnectionDatas[i].m_sPort == sPort);
- }
- #endif
- }
- vector<CConncetionData>& CDownloaderFeedback::GetData()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- // assert(m_pConnector);
- return m_vConnectionDatas;
- }
- //////////////////////////////////////////////////////////////
- //
- // Socket Message
- //
- //////////////////////////////////////////////////////////////
- void CDownloaderFeedback::ShowSocketMessage(long lAddr, short sPort, string strMsg)
- {
- //*****************
- return;
- CAddrPort addr(lAddr, sPort);
- map<CAddrPort, vector<string> >::iterator iter = m_mapMsg.find(addr);
- if (iter == m_mapMsg.end())
- {
- vector<string> vMsg;
- vMsg.push_back(strMsg);
- m_mapMsg[addr] = vMsg;
- }
- else
- {
- m_mapMsg[addr].push_back(strMsg);
- }
- }
- string CDownloaderFeedback::GetSocketMessage(long lAddr, short sPort)
- {
- CAddrPort addr(lAddr, sPort);
- map<CAddrPort, vector<string> >::iterator iter = m_mapMsg.find(addr);
- if (iter == m_mapMsg.end())
- {
- return "Connecting...";
- }
- else
- {
- string strRet;
- vector<string> vMsg = (*iter).second;
- for (int i=0; i<vMsg.size(); i++)
- {
- strRet += vMsg[i] + "rn";
- }
- return strRet;
- }
- return "";
- }
- string CDownloaderFeedback::GetSocketMessageAll()
- {
- string strRet;
- for (map<CAddrPort, vector<string> >::iterator iter = m_mapMsg.begin();
- iter != m_mapMsg.end(); iter++)
- {
- vector<string> vMsg = (*iter).second;
- for (int i=0; i<vMsg.size(); i++)
- {
- strRet += vMsg[i] + "rn";
- }
- strRet += "rn**************************rn";
- }
- return strRet;
- }
- //////////////////////////////////////////////////////////////
- //
- // SystemMessage
- //
- //////////////////////////////////////////////////////////////
- void CDownloaderFeedback::ShowSystemMessage(string strMsg, CSystemMsg::eMSG_TYPE eMsgType)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- if (m_vSystemMsg.size() >= m_lSystemMsgMaxCount)
- m_vSystemMsg.erase(m_vSystemMsg.begin());
- time_t t;
- time(&t);
- m_vSystemMsg.push_back(CSystemMsg(t, strMsg, eMsgType));
- }
- void CDownloaderFeedback::ClearSystemMessage()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- m_vSystemMsg.clear();
- }
- void CDownloaderFeedback::GetSystemMessage(vector<CSystemMsg>& vMsg)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- vMsg = m_vSystemMsg;
- }
- //////////////////////////////////////////////////////////////
- //
- // EncryptedMessage
- //
- //////////////////////////////////////////////////////////////
- void CDownloaderFeedback::ShowEncryptedMessage(string strMsg)
- {
- ;
- }
- void CDownloaderFeedback::errorFunc(string e)
- {
- ShowSystemMessage("******** error : " + e, CSystemMsg::eBad);
- }
- void CDownloaderFeedback::SetCompleteMsg(string strMsg)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- time_t t; time(&t);
- m_vSystemMsg.push_back(CSystemMsg(t, strMsg, CSystemMsg::eCmd));
- if (!IsEventSet(m_pDownload->m_hevDone))
- SetEvent(m_pDownload->m_hevDone);
- }
- void CDownloaderFeedback::SetBadMsg(string strBad, bool bSendMessage)
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert (!m_bBad);
- m_bBad = true;
- m_strBad += strBad + "rn";
- time_t t; time(&t);
- m_vSystemMsg.push_back(CSystemMsg(t, strBad, CSystemMsg::eBad));
- // Stop downloading.
- if (!IsEventSet(m_pDownload->m_hevDone))
- SetEvent(m_pDownload->m_hevDone);
- }
- string CDownloaderFeedback::GetBadMsg()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- return m_strBad;
- }
- bool CDownloaderFeedback::IsBad()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- return m_bBad;
- }
- void CDownloaderFeedback::StorageCheckComplete()
- {
- CSingleLock singleLock(&m_criticalSection, true);
- assert(m_hFeedbackwnd);
- int iRet = ::PostMessage(m_hFeedbackwnd, STORAGE_CHECK_COMPLETE, (long)m_curItem, (long)m_lParam);
- }
- void CDownloaderFeedback::QuitDownload()
- {
- assert(m_hFeedbackwnd);
- int iRet = ::PostMessage(m_hFeedbackwnd, DOWNLOAD_TERMINATED, (long)m_curItem, (long)m_lParam);
- }