mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-01 17:20:09 -06:00
218 lines
9.4 KiB
C++
218 lines
9.4 KiB
C++
/*****************************************************************************************
|
|
* *
|
|
* OpenSpace *
|
|
* *
|
|
* Copyright (c) 2014-2020 *
|
|
* *
|
|
* 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/softwareintegration/network/softwareconnection.h>
|
|
|
|
#include <openspace/rendering/renderable.h>
|
|
#include <openspace/query/query.h>
|
|
#include <ghoul/logging/logmanager.h>
|
|
|
|
#include <iomanip>
|
|
|
|
namespace {
|
|
constexpr const char* _loggerCat = "SoftwareConnection";
|
|
} // namespace
|
|
|
|
namespace openspace {
|
|
|
|
const unsigned int SoftwareConnection::ProtocolVersion = 1;
|
|
|
|
SoftwareConnection::Message::Message(MessageType type, std::vector<char> content)
|
|
: type(type)
|
|
, content(std::move(content))
|
|
{}
|
|
|
|
SoftwareConnection::SoftwareConnectionLostError::SoftwareConnectionLostError()
|
|
: ghoul::RuntimeError("Connection lost", "Connection")
|
|
{}
|
|
|
|
SoftwareConnection::SoftwareConnection(std::unique_ptr<ghoul::io::TcpSocket> socket)
|
|
: _socket(std::move(socket))
|
|
{}
|
|
|
|
bool SoftwareConnection::isConnectedOrConnecting() const {
|
|
return _socket->isConnected() || _socket->isConnecting();
|
|
}
|
|
|
|
void SoftwareConnection::disconnect() {
|
|
if (_socket) {
|
|
_socket->disconnect();
|
|
}
|
|
}
|
|
|
|
ghoul::io::TcpSocket* SoftwareConnection::socket() {
|
|
return _socket.get();
|
|
}
|
|
|
|
SoftwareConnection::Message SoftwareConnection::receiveMessage() {
|
|
// Header consists of version (1 char), message type (4 char) & message size (4 char)
|
|
size_t HeaderSize = 9 * sizeof(char);
|
|
|
|
// Create basic buffer for receiving first part of message
|
|
std::vector<char> headerBuffer(HeaderSize);
|
|
std::vector<char> messageBuffer;
|
|
|
|
// Receive the header data
|
|
if (!_socket->get(headerBuffer.data(), HeaderSize)) {
|
|
LERROR("Failed to read header from socket. Disconnecting.");
|
|
throw SoftwareConnectionLostError();
|
|
}
|
|
|
|
// Read and convert version number: Byte 0
|
|
std::string version;
|
|
version.push_back(headerBuffer[0]);
|
|
const uint32_t protocolVersionIn = std::stoi(version);
|
|
|
|
// Make sure that header matches the protocol version
|
|
if (!(protocolVersionIn == ProtocolVersion)) {
|
|
LERROR(fmt::format(
|
|
"Protocol versions do not match. Remote version: {}, Local version: {}",
|
|
protocolVersionIn,
|
|
ProtocolVersion
|
|
));
|
|
throw SoftwareConnectionLostError();
|
|
}
|
|
|
|
// Read message type: Byte 1-4
|
|
std::string type;
|
|
for(int i = 1; i < 5; i++)
|
|
type.push_back(headerBuffer[i]);
|
|
|
|
// Read and convert message size: Byte 5-8
|
|
std::string messageSizeIn;
|
|
for (int i = 5; i < 9; i++)
|
|
messageSizeIn.push_back(headerBuffer[i]);
|
|
const size_t messageSize = stoi(messageSizeIn);
|
|
|
|
// Receive the message data
|
|
messageBuffer.resize(messageSize);
|
|
if (!_socket->get(messageBuffer.data(), messageSize)) {
|
|
LERROR("Failed to read message from socket. Disconnecting.");
|
|
throw SoftwareConnectionLostError();
|
|
}
|
|
|
|
// And delegate decoding depending on message type
|
|
if (type == "CONN")
|
|
return Message(MessageType::Connection, messageBuffer);
|
|
else if( type == "ASGN")
|
|
return Message(MessageType::AddSceneGraphNode, messageBuffer);
|
|
else if (type == "RSGN")
|
|
return Message(MessageType::RemoveSceneGraphNode, messageBuffer);
|
|
else if (type == "UPCO")
|
|
return Message(MessageType::Color, messageBuffer);
|
|
else if (type == "UPOP")
|
|
return Message(MessageType::Opacity, messageBuffer);
|
|
else if( type == "UPSI")
|
|
return Message(MessageType::Size, messageBuffer);
|
|
else if (type == "DISC")
|
|
return Message(MessageType::Disconnection, messageBuffer);
|
|
else {
|
|
LERROR(fmt::format("Unsupported message type: {}. Disconnecting...", type));
|
|
return Message(MessageType::Disconnection, messageBuffer);
|
|
}
|
|
}
|
|
|
|
bool SoftwareConnection::sendMessage(std::string message) {
|
|
if (!_socket->put<char>(message.data(), message.size())) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void SoftwareConnection::handleProperties(std::string identifier) {
|
|
|
|
const Renderable* myRenderable = renderable(identifier);
|
|
properties::Property* colorProperty = myRenderable->property("Color");
|
|
properties::Property* opacityProperty = myRenderable->property("Opacity");
|
|
properties::Property* sizeProperty = myRenderable->property("Size");
|
|
|
|
// Update color of renderable
|
|
auto updateColor = [colorProperty, identifier]() {
|
|
std::string lengthOfIdentifier = std::to_string(identifier.length());
|
|
std::string propertyValue = colorProperty->getStringValue();
|
|
std::string lengthOfValue = std::to_string(propertyValue.length());
|
|
std::string messageType = "UPCO";
|
|
std::string subject = lengthOfIdentifier + identifier + lengthOfValue + propertyValue;
|
|
|
|
// Format length of subject to always be 4 digits
|
|
std::ostringstream os;
|
|
os << std::setfill('0') << std::setw(4) << subject.length();
|
|
std::string lengthOfSubject = os.str();
|
|
|
|
std::string message = messageType + lengthOfSubject + subject;
|
|
SoftwareConnection send;
|
|
send.sendMessage(message);
|
|
LERROR(fmt::format("Meddelandet som skickas {}", message));
|
|
};
|
|
colorProperty->onChange(updateColor);
|
|
|
|
/*
|
|
|
|
// Update opacity of renderable
|
|
auto updateOpacity = [opacityProperty, identifier]() {
|
|
std::string lengthOfIdentifier = std::to_string(identifier.length());
|
|
std::string propertyValue = opacityProperty->getStringValue();
|
|
std::string lengthOfValue = std::to_string(propertyValue.length());
|
|
std::string messageType = "UPOP";
|
|
std::string subject = lengthOfIdentifier + identifier + lengthOfValue + propertyValue;
|
|
|
|
// Format length of subject to always be 4 digits
|
|
std::ostringstream os;
|
|
os << std::setfill('0') << std::setw(4) << subject.length();
|
|
std::string lengthOfSubject = os.str();
|
|
|
|
std::string message = messageType + lengthOfSubject + subject;
|
|
SoftwareConnection send;
|
|
send.sendMessage(message);
|
|
LERROR(fmt::format("Meddelandet som skickas {}", message));
|
|
};
|
|
opacityProperty->onChange(updateOpacity);
|
|
|
|
// Update size of renderable
|
|
auto updateSize = [sizeProperty, identifier]() {
|
|
std::string lengthOfIdentifier = std::to_string(identifier.length());
|
|
std::string propertyValue = sizeProperty->getStringValue();
|
|
std::string lengthOfValue = std::to_string(propertyValue.length());
|
|
std::string messageType = "UPSI";
|
|
std::string subject = lengthOfIdentifier + identifier + lengthOfValue + propertyValue;
|
|
|
|
// Format length of subject to always be 4 digits
|
|
std::ostringstream os;
|
|
os << std::setfill('0') << std::setw(4) << subject.length();
|
|
std::string lengthOfSubject = os.str();
|
|
|
|
std::string message = messageType + lengthOfSubject + subject;
|
|
SoftwareConnection send;
|
|
send.sendMessage(message);
|
|
LERROR(fmt::format("Meddelandet som skickas {}", message));
|
|
};
|
|
sizeProperty->onChange(updateSize);
|
|
|
|
*/
|
|
}
|
|
|
|
} // namespace openspace
|
|
|