~ubuntu-branches/ubuntu/oneiric/libktorrent/oneiric

« back to all changes in this revision

Viewing changes to src/torrent/advancedchokealgorithm.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2011-05-26 00:33:38 UTC
  • mfrom: (5.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110526003338-2r4zwyzn8bovsxay
Tags: 1.1.1-2
Release to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
 
125
125
        void AdvancedChokeAlgorithm::doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const TorrentStats & stats)
126
126
        {
127
 
                QList<Peer*> ppl;
128
 
                Uint32 np = pman.getNumConnectedPeers();
129
 
                // add all non seeders
130
 
                for (Uint32 i = 0;i < np;i++)
 
127
                QList<Peer*> ppl = pman.getPeers();
 
128
                for (QList<Peer*>::iterator i = ppl.begin();i != ppl.end();)
131
129
                {
132
 
                        Peer* p = pman.getPeer(i);
133
 
                        if (p)
 
130
                        Peer* p = *i;
 
131
                        if (!calcACAScore(p,cman,stats))
134
132
                        {
135
 
                                if (calcACAScore(p,cman,stats))
136
 
                                        ppl.append(p);
137
 
                                else
138
 
                                        // choke seeders they do not want to download from us anyway
139
 
                                        p->choke();
 
133
                                // choke seeders they do not want to download from us anyway
 
134
                                p->choke();
 
135
                                i = ppl.erase(i);
140
136
                        }
 
137
                        else
 
138
                                i++;
141
139
                }
142
140
                
143
141
                // sort list by ACA score
180
178
 
181
179
        void AdvancedChokeAlgorithm::doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const TorrentStats & stats)
182
180
        {
183
 
                QList<Peer*> ppl;
184
 
                Uint32 np = pman.getNumConnectedPeers();
185
 
                // add all non seeders
186
 
                for (Uint32 i = 0;i < np;i++)
 
181
                QList<Peer*> ppl = pman.getPeers();
 
182
                for (QList<Peer*>::iterator i = ppl.begin();i != ppl.end();)
187
183
                {
188
 
                        Peer* p = pman.getPeer(i);
189
 
                        if (p)
 
184
                        Peer* p = *i;
 
185
                        if (!calcACAScore(p,cman,stats))
190
186
                        {
191
 
                                // update the ACA score in the process
192
 
                                if (calcACAScore(p,cman,stats))
193
 
                                        ppl.append(p);
194
 
                                else
195
 
                                        // choke seeders they do not want to download from us anyway
196
 
                                        p->choke();  
 
187
                                // choke seeders they do not want to download from us anyway
 
188
                                p->choke();
 
189
                                i = ppl.erase(i);
197
190
                        }
 
191
                        else
 
192
                                i++;
198
193
                }
 
194
                
199
195
                qSort(ppl.begin(), ppl.end(),UploadRateGreaterThan);
200
196
                
201
197
                doUnchoking(ppl,updateOptimisticPeer(pman,ppl));
203
199
        
204
200
        static Uint32 FindPlannedOptimisticUnchokedPeer(PeerManager& pman,const QList<Peer*> & ppl)
205
201
        {
206
 
                Uint32 num_peers = pman.getNumConnectedPeers();
 
202
                Uint32 num_peers = ppl.size();
207
203
                if (num_peers == 0)
208
204
                        return UNDEFINED_ID;
209
 
                        
 
205
                
210
206
                // find a random peer that is choked and interested
211
207
                Uint32 start = KRandom::random() % num_peers;
212
208
                Uint32 i = (start + 1) % num_peers;
213
209
                while (i != start)
214
210
                {
215
 
                        Peer* p = pman.getPeer(i);
 
211
                        Peer* p = ppl.at(i);
216
212
                        if (p && p->isChoked() && p->isInterested() && !p->isSeeder() && ppl.contains(p))
217
213
                                return p->getID();
218
214
                        i = (i + 1) % num_peers;