From a05e61e0fe3f6643ea8d0383763bcd8fbb8ef449 Mon Sep 17 00:00:00 2001 From: DarthArgus Date: Sun, 22 May 2016 17:44:00 +0000 Subject: [PATCH] the response should never be empty but we need to check the json lib for exceptions and return appropriately... + cleanup a bit - closes #33 --- .../src/shared/CentralServer.cpp | 19 ++++++++----------- .../CentralServer/src/shared/CentralServer.h | 2 +- .../src/shared/ClientConnection.cpp | 15 ++++----------- external/3rd/library/webAPI/webAPI.cpp | 10 +++++++++- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/engine/server/application/CentralServer/src/shared/CentralServer.cpp b/engine/server/application/CentralServer/src/shared/CentralServer.cpp index 97b5a849..8034bbdb 100755 --- a/engine/server/application/CentralServer/src/shared/CentralServer.cpp +++ b/engine/server/application/CentralServer/src/shared/CentralServer.cpp @@ -2786,14 +2786,15 @@ void CentralServer::update() // update the webAPI if specified int webUpdateIntervalSeconds = ConfigCentralServer::getWebUpdateIntervalSeconds(); + std::string updateURL = std::string(ConfigCentralServer::getMetricsDataURL()); // assuming that every 5th frame is ~1 second, we can multiply and then check - if ( webUpdateIntervalSeconds && (++apiLoopCount > (webUpdateIntervalSeconds*1000)) ) + if ( !(updateURL.empty()) && webUpdateIntervalSeconds && (++apiLoopCount > (webUpdateIntervalSeconds*1000)) ) { apiLoopCount = 0; // update the web api - sendMetricsToWebAPI(); + sendMetricsToWebAPI(updateURL); } if ( ConfigCentralServer::getAuctionEnabled() ) // allow auctions? @@ -2863,18 +2864,14 @@ void CentralServer::sendPopulationUpdateToLoginServer() sendToAllLoginServers(upm); } -void CentralServer::sendMetricsToWebAPI() +void CentralServer::sendMetricsToWebAPI(std::string updateURL) { - std::string updateURL = std::string(ConfigCentralServer::getMetricsDataURL()); + std::ostringstream postBuf; - if (!(updateURL.empty())) - { - std::ostringstream postBuf; - - postBuf << "totalPlayerCount=" << m_totalPlayerCount << "&totalGameServers=" << m_gameServers.size() - 1 << "&totalPlanetServers=" << m_planetServers.size() << "&isPublic=" << getIsClusterPublic() << "&isLocked=" << getIsClusterLocked() << "&isSecret=" << getIsClusterSecret() << "&preloadFinished=" << getClusterStartupTime() << "&databasebacklogged=" << isDatabaseBacklogged() << "&totalTutorialSceneCount=" << m_totalTutorialSceneCount << "&totalFalconSceneCount=" << m_totalFalconSceneCount; + postBuf << "totalPlayerCount=" << m_totalPlayerCount << "&totalGameServers=" << m_gameServers.size() - 1 << "&totalPlanetServers=" << m_planetServers.size() << "&isPublic=" << getIsClusterPublic() << "&isLocked=" << getIsClusterLocked() << "&isSecret=" << getIsClusterSecret() << "&preloadFinished=" << getClusterStartupTime() << "&databasebacklogged=" << isDatabaseBacklogged() << "&totalTutorialSceneCount=" << m_totalTutorialSceneCount << "&totalFalconSceneCount=" << m_totalFalconSceneCount; - webAPI::simplePost(updateURL, std::string(postBuf.str()), ""); - } + std::string response = webAPI::simplePost(updateURL, std::string(postBuf.str()), ""); + WARNING((response != "success"), ("Error sending stats: %s", response.c_str())); } //----------------------------------------------------------------------- diff --git a/engine/server/application/CentralServer/src/shared/CentralServer.h b/engine/server/application/CentralServer/src/shared/CentralServer.h index 22e6a7de..a2da8ddb 100755 --- a/engine/server/application/CentralServer/src/shared/CentralServer.h +++ b/engine/server/application/CentralServer/src/shared/CentralServer.h @@ -182,7 +182,7 @@ private: size_t getGameServerCount (void) const; void update(); void sendPopulationUpdateToLoginServer(); - void sendMetricsToWebAPI(); + void sendMetricsToWebAPI(std::string updateURL); ConnectionServerConnection * getConnectionServerForAccount(StationId suid); void addToAccountConnectionMap(StationId suid, ConnectionServerConnection * cconn, uint32 subscriptionBits); void removeFromAccountConnectionMap(int connectionServerConnectionId); diff --git a/engine/server/application/LoginServer/src/shared/ClientConnection.cpp b/engine/server/application/LoginServer/src/shared/ClientConnection.cpp index c9a67d74..f57947f1 100755 --- a/engine/server/application/LoginServer/src/shared/ClientConnection.cpp +++ b/engine/server/application/LoginServer/src/shared/ClientConnection.cpp @@ -192,23 +192,16 @@ void ClientConnection::validateClient(const std::string & id, const std::string std::string response = webAPI::simplePost(authURL, std::string(postBuf.str()), ""); - if (!response.empty()) + if (response == "success") { - if (response == "success") - { - authOK = 1; - } - else - { - ErrorMessage err("Login Failed", response); - this->send(err, true); - } + authOK = 1; } else { - ErrorMessage err("Login Failed", "Error connecting to authentication provider."); + ErrorMessage err("Login Failed", response); this->send(err, true); } + } else { diff --git a/external/3rd/library/webAPI/webAPI.cpp b/external/3rd/library/webAPI/webAPI.cpp index c96effe7..847010f6 100644 --- a/external/3rd/library/webAPI/webAPI.cpp +++ b/external/3rd/library/webAPI/webAPI.cpp @@ -81,7 +81,15 @@ nlohmann::json webAPI::request(std::string endpoint, std::string data, int reqTy if (res == CURLE_OK && http_code == 200 && !(readBuffer.empty())) // check it all out and parse { - response = nlohmann::json::parse(readBuffer); + try { + response = nlohmann::json::parse(readBuffer); + } catch (std::string e) { + response["message"] = e; + response["status"] = "failure"; + } catch (...) { + response["message"] = "JSON parse error for endpoint."; + response["status"] = "failure"; + } } else {