1 /***************************************************************************
3 * Copyright (C) 2006 David Brodsky *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
15 ***************************************************************************/
17 #include "torrentclient.h"
19 #include "connection.h"
21 #include "torrentmanager.h"
22 #include "trackerclient.h"
23 #include "trackermanager.h"
31 /* {{{ TorrentClient::TorrentClient(TorrentStruct *, const String &) */
32 TorrentClient::TorrentClient(TorrentStruct
*t
, const String
&ih
) : downloaded(0), infoHash(ih
), torrent(t
)
34 storage
= new Storage(getMetaInfo()["info"]);
35 storage
->pieceDownloadedSignal
.connect(Tairon::Core::methodFunctor(this, &TorrentClient::pieceDownloadedAndChecked
));
37 trackerClient
= TrackerManager::self()->getTracker(this);
38 trackerClient
->getPeers();
42 /* {{{ TorrentClient::~TorrentClient() */
43 TorrentClient::~TorrentClient()
50 /* {{{ TorrentClient::connectionClosed(Connection *) */
51 void TorrentClient::connectionClosed(Connection
*c
)
58 /* {{{ TorrentClient::getInfoHash() */
59 const String
&TorrentClient::getInfoHash()
65 /* {{{ TorrentClient::getMetaInfo() */
66 const Tairent::Core::BEncode
&TorrentClient::getMetaInfo()
68 return *torrent
->metaInfo
;
72 /* {{{ TorrentClient::gotBitField(Connection *) */
73 void TorrentClient::gotBitField(Connection
*c
)
75 storage
->addBitField(c
->getBitField());
76 if (storage
->shouldBeInterested(c
->getBitField()))
81 /* {{{ TorrentClient::gotPiece(uint32_t, uint32_t, Connection *) */
82 void TorrentClient::gotPiece(uint32_t index
, uint32_t start
, Connection
*c
)
84 storage
->gotPiece(index
, start
, c
);
85 downloaded
+= c
->getPieceLength();
89 /* {{{ TorrentClient::gotUnchoke(Connection *) */
90 void TorrentClient::gotUnchoke(Connection
*c
)
92 if (c
->isInterested())
97 /* {{{ TorrentClient::newConnection(Connection *) */
98 void TorrentClient::newConnection(Connection
*c
)
100 connections
.insert(c
);
101 c
->closedSignal
.connect(Tairon::Core::methodFunctor(this, &TorrentClient::connectionClosed
));
105 /* {{{ TorrentClient::pieceDownloaded(uint32_t, uint32_t, Connection *) */
106 void TorrentClient::pieceDownloaded(uint32_t index
, uint32_t start
, Connection
*c
)
108 storage
->pieceDownloaded(index
, start
, c
);
113 /* {{{ TorrentClient::pieceDownloadedAndChecked(uint32_t) */
114 void TorrentClient::pieceDownloadedAndChecked(uint32_t index
)
116 for (std::set
<Connection
*>::const_iterator it
= connections
.begin(); it
!= connections
.end(); ++it
)
117 (*it
)->sendHave(index
);
121 /* {{{ TorrentClient::requestMore(Connection *) */
122 void TorrentClient::requestMore(Connection
*c
)
128 if (storage
->pickPiece(c
->getBitField(), index
, start
, length
))
129 c
->sendRequest(index
, start
, length
);
131 c
->sendNotInterested();
135 /* {{{ TorrentClient::stop() */
136 void TorrentClient::stop()
138 trackerClient
->stop();
139 // TODO: stop connections
145 }; // namespace Tairent
147 // vim: ai sw=4 ts=4 noet fdm=marker