summaryrefslogtreecommitdiffstats
path: root/libktorrent/kademlia/announcetask.cpp
blob: b7350a2b62cc8a8e4c14d32981c0c29e3dc5a58a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#include <util/log.h>
#include <torrent/globals.h>
#include "announcetask.h"
#include "node.h"
#include "pack.h"

using namespace bt;

namespace dht
{

	AnnounceTask::AnnounceTask(Database* db,RPCServer* rpc, Node* node,const dht::Key & info_hash,bt::Uint16 port)
	: Task(rpc, node),info_hash(info_hash),port(port),db(db)
	{}


	AnnounceTask::~AnnounceTask()
	{}


	void AnnounceTask::callFinished(RPCCall* c, MsgBase* rsp)
	{
	//	Out() << "AnnounceTask::callFinished" << endl;
		// if we do not have a get peers response, return
		// announce_peer's response are just empty anyway
		if (c->getMsgMethod() != dht::GET_PEERS)
			return;
		
		// it is either a GetPeersNodesRsp or a GetPeersValuesRsp
		GetPeersRsp* gpr = dynamic_cast<GetPeersRsp*>(rsp);
		if (!gpr)
			return;
		
		if (gpr->containsNodes())
		{
			const QByteArray & n = gpr->getData();
			Uint32 nval = n.size() / 26;
			for (Uint32 i = 0;i < nval;i++)
			{
				// add node to todo list
				KBucketEntry e = UnpackBucketEntry(n,i*26);
				if (!todo.contains(e) && !visited.contains(e) && 
					todo.count() < 100)
				{
					todo.append(e);
				}
			}
		}
		else
		{
			// store the items in the database
			const DBItemList & items = gpr->getItemList();
			for (DBItemList::const_iterator i = items.begin();i != items.end();i++)
			{
				db->store(info_hash,*i);
				// also add the items to the returned_items list
				returned_items.append(*i);
			}
			
			// add the peer who responded to the answered list, so we can do an announce
			KBucketEntry e(rsp->getOrigin(),rsp->getID());
			if (!answered.contains(KBucketEntryAndToken(e,gpr->getToken())) && !answered_visited.contains(e))
			{
				answered.append(KBucketEntryAndToken(e,gpr->getToken()));
			}
			
			emitDataReady();
		}
	}

	void AnnounceTask::callTimeout(RPCCall* )
	{
		//Out() << "AnnounceTask::callTimeout " << endl;
	}

	void AnnounceTask::update()
	{
/*		Out() << "AnnounceTask::update " << endl;
		Out() << "todo " << todo.count() << " ; answered " << answered.count() << endl;
		Out() << "visited " << visited.count() << " ; answered_visited " << answered_visited.count() << endl;
	*/
		while (!answered.empty() && canDoRequest())
		{
			KBucketEntryAndToken & e = answered.first();
			if (!answered_visited.contains(e))
			{
				AnnounceReq* anr = new AnnounceReq(node->getOurID(),info_hash,port,e.getToken());
				anr->setOrigin(e.getAddress());
				rpcCall(anr);
				answered_visited.append(e);
			}
			answered.pop_front();
		}
		
		// go over the todo list and send get_peers requests
		// until we have nothing left
		while (!todo.empty() && canDoRequest())
		{
			KBucketEntry e = todo.first();
			// onLy send a findNode if we haven't allrready visited the node
			if (!visited.contains(e))
			{
				// send a findNode to the node
				GetPeersReq* gpr = new GetPeersReq(node->getOurID(),info_hash);
				gpr->setOrigin(e.getAddress());
				rpcCall(gpr);
				visited.append(e);
			}
			// remove the entry from the todo list
			todo.pop_front();
		}
		
		if (todo.empty() && answered.empty() && getNumOutstandingRequests() == 0 && !isFinished())
		{
			Out(SYS_DHT|LOG_NOTICE) << "DHT: AnnounceTask done" << endl;
			done();
		}
		else if (answered_visited.count() >= dht::K)
		{
			// if K announces have occurred stop
			Out(SYS_DHT|LOG_NOTICE) << "DHT: AnnounceTask done" << endl;
			done();
		}
	}

	bool AnnounceTask::takeItem(DBItem & item)
	{
		if (returned_items.empty())
			return false;
		
		item = returned_items.first();
		returned_items.pop_front();
		return true;	
	}
}