Skip to content
Snippets Groups Projects
Commit 1c74c220 authored by Adrien Béraud's avatar Adrien Béraud Committed by Sébastien Blin
Browse files

proxy server: cache json StreamBuilder

parent 9319bec7
No related branches found
No related tags found
No related merge requests found
...@@ -271,6 +271,7 @@ private: ...@@ -271,6 +271,7 @@ private:
std::thread server_thread {}; std::thread server_thread {};
std::unique_ptr<restbed::Service> service_; std::unique_ptr<restbed::Service> service_;
std::shared_ptr<DhtRunner> dht_; std::shared_ptr<DhtRunner> dht_;
Json::StreamWriterBuilder jsonBuilder_;
std::mutex schedulerLock_; std::mutex schedulerLock_;
std::condition_variable schedulerCv_; std::condition_variable schedulerCv_;
......
...@@ -67,6 +67,9 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port , ...@@ -67,6 +67,9 @@ DhtProxyServer::DhtProxyServer(std::shared_ptr<DhtRunner> dht, in_port_t port ,
#endif #endif
} }
jsonBuilder_["commentStyle"] = "None";
jsonBuilder_["indentation"] = "";
server_thread = std::thread([this, port]() { server_thread = std::thread([this, port]() {
// Create endpoints // Create endpoints
auto resource = std::make_shared<restbed::Resource>(); auto resource = std::make_shared<restbed::Resource>();
...@@ -222,10 +225,7 @@ DhtProxyServer::getNodeInfo(const Sp<restbed::Session>& session) const ...@@ -222,10 +225,7 @@ DhtProxyServer::getNodeInfo(const Sp<restbed::Session>& session) const
result = nodeInfo_.toJson(); result = nodeInfo_.toJson();
} }
result["public_ip"] = s->get_origin(); // [ipv6:ipv4]:port or ipv4:port result["public_ip"] = s->get_origin(); // [ipv6:ipv4]:port or ipv4:port
Json::StreamWriterBuilder wbuilder; auto output = Json::writeString(jsonBuilder_, result) + "\n";
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";
auto output = Json::writeString(wbuilder, result) + "\n";
s->close(restbed::OK, output); s->close(restbed::OK, output);
} }
else else
...@@ -249,10 +249,7 @@ DhtProxyServer::getStats(const Sp<restbed::Session>& session) const ...@@ -249,10 +249,7 @@ DhtProxyServer::getStats(const Sp<restbed::Session>& session) const
try { try {
if (dht_) { if (dht_) {
#ifdef OPENDHT_JSONCPP #ifdef OPENDHT_JSONCPP
Json::StreamWriterBuilder wbuilder; auto output = Json::writeString(jsonBuilder_, stats_.toJson()) + "\n";
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";
auto output = Json::writeString(wbuilder, stats_.toJson()) + "\n";
s->close(restbed::OK, output); s->close(restbed::OK, output);
#else #else
s->close(restbed::NotFound, "{\"err\":\"JSON not enabled on this instance\"}"); s->close(restbed::NotFound, "{\"err\":\"JSON not enabled on this instance\"}");
...@@ -284,13 +281,10 @@ DhtProxyServer::get(const Sp<restbed::Session>& session) const ...@@ -284,13 +281,10 @@ DhtProxyServer::get(const Sp<restbed::Session>& session) const
infoHash = InfoHash::get(hash); infoHash = InfoHash::get(hash);
} }
s->yield(restbed::OK, "", [=](const Sp<restbed::Session>&) {}); s->yield(restbed::OK, "", [=](const Sp<restbed::Session>&) {});
dht_->get(infoHash, [s](const Sp<Value>& value) { dht_->get(infoHash, [this,s](const Sp<Value>& value) {
if (s->is_closed()) return false; if (s->is_closed()) return false;
// Send values as soon as we get them // Send values as soon as we get them
Json::StreamWriterBuilder wbuilder; auto output = Json::writeString(jsonBuilder_, value->toJson()) + "\n";
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";
auto output = Json::writeString(wbuilder, value->toJson()) + "\n";
s->yield(output, [](const Sp<restbed::Session>& /*session*/){ }); s->yield(output, [](const Sp<restbed::Session>& /*session*/){ });
return true; return true;
}, [s](bool /*ok* */) { }, [s](bool /*ok* */) {
...@@ -338,19 +332,16 @@ DhtProxyServer::listen(const Sp<restbed::Session>& session) ...@@ -338,19 +332,16 @@ DhtProxyServer::listen(const Sp<restbed::Session>& session)
// cache the session to avoid an incrementation of the shared_ptr's counter // cache the session to avoid an incrementation of the shared_ptr's counter
// else, the session->close() will not close the socket. // else, the session->close() will not close the socket.
auto cacheSession = std::weak_ptr<restbed::Session>(s); auto cacheSession = std::weak_ptr<restbed::Session>(s);
listener.token = dht_->listen(infoHash, [cacheSession](const std::vector<Sp<Value>>& values, bool expired) { listener.token = dht_->listen(infoHash, [this,cacheSession](const std::vector<Sp<Value>>& values, bool expired) {
auto s = cacheSession.lock(); auto s = cacheSession.lock();
if (!s) return false; if (!s) return false;
// Send values as soon as we get them // Send values as soon as we get them
if (!s->is_closed()) { if (!s->is_closed()) {
Json::StreamWriterBuilder wbuilder;
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";
for (const auto& value : values) { for (const auto& value : values) {
auto val = value->toJson(); auto val = value->toJson();
if (expired) if (expired)
val["expired"] = true; val["expired"] = true;
auto output = Json::writeString(wbuilder, val) + "\n"; auto output = Json::writeString(jsonBuilder_, val) + "\n";
s->yield(output, [](const Sp<restbed::Session>&){ }); s->yield(output, [](const Sp<restbed::Session>&){ });
} }
} }
...@@ -440,10 +431,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session) ...@@ -440,10 +431,7 @@ DhtProxyServer::subscribe(const std::shared_ptr<restbed::Session>& session)
if (s->is_closed()) if (s->is_closed())
return false; return false;
// Send values as soon as we get them // Send values as soon as we get them
Json::StreamWriterBuilder wbuilder; auto output = Json::writeString(jsonBuilder_, value->toJson()) + "\n";
wbuilder["commentStyle"] = "None";
wbuilder["indentation"] = "";
auto output = Json::writeString(wbuilder, value->toJson()) + "\n";
s->yield(output, [](const Sp<restbed::Session> s->yield(output, [](const Sp<restbed::Session>
& /*session*/) {}); & /*session*/) {});
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment