Updated uri handling on server

This commit is contained in:
Pax1601
2023-03-06 20:17:18 +01:00
parent 0fa29284c8
commit b2429ba256
12 changed files with 84 additions and 65 deletions

View File

@@ -83,16 +83,24 @@ json::value Unit::json(bool fullRefresh)
{
auto json = json::value::object();
/********** Base data **********/
json[L"AI"] = AI;
json[L"name"] = json::value::string(name);
json[L"unitName"] = json::value::string(unitName);
json[L"groupName"] = json::value::string(groupName);
json[L"alive"] = alive;
json[L"category"] = json::value::string(getCategory());
/********** Flight data **********/
json[L"flightData"] = json::value::object();
json[L"flightData"][L"latitude"] = latitude;
json[L"flightData"][L"longitude"] = longitude;
json[L"flightData"][L"altitude"] = altitude;
json[L"flightData"][L"speed"] = speed;
json[L"flightData"][L"heading"] = heading;
if (fullRefresh)
{
/********** Base data **********/
json[L"AI"] = AI;
json[L"name"] = json::value::string(name);
json[L"unitName"] = json::value::string(unitName);
json[L"groupName"] = json::value::string(groupName);
json[L"alive"] = alive;
json[L"category"] = json::value::string(getCategory());
/********** Mission data **********/
json[L"missionData"] = json::value::object();
json[L"missionData"][L"fuel"] = fuel;
@@ -145,14 +153,6 @@ json::value Unit::json(bool fullRefresh)
json[L"optionsData"][L"reactionToThreat"] = json::value::string(reactionToThreat);
}
/********** Flight data **********/
json[L"flightData"] = json::value::object();
json[L"flightData"][L"latitude"] = latitude;
json[L"flightData"][L"longitude"] = longitude;
json[L"flightData"][L"altitude"] = altitude;
json[L"flightData"][L"speed"] = speed;
json[L"flightData"][L"heading"] = heading;
return json;
}

View File

@@ -12,7 +12,7 @@ UnitsManager* unitsManager = nullptr;
Server* server = nullptr;
Scheduler* scheduler = nullptr;
json::value airbasesData;
json::value bullseyeData;
json::value bullseyesData;
mutex mutexLock;
bool initialized = false;
@@ -91,7 +91,7 @@ extern "C" DllExport int coreMissionData(lua_State * L)
if (missionData.has_object_field(L"airbases"))
airbasesData = missionData[L"airbases"];
if (missionData.has_object_field(L"bullseye"))
bullseyeData = missionData[L"bullseye"];
bullseyesData = missionData[L"bullseyes"];
return(0);
}

View File

@@ -10,7 +10,7 @@
extern UnitsManager* unitsManager;
extern Scheduler* scheduler;
extern json::value airbasesData;
extern json::value bullseyeData;
extern json::value bullseyesData;
extern mutex mutexLock;
void handle_eptr(std::exception_ptr eptr)
@@ -71,28 +71,29 @@ void Server::handle_get(http_request request)
std::exception_ptr eptr;
try {
auto answer = json::value::object();
wstring requestUri = request.request_uri().to_string();
log(requestUri);
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(UNITS_URI) + L"/" + wstring(PARTIAL_REFRESH_URI)) == 0)
unitsManager->updateAnswer(answer, false);
auto path = uri::split_path(uri::decode(request.relative_uri().path()));
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(UNITS_URI) + L"/" + wstring(FULL_REFRESH_URI)) == 0)
unitsManager->updateAnswer(answer, true);
/* Get the logs from the logger */
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(LOGS_URI)) == 0)
if (path.size() > 0)
{
auto logs = json::value::object();
getLogsJSON(logs); // By reference, for thread safety
answer[L"logs"] = logs;
if (path[0] == UNITS_URI && path.size() > 1)
{
if (path[1] == UPDATE_URI)
unitsManager->updateAnswer(answer, false);
else if (path[1] == REFRESH_URI)
unitsManager->updateAnswer(answer, true);
}
else if (path[0] == LOGS_URI)
{
auto logs = json::value::object();
getLogsJSON(logs, 100); // By reference, for thread safety. Get the last 100 log entries
answer[L"logs"] = logs;
}
else if (path[0] == AIRBASES_URI)
answer[L"airbases"] = airbasesData;
else if (path[0] == BULLSEYE_URI)
answer[L"bullseyes"] = bullseyesData;
}
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(AIRBASES_URI)) == 0)
answer[L"airbases"] = airbasesData;
if (requestUri.compare(L"/" + wstring(REST_URI) + L"/" + wstring(BULLSEYE_URI)) == 0)
answer[L"bullseye"] = bullseyeData;
response.set_body(answer);
}
catch (...) {

View File

@@ -3,4 +3,4 @@
void DllExport log(const std::string& sMessage);
void DllExport log(const std::wstring& sMessage);
void DllExport getLogsJSON(json::value& json);
void DllExport getLogsJSON(json::value& json, int logsNumber = NULL);

View File

@@ -7,7 +7,7 @@ class Logger
public:
void log(const string& sMessage);
void log(const wstring& sMessage);
void toJSON(json::value& json);
void toJSON(json::value& json, int logsNumber = NULL);
static Logger* GetLogger();

View File

@@ -14,7 +14,7 @@ void log(const wstring& message)
LOGGER->log(message);
}
void getLogsJSON(json::value& json)
void getLogsJSON(json::value& json, int logsNumber)
{
LOGGER->toJSON(json);
LOGGER->toJSON(json, logsNumber);
}

View File

@@ -32,12 +32,16 @@ void Logger::Close()
m_Logfile.close();
}
void Logger::toJSON(json::value& json)
void Logger::toJSON(json::value& json, int logsNumber)
{
lock_guard<mutex> guard(mutexLock);
int i = 0;
for (auto log : m_logs)
json[to_wstring(i++)] = json::value::string(to_wstring(log));
for (auto itr = m_logs.end(); itr != m_logs.begin(); --itr)
{
json[to_wstring(m_logs.size() - 1 - i)] = json::value::string(to_wstring(*itr));
if (logsNumber != 0 && i > logsNumber)
break;
}
}
void Logger::log(const string& message)

View File

@@ -5,10 +5,10 @@
#define REST_ADDRESS L"http://localhost:30000"
#define REST_URI L"olympus"
#define UNITS_URI L"units"
#define PARTIAL_REFRESH_URI L"partial"
#define FULL_REFRESH_URI L"full"
#define UPDATE_URI L"update"
#define REFRESH_URI L"refresh"
#define LOGS_URI L"logs"
#define AIRBASES_URI L"airbases"
#define BULLSEYE_URI L"bullseye"
#define BULLSEYE_URI L"bullseyes"
#define UPDATE_TIME_INTERVAL 0.25