Skip to content

Commit 477452f

Browse files
authored
Merge pull request #478 from HyperloopUPV-H8/FW-126
[FW-126] Use Logger
2 parents 5bbe11c + ebe8a96 commit 477452f

6 files changed

Lines changed: 960 additions & 777 deletions

File tree

Lines changed: 147 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,153 @@
11

22
#pragma once
3+
#include <poll.h>
34

4-
#include "HALALMock/Services/Communication/Ethernet/EthernetNode.hpp"
5-
#include "HALALMock/Services/Communication/Ethernet/Ethernet.hpp"
6-
#include "HALALMock/Models/Packets/Packet.hpp"
7-
#include "HALALMock/Models/Packets/Order.hpp"
8-
#include "HALALMock/Models/Packets/OrderProtocol.hpp"
5+
#include <atomic>
96
#include <iostream>
107
#include <thread>
11-
#include <poll.h>
12-
#include <atomic>
13-
/**
14-
* @brief class that handles a single point to point server client connection, emulating the server side.
15-
*
16-
* The flow of this class goes as follows:
17-
*
18-
* 1. When the constructor is called, the listener is activated and starts working immediately
19-
*
20-
* 2. After a client issues a connection to the ServerSocket and Ethernet#update is executed, the ServerSocket accepts the request
21-
*
22-
* 3. Accepting the request raises an interrupt that calls accept_callback, which closes the listener socket (on server_control_block) and opens the connection socket (on client_control_block)
23-
*
24-
* 4. The connection goes on until one of the ends closes it, which calls the ErrorHandler to send the board into fault as a default behaviour.
25-
*
26-
* @see Ethernet#update
27-
*/
28-
class ServerSocket : public OrderProtocol{
29-
private:
30-
void create_server_socket();
31-
bool configure_server_socket(int& socket);
32-
void listen_for_connection();
33-
void close_inside_thread();
34-
bool accept_callback(int& client_fd, sockaddr_in& client_address);
35-
void receive();
36-
queue<Packet*> tx_packet_buffer;
37-
std::jthread listening_thread;
38-
std::jthread receive_thread;
39-
std::mutex mutex;
40-
int server_socket_fd{-1};
41-
int client_fd{-1};
42-
43-
public:
44-
enum ServerState{
45-
INACTIVE,
46-
LISTENING,
47-
ACCEPTED,
48-
CLOSING,
49-
CLOSED
50-
};
51-
52-
static unordered_map<uint32_t,ServerSocket*> listening_sockets;
53-
IPV4 local_ip;
54-
uint32_t local_port;
55-
IPV4 remote_ip;
56-
ServerState state;
57-
static uint8_t priority;
58-
59-
struct KeepaliveConfig{
60-
uint32_t inactivity_time_until_keepalive = TCP_INACTIVITY_TIME_UNTIL_KEEPALIVE;
61-
uint32_t space_between_tries = TCP_SPACE_BETWEEN_KEEPALIVE_TRIES;
62-
uint32_t tries_until_disconnection = TCP_KEEPALIVE_TRIES_UNTIL_DISCONNECTION;
63-
}keepalive_config;
64-
65-
ServerSocket();
66-
67-
68-
ServerSocket(ServerSocket&& other);
69-
70-
/**
71-
* @brief ServerSocket constructor that receives the server ip on the net as a binary value.
72-
*
73-
* @param local_ip the server ip on.
74-
* @param local_port the port number that the server listens for connections.
75-
*/
76-
ServerSocket(IPV4 local_ip, uint32_t local_port);
77-
ServerSocket(IPV4 local_ip, uint32_t local_port, uint32_t inactivity_time_until_keepalive, uint32_t space_between_tries, uint32_t tries_until_disconnection);
78-
/**
79-
* @brief ServerSocket constructor that uses the EthernetNode class as a parameter
80-
*
81-
* @param local_node the EthernetNode to listen to
82-
*
83-
* @see EthernetNode
84-
*/
85-
ServerSocket(EthernetNode local_node);
86-
~ServerSocket();
87-
88-
void operator=(ServerSocket&& other);
89-
90-
/**
91-
* @brief ends the connection between the server and the client.
92-
*/
93-
void close();
94-
/**
95-
* @brief saves the order data into the tx_packet_buffer so it can be sent when a connection is accepted
96-
*
97-
* @param order the order to send, which contains the data and id of the message
98-
* @return true if the data could be allocated in the buffer, false otherwise
99-
*/
100-
bool add_order_to_queue(Order& order);
101-
102-
/**
103-
* @brief puts the order data into the tx_packet_buffer and sends all the data in the buffer to the client
104-
*
105-
* @param order the order to send, which contains the data and id of the message
106-
* @return true if the data was sent successfully, false otherwise
107-
*/
108-
bool send_order(Order& order) override{
109-
if(state != ACCEPTED){
110-
return false;
111-
}
112-
tx_packet_buffer.push(&order);
113-
send();
114-
return true;
115-
}
116-
117-
/**
118-
* @brief sends all the binary data saved in the tx_packet_buffer to the connected client.
119-
*
120-
* This function is the one that actually handles outgoing communication, sending one by one the packets in the tx_packet_buffer
121-
* The messages in the buffer are all immediately sent after calling this function, unless an error of any kind happened, in which case ErrorHandler is raised
122-
*/
123-
void send();
124-
125-
/**
126-
* @brief function that returns wether or not a client is connected to the ServerSocket
127-
*
128-
* This functions returns a comparison to the state of the ServerSocket, checking wether or not it is on the ACCEPTED state
129-
* This function is equivalent to doing instance->state == ServerSocket#ACCEPT
130-
*
131-
* @return true if a connection with the client was established, false otherwise
132-
*/
133-
bool is_connected();
134-
135-
};
136-
1378

9+
#include "HALALMock/Models/Packets/Order.hpp"
10+
#include "HALALMock/Models/Packets/OrderProtocol.hpp"
11+
#include "HALALMock/Models/Packets/Packet.hpp"
12+
#include "HALALMock/Services/Communication/Ethernet/Ethernet.hpp"
13+
#include "HALALMock/Services/Communication/Ethernet/EthernetNode.hpp"
14+
/**
15+
* @brief class that handles a single point to point server client connection,
16+
* emulating the server side.
17+
*
18+
* The flow of this class goes as follows:
19+
*
20+
* 1. When the constructor is called, the listener is activated and starts
21+
* working immediately
22+
*
23+
* 2. After a client issues a connection to the ServerSocket and Ethernet#update
24+
* is executed, the ServerSocket accepts the request
25+
*
26+
* 3. Accepting the request raises an interrupt that calls accept_callback,
27+
* which closes the listener socket (on server_control_block) and opens the
28+
* connection socket (on client_control_block)
29+
*
30+
* 4. The connection goes on until one of the ends closes it, which calls the
31+
* ErrorHandler to send the board into fault as a default behaviour.
32+
*
33+
* @see Ethernet#update
34+
*/
35+
class ServerSocket : public OrderProtocol {
36+
private:
37+
void create_server_socket();
38+
bool configure_server_socket(int& socket);
39+
void listen_for_connection();
40+
void close_inside_thread();
41+
bool accept_callback(int& client_fd, sockaddr_in& client_address);
42+
void receive();
43+
queue<Packet*> tx_packet_buffer;
44+
std::jthread listening_thread;
45+
std::jthread receive_thread;
46+
std::mutex mutex;
47+
int server_socket_fd{-1};
48+
int client_fd{-1};
49+
50+
public:
51+
enum ServerState { INACTIVE, LISTENING, ACCEPTED, CLOSING, CLOSED };
52+
53+
static unordered_map<uint32_t, ServerSocket*> listening_sockets;
54+
IPV4 local_ip;
55+
uint32_t local_port;
56+
IPV4 remote_ip;
57+
ServerState state;
58+
static uint8_t priority;
59+
60+
struct KeepaliveConfig {
61+
uint32_t inactivity_time_until_keepalive =
62+
TCP_INACTIVITY_TIME_UNTIL_KEEPALIVE;
63+
uint32_t space_between_tries = TCP_SPACE_BETWEEN_KEEPALIVE_TRIES;
64+
uint32_t tries_until_disconnection =
65+
TCP_KEEPALIVE_TRIES_UNTIL_DISCONNECTION;
66+
} keepalive_config;
67+
68+
ServerSocket();
69+
70+
ServerSocket(ServerSocket&& other);
71+
72+
/**
73+
* @brief ServerSocket constructor that receives the server ip on the net as
74+
* a binary value.
75+
*
76+
* @param local_ip the server ip on.
77+
* @param local_port the port number that the server listens for
78+
* connections.
79+
*/
80+
ServerSocket(IPV4 local_ip, uint32_t local_port);
81+
ServerSocket(IPV4 local_ip, uint32_t local_port,
82+
uint32_t inactivity_time_until_keepalive,
83+
uint32_t space_between_tries,
84+
uint32_t tries_until_disconnection);
85+
/**
86+
* @brief ServerSocket constructor that uses the EthernetNode class as a
87+
* parameter
88+
*
89+
* @param local_node the EthernetNode to listen to
90+
*
91+
* @see EthernetNode
92+
*/
93+
ServerSocket(EthernetNode local_node);
94+
~ServerSocket();
95+
96+
void operator=(ServerSocket&& other);
97+
98+
/**
99+
* @brief ends the connection between the server and the client.
100+
*/
101+
void close();
102+
/**
103+
* @brief saves the order data into the tx_packet_buffer so it can be sent
104+
* when a connection is accepted
105+
*
106+
* @param order the order to send, which contains the data and id of the
107+
* message
108+
* @return true if the data could be allocated in the buffer, false
109+
* otherwise
110+
*/
111+
bool add_order_to_queue(Order& order);
112+
113+
/**
114+
* @brief puts the order data into the tx_packet_buffer and sends all the
115+
* data in the buffer to the client
116+
*
117+
* @param order the order to send, which contains the data and id of the
118+
* message
119+
* @return true if the data was sent successfully, false otherwise
120+
*/
121+
bool send_order(Order& order) override {
122+
if (state != ACCEPTED) {
123+
return false;
124+
}
125+
tx_packet_buffer.push(&order);
126+
send();
127+
return true;
128+
}
129+
130+
/**
131+
* @brief sends all the binary data saved in the tx_packet_buffer to the
132+
* connected client.
133+
*
134+
* This function is the one that actually handles outgoing communication,
135+
* sending one by one the packets in the tx_packet_buffer The messages in
136+
* the buffer are all immediately sent after calling this function, unless
137+
* an error of any kind happened, in which case ErrorHandler is raised
138+
*/
139+
void send();
140+
141+
/**
142+
* @brief function that returns wether or not a client is connected to the
143+
* ServerSocket
144+
*
145+
* This functions returns a comparison to the state of the ServerSocket,
146+
* checking wether or not it is on the ACCEPTED state This function is
147+
* equivalent to doing instance->state == ServerSocket#ACCEPT
148+
*
149+
* @return true if a connection with the client was established, false
150+
* otherwise
151+
*/
152+
bool is_connected();
153+
};

0 commit comments

Comments
 (0)