R-Type  2
Doom but in better
Loading...
Searching...
No Matches
ThreadCapsule.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** ThreadCapsule.cpp
6*/
7
9
11 : EntityNode(nodeId), _childNode(nullptr)
12{
13};
14
16{
17 try {
18 _killChild();
19 }
20 catch (const std::exception &e) {
21 PRETTY_ERROR << "Exception during destruction: " << e.what() << std::endl;
22 }
23};
24
26{
27 _spawnChild();
28 PRETTY_DEBUG << "Calling the initalise function from the child class" << std::endl;
29 if (_childNode) {
30 _childNode->initialize();
31 PRETTY_DEBUG << "Child initialise function called" << std::endl;
32 } else {
33 PRETTY_ERROR << "The child node failed to initialise" << std::endl;
34 }
35};
36
38{
39 _spawnChild();
40}
41
42
44{
45 _killChild();
46}
47
49{
50 ENSURE_THREAD_ALIVE("the function in charge of sending messages");
51 _childNode->startGame();
52}
53
55{
56 ENSURE_THREAD_ALIVE("the function in charge of sending messages");
57 _childNode->sendMessage(message);
58};
59
60const std::vector<GUI::Network::MessageNode> GUI::Network::ThreadCapsule::getReceivedMessages()
61{
62 ENSURE_THREAD_ALIVE("the function in charge of returning messages received since the previous call");
63 return _childNode->getBufferedMessages();
64};
65
66void GUI::Network::ThreadCapsule::setPort(const unsigned int port)
67{
68 _port = port;
69};
70
71void GUI::Network::ThreadCapsule::setIp(const std::string &ip)
72{
73 _ip = ip;
74};
75
76void GUI::Network::ThreadCapsule::setPlayerName(const std::string &name)
77{
78 _playerName = name;
79};
80
81void GUI::Network::ThreadCapsule::setAddress(const std::string &ip, const unsigned int port)
82{
83 _ip = ip;
84 _port = port;
85};
86
87
89{
90 if (!isThreadAlive()) {
91 return false;
92 }
93 return _childNode->isConnected();
94};
95
97{
98 std::lock_guard<std::mutex> lock(_mutex);
99 return _childAlive && _childNode != nullptr && _childThread.joinable();
100}
101
102const float GUI::Network::ThreadCapsule::bytesToFloat(const uint8_t *bytes)
103{
104 ENSURE_THREAD_ALIVE("the function in charge of converting bytes to floats");
105 return _childNode->bytesToFloat(bytes);
106};
107
108
109const std::string GUI::Network::ThreadCapsule::bytesToHex(const std::vector<uint8_t> &bytes)
110{
111 ENSURE_THREAD_ALIVE("the function in charge of converting the bytes to hexadecimal");
112 return _childNode->bytesToHex(bytes);
113};
114
116{
117 ENSURE_THREAD_ALIVE("the function in charge of translating the message to a string");
118 return _childNode->translateMessage(message);
119};
120
122{
123 ENSURE_THREAD_ALIVE("the function in charge of receiving messages");
124 _childNode->receiveMessage();
125};
126
127
128void GUI::Network::ThreadCapsule::_spawnChild()
129{
130 if (isThreadAlive()) {
131 PRETTY_ERROR << "There is already an active thread that can be used to call the functions" << std::endl;
132 return;
133 }
134 _childAlive = true;
135 try {
136 _childNode = std::make_shared<NetworkManager>(getEntityNodeId());
137 _childNode->setPlayerName(_playerName);
138 _childNode->setAddress(_ip, _port);
139 _childNode->initialize();
140 _childNode->startReceivingMessages();
141 _childThread = std::thread(&NetworkManager::receiveMessage, _childNode);
142 }
143 catch (const std::exception &e) {
144 PRETTY_ERROR << "Failed to create thread: " << e.what() << std::endl;
145 _childAlive = false;
146 _childNode.reset();
147 throw CustomExceptions::NoActiveNetworkThread("<Thread initialisation failed for the network>");
148 }
149}
150
151void GUI::Network::ThreadCapsule::_killChild()
152{
153 if (!isThreadAlive()) {
154 PRETTY_ERROR << "There is no active thread to terminate, skipping" << std::endl;
155 return;
156 }
157 if (_childNode) {
158 _childNode->stopReceivingMessages();
159 }
160 if (_childThread.joinable()) {
161 PRETTY_INFO << "Joining thread..." << std::endl;
162 _childThread.join();
163 }
164 _childAlive = false;
165 _childNode.reset();
166}
#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 ...
Definition No.hpp:939
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)