avoid adding the same nodes to dht subsys all the time

This commit is contained in:
Miguel Freitas 2013-11-14 15:07:47 -02:00
parent 9c93f1c377
commit ba0b4c8613
3 changed files with 22 additions and 4 deletions

View File

@ -11,6 +11,7 @@
#include "init.h"
#include "base58.h"
#include "main.h"
#include "twister.h"
using namespace std;
using namespace boost;
@ -77,6 +78,7 @@ Value getinfo(const Array& params, bool fHelp)
obj.push_back(Pair("blocks", (int)nBestHeight));
obj.push_back(Pair("timeoffset", (boost::int64_t)GetTimeOffset()));
obj.push_back(Pair("connections", (int)vNodes.size()));
obj.push_back(Pair("dht_nodes", getDhtNodes()));
obj.push_back(Pair("addrman_total", (int)addrman.size()));
obj.push_back(Pair("addrman_get", (int)addrman.GetAddr().size()));
obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string())));

View File

@ -316,6 +316,14 @@ void saveTorrentResumeData()
}
}
int getDhtNodes()
{
if( !ses )
return 0;
session_status ss = ses->status();
return ss.dht_nodes;
}
void ThreadMaintainDHTNodes()
{
RenameThread("maintain-dht-nodes");
@ -325,20 +333,25 @@ void ThreadMaintainDHTNodes()
}
int64 lastSaveResumeTime = GetTime();
int lastTotalNodesCandidates = 0;
while(1) {
session_status ss = ses->status();
int dht_nodes = ss.dht_nodes;
bool nodesAdded = false;
int vNodesSize = 0;
if( ses ) {
{
LOCK(cs_vNodes);
vNodesSize = vNodes.size();
}
if( !ses->is_paused() ) {
vector<CAddress> vAddr = addrman.GetAddr();
int totalNodesCandidates = (int)(vNodesSize + vAddr.size());
if( (!dht_nodes && totalNodesCandidates) ||
(dht_nodes < 5 && totalNodesCandidates > 10) ) {
if( ((!dht_nodes && totalNodesCandidates) ||
(dht_nodes < 5 && totalNodesCandidates > 10)) &&
totalNodesCandidates != lastTotalNodesCandidates ) {
lastTotalNodesCandidates = totalNodesCandidates;
printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n");
BOOST_FOREACH(const CAddress &a, vAddr) {
std::string addr = a.ToStringIP();
@ -347,6 +360,7 @@ void ThreadMaintainDHTNodes()
ses->add_dht_node(std::pair<std::string, int>(addr, port));
nodesAdded = true;
}
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes) {
// if !fInbound we created this connection so ip is reachable.
// we can't use port number of inbound connection, so try standard port.

View File

@ -37,4 +37,6 @@ void receivedSpamMessage(std::string const &message, std::string const &user);
int getBestHeight();
bool shouldDhtResourceExpire(std::string resource, bool multi, int height);
int getDhtNodes();
#endif // TWISTER_H