20 #if OPENDHT_PROXY_SERVER 24 #include "callbacks.h" 28 #include "scheduler.h" 37 #ifdef OPENDHT_JSONCPP 38 #include <json/json.h> 52 class OPENDHT_PUBLIC DhtProxyServer
63 DhtProxyServer(std::shared_ptr<DhtRunner>
dht, in_port_t port = 8000,
const std::string& pushServer =
"");
64 virtual ~DhtProxyServer();
66 DhtProxyServer(
const DhtProxyServer& other) =
delete;
67 DhtProxyServer(DhtProxyServer&& other) =
delete;
68 DhtProxyServer& operator=(
const DhtProxyServer& other) =
delete;
69 DhtProxyServer& operator=(DhtProxyServer&& other) =
delete;
77 size_t pushListenersCount;
83 std::string toString()
const {
84 std::ostringstream ss;
85 ss <<
"Listens: " << listenCount <<
" Puts: " << putCount <<
" PushListeners: " << pushListenersCount << std::endl;
86 ss <<
"Requests: " << requestRate <<
" per second." << std::endl;
89 if (ipv4.table_depth > 1) {
90 ss <<
"IPv4 Network estimation: " << ipv4.getNetworkSizeEstimation() << std::endl;;
93 if (ipv6.table_depth > 1) {
94 ss <<
"IPv6 Network estimation: " << ipv6.getNetworkSizeEstimation() << std::endl;;
99 #ifdef OPENDHT_JSONCPP 103 Json::Value toJson()
const {
105 result[
"listenCount"] =
static_cast<Json::UInt64
>(listenCount);
106 result[
"putCount"] =
static_cast<Json::UInt64
>(putCount);
107 result[
"pushListenersCount"] =
static_cast<Json::UInt64
>(pushListenersCount);
108 result[
"requestRate"] = requestRate;
109 result[
"nodeInfo"] = nodeInfo.toJson();
115 ServerStats stats()
const {
return stats_; }
117 void updateStats()
const;
119 std::shared_ptr<DhtRunner> getNode()
const {
return dht_; }
134 void getNodeInfo(
const std::shared_ptr<restbed::Session>& session)
const;
142 void getStats(
const std::shared_ptr<restbed::Session>& session)
const;
154 void get(
const std::shared_ptr<restbed::Session>& session)
const;
166 void listen(
const std::shared_ptr<restbed::Session>& session);
177 void put(
const std::shared_ptr<restbed::Session>& session);
179 void cancelPut(
const InfoHash& key, Value::Id vid);
181 #if OPENDHT_PROXY_SERVER_IDENTITY 191 void putSigned(
const std::shared_ptr<restbed::Session>& session)
const;
202 void putEncrypted(
const std::shared_ptr<restbed::Session>& session)
const;
203 #endif // OPENDHT_PROXY_SERVER_IDENTITY 215 void getFiltered(
const std::shared_ptr<restbed::Session>& session)
const;
224 void handleOptionsMethod(
const std::shared_ptr<restbed::Session>& session)
const;
230 void removeClosedListeners(
bool testSession =
true);
232 #if OPENDHT_PUSH_NOTIFICATIONS 242 void subscribe(
const std::shared_ptr<restbed::Session>& session);
250 void unsubscribe(
const std::shared_ptr<restbed::Session>& session);
256 void sendPushNotification(
const std::string& key,
const Json::Value& json,
bool isAndroid)
const;
264 void cancelPushListen(
const std::string& pushToken,
const InfoHash& key,
const std::string& clientId);
267 #endif //OPENDHT_PUSH_NOTIFICATIONS 269 using clock = std::chrono::steady_clock;
270 using time_point = clock::time_point;
272 std::thread server_thread {};
273 std::unique_ptr<restbed::Service> service_;
274 std::shared_ptr<DhtRunner> dht_;
276 std::mutex schedulerLock_;
277 std::condition_variable schedulerCv_;
278 Scheduler scheduler_;
279 std::thread schedulerThread_;
281 Sp<Scheduler::Job> printStatsJob_;
282 mutable std::mutex statsMutex_;
283 mutable NodeInfo nodeInfo_ {};
287 std::thread listenThread_;
288 struct SessionToHashToken {
289 std::shared_ptr<restbed::Session> session;
291 std::future<size_t> token;
293 std::vector<SessionToHashToken> currentListeners_;
294 std::mutex lockListener_;
295 std::atomic_bool stopListeners {
false};
299 std::map<InfoHash, SearchPuts> puts_;
301 mutable std::atomic<size_t> requestNum_ {0};
302 mutable std::atomic<time_point> lastStatsReset_ {time_point::min()};
304 const std::string pushServer_;
306 mutable ServerStats stats_;
308 #if OPENDHT_PUSH_NOTIFICATIONS 311 std::mutex lockPushListeners_;
312 std::map<std::string, PushListener> pushListeners_;
313 proxy::ListenToken tokenPushNotif_ {0};
314 #endif //OPENDHT_PUSH_NOTIFICATIONS 319 #endif //OPENDHT_PROXY_SERVER OPENDHT_PUBLIC Blob hash(const Blob &data, size_t hash_length=512/8)