Browse Source

avoid adding the same nodes to dht subsys all the time

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
ba0b4c8613
  1. 2
      src/rpcwallet.cpp
  2. 22
      src/twister.cpp
  3. 2
      src/twister.h

2
src/rpcwallet.cpp

@ -11,6 +11,7 @@
#include "init.h" #include "init.h"
#include "base58.h" #include "base58.h"
#include "main.h" #include "main.h"
#include "twister.h"
using namespace std; using namespace std;
using namespace boost; 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("blocks", (int)nBestHeight));
obj.push_back(Pair("timeoffset", (boost::int64_t)GetTimeOffset())); obj.push_back(Pair("timeoffset", (boost::int64_t)GetTimeOffset()));
obj.push_back(Pair("connections", (int)vNodes.size())); 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_total", (int)addrman.size()));
obj.push_back(Pair("addrman_get", (int)addrman.GetAddr().size())); obj.push_back(Pair("addrman_get", (int)addrman.GetAddr().size()));
obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string()))); obj.push_back(Pair("proxy", (proxy.first.IsValid() ? proxy.first.ToStringIPPort() : string())));

22
src/twister.cpp

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

2
src/twister.h

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

Loading…
Cancel
Save