mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-06 11:39:49 -06:00
Feature/thesis work merge (#566)
Web GUI from Klas Eskilson (three new modules: webgui, webbrowser and cefwebgui) Parallel connection refactorization Wormhole server added to the main repository Transfer function editor work from Cristoffer Särevall Update ghoul
This commit is contained in:
64
modules/server/CMakeLists.txt
Normal file
64
modules/server/CMakeLists.txt
Normal file
@@ -0,0 +1,64 @@
|
||||
##########################################################################################
|
||||
# #
|
||||
# OpenSpace #
|
||||
# #
|
||||
# Copyright (c) 2014-2017 #
|
||||
# #
|
||||
# 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(${OPENSPACE_CMAKE_EXT_DIR}/module_definition.cmake)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
set(HEADER_FILES
|
||||
include/connectionpool.h
|
||||
include/connection.h
|
||||
include/jsonconverters.h
|
||||
include/topic.h
|
||||
include/authorizationtopic.h
|
||||
include/getpropertytopic.h
|
||||
include/luascripttopic.h
|
||||
include/subscriptiontopic.h
|
||||
include/setpropertytopic.h
|
||||
include/timetopic.h
|
||||
include/triggerpropertytopic.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/servermodule.h
|
||||
)
|
||||
source_group("Header Files" FILES ${HEADER_FILES})
|
||||
|
||||
set(SOURCE_FILES
|
||||
src/connectionpool.cpp
|
||||
src/connection.cpp
|
||||
src/jsonconverters.cpp
|
||||
src/topics/topic.cpp
|
||||
src/topics/authorizationtopic.cpp
|
||||
src/topics/getpropertytopic.cpp
|
||||
src/topics/luascripttopic.cpp
|
||||
src/topics/subscriptiontopic.cpp
|
||||
src/topics/setpropertytopic.cpp
|
||||
src/topics/timetopic.cpp
|
||||
src/topics/triggerpropertytopic.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/servermodule.cpp
|
||||
)
|
||||
source_group("Source Files" FILES ${SOURCE_FILES})
|
||||
|
||||
create_new_module(
|
||||
"Server"
|
||||
server_module
|
||||
${HEADER_FILES} ${SOURCE_FILES}
|
||||
)
|
||||
1
modules/server/include.cmake
Normal file
1
modules/server/include.cmake
Normal file
@@ -0,0 +1 @@
|
||||
set(DEFAULT_MODULE ON)
|
||||
68
modules/server/include/authorizationtopic.h
Normal file
68
modules/server/include/authorizationtopic.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__AUTHENTICATION_TOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__AUTHENTICATION_TOPIC_H
|
||||
|
||||
#include <ctime>
|
||||
#include <ext/json/json.hpp>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "topic.h"
|
||||
#include "connection.h"
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class AuthorizationTopic : public Topic {
|
||||
public:
|
||||
AuthorizationTopic();
|
||||
~AuthorizationTopic() {};
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone();
|
||||
|
||||
/* https://httpstatuses.com/ */
|
||||
static const struct Statuses {
|
||||
enum StatusCode {
|
||||
OK = 200,
|
||||
Accepted = 202,
|
||||
|
||||
BadRequest = 400,
|
||||
Unauthorized = 401,
|
||||
NotAcceptable = 406,
|
||||
|
||||
NotImplemented = 501
|
||||
};
|
||||
};
|
||||
private:
|
||||
bool _isAuthenticated;
|
||||
|
||||
const std::string getKey() const;
|
||||
bool authorize(const std::string key);
|
||||
nlohmann::json message(const std::string &message, const int &statusCode = Statuses::NotImplemented);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__AUTHENTICATION_TOPIC_H
|
||||
79
modules/server/include/connection.h
Normal file
79
modules/server/include/connection.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODOULES_SERVER__CONNECTION_H
|
||||
#define OPENSPACE_MODOULES_SERVER__CONNECTION_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <ghoul/io/socket/tcpsocketserver.h>
|
||||
#include <ghoul/io/socket/websocketserver.h>
|
||||
#include <ghoul/misc/templatefactory.h>
|
||||
#include <ext/json/json.hpp>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <fmt/format.h>
|
||||
#include <include/openspace/engine/openspaceengine.h>
|
||||
#include <include/openspace/engine/configurationmanager.h>
|
||||
|
||||
#include "topic.h"
|
||||
|
||||
namespace openspace {
|
||||
|
||||
using TopicId = size_t;
|
||||
|
||||
class Connection {
|
||||
public:
|
||||
Connection(std::shared_ptr<ghoul::io::Socket> s, const std::string &address);
|
||||
|
||||
void handleMessage(std::string message);
|
||||
void sendMessage(const std::string& message);
|
||||
void handleJson(nlohmann::json json);
|
||||
void sendJson(const nlohmann::json& json);
|
||||
void setAuthorized(const bool status);
|
||||
|
||||
bool isAuthorized();
|
||||
|
||||
|
||||
std::shared_ptr<ghoul::io::Socket> socket();
|
||||
std::thread& thread();
|
||||
void setThread(std::thread&& thread);
|
||||
|
||||
private:
|
||||
ghoul::TemplateFactory<Topic> _topicFactory;
|
||||
std::map<TopicId, std::unique_ptr<Topic>> _topics;
|
||||
std::shared_ptr<ghoul::io::Socket> _socket;
|
||||
std::thread _thread;
|
||||
|
||||
std::string _address;
|
||||
bool _requireAuthorization;
|
||||
bool _isAuthorized;
|
||||
std::map <TopicId, std::string> _messageQueue;
|
||||
std::map <TopicId, std::chrono::system_clock::time_point> _sentMessages;
|
||||
|
||||
bool isWhitelisted();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODOULES_SERVER__CONNECTION_H
|
||||
65
modules/server/include/connectionpool.h
Normal file
65
modules/server/include/connectionpool.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef __OPENSPACE_MODULE_SERVER___CONNECTIONPOOL___H__
|
||||
#define __OPENSPACE_MODULE_SERVER___CONNECTIONPOOL___H__
|
||||
|
||||
#include <ghoul/io/socket/socketserver.h>
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <atomic>
|
||||
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class ConnectionPool {
|
||||
public:
|
||||
ConnectionPool(std::function<void(std::shared_ptr<ghoul::io::Socket> socket)> handleSocket);
|
||||
~ConnectionPool();
|
||||
void addServer(std::shared_ptr<ghoul::io::SocketServer> server);
|
||||
void removeServer(ghoul::io::SocketServer* server);
|
||||
void clearServers();
|
||||
void updateConnections();
|
||||
|
||||
private:
|
||||
|
||||
void disconnectAllConnections();
|
||||
std::mutex _connectionMutex;
|
||||
void removeDisconnectedSockets();
|
||||
void acceptNewSockets();
|
||||
|
||||
std::function<void(std::shared_ptr<ghoul::io::Socket>)> _handleSocket;
|
||||
std::vector<std::shared_ptr<ghoul::io::SocketServer>> _socketServers;
|
||||
std::vector<std::shared_ptr<ghoul::io::Socket>> _sockets;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#endif // __OPENSPACE_MODULE_SERVER___CONNECTIONPOOL___H__
|
||||
48
modules/server/include/getpropertytopic.h
Normal file
48
modules/server/include/getpropertytopic.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__GETPROPERTY_TOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__GETPROPERTY_TOPIC_H
|
||||
|
||||
#include <openspace/query/query.h>
|
||||
#include "topic.h"
|
||||
#include "connection.h"
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class GetPropertyTopic : public Topic {
|
||||
public:
|
||||
GetPropertyTopic();
|
||||
~GetPropertyTopic() {};
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone();
|
||||
|
||||
private:
|
||||
nlohmann::json getAllProperties();
|
||||
nlohmann::json getPropertyFromKey(const std::string& key);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__GETPROPERTY_TOPIC_H
|
||||
61
modules/server/include/jsonconverters.h
Normal file
61
modules/server/include/jsonconverters.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__JSON_CONVERTERS_INL
|
||||
#define OPENSPACE_MODULES_SERVER__JSON_CONVERTERS_INL
|
||||
|
||||
#include <openspace/properties/property.h>
|
||||
#include <openspace/scene/scenegraphnode.h>
|
||||
#include <openspace/rendering/renderable.h>
|
||||
#include <ext/json/json.hpp>
|
||||
#include <ghoul/glm.h>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace openspace::properties {
|
||||
|
||||
void to_json(json &j, const Property &p);
|
||||
void to_json(json &j, const Property* pP);
|
||||
void to_json(json &j, const PropertyOwner &p);
|
||||
void to_json(json &j, const PropertyOwner *p);
|
||||
|
||||
} // namespace openspace::properties
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void to_json(json &j, const SceneGraphNode &n);
|
||||
void to_json(json &j, const SceneGraphNode* pN);
|
||||
|
||||
void to_json(json &j, const Renderable &r);
|
||||
void to_json(json &j, const Renderable* pR);
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
namespace glm {
|
||||
|
||||
void to_json(json &j, const dvec3 &v);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
43
modules/server/include/luascripttopic.h
Normal file
43
modules/server/include/luascripttopic.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__LUASCRIPTTOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__LUASCRIPTTOPIC_H
|
||||
|
||||
#include <ext/json/json.hpp>
|
||||
#include <modules/server/include/topic.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class LuaScriptTopic : public Topic {
|
||||
public:
|
||||
LuaScriptTopic() : Topic() {};
|
||||
~LuaScriptTopic() {};
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone() { return true; };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__LUASCRIPTTOPIC_H
|
||||
46
modules/server/include/setpropertytopic.h
Normal file
46
modules/server/include/setpropertytopic.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__SETPROPERTYTOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__SETPROPERTYTOPIC_H
|
||||
|
||||
#include <ext/json/json.hpp>
|
||||
#include <modules/server/include/topic.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class SetPropertyTopic : public Topic {
|
||||
public:
|
||||
SetPropertyTopic() : Topic() {};
|
||||
~SetPropertyTopic() {};
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone() { return true; };
|
||||
|
||||
private:
|
||||
void setTime(const std::string& value);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__SETPROPERTYTOPIC_H
|
||||
52
modules/server/include/subscriptiontopic.h
Normal file
52
modules/server/include/subscriptiontopic.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__SUBSCRIPTION_TOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__SUBSCRIPTION_TOPIC_H
|
||||
|
||||
#include <openspace/util/timemanager.h>
|
||||
#include <openspace/query/query.h>
|
||||
#include "topic.h"
|
||||
#include "connection.h"
|
||||
|
||||
namespace openspace {
|
||||
class property;
|
||||
class SubscriptionTopic : public Topic {
|
||||
public:
|
||||
SubscriptionTopic();
|
||||
~SubscriptionTopic();
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone();
|
||||
|
||||
private:
|
||||
bool _requestedResourceIsSubscribable;
|
||||
bool _isSubscribedTo;
|
||||
int _onChangeHandle;
|
||||
int _onDeleteHandle;
|
||||
properties::Property* _prop;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__SUBSCRIPTION_TOPIC_H
|
||||
50
modules/server/include/timetopic.h
Normal file
50
modules/server/include/timetopic.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__TIME_TOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__TIME_TOPIC_H
|
||||
|
||||
#include <openspace/util/timemanager.h>
|
||||
#include "topic.h"
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class TimeTopic : public Topic {
|
||||
public:
|
||||
TimeTopic();
|
||||
~TimeTopic();
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone();
|
||||
private:
|
||||
nlohmann::json currentTime();
|
||||
nlohmann::json deltaTime();
|
||||
|
||||
int _timeCallbackHandle;
|
||||
int _deltaTimeCallbackHandle;
|
||||
std::chrono::system_clock::time_point _lastUpdateTime;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__TIME_TOPIC_H
|
||||
59
modules/server/include/topic.h
Normal file
59
modules/server/include/topic.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__TOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__TOPIC_H
|
||||
|
||||
#include <ext/json/json.hpp>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class Connection;
|
||||
|
||||
class Topic {
|
||||
public:
|
||||
Topic() {};
|
||||
virtual ~Topic() {};
|
||||
void initialize(Connection* connection, size_t topicId);
|
||||
nlohmann::json wrappedPayload(const nlohmann::json &payload) const;
|
||||
nlohmann::json wrappedError(std::string message = "Could not complete request.", int code = 500);
|
||||
virtual void handleJson(nlohmann::json json) = 0;
|
||||
virtual bool isDone() = 0;
|
||||
|
||||
protected:
|
||||
size_t _topicId;
|
||||
Connection* _connection;
|
||||
};
|
||||
|
||||
class BounceTopic : public Topic {
|
||||
public:
|
||||
BounceTopic() : Topic() {};
|
||||
~BounceTopic() {};
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone() { return false; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__TOPIC_H
|
||||
43
modules/server/include/triggerpropertytopic.h
Normal file
43
modules/server/include/triggerpropertytopic.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef OPENSPACE_MODULES_SERVER__TRIGGERPROPERTYTOPIC_H
|
||||
#define OPENSPACE_MODULES_SERVER__TRIGGERPROPERTYTOPIC_H
|
||||
|
||||
#include <ext/json/json.hpp>
|
||||
#include <modules/server/include/topic.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
class TriggerPropertyTopic : public Topic {
|
||||
public:
|
||||
TriggerPropertyTopic() : Topic() {};
|
||||
~TriggerPropertyTopic() {};
|
||||
void handleJson(nlohmann::json json);
|
||||
bool isDone() { return true; };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OPENSPACE_MODULES_SERVER__TRIGGERPROPERTYTOPIC_H
|
||||
143
modules/server/servermodule.cpp
Normal file
143
modules/server/servermodule.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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/servermodule.h>
|
||||
#include <ghoul/io/socket/websocket.h>
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
#include <openspace/scripting/scriptengine.h>
|
||||
|
||||
namespace {
|
||||
const char* _loggerCat = "ServerModule";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
ServerModule::ServerModule()
|
||||
: OpenSpaceModule(ServerModule::Name)
|
||||
{}
|
||||
|
||||
ServerModule::~ServerModule() {
|
||||
disconnectAll();
|
||||
cleanUpFinishedThreads();
|
||||
}
|
||||
|
||||
void ServerModule::internalInitialize(const ghoul::Dictionary& configuration) {
|
||||
using namespace ghoul::io;
|
||||
|
||||
std::unique_ptr<TcpSocketServer> tcpServer = std::make_unique<TcpSocketServer>();
|
||||
std::unique_ptr<WebSocketServer> wsServer = std::make_unique<WebSocketServer>();
|
||||
|
||||
// Temporary hard coded addresses and ports.
|
||||
tcpServer->listen("localhost", 8000);
|
||||
wsServer->listen("localhost", 8001);
|
||||
LDEBUG(fmt::format("TCP Server listening on {}:{}",
|
||||
tcpServer->address(), tcpServer->port()));
|
||||
|
||||
LDEBUG(fmt::format("WS Server listening on {}:{}",
|
||||
wsServer->address(), wsServer->port()));
|
||||
|
||||
_servers.push_back(std::move(tcpServer));
|
||||
_servers.push_back(std::move(wsServer));
|
||||
|
||||
OsEng.registerModuleCallback(
|
||||
OpenSpaceEngine::CallbackOption::PreSync,
|
||||
[this]() { preSync(); }
|
||||
);
|
||||
}
|
||||
|
||||
void ServerModule::preSync() {
|
||||
// Set up new connections.
|
||||
for (auto& server : _servers) {
|
||||
std::shared_ptr<ghoul::io::Socket> socket;
|
||||
while ((socket = server->nextPendingSocket())) {
|
||||
socket->startStreams();
|
||||
std::shared_ptr<Connection> connection =
|
||||
std::make_shared<Connection>(socket, server->address());
|
||||
connection->setThread(
|
||||
std::thread([this, connection] () { handleConnection(connection); })
|
||||
);
|
||||
_connections.push_back({ std::move(connection), false });
|
||||
}
|
||||
}
|
||||
|
||||
// Consume all messages put into the message queue by the socket threads.
|
||||
consumeMessages();
|
||||
|
||||
// Join threads for sockets that disconnected.
|
||||
cleanUpFinishedThreads();
|
||||
}
|
||||
|
||||
void ServerModule::cleanUpFinishedThreads() {
|
||||
for (auto& connectionData : _connections) {
|
||||
std::shared_ptr<Connection>& connection = connectionData.connection;
|
||||
if (!connection->socket() || !connection->socket()->isConnected()) {
|
||||
if (connection->thread().joinable()) {
|
||||
connection->thread().join();
|
||||
connectionData.markedForRemoval = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
_connections.erase(std::remove_if(
|
||||
_connections.begin(),
|
||||
_connections.end(),
|
||||
[](const auto& connectionData) {
|
||||
return connectionData.markedForRemoval;
|
||||
}
|
||||
), _connections.end());
|
||||
}
|
||||
|
||||
void ServerModule::disconnectAll() {
|
||||
for (auto& connectionData : _connections) {
|
||||
std::shared_ptr<Connection>& connection = connectionData.connection;
|
||||
if (connection->socket() && connection->socket()->isConnected()) {
|
||||
connection->socket()->disconnect(
|
||||
static_cast<int>(ghoul::io::WebSocket::ClosingReason::ClosingAll)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ServerModule::handleConnection(std::shared_ptr<Connection> connection) {
|
||||
std::string messageString;
|
||||
while (connection->socket()->getMessage(messageString)) {
|
||||
std::lock_guard<std::mutex> lock(_messageQueueMutex);
|
||||
_messageQueue.push_back(Message({
|
||||
connection,
|
||||
std::move(messageString)
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
void ServerModule::consumeMessages() {
|
||||
std::lock_guard<std::mutex> lock(_messageQueueMutex);
|
||||
while (_messageQueue.size() > 0) {
|
||||
Message m = _messageQueue.front();
|
||||
_messageQueue.pop_front();
|
||||
if (std::shared_ptr<Connection> c = m.connection.lock()) {
|
||||
c->handleMessage(m.messageString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
80
modules/server/servermodule.h
Normal file
80
modules/server/servermodule.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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. *
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef __OPENSPACE_MODULE_SERVER___SERVERMODULE___H__
|
||||
#define __OPENSPACE_MODULE_SERVER___SERVERMODULE___H__
|
||||
|
||||
#include <openspace/util/openspacemodule.h>
|
||||
|
||||
#include <ghoul/io/socket/tcpsocketserver.h>
|
||||
#include <ghoul/misc/templatefactory.h>
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <ext/json/json.hpp>
|
||||
#include <fmt/format.h>
|
||||
|
||||
#include "include/connection.h"
|
||||
#include "include/topic.h"
|
||||
|
||||
namespace openspace {
|
||||
|
||||
struct Message {
|
||||
std::weak_ptr<Connection> connection;
|
||||
std::string messageString;
|
||||
};
|
||||
|
||||
class ServerModule : public OpenSpaceModule {
|
||||
public:
|
||||
static constexpr const char* Name = "Server";
|
||||
ServerModule();
|
||||
virtual ~ServerModule();
|
||||
protected:
|
||||
void internalInitialize(const ghoul::Dictionary& configuration) override;
|
||||
private:
|
||||
struct ConnectionData {
|
||||
std::shared_ptr<Connection> connection;
|
||||
bool markedForRemoval = false;
|
||||
};
|
||||
|
||||
void handleConnection(std::shared_ptr<Connection> connection);
|
||||
void cleanUpFinishedThreads();
|
||||
void consumeMessages();
|
||||
void disconnectAll();
|
||||
void preSync();
|
||||
|
||||
std::mutex _messageQueueMutex;
|
||||
std::deque<Message> _messageQueue;
|
||||
|
||||
std::vector<ConnectionData> _connections;
|
||||
std::vector<std::unique_ptr<ghoul::io::SocketServer>> _servers;
|
||||
};
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
#endif // __OPENSPACE_MODULE_SERVER___SERVERMODULE___H__
|
||||
200
modules/server/src/connection.cpp
Normal file
200
modules/server/src/connection.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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/connection.h>
|
||||
#include <modules/server/include/authorizationtopic.h>
|
||||
#include <modules/server/include/getpropertytopic.h>
|
||||
#include <modules/server/include/luascripttopic.h>
|
||||
#include <modules/server/include/setpropertytopic.h>
|
||||
#include <modules/server/include/subscriptiontopic.h>
|
||||
#include <modules/server/include/timetopic.h>
|
||||
#include <modules/server/include/triggerpropertytopic.h>
|
||||
|
||||
namespace {
|
||||
const char* _loggerCat = "ServerModule: Connection";
|
||||
|
||||
const char* MessageKeyType = "type";
|
||||
const char* MessageKeyPayload = "payload";
|
||||
const char* MessageKeyTopic = "topic";
|
||||
|
||||
const char* AuthenticationTopicKey = "authorize";
|
||||
const char* GetPropertyTopicKey = "get";
|
||||
const char* LuaScriptTopicKey = "luascript";
|
||||
const char* SetPropertyTopicKey = "set";
|
||||
const char* SubscriptionTopicKey = "subscribe";
|
||||
const char* TimeTopicKey = "time";
|
||||
const char* TriggerPropertyTopicKey = "trigger";
|
||||
const char* BounceTopicKey = "bounce";
|
||||
|
||||
const int ThrottleMessageWaitInMs = 100;
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
Connection::Connection(std::shared_ptr<ghoul::io::Socket> s, const std::string &address)
|
||||
: _socket(s)
|
||||
, _isAuthorized(false)
|
||||
, _address(address)
|
||||
{
|
||||
_topicFactory.registerClass<AuthorizationTopic>(AuthenticationTopicKey);
|
||||
_topicFactory.registerClass<GetPropertyTopic>(GetPropertyTopicKey);
|
||||
_topicFactory.registerClass<LuaScriptTopic>(LuaScriptTopicKey);
|
||||
_topicFactory.registerClass<SetPropertyTopic>(SetPropertyTopicKey);
|
||||
_topicFactory.registerClass<SubscriptionTopic>(SubscriptionTopicKey);
|
||||
_topicFactory.registerClass<TimeTopic>(TimeTopicKey);
|
||||
_topicFactory.registerClass<TriggerPropertyTopic>(TriggerPropertyTopicKey);
|
||||
_topicFactory.registerClass<BounceTopic>(BounceTopicKey);
|
||||
|
||||
// see if the default config for requiring auth (on) is overwritten
|
||||
const bool hasAuthenticationConfiguration = OsEng.configurationManager().hasKeyAndValue<bool>(
|
||||
ConfigurationManager::KeyRequireSocketAuthentication);
|
||||
if (hasAuthenticationConfiguration) {
|
||||
_requireAuthorization = OsEng.configurationManager().value<bool>(
|
||||
ConfigurationManager::KeyRequireSocketAuthentication);
|
||||
} else {
|
||||
_requireAuthorization = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::handleMessage(std::string message) {
|
||||
try {
|
||||
nlohmann::json j = nlohmann::json::parse(message.c_str());
|
||||
try {
|
||||
handleJson(j);
|
||||
}
|
||||
catch (...) {
|
||||
LERROR(fmt::format("JSON handling error from: {}", message));
|
||||
}
|
||||
} catch (...) {
|
||||
if (!isAuthorized()) {
|
||||
LERROR("");
|
||||
_socket->disconnect();
|
||||
LERROR(fmt::format(
|
||||
"Could not parse JSON: '{}'. Connection is unauthorized. Disconnecting.",
|
||||
message
|
||||
));
|
||||
return;
|
||||
} else {
|
||||
LERROR(fmt::format("Could not parse JSON: '{}'", message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::handleJson(nlohmann::json j) {
|
||||
auto topicJson = j.find(MessageKeyTopic);
|
||||
auto payloadJson = j.find(MessageKeyPayload);
|
||||
|
||||
if (topicJson == j.end() || !topicJson->is_number_integer()) {
|
||||
LERROR("Topic must be an integer");
|
||||
return;
|
||||
}
|
||||
|
||||
if (payloadJson == j.end() || !payloadJson->is_object()) {
|
||||
LERROR("Payload must be an object");
|
||||
return;
|
||||
}
|
||||
|
||||
// The topic id may be an already discussed topic, or a new one.
|
||||
TopicId topicId = *topicJson;
|
||||
auto topicIt = _topics.find(topicId);
|
||||
|
||||
if (topicIt == _topics.end()) {
|
||||
// The topic id is not registered: Initialize a new topic.
|
||||
auto typeJson = j.find(MessageKeyType);
|
||||
if (typeJson == j.end() || !typeJson->is_string()) {
|
||||
LERROR(fmt::format("A type must be specified (`{}`) as a string when "
|
||||
"a new topic is initialized", MessageKeyType));
|
||||
return;
|
||||
}
|
||||
std::string type = *typeJson;
|
||||
|
||||
if (!isAuthorized() && type != AuthenticationTopicKey) {
|
||||
LERROR("Connection isn't authorized.");
|
||||
return;
|
||||
}
|
||||
|
||||
std::unique_ptr<Topic> topic = _topicFactory.create(type);
|
||||
topic->initialize(this, topicId);
|
||||
topic->handleJson(*payloadJson);
|
||||
if (!topic->isDone()) {
|
||||
_topics.emplace(topicId, std::move(topic));
|
||||
}
|
||||
} else {
|
||||
if (!isAuthorized()) {
|
||||
LERROR("Connection isn't authorized.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Dispatch the message to the existing topic.
|
||||
std::unique_ptr<Topic> &topic = topicIt->second;
|
||||
topic->handleJson(*payloadJson);
|
||||
if (topic->isDone()) {
|
||||
_topics.erase(topicIt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::sendMessage(const std::string &message) {
|
||||
_socket->putMessage(message);
|
||||
}
|
||||
|
||||
void Connection::sendJson(const nlohmann::json &j) {
|
||||
sendMessage(j.dump());
|
||||
}
|
||||
|
||||
bool Connection::isAuthorized() {
|
||||
// require either auth to be disabled or client to be authenticated
|
||||
return !_requireAuthorization || isWhitelisted() || _isAuthorized;
|
||||
}
|
||||
|
||||
void Connection::setThread(std::thread&& thread) {
|
||||
_thread = std::move(thread);
|
||||
}
|
||||
|
||||
std::thread& Connection::thread() {
|
||||
return _thread;
|
||||
}
|
||||
|
||||
std::shared_ptr<ghoul::io::Socket> Connection::socket() {
|
||||
return _socket;
|
||||
}
|
||||
|
||||
void Connection::setAuthorized(const bool status) {
|
||||
_isAuthorized = status;
|
||||
}
|
||||
|
||||
bool Connection::isWhitelisted() {
|
||||
const bool hasWhitelist = OsEng.configurationManager().hasKeyAndValue<std::string>(
|
||||
ConfigurationManager::KeyServerClientAddressWhitelist);
|
||||
|
||||
if (!hasWhitelist) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto whitelist = OsEng.configurationManager().value<std::string>(
|
||||
ConfigurationManager::KeyServerClientAddressWhitelist);
|
||||
return whitelist.find(_address) != std::string::npos;
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
87
modules/server/src/connectionpool.cpp
Normal file
87
modules/server/src/connectionpool.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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/connectionpool.h>
|
||||
#include <ghoul/io/socket/socket.h>
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
namespace openspace {
|
||||
|
||||
ConnectionPool::ConnectionPool(std::function<void(std::shared_ptr<ghoul::io::Socket> socket)> handleSocket)
|
||||
: _handleSocket(std::move(handleSocket))
|
||||
{}
|
||||
|
||||
ConnectionPool::~ConnectionPool() {
|
||||
disconnectAllConnections();
|
||||
}
|
||||
|
||||
void ConnectionPool::addServer(std::shared_ptr<ghoul::io::SocketServer> server) {
|
||||
_socketServers.push_back(server);
|
||||
}
|
||||
|
||||
void ConnectionPool::removeServer(ghoul::io::SocketServer* server) {
|
||||
std::remove_if(_socketServers.begin(), _socketServers.end(), [server](const auto& s) {
|
||||
return s.get() == server;
|
||||
});
|
||||
}
|
||||
|
||||
void ConnectionPool::clearServers() {
|
||||
_socketServers.clear();
|
||||
}
|
||||
|
||||
void ConnectionPool::updateConnections() {
|
||||
removeDisconnectedSockets();
|
||||
acceptNewSockets();
|
||||
}
|
||||
|
||||
void ConnectionPool::acceptNewSockets() {
|
||||
for (auto& server : _socketServers) {
|
||||
std::shared_ptr<ghoul::io::Socket> socket;
|
||||
while (socket = server->nextPendingSocket()) {
|
||||
_handleSocket(socket);
|
||||
_sockets.push_back(socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ConnectionPool::removeDisconnectedSockets() {
|
||||
std::remove_if(_sockets.begin(), _sockets.end(), [](const std::shared_ptr<ghoul::io::Socket> socket) {
|
||||
return !socket || !socket->isConnected();
|
||||
});
|
||||
}
|
||||
|
||||
void ConnectionPool::disconnectAllConnections() {
|
||||
for (auto& socket : _sockets) {
|
||||
if (socket && socket->isConnected()) {
|
||||
socket->disconnect();
|
||||
}
|
||||
}
|
||||
_sockets.clear();
|
||||
}
|
||||
|
||||
|
||||
} // namespace openspace
|
||||
121
modules/server/src/jsonconverters.cpp
Normal file
121
modules/server/src/jsonconverters.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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/jsonconverters.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include <openspace/scene/scenegraphnode.h>
|
||||
#include <openspace/rendering/renderable.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <ext/json/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace openspace::properties {
|
||||
|
||||
void to_json(json &j, const Property &p) {
|
||||
j = {
|
||||
{ "Description", json::parse(p.generateBaseJsonDescription()) },
|
||||
{ "Value", p.jsonValue() }
|
||||
};
|
||||
j["Description"]["description"] = p.description();
|
||||
}
|
||||
|
||||
void to_json(json &j, const Property* pP) {
|
||||
j = *pP;
|
||||
}
|
||||
|
||||
void to_json(json &j, const PropertyOwner &p) {
|
||||
j = {
|
||||
{ "identifier", p.identifier() },
|
||||
{ "description", p.description() },
|
||||
{ "properties", p.properties() },
|
||||
{ "subowners", p.propertySubOwners() },
|
||||
{ "tag", p.tags() }
|
||||
};
|
||||
}
|
||||
|
||||
void to_json(json &j, const PropertyOwner* p) {
|
||||
j = *p;
|
||||
}
|
||||
|
||||
} // namespace openspace::properties
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void to_json(json &j, const SceneGraphNode &n) {
|
||||
j = {
|
||||
{ "identifier", n.identifier() },
|
||||
{ "worldPosition", n.worldPosition() },
|
||||
{ "position", n.position() },
|
||||
{ "tags", n.tags() },
|
||||
|
||||
{ "propertiesCount", n.properties().size() },
|
||||
{ "properties", n.properties() },
|
||||
|
||||
{ "subowners", n.propertySubOwners() }
|
||||
};
|
||||
/*
|
||||
auto renderable = n.renderable();
|
||||
if (renderable != nullptr) {
|
||||
j["renderable"] = renderable;
|
||||
}*/
|
||||
|
||||
auto parent = n.parent();
|
||||
if (parent != nullptr) {
|
||||
j["parent"] = {
|
||||
{ "identifier", parent->identifier() }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void to_json(json &j, const SceneGraphNode* pN) {
|
||||
// Use reference converter instead of pointer
|
||||
j = *pN;
|
||||
}
|
||||
|
||||
void to_json(json &j, const Renderable &r) {
|
||||
j = {
|
||||
{ "properties", r.properties() },
|
||||
{ "enabled", r.isEnabled() }
|
||||
};
|
||||
}
|
||||
|
||||
void to_json(json &j, const Renderable* pR) {
|
||||
// Use reference converter instead of pointer
|
||||
j = *pR;
|
||||
}
|
||||
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
namespace glm {
|
||||
|
||||
void to_json(json &j, const dvec3 &v) {
|
||||
j = {
|
||||
{ "value", {v.x, v.y, v.z} },
|
||||
{ "type", "vec3" }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
83
modules/server/src/topics/authorizationtopic.cpp
Normal file
83
modules/server/src/topics/authorizationtopic.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 "include/authorizationtopic.h"
|
||||
|
||||
namespace {
|
||||
std::string _loggerCat = "AuthorizationTopic";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
AuthorizationTopic::AuthorizationTopic()
|
||||
: Topic()
|
||||
, _isAuthenticated(false) {};
|
||||
|
||||
bool AuthorizationTopic::isDone() {
|
||||
return _isAuthenticated;
|
||||
}
|
||||
|
||||
void AuthorizationTopic::handleJson(nlohmann::json json) {
|
||||
if (isDone()) {
|
||||
_connection->sendJson(message("Already authorized.", Statuses::OK));
|
||||
} else {
|
||||
try {
|
||||
auto providedKey = json.at("key").get<std::string>();
|
||||
if (authorize(providedKey)) {
|
||||
_connection->sendJson(message("Authorization OK.", Statuses::Accepted));
|
||||
_connection->setAuthorized(true);
|
||||
LINFO("Client successfully authorized.");
|
||||
} else {
|
||||
_connection->sendJson(message("Invalid key", Statuses::NotAcceptable));
|
||||
}
|
||||
} catch (const std::out_of_range& e) {
|
||||
_connection->sendJson(message("Invalid request, key must be provided.", Statuses::BadRequest));
|
||||
} catch (const std::domain_error& e) {
|
||||
_connection->sendJson(message("Invalid request, invalid key format.", Statuses::BadRequest));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
bool AuthorizationTopic::authorize(const std::string key) {
|
||||
_isAuthenticated = key == getKey();
|
||||
return _isAuthenticated;
|
||||
}
|
||||
|
||||
const std::string AuthorizationTopic::getKey() const {
|
||||
const bool hasConfigPassword = OsEng.configurationManager().hasKeyAndValue<std::string>(
|
||||
ConfigurationManager::KeyServerPasskey);
|
||||
if (hasConfigPassword) {
|
||||
return OsEng.configurationManager().value<std::string>(
|
||||
ConfigurationManager::KeyServerPasskey);
|
||||
}
|
||||
|
||||
return "17308";
|
||||
}
|
||||
|
||||
nlohmann::json AuthorizationTopic::message(const std::string &message, const int &statusCode) {
|
||||
nlohmann::json error = {{"message", message}, {"code", statusCode}};
|
||||
return error;
|
||||
}
|
||||
|
||||
}
|
||||
102
modules/server/src/topics/getpropertytopic.cpp
Normal file
102
modules/server/src/topics/getpropertytopic.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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/jsonconverters.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include <openspace/interaction/luaconsole.h>
|
||||
#include <openspace/network/parallelconnection.h>
|
||||
#include <openspace/engine/wrapper/windowwrapper.h>
|
||||
#include <openspace/interaction/navigationhandler.h>
|
||||
#include <openspace/engine/virtualpropertymanager.h>
|
||||
#include <openspace/rendering/screenspacerenderable.h>
|
||||
#include <openspace/scene/scene.h>
|
||||
#include <modules/server/include/getpropertytopic.h>
|
||||
#include <modules/volume/transferfunctionhandler.h>
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
namespace {
|
||||
const char* _loggerCat = "GetPropertyTopic";
|
||||
const char* AllPropertiesValue = "__allProperties";
|
||||
const char* AllNodesValue = "__allNodes";
|
||||
const char* AllScreenSpaceRenderablesValue = "__screenSpaceRenderables";
|
||||
const char* PropertyKey = "property";
|
||||
const char* RootPropertyOwner = "__rootOwner";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
GetPropertyTopic::GetPropertyTopic()
|
||||
: Topic() {}
|
||||
|
||||
bool GetPropertyTopic::isDone() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void GetPropertyTopic::handleJson(json j) {
|
||||
std::string requestedKey = j.at(PropertyKey).get<std::string>();
|
||||
LDEBUG("Getting property '" + requestedKey + "'...");
|
||||
json response;
|
||||
if (requestedKey == AllPropertiesValue) {
|
||||
response = getAllProperties();
|
||||
}
|
||||
else if (requestedKey == AllNodesValue) {
|
||||
response = wrappedPayload(sceneGraph()->allSceneGraphNodes());
|
||||
}
|
||||
else if (requestedKey == AllScreenSpaceRenderablesValue) {
|
||||
response = wrappedPayload({ { "value", OsEng.renderEngine().screenSpaceRenderables() } });
|
||||
}
|
||||
else if (requestedKey == RootPropertyOwner) {
|
||||
response = wrappedPayload(OsEng.rootPropertyOwner());
|
||||
}
|
||||
else {
|
||||
response = getPropertyFromKey(requestedKey);
|
||||
}
|
||||
_connection->sendJson(response);
|
||||
}
|
||||
|
||||
json GetPropertyTopic::getAllProperties() {
|
||||
json payload{
|
||||
{ "value", {
|
||||
OsEng.renderEngine(),
|
||||
OsEng.console(),
|
||||
OsEng.parallelPeer(),
|
||||
OsEng.windowWrapper(),
|
||||
OsEng.navigationHandler(),
|
||||
OsEng.virtualPropertyManager(),
|
||||
}}
|
||||
};
|
||||
return wrappedPayload(payload);
|
||||
}
|
||||
|
||||
json GetPropertyTopic::getPropertyFromKey(const std::string& key) {
|
||||
properties::Property* prop = property(key);
|
||||
if (prop != nullptr) {
|
||||
return wrappedPayload(prop);
|
||||
}
|
||||
|
||||
return wrappedError(fmt::format("property '{}' not found", key), 404);
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
49
modules/server/src/topics/luascripttopic.cpp
Normal file
49
modules/server/src/topics/luascripttopic.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 <openspace/engine/openspaceengine.h>
|
||||
#include <openspace/scripting/scriptengine.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <modules/server/include/luascripttopic.h>
|
||||
|
||||
namespace {
|
||||
const std::string ScriptKey = "script";
|
||||
const std::string _loggerCat = "LuaScriptTopic";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void LuaScriptTopic::handleJson(nlohmann::json json) {
|
||||
try {
|
||||
auto script = json.at(ScriptKey).get<std::string>();
|
||||
LDEBUG("Queueing Lua script: " + script);
|
||||
OsEng.scriptEngine().queueScript(script, scripting::ScriptEngine::RemoteScripting::No);
|
||||
}
|
||||
catch (std::out_of_range& e) {
|
||||
LERROR("Could run script -- key or value is missing in payload");
|
||||
LERROR(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
73
modules/server/src/topics/setpropertytopic.cpp
Normal file
73
modules/server/src/topics/setpropertytopic.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 <openspace/query/query.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
#include <openspace/util/timemanager.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <modules/server/include/setpropertytopic.h>
|
||||
|
||||
namespace {
|
||||
const std::string PropertyKey = "property";
|
||||
const std::string ValueKey = "value";
|
||||
const std::string _loggerCat = "SetPropertyTopic";
|
||||
const std::string SpecialKeyTime = "__time";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void SetPropertyTopic::handleJson(nlohmann::json json) {
|
||||
try {
|
||||
auto propertyKey = json.at(PropertyKey).get<std::string>();
|
||||
auto value = json.at(ValueKey).get<std::string>();
|
||||
|
||||
if (propertyKey == SpecialKeyTime) {
|
||||
setTime(value);
|
||||
}
|
||||
else {
|
||||
auto prop = property(propertyKey);
|
||||
if (prop != nullptr) {
|
||||
LDEBUG("Setting " + propertyKey + " to " + value + ".");
|
||||
if (!prop->setStringValue(value)) {
|
||||
LERROR("Failed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::out_of_range& e) {
|
||||
LERROR("Could not set property -- key or value is missing in payload");
|
||||
LERROR(e.what());
|
||||
}
|
||||
catch (ghoul::RuntimeError e) {
|
||||
LERROR("Could not set property -- runtime error:");
|
||||
LERROR(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
void SetPropertyTopic::setTime(const std::string& timeValue) {
|
||||
OsEng.timeManager().time().setTime(timeValue);
|
||||
}
|
||||
|
||||
}
|
||||
98
modules/server/src/topics/subscriptiontopic.cpp
Normal file
98
modules/server/src/topics/subscriptiontopic.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 <openspace/query/query.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include <modules/server/include/jsonconverters.h>
|
||||
#include "include/subscriptiontopic.h"
|
||||
|
||||
namespace {
|
||||
const char* _loggerCat = "SubscriptionTopic";
|
||||
const char* PropertyKey = "property";
|
||||
const char* EventKey = "event";
|
||||
|
||||
const char* StartSubscription = "start_subscription";
|
||||
const char* StopSubscription = "stop_subscription";
|
||||
|
||||
const int UnsetCallbackHandle = -1;
|
||||
}
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
namespace openspace {
|
||||
|
||||
SubscriptionTopic::SubscriptionTopic()
|
||||
: Topic()
|
||||
, _requestedResourceIsSubscribable(false)
|
||||
, _onChangeHandle(UnsetCallbackHandle)
|
||||
, _onDeleteHandle(UnsetCallbackHandle) {}
|
||||
|
||||
SubscriptionTopic::~SubscriptionTopic() {
|
||||
if (_onChangeHandle != UnsetCallbackHandle) {
|
||||
_prop->removeOnChange(_onChangeHandle);
|
||||
}
|
||||
if (!_onDeleteHandle) {
|
||||
_prop->removeOnDelete(_onDeleteHandle);
|
||||
}
|
||||
}
|
||||
|
||||
bool SubscriptionTopic::isDone() {
|
||||
return !_requestedResourceIsSubscribable || !_isSubscribedTo;
|
||||
}
|
||||
|
||||
void SubscriptionTopic::handleJson(json j) {
|
||||
std::string key = j.at(PropertyKey).get<std::string>();
|
||||
std::string event = j.at(EventKey).get<std::string>();
|
||||
|
||||
if (event == StartSubscription) {
|
||||
LDEBUG("Subscribing to property '" + key + "'...");
|
||||
|
||||
_prop = property(key);
|
||||
if (_prop != nullptr) {
|
||||
_requestedResourceIsSubscribable = true;
|
||||
_isSubscribedTo = true;
|
||||
auto onChange = [this, key]() {
|
||||
LDEBUG("Updating subscription '" + key + "'.");
|
||||
_connection->sendJson(wrappedPayload(_prop));
|
||||
};
|
||||
_onChangeHandle = _prop->onChange(onChange);
|
||||
_prop->onDelete([this]() {
|
||||
_onChangeHandle = UnsetCallbackHandle;
|
||||
_onDeleteHandle = UnsetCallbackHandle;
|
||||
_isSubscribedTo = false;
|
||||
});
|
||||
|
||||
// immediately send the value
|
||||
onChange();
|
||||
}
|
||||
else {
|
||||
LWARNING("Could not subscribe. Property '" + key + "' not found.");
|
||||
}
|
||||
}
|
||||
if (event == StopSubscription) {
|
||||
_isSubscribedTo = false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
108
modules/server/src/topics/timetopic.cpp
Normal file
108
modules/server/src/topics/timetopic.cpp
Normal file
@@ -0,0 +1,108 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 <openspace/query/query.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include "modules/server/include/connection.h"
|
||||
#include "modules/server/include/timetopic.h"
|
||||
#include <chrono>
|
||||
|
||||
namespace {
|
||||
const char* _loggerCat = "TimeTopic";
|
||||
const char* PropertyKey = "property";
|
||||
const char* CurrentTimeKey = "currentTime";
|
||||
const char* DeltaTimeKey = "deltaTime";
|
||||
const int UNSET_ONCHANGE_HANDLE = -1;
|
||||
const std::chrono::milliseconds TimeUpdateInterval(100);
|
||||
}
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
namespace openspace {
|
||||
|
||||
TimeTopic::TimeTopic()
|
||||
: Topic()
|
||||
, _timeCallbackHandle(UNSET_ONCHANGE_HANDLE)
|
||||
, _deltaTimeCallbackHandle(UNSET_ONCHANGE_HANDLE)
|
||||
, _lastUpdateTime(std::chrono::system_clock::now())
|
||||
{
|
||||
LDEBUG("Starting new time subscription");
|
||||
}
|
||||
|
||||
TimeTopic::~TimeTopic() {
|
||||
if (_timeCallbackHandle != UNSET_ONCHANGE_HANDLE) {
|
||||
OsEng.timeManager().removeTimeChangeCallback(_timeCallbackHandle);
|
||||
}
|
||||
if (_deltaTimeCallbackHandle != UNSET_ONCHANGE_HANDLE) {
|
||||
OsEng.timeManager().removeDeltaTimeChangeCallback(_deltaTimeCallbackHandle);
|
||||
}
|
||||
}
|
||||
|
||||
bool TimeTopic::isDone() {
|
||||
return false;
|
||||
}
|
||||
|
||||
void TimeTopic::handleJson(json j) {
|
||||
|
||||
std::string requestedKey = j.at(PropertyKey).get<std::string>();
|
||||
LDEBUG("Subscribing to " + requestedKey);
|
||||
|
||||
if (requestedKey == CurrentTimeKey) {
|
||||
_timeCallbackHandle = OsEng.timeManager().addTimeChangeCallback([this]() {
|
||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||
if (now - _lastUpdateTime > TimeUpdateInterval) {
|
||||
_connection->sendJson(currentTime());
|
||||
_lastUpdateTime = now;
|
||||
}
|
||||
});
|
||||
_connection->sendJson(currentTime());
|
||||
}
|
||||
else if (requestedKey == DeltaTimeKey) {
|
||||
_deltaTimeCallbackHandle = OsEng.timeManager().addDeltaTimeChangeCallback(
|
||||
[this]() {
|
||||
_connection->sendJson(deltaTime());
|
||||
if (_timeCallbackHandle != UNSET_ONCHANGE_HANDLE) {
|
||||
_connection->sendJson(currentTime());
|
||||
_lastUpdateTime = std::chrono::system_clock::now();;
|
||||
}
|
||||
}
|
||||
);
|
||||
_connection->sendJson(deltaTime());
|
||||
}
|
||||
else {
|
||||
LWARNING("Cannot get " + requestedKey);
|
||||
}
|
||||
}
|
||||
|
||||
json TimeTopic::currentTime() {
|
||||
json timeJson = { { "time", OsEng.timeManager().time().ISO8601() } };
|
||||
return wrappedPayload(timeJson);
|
||||
}
|
||||
|
||||
json TimeTopic::deltaTime() {
|
||||
json timeJson = { { "deltaTime", OsEng.timeManager().time().deltaTime() } };
|
||||
return wrappedPayload(timeJson);
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
58
modules/server/src/topics/topic.cpp
Normal file
58
modules/server/src/topics/topic.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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/servermodule.h>
|
||||
#include <modules/server/include/topic.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void Topic::initialize(Connection* connection, size_t topicId) {
|
||||
_connection = connection;
|
||||
_topicId = topicId;
|
||||
};
|
||||
|
||||
nlohmann::json Topic::wrappedPayload(const nlohmann::json &payload) const {
|
||||
// TODO: add message time
|
||||
nlohmann::json j = {
|
||||
{ "topic", _topicId },
|
||||
{ "payload", payload }
|
||||
};
|
||||
return j;
|
||||
};
|
||||
|
||||
nlohmann::json Topic::wrappedError(std::string message, int code) {
|
||||
nlohmann::json j = {
|
||||
{ "topic", _topicId },
|
||||
{ "status", "error" },
|
||||
{ "message", message },
|
||||
{ "code", code }
|
||||
};
|
||||
return j;
|
||||
};
|
||||
|
||||
void BounceTopic::handleJson(nlohmann::json json) {
|
||||
_connection->sendJson(json);
|
||||
};
|
||||
|
||||
}
|
||||
63
modules/server/src/topics/triggerpropertytopic.cpp
Normal file
63
modules/server/src/topics/triggerpropertytopic.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* 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 <openspace/query/query.h>
|
||||
#include <openspace/properties/property.h>
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
#include <openspace/util/timemanager.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <modules/server/include/triggerpropertytopic.h>
|
||||
|
||||
namespace {
|
||||
const std::string PropertyKey = "property";
|
||||
const std::string ValueKey = "value";
|
||||
const std::string _loggerCat = "TriggerPropertyTopic";
|
||||
}
|
||||
|
||||
namespace openspace {
|
||||
|
||||
void TriggerPropertyTopic::handleJson(nlohmann::json json) {
|
||||
try {
|
||||
auto propertyKey = json.at(PropertyKey).get<std::string>();
|
||||
|
||||
auto prop = property(propertyKey);
|
||||
if (prop != nullptr) {
|
||||
LDEBUG("Triggering " + propertyKey);
|
||||
prop->set("poke");
|
||||
}
|
||||
else {
|
||||
LWARNING("Could not find property " + propertyKey);
|
||||
}
|
||||
}
|
||||
catch (std::out_of_range& e) {
|
||||
LERROR("Could not poke property -- key or value is missing in payload");
|
||||
LERROR(e.what());
|
||||
}
|
||||
catch (ghoul::RuntimeError e) {
|
||||
LERROR("Could not poke property -- runtime error:");
|
||||
LERROR(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user