make the webAPI more generic so that we can use it in a more standardized fashion

This commit is contained in:
DarthArgus
2016-08-20 11:19:23 +00:00
parent e0d9f58ba0
commit 5f0e741a72
5 changed files with 302 additions and 132 deletions
@@ -123,9 +123,10 @@
#include <stdio.h>
// Trying todo something here ...
#include "webAPI.h"
using namespace StellaBellum;
namespace CentralServerNamespace
{
bool gs_connectionServersPublic=false;
@@ -2782,15 +2783,14 @@ 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 ( !(updateURL.empty()) && webUpdateIntervalSeconds && (++apiLoopCount > (webUpdateIntervalSeconds*1000)) )
if (webUpdateIntervalSeconds && (++apiLoopCount > (webUpdateIntervalSeconds*1000)) )
{
apiLoopCount = 0;
// update the web api
sendMetricsToWebAPI(updateURL);
sendMetricsToWebAPI();
}
if ( ConfigCentralServer::getAuctionEnabled() ) // allow auctions?
@@ -2860,15 +2860,48 @@ void CentralServer::sendPopulationUpdateToLoginServer()
sendToAllLoginServers(upm);
}
// TODO: for sending metrics and such, would it make more sense to pass an object instead of a huge assed string as below?
void CentralServer::sendMetricsToWebAPI(std::string updateURL)
void CentralServer::sendMetricsToWebAPI()
{
std::ostringstream postBuf;
// create the object
webAPI api = webAPI::webAPI(std::string(ConfigCentralServer::getMetricsDataURL()));
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::statusMessage response = webAPI::simplePost(updateURL, std::string(postBuf.str()));
WARNING(response.status, ("Error sending stats: %s", response.retVal.c_str()));
// add our data
api.addJsonData<int>("totalPlayerCount", m_totalPlayerCount);
api.addJsonData<int>("totalGameServers", (m_gameServers.size() - 1));
api.addJsonData<int>("totalPlanetServers", m_planetServers.size());
api.addJsonData<bool>("isPublic", getIsClusterPublic());
api.addJsonData<bool>("isLocked", getIsClusterLocked());
api.addJsonData<bool>("isSecret", getIsClusterSecret());
api.addJsonData<bool>("preloadFinished", getClusterStartupTime());
api.addJsonData<bool>("databasebacklogged", isDatabaseBacklogged());
api.addJsonData<int>("totalTutorialSceneCount", getClusterStartupTime());
api.addJsonData<int>("totalFalconSceneCount", isDatabaseBacklogged());
#ifdef _DEBUG
if (api.submit()) {
std::string status = api.getRespValue<std::string>("status");
if (status.empty() || status != "success")
{
std::string message = api.getRespValue<std::string>("message");
if (message.empty())
{
message = "No message returned.";
}
WARNING(true, ("Error sending stats: %s", message.c_str()));
}
WARNING(true, ("Success sending server stats to API."));
}
else
{
WARNING(true, ("Error sending stats."));
}
#else
api.submit();
#endif
}
//-----------------------------------------------------------------------
@@ -182,7 +182,7 @@ private:
size_t getGameServerCount (void) const;
void update();
void sendPopulationUpdateToLoginServer();
void sendMetricsToWebAPI(std::string updateURL);
void sendMetricsToWebAPI();
ConnectionServerConnection * getConnectionServerForAccount(StationId suid);
void addToAccountConnectionMap(StationId suid, ConnectionServerConnection * cconn, uint32 subscriptionBits);
void removeFromAccountConnectionMap(int connectionServerConnectionId);
@@ -23,9 +23,12 @@
#include "sharedNetworkMessages/GenericValueTypeMessage.h"
#include "sharedNetworkMessages/LoginEnumCluster.h"
#include "webAPI.h"
#include <algorithm>
#include "webAPI.h"
using namespace StellaBellum;
//-----------------------------------------------------------------------
ClientConnection::ClientConnection(UdpConnectionMT * u, TcpClient * t) :
@@ -179,22 +182,38 @@ void ClientConnection::validateClient(const std::string & id, const std::string
if (!authURL.empty())
{
std::ostringstream postBuf;
postBuf << "user_name=" << id << "&user_password=" << key << "&ip=" << getRemoteAddress();
// create the object
webAPI api = webAPI::webAPI(authURL);
const webAPI::statusMessage response = webAPI::simplePost(authURL, std::string(postBuf.str()), "username");
// add our data
api.addJsonData<std::string>("user_name", id);
api.addJsonData<std::string>("user_password", key);
api.addJsonData<std::string>("ip", getRemoteAddress());
// true indicates that we logged in successfully via the api
// since both cases rely on response.retVal which will never be empty
// ...we needn't check if it's empty/not
if (response.status)
if (api.submit())
{
authOK = true;
uname = response.retVal;
std::string status = api.getRespValue<std::string>("status");
uname = api.getRespValue<std::string>("username");
if (!status.empty() && status == "success" && !uname.empty())
{
authOK = true;
}
else
{
std::string msg = api.getRespValue<std::string>("message");
if (msg.empty())
{
msg = "Invalid username or password.";
}
ErrorMessage err("Login Failed", msg);
this->send(err, true);
}
}
else
{
ErrorMessage err("Login Failed", response.retVal);
ErrorMessage err("Login Failed", "Could not connect to remote.");
this->send(err, true);
}
}