~ubuntu-branches/ubuntu/precise/amule-adunanza/precise

« back to all changes in this revision

Viewing changes to src/DownloadClient.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-02-18 21:16:23 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100218211623-gptwe60zx1knfkmu
Tags: 2010.1+2.2.6-0ubuntu1
* New upstream release (LP: #524697)
  - Drop manpages_spelling_fixes.diff fixed by upstream
  - Drop cryptopp-reference.diff fixed by upstream
  - Bump Standards-Version no changes required
  - Update install files (amule -> amuleadunanza)
  - debian/rules: amule.xpm -> amuleadunanza.xpm
  - Add README.Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//
2
2
// This file is part of the aMule Project.
3
3
//
4
 
// Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
 
4
// Copyright (c) 2003-2009 aMule Team ( admin@amule.org / http://www.amule.org )
5
5
// Copyright (c) 2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6
6
//
7
7
// Any parts of this program derived from the xMule, lMule or eMule project,
154
154
        m_bUDPPending = false;
155
155
        m_dwLastAskedTime = ::GetTickCount();
156
156
        SetDownloadState(DS_CONNECTING);
 
157
// ADUNANZA BEGIN
 
158
// Backport
 
159
        SetSentCancelTransfer(0);
 
160
// ADUNANZA END
157
161
        return TryToConnect();
158
162
}
159
163
 
525
529
// ADUNANZA BEGIN
526
530
#if 0 
527
531
                        m_reqfile->ClientStateChanged( m_nDownloadState, byNewState );
528
 
#endif
 
532
#else
529
533
                        m_reqfile->ClientStateChanged( m_nDownloadState, byNewState, this );
 
534
#endif
530
535
// ADUNANZA END                 
531
536
                
532
537
                        if (byNewState == DS_DOWNLOADING) {
536
541
                        }
537
542
                }
538
543
                if (byNewState == DS_DOWNLOADING) {
 
544
                        msReceivedPrev = GetTickCount();
539
545
                        theStats::AddDownloadingSource();
540
546
                } else if (m_nDownloadState == DS_DOWNLOADING) {
541
547
                        theStats::RemoveDownloadingSource();
618
624
                // Requested_Block_Struct* toadd[count];
619
625
                std::vector<Requested_Block_Struct*> toadd(count);
620
626
 
621
 
                bool bIsAvailable = m_reqfile->GetNextRequestedBlock(this, &(toadd[0]), &count);
 
627
                bool bIsAvailable = m_reqfile->GetNextRequestedBlock(this, toadd, count);
622
628
                //dazzle: if no blocks available, try to drop the slowest source, then try again
623
629
                if (!bIsAvailable) {
624
 
                        if (bSourceHasBeenDropped = m_reqfile->DropSlowestSource(this)) {
 
630
                        bSourceHasBeenDropped = m_reqfile->DropSlowestSource(this);
 
631
                        if (bSourceHasBeenDropped) {
625
632
                                // riporto count al valore iniziale e ri-provo
626
633
                                count = countinit;
627
 
                                bIsAvailable = m_reqfile->GetNextRequestedBlock(this, &(toadd[0]), &count);
 
634
                                bIsAvailable = m_reqfile->GetNextRequestedBlock(this, toadd, count);
628
635
                        }
629
636
                }
630
637
 
652
659
                        m_PendingBlocks_list.push_back(pblock);
653
660
                        m_DownloadBlocks_list.pop_front();
654
661
                } catch (const std::bad_alloc& ba) {
655
 
                        std::cerr << "Allocation failed in CUpDownClient::CreateBlockRequests" << std::endl;
 
662
                        std::cerr << "Allocation failed in CUpDownClient::CreateBlockRequests: " << ba.what() << std::endl;
656
663
                        return bSourceHasBeenDropped ;
657
664
                }
658
665
 
707
714
// rilascio della fonte NON con download rate sotto la media (come l'originale)
708
715
// ma con rate inferiore a tutti gli altri
709
716
//
 
717
// ADUNANZA END
710
718
        if (m_DownloadBlocks_list.empty()) {
711
719
                // Barry - instead of getting 3, just get how many is needed
712
720
                uint16 count = m_MaxBlockRequests - m_PendingBlocks_list.size();
713
 
                std::vector<Requested_Block_Struct*> toadd(count);
714
 
                if (m_reqfile->GetNextRequestedBlock(this,&(toadd[0]),&count)) {
 
721
                std::vector<Requested_Block_Struct*> toadd;
 
722
                if (m_reqfile->GetNextRequestedBlock(this, toadd, count)) {
715
723
                        for (int i = 0; i != count; i++) {
716
724
                                m_DownloadBlocks_list.push_back(toadd[i]);
717
725
                        }
732
740
        }
733
741
        
734
742
        
 
743
// ADUNANZA BEGIN
735
744
// Segue parte del codice della patch di Adunanza (in attesa di eventuali ulterori ritocchi)
736
745
//
737
746
#if 0   
777
786
                if (thePrefs::GetDropSlowSources()) {           
778
787
                        slower_client = m_reqfile->GetSlowerDownloadingClient(m_lastaverage, this);
779
788
                }
780
 
#endif
 
789
#else
781
790
                slower_client = m_reqfile->GetSlowestSource();
 
791
#endif
782
792
// ADUNANZA TEST MR_HYDE END
783
793
                
784
794
                if (slower_client == NULL) {
804
814
                        wxASSERT(m_DownloadBlocks_list.empty());
805
815
                        wxASSERT(m_PendingBlocks_list.empty());
806
816
                        uint16 count = m_MaxBlockRequests;
807
 
                        std::vector<Requested_Block_Struct*> toadd(count);
808
 
                        if (m_reqfile->GetNextRequestedBlock(this, &(toadd[0]),&count)) {
 
817
                        std::vector<Requested_Block_Struct*> toadd;
 
818
                        if (m_reqfile->GetNextRequestedBlock(this, toadd, count)) {
809
819
                                for (int i = 0; i != count; i++) {
810
820
                                        Pending_Block_Struct* pblock = new Pending_Block_Struct;
811
821
                                        pblock->block = toadd[i];
817
827
                                }
818
828
                        }       else {
819
829
                                // WTF, we just freed blocks.
 
830
// ADUNANZA BEGIN
 
831
// Questa ASSERT e' stata segnalata anche sul forum di aMule,
 
832
// per ora la commento (potrei anche NON avere alcun nuovo blocco
 
833
// da scaricare)
 
834
#if 0
820
835
                                wxASSERT(0);
 
836
#endif                          
 
837
// ADUNANZA END
821
838
                                return;
822
839
                        }
823
840
                } else {
1028
1045
// ADUNANZA BEGIN
1029
1046
#if 0           
1030
1047
                theStats::AddDownloadFromSoft(GetClientSoft(),size - header_size);
1031
 
#endif  
 
1048
#else
1032
1049
                // mod Adu
1033
1050
                // Emanem
1034
1051
                // Aggiungo i dati trasferiti all'elenco AdunanzA
1042
1059
                        }
1043
1060
                }
1044
1061
                theStats::AddDownloadFromSoft(GetClientSoft(), (size - header_size), aduBytes, aduFBytes);
 
1062
#endif
1045
1063
// ADUNANZA END         
1046
1064
        
1047
1065
                bytesReceivedCycle += size - header_size;
1314
1332
}
1315
1333
 
1316
1334
 
1317
 
// Emilio: rewrite of eMule code to eliminate use of lists for averaging and fix
1318
 
// errors in calculation (32-bit rollover and time measurement)  This function 
1319
 
// uses a first-order filter with variable time constant (initially very short 
1320
 
// to quickly reach the right value without spiking, then gradually approaching 
1321
 
// the value of 50 seconds which is equivalent to the original averaging period 
1322
 
// used in eMule).  The download rate is measured using actual timestamps.  The 
1323
 
// filter-based averaging however uses a simplified algorithm that assumes a 
1324
 
// fixed loop time - this does not introduce any measurement error, it simply 
1325
 
// makes the degree of smoothing slightly imprecise (the true TC of the filter 
1326
 
// varies inversely with the true loop time), which is of no importance here.
 
1335
// Speed is now updated only when data was received, calculated as
 
1336
// (data received) / (time since last receiption)
 
1337
// and slightly filtered (10s average).
 
1338
// Result is quite precise now and makes the DownloadRateAdjust workaround obsolete.
1327
1339
 
1328
1340
float CUpDownClient::CalculateKBpsDown()
1329
1341
{
1330
 
                                                                                                // -- all timing values are in seconds --
1331
 
        const   float tcLoop   =  0.1f;                         // _assumed_ Process() loop time = 0.1 sec
1332
 
        const   float tcInit   =  0.4f;                         // initial filter time constant
1333
 
        const   float tcFinal  = 50.0f;                         // final filter time constant
1334
 
        const   float tcReduce =  5.0f;                         // transition from tcInit to tcFinal
1335
 
        
1336
 
        const   float fInit  = tcLoop/tcInit;           // initial averaging factor
1337
 
        const   float fFinal = tcLoop/tcFinal;          // final averaging factor
1338
 
        const   float fReduce = std::exp(std::log(fFinal/fInit) / (tcReduce/tcLoop)) * 0.99999;
1339
 
        
1340
 
        uint32  msCur = ::GetTickCount();
 
1342
        const   float tAverage = 10.0;
 
1343
        uint32  msCur = GetTickCount();
1341
1344
 
1342
 
        if (msReceivedPrev == 0) {  // initialize the averaging filter
1343
 
                fDownAvgFilter = fInit;
1344
 
                // "kBpsDown =  bytesReceivedCycle/1024.0 / tcLoop"  would be technically correct,
1345
 
                // but the first loop often receives a large chunk of data and then produces a spike
1346
 
                kBpsDown = /* 0.0 * (1.0-fInit) + */ bytesReceivedCycle/1024.0 / tcLoop * fInit;
1347
 
                bytesReceivedCycle = 0;
1348
 
        } else if (msCur != msReceivedPrev) {   // (safeguard against divide-by-zero)
1349
 
                if (fDownAvgFilter > fFinal) {          // reduce time constant during ramp-up phase
1350
 
                        fDownAvgFilter *= fReduce;              // this approximates averaging a lengthening list
1351
 
                }
1352
 
                kBpsDown = kBpsDown * (1.0 - fDownAvgFilter) 
1353
 
                                        + (bytesReceivedCycle/1.024)/((float)(msCur-msReceivedPrev)) * fDownAvgFilter;
1354
 
                bytesReceivedCycle = 0;
 
1345
        if (bytesReceivedCycle) {
 
1346
                float dt = (msCur - msReceivedPrev) / 1000.0; // time since last reception
 
1347
                if (dt < 0.01) {        // (safeguard against divide-by-zero)
 
1348
                        dt = 0.01f;             //  diff should be 100ms actually
 
1349
                }
 
1350
                float kBpsDownCur = bytesReceivedCycle / 1024.0 / dt;
 
1351
                if (dt >= tAverage) {
 
1352
                        kBpsDown = kBpsDownCur;
 
1353
                } else {
 
1354
                        kBpsDown = (kBpsDown * (tAverage - dt) + kBpsDownCur * dt) / tAverage;
 
1355
                }
 
1356
                AddDebugLogLineM( false, logLocalClient, CFormat(wxT("CalculateKBpsDown %p kbps %.1f kbpsCur %.1f dt %.3f rcv %d ")) 
 
1357
                                        % this % kBpsDown  % kBpsDownCur % dt % bytesReceivedCycle);
 
1358
                bytesReceivedCycle = 0;
 
1359
                msReceivedPrev = msCur; 
1355
1360
        }
1356
 
        msReceivedPrev = msCur; 
1357
1361
 
1358
1362
        m_cShowDR++;
1359
1363
        if (m_cShowDR == 30){
1360
1364
                m_cShowDR = 0;
1361
1365
                UpdateDisplayedInfo();
1362
1366
        }
1363
 
        if ((::GetTickCount() - m_dwLastBlockReceived) > DOWNLOADTIMEOUT){
 
1367
        if (msCur - m_dwLastBlockReceived > DOWNLOADTIMEOUT) {
1364
1368
                if (!GetSentCancelTransfer()){
1365
1369
                        CPacket* packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
1366
1370
                        theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
1700
1704
// ADUNANZA BEGIN
1701
1705
#if 0
1702
1706
                        m_reqfile->ClientStateChanged( GetDownloadState(), -1 );
1703
 
#endif
 
1707
#else
1704
1708
                        m_reqfile->ClientStateChanged( GetDownloadState(), -1, this );
 
1709
#endif
1705
1710
// ADUNANZA END
1706
1711
                        m_reqfile->UpdatePartsFrequency( this, false );
1707
1712
                }
1716
1721
// ADUNANZA BEGIN
1717
1722
#if 0
1718
1723
                        m_reqfile->ClientStateChanged( -1, GetDownloadState() );
1719
 
#endif
 
1724
#else
1720
1725
                        m_reqfile->ClientStateChanged( -1, GetDownloadState(), this );
 
1726
#endif
1721
1727
// ADUNANZA END
1722
1728
 
1723
1729
                        m_nPartCount = reqfile->GetPartCount();