diff --git a/src/sender.c b/src/sender.c index 6e858660cd5de1282a283c65ac2d366c29faa28b..9fe8f3692788c8612d4c4080ad87053d185b2c4e 100644 --- a/src/sender.c +++ b/src/sender.c @@ -132,7 +132,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - // Setting a timer only when waiting for the last ACK (so the sender buffer is 30) + // Setting a timer only when waiting for the very last ACK gettimeofday(&curr_time, NULL); if (state->last_pkt_sent == CLOSING_PKT && ((curr_time.tv_sec - closing_pkt_sent_time.tv_sec) > SENDER_INACTIVE_TIMEOUT)) { @@ -179,8 +179,8 @@ int main(int argc, char **argv) { rvalue = checking_timer(state, socket_fd); if (rvalue == -1) { - // If an error occured when trying to send back the closing pkt, - // we guess that the receiver has simply disconnected and the ACK of the closing pkt was lost. + // If an error occured when trying to send back the CLOSING_PKT, + // we guess that the receiver has simply disconnected and the ACK of the CLOSING_PKT was lost. if (state->last_pkt_sent == CLOSING_PKT) { DEBUG("The sender can't send anything to the receiver anymore (which has probably disconnected) so it'll disconnect !"); diff --git a/src/sender_utils.c b/src/sender_utils.c index 9189c6e643fee1c63873d7ed2281c718e2cc27ff..2179cddf72d2eef2035e7c168031a16a1d9ac271 100644 --- a/src/sender_utils.c +++ b/src/sender_utils.c @@ -38,7 +38,7 @@ void state_del(sender_state_t *state) free(state); } -int can_send(sender_state_t *state) +bool can_send(sender_state_t *state) { if (state->last_pkt_sent == RANDOM_DATA_PKT) { @@ -48,7 +48,7 @@ int can_send(sender_state_t *state) { // I want to be sure all the data pkt has been received before letting know the receiver // it was the end of the file (so that I can set a timer for timeout) - return state->s_window_size == WINDOW_SIZE; + return state->s_window_size == MAX_WINDOW_SIZE; } else { @@ -127,7 +127,7 @@ int handle_returning_ack_nack(sender_state_t *state, int socket_fd) uint8_t place_last_ack = state->map_seqnum_to_buffer_place[seqnum_ack]; uint8_t place_last_nack = state->map_seqnum_to_buffer_place[seqnum_nack]; - // Nothing new need to be acked + // Nothing need to be acked if (place_last_ack == OUT_OFF_WINDOW) { // Checking if it's in my window: @@ -178,7 +178,6 @@ int handle_returning_ack_nack(sender_state_t *state, int socket_fd) int checking_timer(sender_state_t *state, int socket_fd) { struct timeval time; - // Checking if the slot contains a sended packet if (state->buffer[state->tail] != NULL) { diff --git a/src/sender_utils.h b/src/sender_utils.h index 948fabefc0e8e15e1e1340f8df5194d89c91e110..c6eb106e9197e1f4d5ed78279f9436a67fce29da 100644 --- a/src/sender_utils.h +++ b/src/sender_utils.h @@ -17,13 +17,13 @@ #include "packet_interface.h" -#define SEQNUM_RANGE 256 -#define TIMER_LIMIT 2 // It's in seconds, in this network, the latency to send is = [0, 2s] -#define SENDER_INACTIVE_TIMEOUT 30 // Only waits 30sec for the last ACK -#define WINDOW_SIZE 31 -#define OUT_OFF_WINDOW 255 +#define SEQNUM_RANGE 256 +#define TIMER_LIMIT 2 // It's in seconds, in this network, the latency to send is = [0, 2s] +#define SENDER_INACTIVE_TIMEOUT 30 // Only waits 30sec for the ACK of the CLOSING_PKT (the very last sended pkt) +#define WINDOW_SIZE 31 +#define OUT_OFF_WINDOW 255 -// It is used to identify what kind of pkt what send lately +// It is used to identify what kind of pkt was sent lately #define RANDOM_DATA_PKT 0 #define LAST_DATA_PKT 1 #define CLOSING_PKT 2 @@ -37,8 +37,8 @@ typedef struct state { uint8_t s_window_size; // sender (our) buffer space time_t timers[WINDOW_SIZE]; // Time in seconds (corresponds to the timers of the sended pkt) pkt_t *buffer[WINDOW_SIZE]; // When the buffer fields are not used, they MUST be se to NULL - uint8_t head; // place last element insert +1 in the buffer (free place) | Head and tail are used - uint8_t tail; // place oldest element insert in the buffer | used to know the start of the sended window + uint8_t head; // place last element insert +1 in the buffer (free place) | Head and tail are used to know + uint8_t tail; // place oldest element insert in the buffer | the start and end of the sender window (of the sended pkt) uint8_t next_seqnum; uint8_t map_seqnum_to_buffer_place[SEQNUM_RANGE]; // Default value is: OUT_OFF_WINDOW uint8_t last_pkt_sent; // Can either be: RANDOM_DATA_PKT, LAST_DATA_PKT or CLOSING_PKT @@ -60,12 +60,16 @@ sender_state_t *state_new(); void state_del(sender_state_t *state); /** - * @brief Checking if both the sender and receiver buffer has some room left + * @brief Checking if the sender is allowed to send a NEW pkt * * @param state : The variable representing the sender (state). - * @return int : 1 if there are enough space in the buffer, 0 otherwise. + * @return bool : + * - true : if there are enough room in the buffers and some pkt still need to be sent + * - false: if there are no room in the buffer + * or if we're waiting for the last ACK of the DATATYPE + * or if all pkt have been sent */ -int can_send(sender_state_t *state); +bool can_send(sender_state_t *state); /** * @brief Cautious this function is used for sending and sending back pkt ! @@ -79,32 +83,32 @@ int can_send(sender_state_t *state); int send_pkt(sender_state_t *state, pkt_t *pkt, uint8_t position, int socket_fd); /** - * @brief + * @brief The function handle ACK and NACK pkt. * - * @param state - * @param socket_fd - * @return int + * @param state : The variable representing the sender (state). + * @param socket_fd : The socket on which pkt are sent. + * @return int : 0 if no error, -1 otherwise */ int handle_returning_ack_nack(sender_state_t *state, int socket_fd); /** - * @brief + * @brief The function checks if the oldest not acked pkt has exceeded the timer (TIMER_LIMIT). + * If it has exceeded the timer then it is sent back. * - * @param state - * @param socket_fd - * @return int + * @param state : The variable representing the sender (state). + * @param socket_fd : The socket on which pkt are sent. + * @return int : 0 if no error, -1 otherwise */ int checking_timer(sender_state_t *state, int socket_fd); /** - * @brief When this function is called, the sender MUST be allowed to send a data pkt + * @brief When this function is called, the sender MUST be allowed to send a data pkt. + * It sends the next pkt and update the variable 'last_pkt_sent' of state. * - * @param state - * @param sending_fd - * @param socket_fd - * @return int : 1 if it has sent the empty datatype pkt else 0 - * -1 if an error occured - * -2 Both sender and receiver buffer are full + * @param state : The variable representing the sender (state). + * @param sending_fd : The file descriptor of the file to be send + * @param socket_fd : The socket on which pkt are sent. + * @return int : 0 if no error, -1 otherwise */ int read_and_send(sender_state_t *state, int sending_fd, int socket_fd); diff --git a/tests/simple_test.sh b/tests/simple_test.sh index 71cc610250100f0effd212ef080d4a64c0423159..b70b30ff7fb5cb44838e539182304e75b8cc7f49 100755 --- a/tests/simple_test.sh +++ b/tests/simple_test.sh @@ -33,9 +33,9 @@ cleanup() } trap cleanup SIGINT # Kill les process en arrière plan en cas de ^-C -# On démarre le transfert +# We start the transfer if ! valgrind --leak-check=full --log-file=./unwanted_logs/valgrind_${BSNM_PRE}_sender.log ./sender -f ${FILENAME} ::1 65197 2> ./unwanted_logs/${BSNM_PRE}_sender.log ; then - echo "Crash du sender!" + echo "The sender crashed!" cat ./unwanted_logs/${BSNM_PRE}_sender.log err=1 # On enregistre l'erreur fi @@ -46,21 +46,21 @@ if kill -0 $receiver_pid &> /dev/null ; then echo "Le receiver ne s'est pas arreté à la fin du transfert!" kill -9 $receiver_pid err=1 -else # On teste la valeur de retour du receiver +else # We check the return value of the receiver if ! wait $receiver_pid ; then - echo "Crash du receiver!" + echo "The receiver crashed!" cat ./unwanted_logs/${BSNM_PRE}_receiver.log err=1 fi fi -# On vérifie que le transfert s'est bien déroulé +# We check that the transfer ran through properly if [[ "$(md5sum ${FILENAME} | awk '{print $1}')" != "$(md5sum ./unwanted_logs/${BSNM_PRE}_received_file.${BSNM_EXT} | awk '{print $1}')" ]]; then - echo "Le transfert a corrompu le fichier!" + echo "The transfer has corrupted the file!" echo "Diff binaire des deux fichiers: (attendu vs produit)" diff -C 9 <(od -Ax -t x1z ${FILENAME}) <(od -Ax -t x1z ./unwanted_logs/${BSNM_PRE}_received_file.${BSNM_EXT}) exit 1 else - echo -e "${GREEN}Le transfert est réussi!${NC}" - exit ${err:-0} # En cas d'erreurs avant, on renvoie le code d'erreur + echo -e "${GREEN}The transfer has succeeded!${NC}" + exit ${err:-0} # In case of error, we send back the error code. fi \ No newline at end of file