mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-05-04 18:11:01 -05:00
Code cleanup branch (#618)
* Make height map fallback layer work again * Add documentation to joystick button bindings * Removed grouped property headers * Add new version number constant generated by CMake * Make Joystick deadzone work properly * Change the startup date on Earth to today * Fix key modifier handling * Add debugging indices for TreeNodeDebugging * Fix script schedule for OsirisRex * Do not open Mission schedule automatically * Upload default projection texture automatically * General code cleanup * Fix check_style_guide warnings * Remove .clang-format * MacOS compile fixes * Clang analyzer fixes
This commit is contained in:
@@ -24,14 +24,22 @@
|
||||
|
||||
#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>
|
||||
#include <modules/server/include/topics/authorizationtopic.h>
|
||||
#include <modules/server/include/topics/bouncetopic.h>
|
||||
#include <modules/server/include/topics/getpropertytopic.h>
|
||||
#include <modules/server/include/topics/luascripttopic.h>
|
||||
#include <modules/server/include/topics/setpropertytopic.h>
|
||||
#include <modules/server/include/topics/subscriptiontopic.h>
|
||||
#include <modules/server/include/topics/timetopic.h>
|
||||
#include <modules/server/include/topics/topic.h>
|
||||
#include <modules/server/include/topics/triggerpropertytopic.h>
|
||||
#include <openspace/engine/configuration.h>
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
#include <ghoul/io/socket/socket.h>
|
||||
#include <ghoul/io/socket/tcpsocketserver.h>
|
||||
#include <ghoul/io/socket/websocketserver.h>
|
||||
#include <ghoul/logging/logmanager.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
namespace {
|
||||
constexpr const char* _loggerCat = "ServerModule: Connection";
|
||||
@@ -48,17 +56,16 @@ namespace {
|
||||
constexpr const char* TimeTopicKey = "time";
|
||||
constexpr const char* TriggerPropertyTopicKey = "trigger";
|
||||
constexpr const char* BounceTopicKey = "bounce";
|
||||
|
||||
constexpr const int ThrottleMessageWaitInMs = 100;
|
||||
} // namespace
|
||||
|
||||
namespace openspace {
|
||||
|
||||
Connection::Connection(std::shared_ptr<ghoul::io::Socket> s, const std::string &address)
|
||||
: _socket(s)
|
||||
, _isAuthorized(false)
|
||||
, _address(address)
|
||||
Connection::Connection(std::unique_ptr<ghoul::io::Socket> s, std::string address)
|
||||
: _socket(std::move(s))
|
||||
, _address(std::move(address))
|
||||
{
|
||||
ghoul_assert(_socket, "Socket must not be nullptr");
|
||||
|
||||
_topicFactory.registerClass<AuthorizationTopic>(AuthenticationTopicKey);
|
||||
_topicFactory.registerClass<GetPropertyTopic>(GetPropertyTopicKey);
|
||||
_topicFactory.registerClass<LuaScriptTopic>(LuaScriptTopicKey);
|
||||
@@ -72,7 +79,7 @@ Connection::Connection(std::shared_ptr<ghoul::io::Socket> s, const std::string &
|
||||
_requireAuthorization = OsEng.configuration().doesRequireSocketAuthentication;
|
||||
}
|
||||
|
||||
void Connection::handleMessage(std::string message) {
|
||||
void Connection::handleMessage(const std::string& message) {
|
||||
try {
|
||||
nlohmann::json j = nlohmann::json::parse(message.c_str());
|
||||
try {
|
||||
@@ -83,7 +90,6 @@ void Connection::handleMessage(std::string message) {
|
||||
}
|
||||
} catch (...) {
|
||||
if (!isAuthorized()) {
|
||||
LERROR("");
|
||||
_socket->disconnect();
|
||||
LERROR(fmt::format(
|
||||
"Could not parse JSON: '{}'. Connection is unauthorized. Disconnecting.",
|
||||
@@ -96,16 +102,16 @@ void Connection::handleMessage(std::string message) {
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::handleJson(nlohmann::json j) {
|
||||
auto topicJson = j.find(MessageKeyTopic);
|
||||
auto payloadJson = j.find(MessageKeyPayload);
|
||||
void Connection::handleJson(const nlohmann::json& json) {
|
||||
auto topicJson = json.find(MessageKeyTopic);
|
||||
auto payloadJson = json.find(MessageKeyPayload);
|
||||
|
||||
if (topicJson == j.end() || !topicJson->is_number_integer()) {
|
||||
if (topicJson == json.end() || !topicJson->is_number_integer()) {
|
||||
LERROR("Topic must be an integer");
|
||||
return;
|
||||
}
|
||||
|
||||
if (payloadJson == j.end() || !payloadJson->is_object()) {
|
||||
if (payloadJson == json.end() || !payloadJson->is_object()) {
|
||||
LERROR("Payload must be an object");
|
||||
return;
|
||||
}
|
||||
@@ -116,10 +122,12 @@ void Connection::handleJson(nlohmann::json j) {
|
||||
|
||||
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));
|
||||
auto typeJson = json.find(MessageKeyType);
|
||||
if (typeJson == json.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;
|
||||
@@ -142,23 +150,23 @@ void Connection::handleJson(nlohmann::json j) {
|
||||
}
|
||||
|
||||
// Dispatch the message to the existing topic.
|
||||
std::unique_ptr<Topic> &topic = topicIt->second;
|
||||
topic->handleJson(*payloadJson);
|
||||
if (topic->isDone()) {
|
||||
Topic& topic = *topicIt->second;
|
||||
topic.handleJson(*payloadJson);
|
||||
if (topic.isDone()) {
|
||||
_topics.erase(topicIt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::sendMessage(const std::string &message) {
|
||||
void Connection::sendMessage(const std::string& message) {
|
||||
_socket->putMessage(message);
|
||||
}
|
||||
|
||||
void Connection::sendJson(const nlohmann::json &j) {
|
||||
sendMessage(j.dump());
|
||||
void Connection::sendJson(const nlohmann::json& json) {
|
||||
sendMessage(json.dump());
|
||||
}
|
||||
|
||||
bool Connection::isAuthorized() {
|
||||
bool Connection::isAuthorized() const {
|
||||
// require either auth to be disabled or client to be authenticated
|
||||
return !_requireAuthorization || isWhitelisted() || _isAuthorized;
|
||||
}
|
||||
@@ -171,15 +179,15 @@ std::thread& Connection::thread() {
|
||||
return _thread;
|
||||
}
|
||||
|
||||
std::shared_ptr<ghoul::io::Socket> Connection::socket() {
|
||||
return _socket;
|
||||
ghoul::io::Socket* Connection::socket() {
|
||||
return _socket.get();
|
||||
}
|
||||
|
||||
void Connection::setAuthorized(const bool status) {
|
||||
void Connection::setAuthorized(bool status) {
|
||||
_isAuthorized = status;
|
||||
}
|
||||
|
||||
bool Connection::isWhitelisted() {
|
||||
bool Connection::isWhitelisted() const {
|
||||
const std::vector<std::string>& wl = OsEng.configuration().clientAddressWhitelist;
|
||||
return std::find(wl.begin(), wl.end(), _address) != wl.end();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user