Skip to content
Extraits de code Groupes Projets
Valider 5e56c37f rédigé par Vany Ingenzi's avatar Vany Ingenzi
Parcourir les fichiers

Advanced on the receiver

parent 739663ac
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -7,4 +7,20 @@ ...@@ -7,4 +7,20 @@
#ifndef __RECEIVER_UTILS_ #ifndef __RECEIVER_UTILS_
#define __RECEIVER_UTILS_ #define __RECEIVER_UTILS_
/* We are using 8 bits to encode the seqnum therefore to fight redondance our window is of 2**(8)/2 */
#define MAX_SELECTIVE_REPEAT_WINDOW 8
/* Represent the receiver's connection state */
typedef struct __attribute__((__packed__))
{
} con_state_t;
/**
* Loop reading on socket and printing to the stdout
* @sfd : The socket file descriptor. It is both bound and connected.
* @return: as soon as the whole transfer is done.
*/
void receiver_read_write_loop(int sfd);
#endif #endif
\ No newline at end of file
...@@ -78,7 +78,7 @@ int create_socket_connect(struct sockaddr_in6 * dest_addr) ...@@ -78,7 +78,7 @@ int create_socket_connect(struct sockaddr_in6 * dest_addr)
int create_socket(struct sockaddr_in6 *source_addr, int src_port, struct sockaddr_in6 *dest_addr, int dst_port) int create_socket(struct sockaddr_in6 *source_addr, int src_port, struct sockaddr_in6 *dest_addr, int dst_port)
{ {
if ( source_addr != NULL && dest_addr == NULL ) if ( source_addr != NULL && dest_addr == NULL )
{ {
source_addr->sin6_port = htons(src_port); source_addr->sin6_port = htons(src_port);
return create_socket_bind(source_addr); return create_socket_bind(source_addr);
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
#include "log.h" #include "log.h"
#include "packet_interface.h" #include "packet_interface.h"
#include "our_utils.h" #include "our_utils.h"
#include "receiver_utils.h"
int print_usage(char *prog_name) { int print_usage(char *prog_name) {
ERROR("Usage:\n\t%s [-s stats_filename] listen_ip listen_port", prog_name); ERROR("Usage:\n\t%s [-s stats_filename] listen_ip listen_port", prog_name);
...@@ -73,20 +73,11 @@ int main(int argc, char **argv) { ...@@ -73,20 +73,11 @@ int main(int argc, char **argv) {
ERROR("An error occured when creating the socket\n"); ERROR("An error occured when creating the socket\n");
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
// 2. Wait for a client
char * buff[PKT_MAX_LEN];
ssize_t len;
int err = wait_for_client(socket, PKT_MAX_LEN,(char *)buff, &len);
if (err == 0)
{
close(socket);
return (EXIT_FAILURE);
}
DEBUG_DUMP(buff, len);
// 3. Start listening till end ASSERT(stats_filename == NULL);
receiver_read_write_loop(socket);
close(socket); close(socket);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
#include "log.h"
#include "packet_interface.h"
#include <poll.h>
#include "our_utils.h"
#include "receiver_utils.h"
int send_data_if_inneed()
{
return 1;
}
int handle_incoming_data()
{
/*
char * buff;
ssize_t read_bytes = read(pfd->fd, (void *) buff, MAX_SIZE);
if (read_bytes == -1)
return 0;
err = fwrite((void *) buff, sizeof(char), read_bytes, stdout);
if (err == 0)
return 0;
err = fflush(stdout);
*/
return 1;
}
int handle_revents(struct pollfd * pfd)
{
DEBUG(" events: %s%s%s%s",
(pfd->revents & POLLIN) ? "POLLIN " : "",
(pfd->revents & POLLOUT) ? "POLLOUT " : "",
(pfd->revents & POLLHUP) ? "POLLHUP " : "",
(pfd->revents & POLLERR) ? "POLLERR " : "");
if (pfd->revents & POLLIN) /* POLLIN : There's data to read */
{
return handle_incoming_data();
} else if (pfd->revents & POLLHUP) /* POLLHUP : The other user disconnected from their side */
{
DEBUG("The sender disconnected.");
return 0;
} else if (pfd->revents & POLLOUT) /* POLLOUT : Possible to send that */
{
return send_data_if_inneed();
} else { /* POLLERR : An error occurred */
ERROR("Poll revents marks error.");
return 0;
}
return 1;
}
void reception_loop(struct pollfd * pfd, con_state_t * state, pkt_t * pkt)
{
ASSERT(state != NULL || pkt != NULL);
int not_eof = 1;
while (not_eof)
{
int ready = poll(pfd, 1, -1);
if (ready == -1)
return;
if (pfd->revents != 0)
not_eof = handle_revents(pfd);
}
}
/**
* Waits for a client to send data to the socket and then connectes to the client
* @return: 0 upon success, -1 in case of an error. a message will be displayed to the stderr
*/
int connect_to_a_client(int sfd, pkt_t * init_pkt)
{
char * buffer[PKT_MAX_LEN];
ssize_t written_in_buffer;
// We suppose that the first contact is going to be our client no need to check for more
int err = wait_for_client(sfd, PKT_MAX_LEN, (char *)buffer, &written_in_buffer);
if (err != 0) return -1;
DEBUG_DUMP(buffer, written_in_buffer);
pkt_status_code status = pkt_decode((char *) buffer, (size_t) written_in_buffer, init_pkt);
if (status != PKT_OK)
{
ERROR("When encoding the init_packet got pkt_status = %d", status);
return -1;
}
return 0;
}
/**
* This main loop for the receiver.
* @sfd: A valid socket.
* @return: As soon as an error occurs or, a the total transfer came through.
*/
void receiver_read_write_loop(int sfd)
{
pkt_t * pkt = pkt_new();
con_state_t * state = malloc(sizeof(con_state_t));
struct pollfd * pfd = (struct pollfd *) calloc(1, sizeof(struct pollfd));
if (pkt == NULL || state == NULL || pfd == NULL) return;
pfd->fd = sfd;
pfd->events = POLLIN | POLLOUT;
int err = connect_to_a_client(sfd, pkt);
if (err == 0)
return;
// Handle initial packet.
err = fwrite((void *) pkt_get_payload(pkt), sizeof(char), pkt_get_length(pkt), stdout);
err = fflush(stdout);
/*
if (err == 0)
reception_loop(pfd, state, pkt);
*/
char * buffer[PKT_MAX_LEN];
size_t len = PKT_MAX_LEN;
err = pkt_encode(pkt, (char *) buffer, &len);
if (err == 0)
err = write(pfd->fd, (void *) buffer, len);
pkt_del(pkt);
free(state);
free(pfd);
}
\ No newline at end of file
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter