1 #include "callbacks.h"
2 
3 namespace dht {
4 
5 
6 GetCallbackSimple
bindGetCb(const GetCallbackRaw & raw_cb,void * user_data)7 bindGetCb(const GetCallbackRaw& raw_cb, void* user_data)
8 {
9     if (not raw_cb) return {};
10     return [=](const std::shared_ptr<Value>& value) {
11         return raw_cb(value, user_data);
12     };
13 }
14 
15 GetCallback
bindGetCb(const GetCallbackSimple & cb)16 bindGetCb(const GetCallbackSimple& cb)
17 {
18     if (not cb) return {};
19     return [=](const std::vector<std::shared_ptr<Value>>& values) {
20         for (const auto& v : values)
21             if (not cb(v))
22                 return false;
23         return true;
24     };
25 }
26 
27 ShutdownCallback
bindShutdownCb(const ShutdownCallbackRaw & shutdown_cb_raw,void * user_data)28 bindShutdownCb(const ShutdownCallbackRaw& shutdown_cb_raw, void* user_data)
29 {
30     return [=]() { shutdown_cb_raw(user_data); };
31 }
32 
33 DoneCallback
bindDoneCb(DoneCallbackSimple donecb)34 bindDoneCb(DoneCallbackSimple donecb)
35 {
36     if (not donecb) return {};
37     using namespace std::placeholders;
38     return std::bind(donecb, _1);
39 }
40 
41 DoneCallback
bindDoneCb(const DoneCallbackRaw & raw_cb,void * user_data)42 bindDoneCb(const DoneCallbackRaw& raw_cb, void* user_data)
43 {
44     if (not raw_cb) return {};
45     return [=](bool success, const std::vector<std::shared_ptr<Node>>& nodes) {
46         raw_cb(success, (std::vector<std::shared_ptr<Node>>*)&nodes, user_data);
47     };
48 }
49 
50 DoneCallbackSimple
bindDoneCbSimple(const DoneCallbackSimpleRaw & raw_cb,void * user_data)51 bindDoneCbSimple(const DoneCallbackSimpleRaw& raw_cb, void* user_data) {
52     if (not raw_cb) return {};
53     return [=](bool success) {
54         raw_cb(success, user_data);
55     };
56 }
57 
58 std::string
toString() const59 NodeStats::toString() const
60 {
61     std::stringstream ss;
62     ss << "Known nodes: " << good_nodes << " good, " << dubious_nodes << " dubious, " << incoming_nodes << " incoming." << std::endl;
63     if (table_depth > 1) {
64         ss << "Routing table depth: " << table_depth << std::endl;
65         ss << "Network size estimation: " << getNetworkSizeEstimation() << " nodes" << std::endl;
66     }
67     return ss.str();
68 }
69 
70 #ifdef OPENDHT_JSONCPP
71 /**
72  * Build a json object from a NodeStats
73  */
74 Json::Value
toJson() const75 NodeStats::toJson() const
76 {
77     Json::Value val;
78     val["good"] = static_cast<Json::LargestUInt>(good_nodes);
79     val["dubious"] = static_cast<Json::LargestUInt>(dubious_nodes);
80     val["incoming"] = static_cast<Json::LargestUInt>(incoming_nodes);
81     if (table_depth > 1) {
82         val["table_depth"] = static_cast<Json::LargestUInt>(table_depth);
83         val["network_size_estimation"] = static_cast<Json::LargestUInt>(getNetworkSizeEstimation());
84     }
85     return val;
86 }
87 
NodeStats(const Json::Value & val)88 NodeStats::NodeStats(const Json::Value& val)
89 {
90     if (val.isMember("good"))
91         good_nodes = static_cast<unsigned>(val["good"].asLargestUInt());
92     if (val.isMember("dubious"))
93         dubious_nodes = static_cast<unsigned>(val["dubious"].asLargestUInt());
94     if (val.isMember("incoming"))
95         incoming_nodes = static_cast<unsigned>(val["incoming"].asLargestUInt());
96     if (val.isMember("table_depth"))
97         table_depth = static_cast<unsigned>(val["table_depth"].asLargestUInt());
98 }
99 
100 /**
101  * Build a json object from a NodeStats
102  */
103 Json::Value
toJson() const104 NodeInfo::toJson() const
105 {
106     Json::Value val;
107     if (id)
108         val["id"] = id.toString();
109     val["node_id"] = node_id.toString();
110     val["ipv4"] = ipv4.toJson();
111     val["ipv6"] = ipv6.toJson();
112     return val;
113 }
114 
NodeInfo(const Json::Value & v)115 NodeInfo::NodeInfo(const Json::Value& v)
116 {
117     if (v.isMember("id"))
118         id = InfoHash(v["id"].asString());
119     node_id = InfoHash(v["node_id"].asString());
120     ipv4 = NodeStats(v["ipv4"]);
121     ipv6 = NodeStats(v["ipv6"]);
122 }
123 
124 #endif
125 
126 
127 }
128