Skip to content

Commit ccada70

Browse files
committed
Added code documentation for server classes
1 parent 7aa2fb0 commit ccada70

11 files changed

+254
-37
lines changed

server_src/accepter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ class Accepter: public Thread {
1414
ClientManager& clients;
1515

1616
public:
17+
/*
18+
* The Accepter is an active class (runs code in it's own thread)
19+
* It will accept new clients in a blocking manner (blocking it's own thread) and save the
20+
* socket} in the ClientManager to add the client to the current connected clients.
21+
*/
1722
explicit Accepter(GameProtocolServer& server_protocol, ClientManager& clients);
1823

24+
/*
25+
* Method that the parent class Thread will run in it's own thread when calling
26+
* Accepter::start. Contains the logic of the Accepter
27+
*/
1928
virtual void run() override;
2029
};
2130

server_src/army.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,42 @@ class Army {
1919
// cppcheck-suppress unusedStructMember
2020
std::vector<Enemy> army_vector;
2121

22+
/*
23+
* Updates the counters based on an enemy being just killed
24+
*/
2225
void kill_one_enemy();
2326

27+
/*
28+
* Updates the counters based on an enemy being just revived
29+
*/
2430
void revive_one_enemy();
2531

32+
/*
33+
* Notifies all the clients that a death has ocurred via the ClientManager
34+
*/
2635
void notify_death(ClientManager& clients);
2736

37+
/*
38+
* Notifies all the clients that a revival has ocurred via the ClientManager
39+
*/
2840
void notify_revival(ClientManager& clients);
2941

3042
public:
43+
/*
44+
* Army coordinates the logic to interact with the Enemies in an orderly manner
45+
*/
3146
explicit Army(uint16_t size);
3247

48+
/*
49+
* If there is at least one enemy alive, it kills them and notifies all the clients that a
50+
* death has ocurred via the ClientManager
51+
*/
3352
const bool receive_attack(ClientManager& clients);
3453

54+
/*
55+
* Revives all the enemies fit for revival and notifies all the clients that a revival has
56+
* ocurred via the ClientManager
57+
*/
3558
const uint16_t update_tick(ClientManager& clients);
3659
};
3760

server_src/client_connection.h

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,88 @@ class ClientConnection: public Thread {
2121
// cppcheck-suppress unusedStructMember
2222
bool has_finished_running;
2323

24+
/*
25+
* ClientConnection stays with the ownership of the Socket until destruction
26+
*/
2427
Socket client_skt;
2528
GameProtocolServer& server_protocol;
2629

2730
Queue<std::string>& client_commands_for_server;
28-
Queue<GameMessage> messages_from_server;
2931

3032
ClientConnectionReceiver receiver;
3133
ClientConnectionSender sender;
3234

35+
/*
36+
* Each ClientConnection has it's own queue to be used in a blocking manner
37+
* when keeping messages from the server to be sent to the destination client.
38+
* It will block ClientConnectionSender's thread in the pop method
39+
*/
40+
Queue<GameMessage> messages_from_server;
41+
3342

3443
public:
44+
/*
45+
* The ClientConnection is an active class (runs code in it's own thread)
46+
* It will use it's class members ClientConnectionReceiver and ClientConnectionSender in order
47+
* to connect with a client and send and receive in an async manner messages via the protocol
48+
*
49+
* ClientConnectionReceiver and ClientConnectionSender are both active objects so the blocking
50+
* does not affect ClientConnection's thread
51+
*/
3552
explicit ClientConnection(Socket&& client_skt, GameProtocolServer& server_protocol,
3653
Queue<std::string>& client_commands_for_server);
3754

55+
56+
/*
57+
* Disable copy constructor and asignment copy operator
58+
* ClientConnection is not meant to be copied
59+
*/
3860
ClientConnection(const ClientConnection&) = delete;
3961
ClientConnection& operator=(const ClientConnection&) = delete;
4062

4163
/*
42-
* Hacemos ClientConnection sea movible.
43-
* */
64+
* Movement constructor and movement asignment operator are defined
65+
*/
4466
ClientConnection(ClientConnection&&);
4567
ClientConnection& operator=(ClientConnection&&);
4668

69+
70+
/*
71+
* Method that the parent class Thread will run in it's own thread when calling
72+
* ClientConnection::start. Starts it's members ClientConnectionReceiver and
73+
* ClientConnectionSender
74+
*/
4775
virtual void run() override;
4876

77+
/*
78+
* Returns the unique randomly generated id of this ClientConnection
79+
*/
4980
const std::string get_client_id() const;
5081

82+
/*
83+
* It will push a message detailing an enemy's death to the
84+
* ClientConnection::messages_from_server queue
85+
*/
5186
void notify_death(const uint16_t& enemies_alive, const uint16_t& enemies_dead);
5287

88+
/*
89+
* It will push a message detailing an enemy's revival to the
90+
* ClientConnection::messages_from_server queue
91+
*/
5392
void notify_revival(const uint16_t& enemies_alive, const uint16_t& enemies_dead);
5493

94+
/*
95+
* It will kill this ClientConnection
96+
* After executing this method the ClientConnection will be no longer be able to be used
97+
* It will shutdown the socket connection and join the ClientConnectionReceiver and
98+
* ClientConnectionSender threads into the ClientConnection's thread
99+
*/
55100
void kill();
56101

102+
/*
103+
* Joins ClientConnectionReceiver and ClientConnectionSender threads into ClientConnection's
104+
* thread if they were not joined before and shutdowns the socket connection it it's still open
105+
*/
57106
~ClientConnection();
58107
};
59108

server_src/client_connection_receiver.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ class ClientConnectionReceiver: public Thread {
1818
Queue<std::string>& client_commands_for_server;
1919

2020
public:
21+
/*
22+
* The ClientConnectionReceiver is an active class (runs code in it's own thread)
23+
* It will use the Socket via the GameProtocolServer in order to receive command messages and
24+
* push them in a blocking manner into the client_commands_for_server queue, blocking it's own
25+
* thread in the receive part of the Socket communication
26+
*/
2127
explicit ClientConnectionReceiver(const std::string& client_id, Socket& skt,
2228
GameProtocolServer& server_protocol,
2329
Queue<std::string>& client_commands_for_server);
24-
30+
/*
31+
* Method that the parent class Thread will run in it's own thread when calling
32+
* ClientConnectionReceiver::start. Contains the logic of the ClientConnectionReceiver.
33+
* It will stop receiving if the socket connection is shutdown.
34+
*/
2535
virtual void run() override;
2636
};
2737

server_src/client_connection_sender.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,21 @@ class ClientConnectionSender: public Thread {
1818
Queue<GameMessage>& messages_from_server;
1919

2020
public:
21+
/*
22+
* The ClientConnectionSender is an active class (runs code in it's own thread)
23+
* It will use the Socket via the GameProtocolServer in order to send command messages after
24+
* being poped in a blocking manner from the messages_from_server queue, blocking it's own
25+
* thread in the pop part of the process
26+
*/
2127
explicit ClientConnectionSender(const std::string& client_id, Socket& skt,
2228
GameProtocolServer& server_protocol,
2329
Queue<GameMessage>& messages_from_server);
24-
30+
/*
31+
* Method that the parent class Thread will run in it's own thread when calling
32+
* ClientConnectionSender::start. Contains the logic of the ClientConnectionSender.
33+
* It will stop sending if the socket connection is shutdown or if the messages_from_server
34+
* queue is closed
35+
*/
2536
virtual void run() override;
2637
};
2738

server_src/client_manager.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,53 @@ class ClientManager {
2020
std::mutex mutex;
2121

2222
public:
23+
/*
24+
* ClientManager is a class of type Monitor
25+
* It handles the access to the clients of type ClientConnection, given that the clients are
26+
* active objects and are running code in their own threads
27+
*
28+
* It's also the single entry point for the ClientManager::client_commands_queue queue, that
29+
* keeps all the messages received from all the clients
30+
*/
2331
explicit ClientManager(GameProtocolServer& server_protocol);
2432

33+
/*
34+
* Receives the ownership of a Socket to give it to a newly created ClientConnection and then
35+
* save it in the vector ClientManager::clients as a heap allocated object
36+
*/
2537
void add_client(Socket&& new_client_skt);
38+
39+
/*
40+
* Returns the number of connections currently stored
41+
*/
2642
const uint16_t current_connections();
27-
const bool try_next_command(std::string& next_cmd);
2843

29-
void notify_all(const uint16_t& just_died, const uint16_t& just_revived);
44+
/*
45+
* Performs a non-blocking pop onto the ClientManager::client_commands_queue queue
46+
*/
47+
const bool try_next_command(std::string& next_cmd);
3048

49+
/*
50+
* Sends an enemy death event to each connected client and prints that event to std::out
51+
*/
3152
void notify_death_all(const uint16_t enemies_alive, const uint16_t enemies_dead);
53+
54+
/*
55+
* Sends an enemy revival event to each connected client and prints that event to std::out
56+
*/
3257
void notify_revival_all(const uint16_t enemies_alive, const uint16_t enemies_dead);
3358

59+
/*
60+
* It will kill all the ClientConnections
61+
* After executing this method the ClientConnections will be no longer be able to be used
62+
* It will shutdown the sockets connections and join the all the ClientConnection threads into
63+
* the ClientManager's thread
64+
*/
3465
void kill_all();
66+
67+
/*
68+
* Frees all the heap allocated memory for the ClientConnections
69+
*/
3570
~ClientManager();
3671
};
3772

server_src/enemy.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,38 @@ class Enemy {
1212
// cppcheck-suppress unusedStructMember
1313
uint32_t ticks_until_revived;
1414

15+
/*
16+
* Returns true if the enemy is dead has already reached Enemy::TICKS_FOR_REVIVAL ticks dead
17+
*/
1518
const bool should_revive() const;
1619

20+
/*
21+
* Revives the enemy and makes it fit for being attacked
22+
*/
1723
void revive();
1824

1925
public:
26+
/*
27+
* Encapsulates the logic and inner workings of an enemy
28+
* Enemy::TICKS_FOR_REVIVAL defines the number of ticks needed for an enemy to revive
29+
* after being killed
30+
*/
2031
Enemy();
2132

33+
/*
34+
* Returns true is the enemy is alive
35+
*/
2236
const bool is_alive() const;
2337

38+
/*
39+
* Performs the logic for receiving an attack. Killing the enemy and starting the revival
40+
* process
41+
*/
2442
const bool receive_attack();
2543

44+
/*
45+
* Performs the updated expected for each tick. Updates the progress of revival
46+
*/
2647
const bool update_tick();
2748
};
2849

server_src/game_protocol_server.h

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class GameProtocolServer {
3131
* serialization of data and the socket communication. It should be paired at the other end
3232
* with a GameProtocolClient
3333
*
34-
* For GameProtocolServer to be fully constructed it will wait for an inbound socket
35-
* connection. This means that until a client connects, this class will not be fully
36-
* constructed. A GameProtocolServer already instantiated object naturally means it has
37-
* previously established a connection with a client
34+
* It has the ownership of the main socket of the server
35+
* It serves as a translation layer for other classes that has ownership of their own sockets
36+
*
37+
* A mutex is used to ensure that a race condition is not possible when reading the state of
38+
* the main socket
3839
*/
3940
explicit GameProtocolServer(const std::string& servname);
4041

@@ -53,42 +54,63 @@ class GameProtocolServer {
5354
GameProtocolServer& operator=(GameProtocolServer&&) = default;
5455

5556
/*
56-
* Receives action codes from inbound_client until the end_of_sequence action code is sent
57+
* Returns the Socket of an accepted connection to the main server socket
5758
*/
58-
// const std::vector<std::string> get_action_sequence();
59+
Socket accept_client();
5960

6061
/*
61-
* Returns true if the connection with the inbound_client is still alive. Return false
62-
* if it has closed
62+
* Returns true if the main server socket is closed or false otherwise
6363
*/
64-
// const bool is_client_connection_alive() const;
64+
const bool is_socket_closed();
6565

6666
/*
67-
* Sends to inbound_client as a halfword (2 bytes) the header of the payload (size of the
68-
* payload) and then sends bytewise the payload (binary representation of a string)
67+
* Returns true if the input command string is an attack command, returns false otherwise
6968
*/
70-
// void send_action_sequence_with_combos(const std::string& combo_response);
71-
72-
Socket accept_client();
73-
74-
const bool is_socket_closed();
75-
7669
const bool is_command_attack(const std::string& command) const;
7770

71+
/*
72+
* Builds and returns a GameMessage that represents a death event with the corresponding
73+
* parameters
74+
*/
7875
const GameMessage get_message_for_death_event(const uint16_t enemies_alive,
7976
const uint16_t enemies_dead) const;
8077

78+
/*
79+
* Builds and returns a GameMessage that represents a revival event with the corresponding
80+
* parameters
81+
*/
8182
const GameMessage get_message_for_revival_event(const uint16_t enemies_alive,
8283
const uint16_t enemies_dead) const;
8384

85+
/*
86+
* Sends a complete message to the client. First a byte for signaling the beginning of the
87+
* broadcast then it sends 2 halfwords (each one of 2 bytes) representing the number of enemies
88+
* alive and dead, in that order, and finally it sends a byte for signaling if an enemy has been
89+
* killed or has revived
90+
*/
8491
void send_message_to_client(Socket& client_skt, const GameMessage& message, bool& was_closed);
8592

93+
/*
94+
* Receives a byte from the client_skt that represents the desired command. Returns that
95+
* command name
96+
*/
8697
const std::string receive_message_from_client(Socket& client_skt, bool& was_closed);
8798

99+
/*
100+
* Prints to std::out the current death event with the number of enemies alive and dead
101+
* represented by the params
102+
*/
88103
void print_death_in_server(const uint16_t& enemies_alive, const uint16_t& enemies_dead) const;
89104

105+
/*
106+
* Prints to std::out the current revival event with the number of enemies alive and dead
107+
* represented by the params
108+
*/
90109
void print_revival_in_server(const uint16_t& enemies_alive, const uint16_t& enemies_dead) const;
91110

111+
/*
112+
* Shutdowns and closes the main server socket
113+
*/
92114
void turn_off_connections();
93115
};
94116

0 commit comments

Comments
 (0)