Skip to content
Snippets Groups Projects
Commit b98cc78b authored by Seva's avatar Seva
Browse files

dhtproxy: add if logger before using it

parent 789d41ff
No related branches found
No related tags found
No related merge requests found
...@@ -139,11 +139,13 @@ DhtProxyServer::makeHttpServerSettings() ...@@ -139,11 +139,13 @@ DhtProxyServer::makeHttpServerSettings()
void void
DhtProxyServer::stop() DhtProxyServer::stop()
{ {
if (logger_)
logger_->d("[restinio] closing http server async operations"); logger_->d("[restinio] closing http server async operations");
httpServer_->io_context().reset(); httpServer_->io_context().reset();
httpServer_->io_context().stop(); httpServer_->io_context().stop();
if (httpServerThread_.joinable()) if (httpServerThread_.joinable())
httpServerThread_.join(); httpServerThread_.join();
if (logger_)
logger_->d("[restinio] http server closed"); logger_->d("[restinio] http server closed");
} }
...@@ -177,6 +179,7 @@ DhtProxyServer::asyncPrintStats() ...@@ -177,6 +179,7 @@ DhtProxyServer::asyncPrintStats()
nodeInfo_ = std::move(newInfo); nodeInfo_ = std::move(newInfo);
auto json = nodeInfo_.toJson(); auto json = nodeInfo_.toJson();
auto str = Json::writeString(jsonBuilder_, json); auto str = Json::writeString(jsonBuilder_, json);
if (logger_)
logger_->d("[stats] %s", str.c_str()); logger_->d("[stats] %s", str.c_str());
} }
printStatsTimer_->expires_at(printStatsTimer_->expiry() + PRINT_STATS_PERIOD); printStatsTimer_->expires_at(printStatsTimer_->expiry() + PRINT_STATS_PERIOD);
...@@ -413,6 +416,7 @@ DhtProxyServer::subscribe(restinio::request_handle_t request, ...@@ -413,6 +416,7 @@ DhtProxyServer::subscribe(restinio::request_handle_t request,
auto isAndroid = platform == "android"; auto isAndroid = platform == "android";
auto clientId = root.isMember("client_id") ? root["client_id"].asString() : std::string(); auto clientId = root.isMember("client_id") ? root["client_id"].asString() : std::string();
if (logger_)
logger_->w("[subscribe] %s client: %s", infoHash.toString().c_str(), clientId.c_str()); logger_->w("[subscribe] %s client: %s", infoHash.toString().c_str(), clientId.c_str());
// ================ Search for existing listener =================== // ================ Search for existing listener ===================
// start the timer // start the timer
...@@ -424,6 +428,7 @@ DhtProxyServer::subscribe(restinio::request_handle_t request, ...@@ -424,6 +428,7 @@ DhtProxyServer::subscribe(restinio::request_handle_t request,
auto pushListeners = pushListener->second.listeners.emplace(infoHash, std::vector<Listener>{}).first; auto pushListeners = pushListener->second.listeners.emplace(infoHash, std::vector<Listener>{}).first;
for (auto &listener: pushListeners->second){ for (auto &listener: pushListeners->second){
if (logger_)
logger_->w("[subscribe] found client_id: %s", listener.clientId.c_str()); logger_->w("[subscribe] found client_id: %s", listener.clientId.c_str());
// Found -> Resubscribe // Found -> Resubscribe
if (listener.clientId == clientId){ if (listener.clientId == clientId){
...@@ -490,9 +495,12 @@ DhtProxyServer::subscribe(restinio::request_handle_t request, ...@@ -490,9 +495,12 @@ DhtProxyServer::subscribe(restinio::request_handle_t request,
listener.expireNotifyTimer->async_wait( listener.expireNotifyTimer->async_wait(
[this, infoHash, pushToken, isAndroid, clientId](const asio::error_code &ec){ [this, infoHash, pushToken, isAndroid, clientId](const asio::error_code &ec){
if (logger_)
logger_->d("[subscribe] sending refresh %s", infoHash.toString().c_str()); logger_->d("[subscribe] sending refresh %s", infoHash.toString().c_str());
if (ec) if (ec){
if (logger_)
logger_->d("[subscribe] error sending refresh: %s", ec.message().c_str()); logger_->d("[subscribe] error sending refresh: %s", ec.message().c_str());
}
Json::Value json; Json::Value json;
json["timeout"] = infoHash.toString(); json["timeout"] = infoHash.toString();
json["to"] = clientId; json["to"] = clientId;
...@@ -561,6 +569,7 @@ DhtProxyServer::unsubscribe(restinio::request_handle_t request, ...@@ -561,6 +569,7 @@ DhtProxyServer::unsubscribe(restinio::request_handle_t request,
void void
DhtProxyServer::cancelPushListen(const std::string& pushToken, const dht::InfoHash& key, const std::string& clientId) DhtProxyServer::cancelPushListen(const std::string& pushToken, const dht::InfoHash& key, const std::string& clientId)
{ {
if (logger_)
logger_->d("[cancelpushlisten] %s %s", key.toString().c_str(), clientId.c_str()); logger_->d("[cancelpushlisten] %s %s", key.toString().c_str(), clientId.c_str());
std::lock_guard<std::mutex> lock(*lockListener_); std::lock_guard<std::mutex> lock(*lockListener_);
......
...@@ -90,10 +90,12 @@ ConnectionListener::state_changed(const restinio::connection_state::notice_t &no ...@@ -90,10 +90,12 @@ ConnectionListener::state_changed(const restinio::connection_state::notice_t &no
if (listeners_->find(id) != listeners_->end()){ if (listeners_->find(id) != listeners_->end()){
if (notice.cause() == restinio::connection_state::cause_t::closed){ if (notice.cause() == restinio::connection_state::cause_t::closed){
if (logger_)
logger_->d("[restinio] [connection:%li] cancelling listener", id); logger_->d("[restinio] [connection:%li] cancelling listener", id);
dht_->cancelListen(listeners_->at(id).hash, dht_->cancelListen(listeners_->at(id).hash,
std::move(listeners_->at(id).token)); std::move(listeners_->at(id).token));
listeners_->erase(id); listeners_->erase(id);
if (logger_)
logger_->d("[restinio] %li listeners are connected", listeners_->size()); logger_->d("[restinio] %li listeners are connected", listeners_->size());
} }
} }
...@@ -160,6 +162,7 @@ Client::open_conn(){ ...@@ -160,6 +162,7 @@ Client::open_conn(){
connId_, connId_,
std::move(asio::ip::tcp::socket{resolver_.get_io_context()}) std::move(asio::ip::tcp::socket{resolver_.get_io_context()})
); );
if (logger_)
logger_->d("[connection:%i] created", conn->id()); logger_->d("[connection:%i] created", conn->id());
connId_++; connId_++;
return conn; return conn;
...@@ -226,17 +229,20 @@ Client::async_request(std::string request, ...@@ -226,17 +229,20 @@ Client::async_request(std::string request,
resolver_.async_resolve(build_query(), [=](std::error_code ec, resolver_.async_resolve(build_query(), [=](std::error_code ec,
tcp::resolver::results_type res){ tcp::resolver::results_type res){
if (ec or res.empty()){ if (ec or res.empty()){
if (logger_)
logger_->e("[connection:%i] error resolving", conn->id()); logger_->e("[connection:%i] error resolving", conn->id());
conn->close(); conn->close();
return; return;
} }
for (auto da = res.begin(); da != res.end(); ++da){ for (auto da = res.begin(); da != res.end(); ++da){
if (logger_)
logger_->d("[connection:%i] resolved host=%s service=%s", logger_->d("[connection:%i] resolved host=%s service=%s",
conn->id(), da->host_name().c_str(), da->service_name().c_str()); conn->id(), da->host_name().c_str(), da->service_name().c_str());
conn->start(da); conn->start(da);
break; break;
} }
if (!conn->is_open()){ if (!conn->is_open()){
if (logger_)
logger_->e("[connection:%i] error closed connection", conn->id()); logger_->e("[connection:%i] error closed connection", conn->id());
return; return;
} }
...@@ -244,6 +250,7 @@ Client::async_request(std::string request, ...@@ -244,6 +250,7 @@ Client::async_request(std::string request,
logger_->d("[connection:%i] request write", conn->id()); logger_->d("[connection:%i] request write", conn->id());
conn->write(request, ec); conn->write(request, ec);
if (ec and ec != asio::error::eof){ if (ec and ec != asio::error::eof){
if (logger_)
logger_->e("[connection:%i] error: %s", conn->id(), ec.message().c_str()); logger_->e("[connection:%i] error: %s", conn->id(), ec.message().c_str());
return; return;
} }
...@@ -262,6 +269,7 @@ Client::async_request(std::string request, ...@@ -262,6 +269,7 @@ Client::async_request(std::string request,
// detect parsing errors // detect parsing errors
if (HPE_OK != parser->http_errno && HPE_PAUSED != parser->http_errno){ if (HPE_OK != parser->http_errno && HPE_PAUSED != parser->http_errno){
auto err = HTTP_PARSER_ERRNO(parser.get()); auto err = HTTP_PARSER_ERRNO(parser.get());
if (logger_)
logger_->e("[connection:%i] error parsing: %s", logger_->e("[connection:%i] error parsing: %s",
conn->id(), http_errno_name(err)); conn->id(), http_errno_name(err));
} }
...@@ -269,6 +277,7 @@ Client::async_request(std::string request, ...@@ -269,6 +277,7 @@ Client::async_request(std::string request,
if (ec != asio::error::eof) if (ec != asio::error::eof)
throw std::runtime_error{fmt::format( throw std::runtime_error{fmt::format(
"[connection:{}] error parsing: {}", conn->id(), ec)}; "[connection:{}] error parsing: {}", conn->id(), ec)};
if (logger_)
logger_->d("[connection:%i] request finished", conn->id()); logger_->d("[connection:%i] request finished", conn->id());
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment