HY335a-Project
A_multithread_chat_Server_and_chat_client
 All Classes Files Functions Variables Typedefs Enumerations Macros
chatserver.h
Go to the documentation of this file.
1 #ifndef CHATSERVER_H
2 #define CHATSERVER_H
3 
4 #include <pthread.h>
5 
35 #define BACKLOG_SIZE 1000
36 
56 typedef enum {
57  LOGIN_MSG = 0x1,
58  LOGOUT_MSG = 0x2,
59  TEXT_MSG = 0x3,
60  USERNAME_NOT_EXIST = 0x4,
61  USER_ALREADY_LOGGED = 0x5,
62  USERS_LOGGED_REQUEST = 0x6,
63  USERS_LOGGED_RESPONSE = 0x7,
64  BROADCAST_MSG = 0x8,
65  UKNOWN_MSG_TYPE = 0xFE,
66  GENERAL_ERROR = 0xFF
67 } msg_type_t;
68 
69 
82 typedef struct connection{
84  char *username;
85  char *ip;
86  bool is_logged_in;
87  struct connection *previous;
88  struct connection *next;
89 } connection_t;
90 
95 typedef struct client_thread_params {
99 
106  int port;
107  char *server_ip;
110 
111 
112 class chatserver {
113 private:
114  int d_port_number;
115  connection_t *d_connections_list;
116  /* 3 different global counters
117  * NOTE; These counters are accessed
118  * simutanously by all threads, so race conditions
119  * may rise. Go to the threads lab and check
120  * about race conditions and locking.
121  *
122  * Some setters and getters functions may
123  * need to access those counters
124  */
125 
126  unsigned long int d_total_msgs_received;
127  unsigned long int d_total_bytes_sent;
128  unsigned long int d_total_bytes_received;
129 
130  /*
131  * Three different mutexes that you are going to use for
132  * locking in your threads.
133  */
134  pthread_mutex_t d_print_mutex;
135  pthread_mutex_t d_counters_mutex;
136  pthread_mutex_t d_active_connections_mutex;
137 
138 public:
147  chatserver(int port, bool automatic_server_discovery);
148 
159 
171  void *print_stats_counters_thread(void *refresh_rate);
172 
183  void handle_incoming_connections(int server_socket);
184 
202  void *handle_client_connection_thread(void *thread_parameter);
203 
214  msg_type_t get_message_type(char *buffer, size_t buf_len);
215 
224  int send_to_client_online_users(connection_t *client_connection);
225 
239  bool handle_login_message(connection_t *client_connection,
240  char *buffer,
241  size_t buf_len);
242 
251  bool handle_logout_message( connection_t *client_connection);
252 
253 
267  int handle_text_message(connection_t *client_connection,
268  char *buffer,
269  size_t buf_len);
270 
283  int send_back_error_message(connection_t *client_connection,
284  msg_type_t error_type,
285  char *msg_content,
286  size_t msg_content_len);
287 
294  void *broadcast_invitation_tread(void *broadcast_params);
295 
296 
297 };
298 
299 #endif