コード例 #1
0
	void NodeLookup::update()
	{
	//	Out() << "NodeLookup::update" << endl;
	//	Out() << "todo = " << todo.count() << " ; visited = " << visited.count() << endl;
		// go over the todo list and send find node calls
		// 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
				FindNodeReq* fnr = new FindNodeReq(node->getOurID(),node_id);
				fnr->setOrigin(e.getAddress());
				rpcCall(fnr);
				visited.append(e);
			}
			// remove the entry from the todo list
			todo.pop_front();
		}
		
		if (todo.empty() && getNumOutstandingRequests() == 0 && !isFinished())
			done();
		else if (num_nodes_rsp > 50)
			done(); // quit after 50 nodes responses
	}
コード例 #2
0
	void KClosestNodesSearch::tryInsert(const KBucketEntry & e)
	{
		// calculate distance between key and e
		dht::Key d = dht::Key::distance(key,e.getID());
		
		if (emap.size() < max_entries)
		{
			// room in the map so just insert
			emap.insert(std::make_pair(d,e));
		}
		else
		{
			// now find the max distance
			// seeing that the last element of the map has also 
			// the biggest distance to key (std::map is sorted on the distance)
			// we just take the last
			const dht::Key & max = emap.rbegin()->first;
			if (d < max)
			{
				// insert if d is smaller then max
				emap.insert(std::make_pair(d,e));
				// erase the old max value
				emap.erase(max);
			}
		}
		
	}
コード例 #3
0
	void NodeLookup::callFinished(RPCCall* ,MsgBase* rsp)
	{
	//	Out() << "NodeLookup::callFinished" << endl;
		if (isFinished())
			return;
		
		// check the response and see if it is a good one
		if (rsp->getMethod() == dht::FIND_NODE && rsp->getType() == dht::RSP_MSG)
		{
			FindNodeRsp* fnr = (FindNodeRsp*)rsp;
			const QByteArray & nodes = fnr->getNodes();
			Uint32 nnodes = nodes.size() / 26;
			for (Uint32 j = 0;j < nnodes;j++)
			{
				// unpack an entry and add it to the todo list
				KBucketEntry e = UnpackBucketEntry(nodes,j*26);
				// lets not talk to ourself
				if (e.getID() != node->getOurID() && !todo.contains(e) && !visited.contains(e))
					todo.append(e);
			}
			num_nodes_rsp++;
		}
	}