Implement hash checking.
[tairent.git] / src / main / torrentclient.cpp
blobdaef9a733a990673584c9f0d78232f78faa4d6d8
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
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. *
9 * *
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. *
14 * *
15 ***************************************************************************/
17 #include "torrentclient.h"
19 #include "connection.h"
20 #include "storage.h"
21 #include "torrentmanager.h"
22 #include "trackerclient.h"
23 #include "trackermanager.h"
25 namespace Tairent
28 namespace Main
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();
40 /* }}} */
42 /* {{{ TorrentClient::~TorrentClient() */
43 TorrentClient::~TorrentClient()
45 delete trackerClient;
46 delete storage;
48 /* }}} */
50 /* {{{ TorrentClient::connectionClosed(Connection *) */
51 void TorrentClient::connectionClosed(Connection *c)
53 connections.erase(c);
54 delete c;
56 /* }}} */
58 /* {{{ TorrentClient::getInfoHash() */
59 const String &TorrentClient::getInfoHash()
61 return infoHash;
63 /* }}} */
65 /* {{{ TorrentClient::getMetaInfo() */
66 const Tairent::Core::BEncode &TorrentClient::getMetaInfo()
68 return *torrent->metaInfo;
70 /* }}} */
72 /* {{{ TorrentClient::gotBitField(Connection *) */
73 void TorrentClient::gotBitField(Connection *c)
75 storage->addBitField(c->getBitField());
76 if (storage->shouldBeInterested(c->getBitField()))
77 c->sendInterested();
79 /* }}} */
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();
87 /* }}} */
89 /* {{{ TorrentClient::gotUnchoke(Connection *) */
90 void TorrentClient::gotUnchoke(Connection *c)
92 if (c->isInterested())
93 requestMore(c);
95 /* }}} */
97 /* {{{ TorrentClient::newConnection(Connection *) */
98 void TorrentClient::newConnection(Connection *c)
100 connections.insert(c);
101 c->closedSignal.connect(Tairon::Core::methodFunctor(this, &TorrentClient::connectionClosed));
103 /* }}} */
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);
109 requestMore(c);
111 /* }}} */
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);
119 /* }}} */
121 /* {{{ TorrentClient::requestMore(Connection *) */
122 void TorrentClient::requestMore(Connection *c)
124 uint32_t index;
125 uint32_t start;
126 uint32_t length;
128 if (storage->pickPiece(c->getBitField(), index, start, length))
129 c->sendRequest(index, start, length);
130 else
131 c->sendNotInterested();
133 /* }}} */
135 /* {{{ TorrentClient::stop() */
136 void TorrentClient::stop()
138 trackerClient->stop();
139 // TODO: stop connections
141 /* }}} */
143 }; // namespace Main
145 }; // namespace Tairent
147 // vim: ai sw=4 ts=4 noet fdm=marker