mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-03-12 00:08:38 -05:00
Feature/documentation topic (#822)
- Implement documentation topic that can be used to query documentation using the network API. - Implement a way to pass arguments to lua scripts using json (rather than formatting entire lua string clientside) - Implement ability to attach callback to lua script executions - Implement abillity to transport return values from lua scripts back to network API clients. - Do not initialize server interface on slave nodes. - Implement Dictionary -> json converter using nlohmann json library
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <modules/server/include/topics/authorizationtopic.h>
|
||||
#include <modules/server/include/topics/bouncetopic.h>
|
||||
#include <modules/server/include/topics/documentationtopic.h>
|
||||
#include <modules/server/include/topics/getpropertytopic.h>
|
||||
#include <modules/server/include/topics/luascripttopic.h>
|
||||
#include <modules/server/include/topics/setpropertytopic.h>
|
||||
@@ -52,6 +53,7 @@ namespace {
|
||||
|
||||
constexpr const char* VersionTopicKey = "version";
|
||||
constexpr const char* AuthenticationTopicKey = "authorize";
|
||||
constexpr const char* DocumentationTopicKey = "documentation";
|
||||
constexpr const char* GetPropertyTopicKey = "get";
|
||||
constexpr const char* LuaScriptTopicKey = "luascript";
|
||||
constexpr const char* SetPropertyTopicKey = "set";
|
||||
@@ -81,6 +83,7 @@ Connection::Connection(std::unique_ptr<ghoul::io::Socket> s,
|
||||
}
|
||||
);
|
||||
|
||||
_topicFactory.registerClass<DocumentationTopic>(DocumentationTopicKey);
|
||||
_topicFactory.registerClass<GetPropertyTopic>(GetPropertyTopicKey);
|
||||
_topicFactory.registerClass<LuaScriptTopic>(LuaScriptTopicKey);
|
||||
_topicFactory.registerClass<SetPropertyTopic>(SetPropertyTopicKey);
|
||||
|
||||
@@ -68,6 +68,68 @@ void to_json(json& j, const PropertyOwner* p) {
|
||||
|
||||
} // namespace openspace::properties
|
||||
|
||||
namespace ghoul {
|
||||
|
||||
void to_json(json& j, const Dictionary& dictionary) {
|
||||
json object;
|
||||
for (const std::string& key : dictionary.keys()) {
|
||||
if (dictionary.hasValue<glm::vec4>(key)) {
|
||||
const glm::vec4 v = dictionary.value<glm::vec4>(key);
|
||||
object[key] = json::array({ v[0], v[1], v[2], v[3] });
|
||||
}
|
||||
else if (dictionary.hasValue<glm::vec3>(key)) {
|
||||
const glm::vec3 v = dictionary.value<glm::vec3>(key);
|
||||
object[key] = json::array({ v[0], v[1], v[2] });
|
||||
}
|
||||
else if (dictionary.hasValue<glm::vec2>(key)) {
|
||||
const glm::vec2 v = dictionary.value<glm::vec2>(key);
|
||||
object[key] = json::array({ v[0], v[1] });
|
||||
}
|
||||
else if (dictionary.hasValue<glm::dvec4>(key)) {
|
||||
const glm::dvec4 v = dictionary.value<glm::dvec4>(key);
|
||||
object[key] = json::array({ v[0], v[1], v[2], v[3] });
|
||||
}
|
||||
else if (dictionary.hasValue<glm::dvec3>(key)) {
|
||||
const glm::dvec3 v = dictionary.value<glm::dvec3>(key);
|
||||
object[key] = json::array({ v[0], v[1], v[2] });
|
||||
}
|
||||
else if (dictionary.hasValue<glm::dvec2>(key)) {
|
||||
const glm::dvec2 v = dictionary.value<glm::dvec2>(key);
|
||||
object[key] = json::array({ v[0], v[1] });
|
||||
}
|
||||
else if (dictionary.hasValue<float>(key)) {
|
||||
object[key] = dictionary.value<float>(key);
|
||||
}
|
||||
else if (dictionary.hasValue<double>(key)) {
|
||||
object[key] = dictionary.value<double>(key);
|
||||
}
|
||||
else if (dictionary.hasValue<int>(key)) {
|
||||
object[key] = dictionary.value<int>(key);
|
||||
}
|
||||
else if (dictionary.hasValue<unsigned int>(key)) {
|
||||
object[key] = dictionary.value<unsigned int>(key);
|
||||
}
|
||||
else if (dictionary.hasValue<std::string>(key)) {
|
||||
object[key] = dictionary.value<std::string>(key);
|
||||
}
|
||||
else if (dictionary.hasValue<Dictionary>(key)) {
|
||||
json child;
|
||||
to_json(child, dictionary.value<Dictionary>(key));
|
||||
object[key] = child;
|
||||
}
|
||||
else {
|
||||
object[key] = nullptr;
|
||||
}
|
||||
}
|
||||
j = object;
|
||||
}
|
||||
|
||||
void to_json(json& j, const Dictionary* d) {
|
||||
j = *d;
|
||||
}
|
||||
|
||||
} // namespace ghoul
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void to_json(json& j, const SceneGraphNode& n) {
|
||||
|
||||
72
modules/server/src/topics/documentationtopic.cpp
Normal file
72
modules/server/src/topics/documentationtopic.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2019 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#include <modules/server/include/topics/documentationtopic.h>
|
||||
|
||||
#include <modules/server/include/connection.h>
|
||||
#include <modules/server/include/jsonconverters.h>
|
||||
#include <openspace/engine/globals.h>
|
||||
|
||||
#include <openspace/scripting/scriptengine.h>
|
||||
#include <openspace/util/factorymanager.h>
|
||||
#include <openspace/interaction/keybindingmanager.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
namespace {
|
||||
constexpr const char* _loggerCat = "DocumentationTopic";
|
||||
constexpr const char* KeyType = "type";
|
||||
constexpr const char* TypeLua = "lua";
|
||||
constexpr const char* TypeFactories = "factories";
|
||||
constexpr const char* TypeKeyboard = "keyboard";
|
||||
} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void DocumentationTopic::handleJson(const nlohmann::json& json) {
|
||||
std::string requestedType = json.at(KeyType).get<std::string>();
|
||||
|
||||
nlohmann::json response;
|
||||
|
||||
// @emiax: Proposed future refector.
|
||||
// Do not parse generated json. Instead implement ability to get
|
||||
// ghoul::Dictionary objects from ScriptEngine, FactoryManager, and KeybindingManager.
|
||||
if (requestedType == TypeLua) {
|
||||
response = json::parse(global::scriptEngine.generateJson());
|
||||
} else if (requestedType == TypeFactories) {
|
||||
response = json::parse(FactoryManager::ref().generateJson());
|
||||
} else if (requestedType == TypeKeyboard) {
|
||||
response = json::parse(global::keybindingManager.generateJson());
|
||||
}
|
||||
|
||||
_connection->sendJson(wrappedPayload(response));
|
||||
}
|
||||
|
||||
bool DocumentationTopic::isDone() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
@@ -24,25 +24,137 @@
|
||||
|
||||
#include <modules/server/include/topics/luascripttopic.h>
|
||||
|
||||
#include <modules/server/include/jsonconverters.h>
|
||||
#include <modules/server/include/connection.h>
|
||||
#include <openspace/json.h>
|
||||
#include <openspace/engine/globals.h>
|
||||
#include <openspace/scripting/scriptengine.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <ghoul/misc/dictionary.h>
|
||||
|
||||
namespace {
|
||||
constexpr const char* ScriptKey = "script";
|
||||
constexpr const char* KeyScript = "script";
|
||||
constexpr const char* KeyFunction = "function";
|
||||
constexpr const char* KeyArguments = "arguments";
|
||||
constexpr const char* KeyReturn = "return";
|
||||
constexpr const char* TypeKey = "type";
|
||||
constexpr const char* _loggerCat = "LuaScriptTopic";
|
||||
|
||||
std::string formatLua(const nlohmann::json::const_iterator& it);
|
||||
|
||||
std::string escapeLuaString(const std::string& s) {
|
||||
std::string output;
|
||||
for (const char& c : s) {
|
||||
switch (c) {
|
||||
case '\a': output += "\\\a"; break;
|
||||
case '\b': output += "\\\b"; break;
|
||||
case '\f': output += "\\\f"; break;
|
||||
case '\n' : output += "\\\n"; break;
|
||||
case '\r' : output += "\\\r"; break;
|
||||
case '\t' : output += "\\\t"; break;
|
||||
case '\v' : output += "\\\v"; break;
|
||||
case '\\' : output += "\\\\"; break;
|
||||
case '"' : output += "\\\""; break;
|
||||
case '\'' : output += "\\'"; break;
|
||||
default: output += c;
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string formatLuaString(const std::string& s) {
|
||||
return "\"" + escapeLuaString(s) + "\"";
|
||||
}
|
||||
|
||||
std::string formatKeyValuePair(const nlohmann::json::const_iterator& it) {
|
||||
return "[" + formatLuaString(it.key()) + "] = " + formatLua(it);
|
||||
}
|
||||
|
||||
std::string formatLuaTable(const nlohmann::json& json) {
|
||||
std::string output = "{";
|
||||
auto it = json.begin();
|
||||
for (size_t i = 0; i < json.size(); ++i, ++it) {
|
||||
output += formatKeyValuePair(it);
|
||||
if (i < json.size() - 1) {
|
||||
output += ",";
|
||||
}
|
||||
}
|
||||
return output + "}";
|
||||
}
|
||||
|
||||
std::string formatLua(const nlohmann::json::const_iterator& it) {
|
||||
if (it->is_object()) {
|
||||
return formatLuaTable(it->get<nlohmann::json>());
|
||||
}
|
||||
if (it->is_number()) {
|
||||
return fmt::format("{:E}", it->get<double>());
|
||||
}
|
||||
if (it->is_string()) {
|
||||
return formatLuaString(it->get<std::string>());
|
||||
}
|
||||
if (it->is_boolean()) {
|
||||
return it->get<bool>() ? "true" : "false";
|
||||
}
|
||||
if (it->is_null()) {
|
||||
return "nil";
|
||||
}
|
||||
throw ghoul::lua::LuaFormatException("Format error.");
|
||||
}
|
||||
|
||||
std::string generateScript(const std::string& function,
|
||||
const std::vector<std::string>& args)
|
||||
{
|
||||
std::string script = "return " + function + "(";
|
||||
auto it = args.begin();
|
||||
for (size_t i = 0; i < args.size(); ++i, ++it) {
|
||||
script += *it;
|
||||
if (i < args.size() - 1) {
|
||||
script += ",";
|
||||
}
|
||||
}
|
||||
return script + ")";
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void LuaScriptTopic::handleJson(const nlohmann::json& json) {
|
||||
try {
|
||||
std::string script = json.at(ScriptKey).get<std::string>();
|
||||
global::scriptEngine.queueScript(
|
||||
std::move(script),
|
||||
scripting::ScriptEngine::RemoteScripting::No
|
||||
);
|
||||
nlohmann::json::const_iterator script = json.find(KeyScript);
|
||||
nlohmann::json::const_iterator function = json.find(KeyFunction);
|
||||
|
||||
if (script != json.end() && script->is_string()) {
|
||||
std::string luaScript = script->get<std::string>();
|
||||
nlohmann::json::const_iterator ret = json.find(KeyReturn);
|
||||
bool shouldReturn = (ret != json.end()) &&
|
||||
ret->is_boolean() &&
|
||||
ret->get<bool>();
|
||||
|
||||
runScript(luaScript, shouldReturn);
|
||||
}
|
||||
else if (function != json.end() && function->is_string()) {
|
||||
std::string luaFunction = function->get<std::string>();
|
||||
nlohmann::json::const_iterator ret = json.find(KeyReturn);
|
||||
bool shouldReturn = (ret != json.end()) &&
|
||||
ret->is_boolean() &&
|
||||
ret->get<bool>();
|
||||
|
||||
nlohmann::json::const_iterator args = json.find(KeyArguments);
|
||||
if (!args->is_array()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::string> formattedArgs;
|
||||
formattedArgs.reserve(args->size());
|
||||
for (auto it = args->begin(); it != args->end(); ++it) {
|
||||
formattedArgs.push_back(formatLua(it));
|
||||
}
|
||||
|
||||
std::string luaScript = generateScript(luaFunction, formattedArgs);
|
||||
runScript(luaScript, shouldReturn);
|
||||
}
|
||||
}
|
||||
catch (const std::out_of_range& e) {
|
||||
LERROR("Could not run script -- key or value is missing in payload");
|
||||
@@ -50,8 +162,29 @@ void LuaScriptTopic::handleJson(const nlohmann::json& json) {
|
||||
}
|
||||
}
|
||||
|
||||
void LuaScriptTopic::runScript(const std::string& script, bool shouldReturn) {
|
||||
scripting::ScriptEngine::ScriptCallback callback;
|
||||
if (shouldReturn) {
|
||||
callback = [this](ghoul::Dictionary data) {
|
||||
nlohmann::json j = data;
|
||||
_connection->sendJson(wrappedPayload(j));
|
||||
_waitingForReturnValue = false;
|
||||
};
|
||||
_waitingForReturnValue = true;
|
||||
}
|
||||
else {
|
||||
_waitingForReturnValue = false;
|
||||
}
|
||||
|
||||
global::scriptEngine.queueScript(
|
||||
std::move(script),
|
||||
scripting::ScriptEngine::RemoteScripting::No,
|
||||
callback
|
||||
);
|
||||
}
|
||||
|
||||
bool LuaScriptTopic::isDone() const {
|
||||
return true;
|
||||
return !_waitingForReturnValue;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
@@ -68,10 +68,11 @@ void SubscriptionTopic::resetCallbacks() {
|
||||
}
|
||||
|
||||
void SubscriptionTopic::handleJson(const nlohmann::json& json) {
|
||||
std::string key = json.at(PropertyKey).get<std::string>();
|
||||
const std::string& event = json.at(EventKey).get<std::string>();
|
||||
|
||||
if (event == StartSubscription) {
|
||||
std::string key = json.at(PropertyKey).get<std::string>();
|
||||
|
||||
_prop = property(key);
|
||||
resetCallbacks();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user