Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resurrect ServerAgent for external tool access #6044

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ option(USE_SYSTEM_LIBGLEW "Use the system's libglew" OFF)
option(USE_SYSTEM_LIBLUA "Use the system's liblua" OFF)
option(PROFILER_ENABLED "Build pioneer with profiling support built-in." OFF)
option(REMOTE_LUA_REPL "Enable remote LUA console" OFF)
option(USE_SERVER_AGENT "Enable server agent and HTTP access" ON)

if (REMOTE_LUA_REPL)
set(REMOTE_LUA_REPL_PORT 12345 CACHE STRING "TCP port for remote LUA console")
Expand Down Expand Up @@ -235,6 +236,10 @@ if (PROFILER_ENABLED)
add_definitions(-DPIONEER_PROFILER=1)
endif(PROFILER_ENABLED)

if (USE_SERVER_AGENT)
add_definitions(-DENABLE_SERVER_AGENT=1)
endif(USE_SERVER_AGENT)

if (WIN32)
add_definitions(-DPSAPI_VERSION=1)
endif (WIN32)
Expand Down
4 changes: 4 additions & 0 deletions src/Pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
#include "pigui/PerfInfo.h"
#include "pigui/PiGui.h"

#ifdef ENABLE_SERVER_AGENT
#include "ServerAgent.h"
#endif // ENABLE_SERVER_AGENT

#include "sound/AmbientSounds.h"
#include "sound/Sound.h"
#include "sound/SoundMusic.h"
Expand Down
14 changes: 8 additions & 6 deletions src/ServerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#pragma comment(lib, "libcurl.lib")
#endif

#include "buildopts.h"
#include "ServerAgent.h"
#include "StringF.h"
#include <curl/curl.h>
#include <SDL.h>
#include <SDL_Thread.h>

void NullServerAgent::Call(const std::string &method, const Json &data, SuccessCallback onSuccess, FailCallback onFail, void *userdata)
{
Expand Down Expand Up @@ -153,8 +156,7 @@ void HTTPServerAgent::ThreadMain()
// done with the queue
SDL_UnlockMutex(m_requestQueueLock);

Json::FastWriter writer;
req.buffer = writer.write(req.data);
req.buffer = req.data;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sturnclaw this is one of the two problems I could do with some advice about, this line is not an equivalent replacement for the writer.write(req.data) which used to be there which I believe serialised the data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want req.buffer = req.data.dump(); the arguments to the dump function are the amount of indentation and the indent character, neither of which should be used for over-the-wire serialization.


Response resp(req.onSuccess, req.onFail, req.userdata);

Expand All @@ -178,10 +180,10 @@ void HTTPServerAgent::ThreadMain()
}

if (resp.success) {
Json::Reader reader;
resp.success = reader.parse(resp.buffer, resp.data, false);
if (!resp.success)
resp.buffer = std::string("JSON parse error: " + reader.getFormattedErrorMessages());
//Json::Reader reader;
Copy link
Contributor Author

@fluffyfreak fluffyfreak Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sturnclaw second is this section which I think deserialised a response

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be resp.data = Json::parse(resp.buffer); resp.success = !resp.data.is_null();. Substitute the !is_null() check for an is_object() or is_array() depending on the wire format of your message type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll likely also need to wrap it in a try block or use a non-throwing method; still getting my development environment back up so that's an exercise left to the reader.

//resp.success = reader.parse(resp.buffer, resp.data, false);
//if (!resp.success)
// resp.buffer = std::string("JSON parse error: " + reader.getFormattedErrorMessages());
}

SDL_LockMutex(m_responseQueueLock);
Expand Down
7 changes: 6 additions & 1 deletion src/ServerAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

#include "libs.h"
#include <curl/curl.h>
#include <json/json.h>
#include "json.h"
#include <map>
#include <queue>

// fwd decl
struct SDL_Thread;
struct SDL_mutex;
struct SDL_cond;

class ServerAgent {
public:
virtual ~ServerAgent() {}
Expand Down
3 changes: 3 additions & 0 deletions src/lua/Lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "LuaMusic.h"
#include "LuaNameGen.h"
#include "LuaSerializer.h"
#ifdef ENABLE_SERVER_AGENT
#include "LuaServerAgent.h"
#endif // ENABLE_SERVER_AGENT
#include "LuaShipDef.h"
#include "LuaSpace.h"
#include "LuaTimer.h"
Expand Down
28 changes: 14 additions & 14 deletions src/lua/LuaServerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "LuaRef.h"
#include "Pi.h"
#include "ServerAgent.h"
#include <json/json.h>
#include "json.h"

/*
* Interface: ServerAgent
Expand Down Expand Up @@ -53,7 +53,7 @@ static Json _lua_to_json(lua_State *l, int idx)

// XXX handle arrays

Json object(Json::objectValue);
Json object(Json::object);

lua_pushnil(l);
while (lua_next(l, data)) {
Expand Down Expand Up @@ -82,27 +82,27 @@ static void _json_to_lua(lua_State *l, const Json &data)
LUA_DEBUG_START(l);

switch (data.type()) {
case Json::nullValue:
case Json::value_t::null:
lua_pushnil(l);
break;

case Json::intValue:
case Json::uintValue:
case Json::realValue:
lua_pushnumber(l, data.asDouble());
case Json::value_t::number_integer:
case Json::value_t::number_unsigned:
case Json::value_t::number_float:
lua_pushnumber(l, data.get<double>());
break;

case Json::stringValue: {
const std::string &str(data.asString());
case Json::value_t::string: {
const std::string &str(data.get<std::string>());
lua_pushlstring(l, str.c_str(), str.size());
break;
}

case Json::booleanValue:
lua_pushboolean(l, data.asBool());
case Json::value_t::boolean:
lua_pushboolean(l, data.get<bool>());
break;

case Json::arrayValue: {
case Json::value_t::array: {
lua_newtable(l);
for (int i = 0; i < int(data.size()); i++) {
lua_pushinteger(l, i + 1);
Expand All @@ -112,10 +112,10 @@ static void _json_to_lua(lua_State *l, const Json &data)
break;
}

case Json::objectValue: {
case Json::value_t::object: {
lua_newtable(l);
for (Json::const_iterator i = data.begin(); i != data.end(); ++i) {
const std::string &key(i.key().asString());
const std::string &key(i.key());
lua_pushlstring(l, key.c_str(), key.size());
_json_to_lua(l, *i);
lua_rawset(l, -3);
Expand Down
Loading