Implemented basic stop and wait

This commit is contained in:
keplyx 2020-05-07 08:28:32 +02:00
parent 2afe7b5bf8
commit bad1e6c08c
2 changed files with 28 additions and 9 deletions

View file

@ -27,6 +27,11 @@ struct app_buffer_entry {
/* Condition variable used for passive wait when buffer is empty */
pthread_cond_t buffer_empty_cond;
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_BOLD "\x1b[1m"
#define COLOR_RESET "\x1b[0m"
/* ************************
* Fonctions Utilitaires *
* ************************/
@ -213,7 +218,7 @@ int mic_tcp_core_send(mic_tcp_payload buff)
if(random > lr_tresh) {
result = sendto(sys_socket, buff.data, buff.size, 0, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr));
} else {
printf("[MICTCP-CORE] Perte du paquet\n");
printf("[MICTCP-CORE] "COLOR_RED"Perte du paquet"COLOR_RESET"\n");
}
return result;

View file

@ -1,6 +1,11 @@
#include <mictcp.h>
#include <api/mictcp_core.h>
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_BOLD "\x1b[1m"
#define COLOR_RESET "\x1b[0m"
struct mic_tcp_sock g_sock;
int next_msg_num;
short timeout_msec = 5000;
@ -96,17 +101,23 @@ int mic_tcp_send(int socket, char *mesg, int mesg_size) {
pdu.header.ack = 0;
pdu.header.fin = 0;
printf("\n[MIC-TCP] Sending PDU\n");
if ((result = IP_send(pdu, g_sock.addr)) != -1) {
struct mic_tcp_pdu rcv_pdu;
struct mic_tcp_sock_addr rcv_sock_addr;
printf("[MIC-TCP] Waiting for ACK\n");
result = IP_recv(&rcv_pdu, &rcv_sock_addr, timeout_msec);
if (result == -1)
printf("[LOG] Timeout\n");
if (result != -1 && rcv_pdu.header.ack_num != next_msg_num + 1) {
printf("[LOG] ack_num: %d\n", rcv_pdu.header.ack_num);
printf("[MIC-TCP] "COLOR_BOLD"Timeout"COLOR_RESET"\n");
else if (rcv_pdu.header.ack_num != next_msg_num + 1) {
printf("[MIC-TCP] Received ACK: %d | "COLOR_RED"INVALID"COLOR_RESET"\n", rcv_pdu.header.ack_num);
printf("[MIC-TCP] | Expected: %d\n", next_msg_num + 1);
printf("[MIC-TCP] | Received: %d\n", rcv_pdu.header.ack_num);
result = -1;
}
}
} else
printf("[MIC-TCP] Received ACK: "COLOR_GREEN"VALID"COLOR_RESET"\n");
} else
printf("[MIC-TCP] Ip_send "COLOR_RED"ERROR"COLOR_RESET"\n");
}
next_msg_num = next_msg_num + 1;
@ -172,14 +183,17 @@ void process_received_PDU(mic_tcp_pdu pdu, mic_tcp_sock_addr addr) {
ack_pdu.header.ack = 0;
ack_pdu.header.fin = 0;
printf("[LOG] next_msg_num: %d\n", next_msg_num);
printf("[LOG] seq_num: %d\n", pdu.header.seq_num);
if (pdu.header.seq_num == next_msg_num) {
next_msg_num = next_msg_num + 1;
app_buffer_put(pdu.payload);
printf("[MIC-TCP] Received PDU: "COLOR_GREEN"VALID"COLOR_RESET"\n");
} else {
printf("[MIC-TCP] Received PDU: "COLOR_RED"INVALID"COLOR_RESET"\n");
printf("[MIC-TCP] | Expected: %d\n", next_msg_num);
printf("[MIC-TCP] | Received: %d\n", pdu.header.seq_num);
}
ack_pdu.header.ack_num = next_msg_num;
printf("[LOG] Sending ack: %d\n", next_msg_num);
printf("[MIC-TCP] Sending ack: %d\n", next_msg_num);
IP_send(ack_pdu, addr);
}