HY335a-Project
A_multithread_chat_Server_and_chat_client
 All Data Structures Files Functions Variables Typedefs Enumerations Macros
Data Structures | Macros | Typedefs | Enumerations | Functions
chatserver.h File Reference

The header file of the chatserver. More...

#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  connection
struct  client_thread_params
struct  broadcast_invitation_tread_params

Macros

#define TRUE   1
#define FALSE   0
#define BACKLOG_SIZE   1000

Typedefs

typedef char boolean
typedef struct connection connection_t
typedef struct client_thread_params client_thread_params_t
typedef struct
broadcast_invitation_tread_params 
broadcast_invitation_tread_params_t

Enumerations

enum  msg_type_t {
  LOGIN_MSG = 0x1, LOGOUT_MSG = 0x2, TEXT_MSG = 0x3, USERNAME_NOT_EXIST = 0x4,
  USER_ALREADY_LOGGED = 0x5, USERS_LOGGED_REQUEST = 0x6, USERS_LOGGED_RESPONSE = 0x7, BROADCAST_MSG = 0x8,
  UKNOWN_MSG_TYPE = 0xFE, GENERAL_ERROR = 0xFF, LOGIN_MSG = 0x1, LOGOUT_MSG = 0x2,
  TEXT_MSG = 0x3, USERNAME_NOT_EXIST = 0x4, USER_ALREADY_LOGGED = 0x5, USERS_LOGGED_REQUEST = 0x6,
  USERS_LOGGED_RESPONSE = 0x7, BROADCAST_MSG = 0x8, UKNOWN_MSG_TYPE = 0xFE, GENERAL_ERROR = 0xFF
}

Functions

int create_tcp_server_socket (int port)
void * print_stats_counters_thread (void *refresh_rate)
void handle_incoming_connections (int server_socket, connection_t *connections_list)
void * handle_client_connection_thread (void *thread_parameter)
msg_type_t get_message_type (char *buffer, size_t buf_len)
int send_to_client_online_users (connection_t *connections_list, connection_t *client_connection)
boolean handle_login_message (connection_t *connections_list, connection_t *client_connection, char *buffer, size_t buf_len)
boolean handle_logout_message (connection_t *connections_list, connection_t *client_connection)
int handle_text_message (connection_t *connections_list, connection_t *client_connection, char *buffer, size_t buf_len)
int send_back_error_message (connection_t *client_connection, msg_type_t error_type, char *msg_content, size_t msg_content_len)
void * broadcast_invitation_tread (void *broadcast_params)

Detailed Description

The header file of the chatserver.

Author:
surli.nosp@m.gas@.nosp@m.csd.u.nosp@m.oc.g.nosp@m.r

This header file contains several structures that are vital, in order to implement your server.

Also contains the declarations of several functions. All the functions below MUST be implemented, WITHOUT changing its parameters or their return types.

Although you are welcome to add your own.

Note that in your program you should use, all of them, having in mind that some of them are called inside some other. Eg handle_client_connection_thread() will definitly called from the handle_incoming_connections() function.

For questions, problems, or bugs of this header file just email me.

May the code be with you... :)


Macro Definition Documentation

#define BACKLOG_SIZE   1000

This is your default backlog size. Use it at the right function.


Typedef Documentation

typedef char boolean

An easy way to support boolean type in C

Pass this struct as parameter to the thread that broadcasts the invitation messages for the automatic server discovery.

At every client thread we pass this struct as parameter that contains all the info that you need.

typedef struct connection connection_t

In this struct we keep information for every active connection.

The server keeps a list with all the connections. If a new client connects the server adds a node to the list. Until the client performs a loggin, is_logged_in remains FALSE, and username is NULL.

NOTE: Fill free to add your own fields


Enumeration Type Documentation

enum msg_type_t

The different types of messges. The message type is the first byte of every message from the client to the server and vice versa.

As the message type is only one byte we do not care about endianess problems.

NOTE 1: Each message number is in hexademical form. NOTE 2: Enum in C are at least two bytes, so make sure that you take the right byte when you send it. A simple cast to unsigned char would be enough. (Or not? :P ) NOTE 3: Fill free to add your own message types However, they must be unique, one byte long, and be carefull to be the same at the server and at the client.


Function Documentation

void* broadcast_invitation_tread ( void *  broadcast_params)

Thead that performs the broadcast for the automatic server discovery.

Parameters:
[in]broadcast_paramsall the necessary parameters for the automatic server discovery.
int create_tcp_server_socket ( int  port)

This function creates a server socket that listens at a specific port and is binded to all available network interfaces of the host.

Parameters:
[in]portThe port number in which the server should listen.
Returns:
the socket descriptor or -1 if something went wrong. If something failed, a descriptive error message should be printed by using perror() inside this function.
msg_type_t get_message_type ( char *  buffer,
size_t  buf_len 
)

Function that finds the type of the received message.

Parameters:
[in]bufferThe buffer containing the data received from the client.
[in]buf_lenThe length of the buffer.
Returns:
the message type. If the type could NOT be found UKNOWN_MSG_TYPE should be returned.
void* handle_client_connection_thread ( void *  thread_parameter)

This function runs on a new thread that has been created by they handle_incoming_connections() function, and is responsible for sending and receiving message from the connected client.

If your are curious why the parameter is void *, go to the thread Lab and study it AGAIN carefully.

The thread terminates, when the server is killed by the user.

Parameters:
[in]thread_parameterA struct that contains all the appropriate info for the client thread. Use the client_thread_params_t struct that has been casted to void *, as we said at the LAB.
void handle_incoming_connections ( int  server_socket,
connection_t connections_list 
)

This function runs until the user kills the server and waits for incoming connections. When accept() returns a new connection, adds it at the connections_list and pass the node (that contains all the necessary that you need) at a new thread that handles the client. The pthread_create() takes as arguement the function handle_client_thread(void *connection_info)

Parameters:
[in]server_socketa valid server socket discriptor
[in,out]connections_listthe list with the active connections
boolean handle_login_message ( connection_t connections_list,
connection_t client_connection,
char *  buffer,
size_t  buf_len 
)

When a login message received, try to login the user. In any case of error this function sends back to the client the appropriate error message. Also if the user succesfully logged in set the username, at the appropriate field of the client_connection node.

Parameters:
[in]connections_listthe list with all the active connections.
[in]client_connectionthe node of the connections_list, that corresponds to the current client.
[in]bufferthe data as they received from the client
[in]buf_lenthe length of the buffer
Returns:
TRUE if the user succesfully logged in FALSE otherwise.
boolean handle_logout_message ( connection_t connections_list,
connection_t client_connection 
)

When a logout message received, logout the client by deleting the node at the connections_list list and closing the established connection.

Parameters:
[in,out]connections_listthe list with all the active connections.
[in]client_connectionthe node of the connections_list, that corresponds to the current client.
Returns:
TRUE if the user succesfully logged out, FALSE otherwise.
int handle_text_message ( connection_t connections_list,
connection_t client_connection,
char *  buffer,
size_t  buf_len 
)

Function responsible for propagating a message received from a client, to the right receiver that must be connected and logged in.

Parameters:
[in]connections_listthe list with all the active connections.
[in]client_connectionthe node of the connections_list, that corresponds to the current client.
[in]bufferthe data as they received from the client
[in]buf_lenthe length of the buffer
Returns:
the number of bytes sent, or -1 in case of error. Note that in case of error, this function should send back an error message to the client.
void* print_stats_counters_thread ( void *  refresh_rate)

This function runs on a seperate thread and prints every refresh_rate seconds the values of the three counters of the server.

If your are curious why the parameter is void *, go to the thread Lab and study it AGAIN carefully.

Parameters:
[in]refresh_rateThe interval in seconds between two prints of the counters
int send_back_error_message ( connection_t client_connection,
msg_type_t  error_type,
char *  msg_content,
size_t  msg_content_len 
)

General function that sends back to the client, a specified error message.

Parameters:
[in]error_typethe message type that describes the desired error.
[in]msg_contentthe contents of the error message. This can be a discriptive human readable text.
[in]msg_content_lenthe length in bytes, of the msg_content
Returns:
the number of bytes sent, or -1 in case of error.
int send_to_client_online_users ( connection_t connections_list,
connection_t client_connection 
)

Function that sends back to the client a list with all the online users.

Parameters:
[in]connections_listthe list with all the active connections.
[in]client_connectionthe node of the connections_list, that corresponds to the current client.
Returns:
The total number of bytes that were sent, or -1 in case of error.