11 : EntityNode(nodeId), _childNode(nullptr)
20 catch (
const std::exception &e) {
21 PRETTY_ERROR <<
"Exception during destruction: " << e.what() << std::endl;
28 PRETTY_DEBUG <<
"Calling the initalise function from the child class" << std::endl;
30 _childNode->initialize();
31 PRETTY_DEBUG <<
"Child initialise function called" << std::endl;
33 PRETTY_ERROR <<
"The child node failed to initialise" << std::endl;
51 _childNode->startGame();
57 _childNode->sendMessage(message);
62 ENSURE_THREAD_ALIVE(
"the function in charge of returning messages received since the previous call");
63 return _childNode->getBufferedMessages();
90 if (!isThreadAlive()) {
93 return _childNode->isConnected();
98 std::lock_guard<std::mutex> lock(_mutex);
99 return _childAlive && _childNode !=
nullptr && _childThread.joinable();
105 return _childNode->bytesToFloat(bytes);
112 return _childNode->bytesToHex(bytes);
118 return _childNode->translateMessage(message);
124 _childNode->receiveMessage();
128void GUI::Network::ThreadCapsule::_spawnChild()
130 if (isThreadAlive()) {
131 PRETTY_ERROR <<
"There is already an active thread that can be used to call the functions" << std::endl;
136 _childNode = std::make_shared<NetworkManager>(getEntityNodeId());
137 _childNode->setPlayerName(_playerName);
138 _childNode->setAddress(_ip, _port);
139 _childNode->initialize();
140 _childNode->startReceivingMessages();
143 catch (
const std::exception &e) {
144 PRETTY_ERROR <<
"Failed to create thread: " << e.what() << std::endl;
151void GUI::Network::ThreadCapsule::_killChild()
153 if (!isThreadAlive()) {
154 PRETTY_ERROR <<
"There is no active thread to terminate, skipping" << std::endl;
158 _childNode->stopReceivingMessages();
160 if (_childThread.joinable()) {
#define PRETTY_ERROR
Error log with details and colour.
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_INFO
Info log with details and colour.
#define ENSURE_THREAD_ALIVE(method)
This is the class in charge of informing the user that the program could not find any active threads ...
void receiveMessage()
Receives messages continuously from the server.
void sendMessage(const MessageNode &message)
void setPort(const unsigned int port)
const bool isConnected() const
const std::vector< GUI::Network::MessageNode > getReceivedMessages()
const float bytesToFloat(const uint8_t *bytes)
ThreadCapsule(const std::uint32_t nodeId=0)
void setPlayerName(const std::string &name)
void setAddress(const std::string &ip, const unsigned int port)
const std::string bytesToHex(const std::vector< uint8_t > &bytes)
const bool isThreadAlive() const
const GUI::Network::MessageNode translateMessage(const std::vector< uint8_t > &message)
void setIp(const std::string &ip)