20#if OPENDHT_PROXY_SERVER
48class OPENDHT_PUBLIC DhtProxyServer
59 DhtProxyServer(std::shared_ptr<DhtRunner>
dht, in_port_t port = 8000,
const std::string& pushServer =
"");
60 virtual ~DhtProxyServer();
62 DhtProxyServer(
const DhtProxyServer& other) =
delete;
63 DhtProxyServer(DhtProxyServer&& other) =
delete;
64 DhtProxyServer& operator=(
const DhtProxyServer& other) =
delete;
65 DhtProxyServer& operator=(DhtProxyServer&& other) =
delete;
73 size_t pushListenersCount;
77 std::string toString()
const {
78 std::ostringstream ss;
79 ss <<
"Listens: " << listenCount <<
" Puts: " << putCount <<
" PushListeners: " << pushListenersCount << std::endl;
80 ss <<
"Requests: " << requestRate <<
" per second." << std::endl;
84 ServerStats getStats()
const;
86 std::shared_ptr<DhtRunner> getNode()
const {
return dht_; }
101 void getNodeInfo(
const std::shared_ptr<restbed::Session>& session)
const;
113 void get(
const std::shared_ptr<restbed::Session>& session)
const;
125 void listen(
const std::shared_ptr<restbed::Session>& session);
136 void put(
const std::shared_ptr<restbed::Session>& session);
138 void cancelPut(
const InfoHash& key, Value::Id vid);
140#if OPENDHT_PROXY_SERVER_IDENTITY
150 void putSigned(
const std::shared_ptr<restbed::Session>& session)
const;
161 void putEncrypted(
const std::shared_ptr<restbed::Session>& session)
const;
174 void getFiltered(
const std::shared_ptr<restbed::Session>& session)
const;
183 void handleOptionsMethod(
const std::shared_ptr<restbed::Session>& session)
const;
189 void removeClosedListeners(
bool testSession =
true);
191#if OPENDHT_PUSH_NOTIFICATIONS
201 void subscribe(
const std::shared_ptr<restbed::Session>& session);
209 void unsubscribe(
const std::shared_ptr<restbed::Session>& session);
215 void sendPushNotification(
const std::string& key,
const Json::Value& json,
bool isAndroid)
const;
223 void cancelPushListen(
const std::string& pushToken,
const InfoHash& key,
const std::string& clientId);
228 using clock = std::chrono::steady_clock;
229 using time_point = clock::time_point;
231 std::thread server_thread {};
232 std::unique_ptr<restbed::Service> service_;
233 std::shared_ptr<DhtRunner> dht_;
235 std::mutex schedulerLock_;
236 std::condition_variable schedulerCv_;
237 Scheduler scheduler_;
238 std::thread schedulerThread_;
240 Sp<Scheduler::Job> printStatsJob_;
241 mutable std::mutex statsMutex_;
242 mutable NodeInfo nodeInfo_ {};
246 std::thread listenThread_;
247 struct SessionToHashToken {
248 std::shared_ptr<restbed::Session> session;
250 std::future<size_t> token;
252 std::vector<SessionToHashToken> currentListeners_;
253 std::mutex lockListener_;
254 std::atomic_bool stopListeners {
false};
258 std::map<InfoHash, SearchPuts> puts_;
260 mutable std::atomic<size_t> requestNum_ {0};
261 mutable std::atomic<time_point> lastStatsReset_ {time_point::min()};
263 const std::string pushServer_;
265#if OPENDHT_PUSH_NOTIFICATIONS
268 std::mutex lockPushListeners_;
269 std::map<std::string, PushListener> pushListeners_;
270 proxy::ListenToken tokenPushNotif_ {0};
OPENDHT_PUBLIC Blob hash(const Blob &data, size_t hash_length=512/8)