RateMeasure.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:2k
源码类别:
P2P编程
开发平台:
Visual C++
- // RateMeasure.cpp: implementation of the CRateMeasure class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "RateMeasure.h"
- #include "StorageWrapper.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRateMeasure::CRateMeasure(BLONG lAmountLeft, CStorageWrapper* pStorageWrapper)
- {
- m_lStart = -1;
- m_lLast = -1;
- m_fRate = 0;
- m_lRemaining = -1;
- m_lLeft = lAmountLeft;
- m_bBroke = false;
- m_bGotAnything = false;
- m_pStorageWrapper = pStorageWrapper;
- }
- CRateMeasure::~CRateMeasure()
- {
- }
- void CRateMeasure::data_came_in(long lAmount)
- {
- if (!m_bGotAnything)
- {
- m_bGotAnything = true;
- time(&m_lLast);
- m_lStart = m_lLast - 2;
- m_lLeft -= lAmount;
- assert(m_lLeft >= 0);
- return;
- }
- long ltime;
- time(<ime);
- update(ltime, lAmount);
- }
- void CRateMeasure::data_rejected(long lAmount)
- {
- m_lLeft += lAmount;
- }
- long CRateMeasure::get_time_left()
- {
- if (!m_bGotAnything)
- return -1;
- long ltime;
- time(<ime);
- if (ltime < m_lLast)
- m_lLast = ltime;
- if ((ltime - m_lLast) > 15)
- update(ltime, 0);
- return m_lRemaining;
- }
- BLONG CRateMeasure::get_size_left()
- {
- assert(false);
- return m_lLeft;
- }
- void CRateMeasure::update(long ltime, long lAmount)
- {
- //
- // unknown the real income when files unneeded are changed. so temply get the left value approximately.
- //
- m_lLeft -= lAmount;
- m_lLeft = m_pStorageWrapper->get_amount_left_part_include_temp();
- assert(m_lLeft >= 0);
- if (ltime == m_lStart)
- return;
- m_fRate = (m_fRate * (m_lLast - m_lStart) + lAmount) / (ltime - m_lStart);
- assert(m_fRate >= 0);
- m_lLast = ltime;
- if (m_fRate == 0)
- m_lRemaining = -1;
- else
- {
- m_lRemaining = m_lLeft/m_fRate;
- if (m_lRemaining < 0)
- {
- m_lRemaining = -1;
- assert(false);
- }
- }
- if (m_bBroke && ((m_lLast - m_lStart) < 20))
- m_lStart = m_lLast - 20;
- if ( (m_lLast - m_lStart) > 20)
- m_bBroke = true;
- }
- void RateMeasureTest()
- {
- /*
- long s = 0;
- long t = 0;
- CRateMeasure r(1000);
- for (int i=0; i<10; i++)
- {
- r.data_came_in(100);
- s = r.get_size_left();
- t = r.get_time_left();
- Sleep(100);
- }
- //*/
- }