Skip to content
Snippets Groups Projects
Commit 8077c000 authored by Clément's avatar Clément
Browse files

endpoint + client

parent 8956c1ca
Branches
No related merge requests found
Pipeline #107849 passed with stages
in 4 seconds
.DS_Store 0 → 100644
File added
......@@ -18,6 +18,28 @@ struct data {
uint16_t residue;
};
int send_data(uint16_t data, struct pollfd *fd_poll, int server)
{
uint16_t residue;
uint32_t ack;
residue = crc_crc((uint8_t *)&(data), 2, crc16_xmodem);
printf("data: %x, residue: %x\n", data, residue);
struct data data_to_send = { data, htons(residue) };
//on vérifie que le reste est nul
assert(crc_crc((uint8_t *)&(data_to_send), 4, crc16_xmodem) == 0);
do {
if (send(server, &data_to_send, sizeof(data_to_send), 0) <= 0)
return -1;
CHK(poll(fd_poll, 1, -1));
if (fd_poll[0].revents != 0) {
CHK(recv(server, &ack, sizeof(ack), 0));
}
} while (
popcount(ack) >
16); //il y a une chance infime que plus de 16 bits soient corrompus
return 1;
}
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
......@@ -47,30 +69,17 @@ int main(int argc, char *argv[])
struct stat statbuf;
CHK(fstat(fd, &statbuf));
uint16_t data;
uint16_t residue;
uint32_t ack;
struct pollfd fd_poll[1] = { { server, POLLIN, 0 } };
CHK(send_data((uint16_t)statbuf.st_size % 2, fd_poll, server));
printf("size: %d\n", (uint16_t)statbuf.st_size);
int size = (int)(statbuf.st_size / sizeof(data) +
statbuf.st_size % sizeof(data));
for (int i = 0; i < (int)(statbuf.st_size / sizeof(data) +
statbuf.st_size % sizeof(data));
i++) {
for (int i = 0; i < size; i++) {
CHK(read(fd, &data, sizeof(data)));
residue = crc_crc((uint8_t *)&(data), 2, crc16_xmodem);
struct data data_to_send = { data, htons(residue) };
//on vérifie que le reste est nul
assert(crc_crc((uint8_t *)&(data_to_send), 4, crc16_xmodem));
do {
if (send(server, &data_to_send, sizeof(data_to_send),
0) <= 0)
break;
CHK(poll(fd_poll, 1, -1));
if (fd_poll[0].revents != 0) {
CHK(recv(server, &ack, sizeof(ack), 0));
}
} while (
popcount(ack) >
16); //il y a une chance infime que plus de 16 bits soient corrompus
CHK(send_data(data, fd_poll, server));
}
CHK(close(fd));
CHK(close(server));
return EXIT_SUCCESS;
}
......@@ -9,57 +9,93 @@
#include <unistd.h>
#include "utils/crc.h"
#define LEN_DATA uint16_t
//le server récupère des données de taille uint16_t et un crc de taille 16 bits
struct data {
LEN_DATA data;
LEN_DATA residue;
uint16_t data;
uint16_t residue;
};
enum { recv_incorect = 0, recv_correct = 1, recv_end = -1 };
int process_data(int *correction_table, struct data *data)
{
uint16_t residue;
int correct = 1;
(void)correct;
if ((residue = crc_crc((uint8_t *)(data), 4, crc16_xmodem)) != 0) {
if (correction_table[residue] != 0) {
data->data ^= correction_table[residue]; //pas bon
data->data ^= 1 << correction_table[residue];
correct = 1;
printf("corrected %x\n", data->data);
} else {
correct = 0;
}
}
printf("data: %x, residue: %x, correct: %d\n", data->data, residue,
correct);
return correct;
}
void server_thread(int proxy, void *arg)
int receive_data(struct pollfd *fd_poll, int proxy, struct data *buff,
int *correction_table)
{
int *correction_table = arg;
uint32_t ack = 0;
uint32_t nack = 0xffffffff;
CHK(poll(fd_poll, 1, -1));
if ((recv(proxy, buff, sizeof(*buff), 0)) <= 0)
return recv_end;
if (process_data(correction_table, buff)) {
if (send(proxy, &ack, sizeof(ack), 0) <= 0)
return recv_end;
fd_poll[0].revents = 0;
printf("correct\n");
return 1;
} else {
if (send(proxy, &nack, sizeof(nack), 0) <= 0)
return recv_end;
fd_poll[0].revents = 0;
printf("incorrect\n");
return recv_incorect;
}
}
void server_thread(int proxy, void *arg)
{
int *correction_table = arg;
int fd = open("output", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
PERROR("open");
return;
}
struct pollfd fd_poll[1] = { { proxy, POLLIN, 0 } };
struct data buff;
int odd = 0;
int received = 0;
while (received != recv_correct) {
received =
receive_data(fd_poll, proxy, &buff, correction_table);
if (received == recv_end) {
close(fd);
return;
}
}
odd = buff.data;
int n = 0;
while (1) {
CHK(poll(fd_poll, 1, -1));
struct data buff;
ssize_t len;
if (fd_poll[0].revents != 0) {
if ((len = recv(proxy, &buff, sizeof(buff), 0)) <= 0)
break;
if (process_data(correction_table, &buff)) {
if (send(proxy, &ack, sizeof(ack), 0) <= 0)
break;
if (write(fd, &buff.data, sizeof(buff.data)) <=
0)
break;
} else {
if (send(proxy, &nack, sizeof(nack), 0) <= 0)
break;
}
fd_poll[0].revents = 0;
if (receive_data(fd_poll, proxy, &buff, correction_table) ==
recv_end)
break;
if ((n = write(fd, &buff.data, sizeof(buff.data))) <= 0)
break;
if (n == -1) {
PERROR("write");
break;
}
}
if (odd)
ftruncate(fd, lseek(fd, 0, SEEK_CUR) - 1);
close(fd);
}
int main(int argc, char *argv[])
......
......@@ -49,6 +49,9 @@ void proxy_thread(int client, void *arg)
if (fds[1].revents != 0) {
if ((len = recv(server, &buff, sizeof(buff), 0)) <= 0)
break;
*buff = generate_error((uint32_t)*buff,
args->error_rate,
args->max_error);
if (send(client, &buff, len, 0) <= 0)
break;
......
......@@ -86,7 +86,7 @@ int *crc_sympt_map(int code_word_size, struct crc_model model)
for (int i = 0; i < code_word_size; i++) {
for (int j = 0; j < 8; j++) {
buff[i] = 1 << j;
uint32_t crc = crc_crc(buff, i, model);
uint32_t crc = crc_crc(buff, code_word_size, model);
buff[i] = 0;
if (map[crc] != 0)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment