Browse Source

net: move added node functions to CConnman

0.14
Cory Fields 9 years ago
parent
commit
8ae2dac1c6
  1. 29
      src/net.cpp
  2. 28
      src/net.h
  3. 17
      src/rpc/net.cpp

29
src/net.cpp

@ -91,9 +91,6 @@ std::vector<CNode*> vNodes; @@ -91,9 +91,6 @@ std::vector<CNode*> vNodes;
CCriticalSection cs_vNodes;
limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
std::vector<std::string> vAddedNodes;
CCriticalSection cs_vAddedNodes;
NodeId nLastNodeId = 0;
CCriticalSection cs_nLastNodeId;
@ -1718,7 +1715,7 @@ void CConnman::ThreadOpenConnections() @@ -1718,7 +1715,7 @@ void CConnman::ThreadOpenConnections()
}
}
std::vector<AddedNodeInfo> GetAddedNodeInfo()
std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
{
std::vector<AddedNodeInfo> ret;
@ -2246,6 +2243,30 @@ std::vector<CAddress> CConnman::GetAddresses() @@ -2246,6 +2243,30 @@ std::vector<CAddress> CConnman::GetAddresses()
return addrman.GetAddr();
}
bool CConnman::AddNode(const std::string& strNode)
{
LOCK(cs_vAddedNodes);
for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
if (strNode == *it)
return false;
}
vAddedNodes.push_back(strNode);
return true;
}
bool CConnman::RemoveAddedNode(const std::string& strNode)
{
LOCK(cs_vAddedNodes);
for(std::vector<std::string>::iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
if (strNode == *it) {
vAddedNodes.erase(it);
return true;
}
}
return false;
}
void RelayTransaction(const CTransaction& tx)
{
CInv inv(MSG_TX, tx.GetHash());

28
src/net.h

@ -87,6 +87,14 @@ unsigned int SendBufferSize(); @@ -87,6 +87,14 @@ unsigned int SendBufferSize();
typedef int NodeId;
struct AddedNodeInfo
{
std::string strAddedNode;
CService resolvedAddress;
bool fConnected;
bool fInbound;
};
CNode* FindNode(const CNetAddr& ip);
CNode* FindNode(const CSubNet& subNet);
CNode* FindNode(const std::string& addrName);
@ -137,6 +145,11 @@ public: @@ -137,6 +145,11 @@ public:
void SetBanned(const banmap_t &banmap);
void AddOneShot(const std::string& strDest);
bool AddNode(const std::string& node);
bool RemoveAddedNode(const std::string& node);
std::vector<AddedNodeInfo> GetAddedNodeInfo();
private:
struct ListenSocket {
SOCKET socket;
@ -173,6 +186,8 @@ private: @@ -173,6 +186,8 @@ private:
CAddrMan addrman;
std::deque<std::string> vOneShots;
CCriticalSection cs_vOneShots;
std::vector<std::string> vAddedNodes;
CCriticalSection cs_vAddedNodes;
};
extern std::unique_ptr<CConnman> g_connman;
void MapPort(bool fUseUPnP);
@ -252,9 +267,6 @@ extern std::vector<CNode*> vNodes; @@ -252,9 +267,6 @@ extern std::vector<CNode*> vNodes;
extern CCriticalSection cs_vNodes;
extern limitedmap<uint256, int64_t> mapAlreadyAskedFor;
extern std::vector<std::string> vAddedNodes;
extern CCriticalSection cs_vAddedNodes;
extern NodeId nLastNodeId;
extern CCriticalSection cs_nLastNodeId;
@ -807,14 +819,4 @@ void RelayTransaction(const CTransaction& tx); @@ -807,14 +819,4 @@ void RelayTransaction(const CTransaction& tx);
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds);
struct AddedNodeInfo
{
std::string strAddedNode;
CService resolvedAddress;
bool fConnected;
bool fInbound;
};
std::vector<AddedNodeInfo> GetAddedNodeInfo();
#endif // BITCOIN_NET_H

17
src/rpc/net.cpp

@ -226,23 +226,15 @@ UniValue addnode(const UniValue& params, bool fHelp) @@ -226,23 +226,15 @@ UniValue addnode(const UniValue& params, bool fHelp)
return NullUniValue;
}
LOCK(cs_vAddedNodes);
vector<string>::iterator it = vAddedNodes.begin();
for(; it != vAddedNodes.end(); it++)
if (strNode == *it)
break;
if (strCommand == "add")
{
if (it != vAddedNodes.end())
if(!g_connman->AddNode(strNode))
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Node already added");
vAddedNodes.push_back(strNode);
}
else if(strCommand == "remove")
{
if (it == vAddedNodes.end())
if(!g_connman->RemoveAddedNode(strNode))
throw JSONRPCError(RPC_CLIENT_NODE_NOT_ADDED, "Error: Node has not been added.");
vAddedNodes.erase(it);
}
return NullUniValue;
@ -299,7 +291,10 @@ UniValue getaddednodeinfo(const UniValue& params, bool fHelp) @@ -299,7 +291,10 @@ UniValue getaddednodeinfo(const UniValue& params, bool fHelp)
+ HelpExampleRpc("getaddednodeinfo", "true, \"192.168.0.201\"")
);
std::vector<AddedNodeInfo> vInfo = GetAddedNodeInfo();
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
std::vector<AddedNodeInfo> vInfo = g_connman->GetAddedNodeInfo();
if (params.size() == 1) {
bool found = false;

Loading…
Cancel
Save