#include "db.h"
#include "walletdb.h"
#include "net.h"
+#include "ntp.h"
using namespace json_spirit;
using namespace std;
}
}
+struct addrManItemSort {
+ bool operator()(const CAddrInfo &leftItem, const CAddrInfo &rightItem) {
+ int64_t nTime = GetTime();
+ return leftItem.GetChance(nTime) > rightItem.GetChance(nTime);
+ }
+};
+
Value getaddrmaninfo(const Array& params, bool fHelp)
{
- if (fHelp || params.size() != 0)
+ if (fHelp || params.size() > 1)
throw runtime_error(
- "getaddrmaninfo\n"
+ "getaddrmaninfo [networkType]\n"
"Returns a dump of addrman data.");
- // Return a full list of "online" address items
+ // Get a full list of "online" address items
vector<CAddrInfo> vAddr = addrman.GetOnlineAddr();
- Array ret;
+ // Sort by the GetChance result backwardly
+ sort(vAddr.begin(), vAddr.end(), addrManItemSort());
+
+ string strFilterNetType = "";
+ if (params.size() == 1)
+ strFilterNetType = params[0].get_str();
+ Array ret;
BOOST_FOREACH(const CAddrInfo &addr, vAddr) {
if (!addr.IsRoutable() || addr.IsLocal())
continue;
string strNetType;
switch(addr.GetNetwork())
{
-// case NET_TOR:
-// strNetType = "tor";
-// break;
+ case NET_TOR:
+ strNetType = "tor";
+ break;
// case NET_I2P:
// strNetType = "i2p";
// break;
- case NET_IPV4:
- strNetType = "ipv4";
- break;
- default:
case NET_IPV6:
strNetType = "ipv6";
+ break;
+ default:
+ case NET_IPV4:
+ strNetType = "ipv4";
}
+
+ if (strFilterNetType.size() != 0 && strNetType != strFilterNetType)
+ continue;
+
addrManItem.push_back(Pair("chance", addr.GetChance(GetTime())));
addrManItem.push_back(Pair("type", strNetType));
addrManItem.push_back(Pair("time", (int64_t)addr.nTime));
obj.push_back(Pair("addr", stats.addrName));
obj.push_back(Pair("services", strprintf("%08" PRIx64, stats.nServices)));
- obj.push_back(Pair("lastsend", (boost::int64_t)stats.nLastSend));
- obj.push_back(Pair("lastrecv", (boost::int64_t)stats.nLastRecv));
- obj.push_back(Pair("bytessent", (boost::int64_t)stats.nSendBytes));
- obj.push_back(Pair("bytesrecv", (boost::int64_t)stats.nRecvBytes));
- obj.push_back(Pair("conntime", (boost::int64_t)stats.nTimeConnected));
+ obj.push_back(Pair("lastsend", (int64_t)stats.nLastSend));
+ obj.push_back(Pair("lastrecv", (int64_t)stats.nLastRecv));
+ obj.push_back(Pair("bytessent", (int64_t)stats.nSendBytes));
+ obj.push_back(Pair("bytesrecv", (int64_t)stats.nRecvBytes));
+ obj.push_back(Pair("conntime", (int64_t)stats.nTimeConnected));
obj.push_back(Pair("version", stats.nVersion));
obj.push_back(Pair("subver", stats.strSubVer));
obj.push_back(Pair("inbound", stats.fInbound));
- obj.push_back(Pair("releasetime", (boost::int64_t)stats.nReleaseTime));
+ obj.push_back(Pair("releasetime", (int64_t)stats.nReleaseTime));
obj.push_back(Pair("startingheight", stats.nStartingHeight));
obj.push_back(Pair("banscore", stats.nMisbehavior));
if (stats.fSyncNode)
return ret;
}
-extern CCriticalSection cs_mapAlerts;
-extern map<uint256, CAlert> mapAlerts;
-
-// ppcoin: send alert.
// There is a known deadlock situation with ThreadMessageHandler
// ThreadMessageHandler: holds cs_vSend and acquiring cs_main in SendMessages()
// ThreadRPCServer: holds cs_main and acquiring cs_vSend in alert.RelayTo()/PushMessage()/BeginMessage()
"and current time.");
Object obj;
- obj.push_back(Pair("totalbytesrecv", static_cast< boost::uint64_t>(CNode::GetTotalBytesRecv())));
- obj.push_back(Pair("totalbytessent", static_cast<boost::uint64_t>(CNode::GetTotalBytesSent())));
- obj.push_back(Pair("timemillis", static_cast<boost::int64_t>(GetTimeMillis())));
+ obj.push_back(Pair("totalbytesrecv", static_cast<uint64_t>(CNode::GetTotalBytesRecv())));
+ obj.push_back(Pair("totalbytessent", static_cast<uint64_t>(CNode::GetTotalBytesSent())));
+ obj.push_back(Pair("timemillis", static_cast<int64_t>(GetTimeMillis())));
+ return obj;
+}
+
+/*
+05:53:45 ntptime
+05:53:48
+{
+"epoch" : 1442494427,
+"time" : "2015-09-17 12:53:47 UTC"
+}
+
+05:53:56 ntptime time.windows.com
+05:53:57
+{
+"epoch" : 1442494436,
+"time" : "2015-09-17 12:53:56 UTC"
+}
+
+05:54:33 ntptime time-a.nist.gov
+05:54:34
+{
+"epoch" : 1442494473,
+"time" : "2015-09-17 12:54:33 UTC"
+}*/
+
+Value ntptime(const Array& params, bool fHelp)
+{
+ if (fHelp || params.size() > 1)
+ throw runtime_error(
+ "ntptime [ntpserver]\n"
+ "Returns current time from specific or random NTP server.");
+
+ int64_t nTime;
+ if (params.size() > 0) {
+ string strHostName = params[0].get_str();
+ nTime = NtpGetTime(strHostName);
+ }
+ else {
+ CNetAddr ip;
+ nTime = NtpGetTime(ip);
+ }
+
+ Object obj;
+ switch (nTime) {
+ case -1:
+ throw runtime_error("Socket initialization error");
+ case -2:
+ throw runtime_error("Switching socket mode to non-blocking failed");
+ case -3:
+ throw runtime_error("Unable to send data");
+ case -4:
+ throw runtime_error("Receive timed out");
+ default:
+ if (nTime > 0 && nTime != 2085978496) {
+ obj.push_back(Pair("epoch", nTime));
+ obj.push_back(Pair("time", DateTimeStrFormat(nTime)));
+ }
+ else throw runtime_error("Unexpected response");
+ }
+
return obj;
-}
\ No newline at end of file
+}