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

« back to all changes in this revision

Viewing changes to src/peer/peerconnector.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:
19
19
 ***************************************************************************/
20
20
 
21
21
#include "peerconnector.h"
 
22
#include <QSet>
22
23
#include <interfaces/serverinterface.h>
23
24
#include <mse/encryptedauthenticate.h>
24
25
#include <torrent/torrent.h>
28
29
 
29
30
namespace bt
30
31
{
31
 
        
32
 
        PeerConnector::PeerConnector(const QString & ip,Uint16 port,bool local,PeerManager* pman) 
33
 
                : QObject(pman),ip(ip),port(port),local(local),pman(pman),auth(0),stopping(false)
 
32
        static ResourceManager half_open_connections(50);
 
33
        
 
34
        class PeerConnector::Private
 
35
        {
 
36
        public:
 
37
                Private(PeerConnector* p,const QString & ip,Uint16 port,bool local,PeerManager* pman)
 
38
                : p(p),ip(ip),port(port),local(local),pman(pman),stopping(false),do_not_start(false)
 
39
                {
 
40
                }
 
41
                
 
42
                ~Private()
 
43
                {
 
44
                        if (auth.data())
 
45
                        {
 
46
                                stopping = true;
 
47
                                auth.data()->stop();
 
48
                                stopping = false;
 
49
                        }
 
50
                }
 
51
                
 
52
                void start(Method method);
 
53
                void authenticationFinished(Authenticate* auth, bool ok);
 
54
                
 
55
        public:
 
56
                PeerConnector* p;
 
57
                QSet<Method> tried_methods;
 
58
                Method current_method;
 
59
                QString ip;
 
60
                Uint16 port;
 
61
                bool local;
 
62
                QWeakPointer<PeerManager> pman;
 
63
                QWeakPointer<Authenticate> auth;
 
64
                bool stopping;
 
65
                bool do_not_start;
 
66
                PeerConnector::WPtr self;
 
67
        };
 
68
        
 
69
        PeerConnector::PeerConnector(const QString& ip, Uint16 port, bool local, bt::PeerManager* pman) 
 
70
                : Resource(&half_open_connections,pman->getTorrent().getInfoHash().toString()),
 
71
                d(new Private(this,ip,port,local,pman))
34
72
        {
35
73
        }
36
74
 
37
75
        PeerConnector::~PeerConnector()
38
76
        {
39
 
                if (auth)
40
 
                {
41
 
                        stopping = true;
42
 
                        auth->stop();
43
 
                        stopping = false;
44
 
                }
 
77
                delete d;
 
78
        }
 
79
        
 
80
        void PeerConnector::setWeakPointer(PeerConnector::WPtr ptr)
 
81
        {
 
82
                d->self = ptr;
 
83
        }
 
84
 
 
85
        
 
86
        void PeerConnector::setMaxActive(Uint32 mc)
 
87
        {
 
88
                half_open_connections.setMaxActive(mc);
45
89
        }
46
90
        
47
91
        void PeerConnector::start()
48
92
        {
 
93
                half_open_connections.add(this);
 
94
        }
 
95
        
 
96
        void PeerConnector::acquired()
 
97
        {
 
98
                PeerManager* pm = d->pman.data();
 
99
                if (!pm || !pm->isStarted())
 
100
                        return;
 
101
                
49
102
                bool encryption = ServerInterface::isEncryptionEnabled();
50
103
                bool utp = ServerInterface::isUtpEnabled();
51
104
                
52
105
                if (encryption)
53
106
                {
54
107
                        if (utp)
55
 
                                start(UTP_WITH_ENCRYPTION);
 
108
                                d->start(UTP_WITH_ENCRYPTION);
56
109
                        else
57
 
                                start(TCP_WITH_ENCRYPTION);
 
110
                                d->start(TCP_WITH_ENCRYPTION);
58
111
                }
59
112
                else
60
113
                {
61
114
                        if (utp)
62
 
                                start(UTP_WITHOUT_ENCRYPTION);
 
115
                                d->start(UTP_WITHOUT_ENCRYPTION);
63
116
                        else
64
 
                                start(TCP_WITHOUT_ENCRYPTION);
65
 
                }
66
 
        }
67
 
        
68
 
        void PeerConnector::stop()
69
 
        {
70
 
                if (auth)
71
 
                {
72
 
                        stopping = true;
73
 
                        auth->stop();
74
 
                        stopping = false;
75
 
                }
76
 
        }
77
 
 
78
 
 
 
117
                                d->start(TCP_WITHOUT_ENCRYPTION);
 
118
                }
 
119
        }
79
120
 
80
121
        void PeerConnector::authenticationFinished(Authenticate* auth, bool ok)
81
122
        {
82
 
                this->auth = 0;
 
123
                d->authenticationFinished(auth,ok);
 
124
        }
 
125
        
 
126
        void PeerConnector::Private::authenticationFinished(Authenticate* auth, bool ok)
 
127
        {
 
128
                this->auth.clear();
83
129
                if (stopping)
84
130
                        return;
85
131
                
 
132
                PeerManager* pm = pman.data();
 
133
                if (!pm)
 
134
                        return;
 
135
                
86
136
                if (ok)
87
137
                {
88
 
                        pman->peerAuthenticated(auth,this,ok);
 
138
                        pm->peerAuthenticated(auth,self,ok);
89
139
                        return;
90
140
                }
91
141
                
111
161
                        allowed.removeAll(m);
112
162
                
113
163
                if (allowed.isEmpty())
114
 
                        pman->peerAuthenticated(auth,this,false);
 
164
                        pm->peerAuthenticated(auth,self,false);
115
165
                else
116
166
                        start(allowed.front());
117
167
        }
118
168
 
119
 
        void PeerConnector::start(PeerConnector::Method method)
 
169
        void PeerConnector::Private::start(PeerConnector::Method method)
120
170
        {
 
171
                PeerManager* pm = pman.data();
 
172
                if (!pm)
 
173
                        return;
 
174
                
121
175
                current_method = method;
122
 
                Torrent & tor = pman->getTorrent();
 
176
                const Torrent & tor = pm->getTorrent();
123
177
                TransportProtocol proto = (method == TCP_WITH_ENCRYPTION || method == TCP_WITHOUT_ENCRYPTION) ? TCP : UTP;
124
178
                if (method == TCP_WITH_ENCRYPTION || method == UTP_WITH_ENCRYPTION)
125
 
                        auth = new mse::EncryptedAuthenticate(ip,port,proto,tor.getInfoHash(),tor.getPeerID(),this);
 
179
                        auth = new mse::EncryptedAuthenticate(ip,port,proto,tor.getInfoHash(),tor.getPeerID(),self);
126
180
                else
127
 
                        auth = new Authenticate(ip,port,proto,tor.getInfoHash(),tor.getPeerID(),this);
 
181
                        auth = new Authenticate(ip,port,proto,tor.getInfoHash(),tor.getPeerID(),self);
128
182
                
129
183
                if (local)
130
 
                        auth->setLocal(true);
 
184
                        auth.data()->setLocal(true);
131
185
                
132
 
                AuthenticationMonitor::instance().add(auth);
 
186
                AuthenticationMonitor::instance().add(auth.data());
133
187
        }
134
188
 
135
189
}