Commit d232a1a0 authored by Hubert Denkmair's avatar Hubert Denkmair

add basic http response for stats

parent 3ea8a1af
#include "RelayServer.h"
#include <iostream>
#include <string>
#include <sstream>
#include <TcpServer/EPoll.h>
#include "JsonProtocol.h"
......@@ -55,6 +56,18 @@ int RelayServer::Run()
}
}
);
_tcpProtocol.SetStatsReceivedCallback([this](const MsgPackProtocol::BotStatsMessage& msg) {
/*std::string content = json(msg).dump();
std::stringstream s;
s << "HTTP/1.0 200 OK\r\n";
s << "Content-Length: " << content.size() << "\r\n";
s << "Content-Type: application/json\r\n\r\n";
s << content;
_statsHTTPResponse = s.str();*/
_statsHTTPResponse = json(msg).dump();
});
epoll.AddFileDescriptor(_clientSocket, EPOLLIN|EPOLLPRI|EPOLLERR);
h.onConnection(
......@@ -98,9 +111,14 @@ int RelayServer::Run()
}
});
std::string response = "Hello!";
std::string response = "nope.";
h.onHttpRequest([&](uWS::HttpResponse *res, uWS::HttpRequest req, char *data, size_t length, size_t remainingBytes)
{
if ((req.getMethod()==uWS::METHOD_GET) && (req.getUrl().toString()=="/stats"))
{
res->end(_statsHTTPResponse.data(), _statsHTTPResponse.length());
return;
}
res->end(response.data(), response.length());
});
......
......@@ -12,6 +12,7 @@ class RelayServer
private:
int _clientSocket;
TcpProtocol _tcpProtocol;
std::string _statsHTTPResponse;
static constexpr const char* ENV_GAMESERVER_HOST = "GAMESERVER_HOST";
static constexpr const char* ENV_GAMESERVER_HOST_DEFAULT = "localhost";
......
......@@ -16,6 +16,11 @@ void TcpProtocol::SetFrameCompleteCallback(TcpProtocol::FrameCompleteCallback ca
_frameCompleteCallback = callback;
}
void TcpProtocol::SetStatsReceivedCallback(StatsReceivedCallback callback)
{
_statsReceivedCallback = callback;
}
bool TcpProtocol::Read(int socket)
{
ssize_t bytesRead = read(socket, &_buf[_bufTail], _buf.size()-_bufTail);
......@@ -179,7 +184,10 @@ void TcpProtocol::OnTickReceived(std::unique_ptr<MsgPackProtocol::TickMessage> m
{
auto frame_id = msg->frame_id;
_pendingMessages.push_back(std::move(msg));
_frameCompleteCallback(frame_id);
if (_frameCompleteCallback!=nullptr)
{
_frameCompleteCallback(frame_id);
}
_pendingMessages.clear();
}
......@@ -252,6 +260,10 @@ void TcpProtocol::OnBotLogReceived(std::unique_ptr<MsgPackProtocol::BotLogMessag
void TcpProtocol::OnBotStatsReceived(std::unique_ptr<MsgPackProtocol::BotStatsMessage> msg)
{
_botStats = *msg;
if (_statsReceivedCallback!=nullptr)
{
_statsReceivedCallback(_botStats);
}
_pendingMessages.push_back(std::move(msg));
}
......
......@@ -15,10 +15,12 @@ class TcpProtocol
{
public:
typedef std::function<void(uint64_t frame_id)> FrameCompleteCallback;
typedef std::function<void(const MsgPackProtocol::BotStatsMessage& msg)> StatsReceivedCallback;
static constexpr const size_t BUFFER_SIZE = 1024*1024;
TcpProtocol();
void SetFrameCompleteCallback(FrameCompleteCallback callback);
void SetStatsReceivedCallback(StatsReceivedCallback callback);
bool Read(int socket);
const MsgPackProtocol::GameInfoMessage& GetGameInfo() const { return _gameInfo; }
......@@ -36,6 +38,7 @@ class TcpProtocol
size_t _bufTail=0;
FrameCompleteCallback _frameCompleteCallback;
StatsReceivedCallback _statsReceivedCallback;
MsgPackProtocol::GameInfoMessage _gameInfo;
MsgPackProtocol::BotStatsMessage _botStats;
std::map<guid_t,FoodItem> _foodMap;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment