From 55abb02f8aca558649f4ce6715afbbaeff2564c9 Mon Sep 17 00:00:00 2001 From: Nikhil Samith Bysani Date: Fri, 26 Dec 2008 02:01:42 -0500 Subject: [PATCH] Temporary work, trying to get things sorted out. --- src/base/bang-com.c | 36 +++++++++++++++++++++++++++++++++--- src/base/bang-com.h | 2 +- src/base/bang-types.h | 18 +++++++++++------- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/base/bang-com.c b/src/base/bang-com.c index 8b5a21b..b910199 100644 --- a/src/base/bang-com.c +++ b/src/base/bang-com.c @@ -160,7 +160,7 @@ void free_BANGRequests(BANG_requests *requests) { /** * \param self The current peer. * - * Closes one of the two peer threads, after the connection has formally stopped + * \brief Closes one of the two peer threads, after the connection has formally stopped * and the mess has to been cleaned up.. */ void peer_self_close(peer *self) { @@ -177,6 +177,34 @@ void peer_self_close(peer *self) { pthread_exit(NULL); } +/** + * \param sock An open socket. + * \brief Extracts a char* message from the socket per the specifications of all messages. + * (unsigned long length)(message of that length) + */ +char* extract_message(peer *self) { + char *message = NULL; + unsigned int length; + int check_read, read = 0; + char extracting = 1; + + while (extracting) { + check_read = read(self->socket,&length,LENGTH_OF_LENGTH); + + if (poll(&(self->pfd),1,-1) != -1 && self->pfd.revents & POLLIN) { + if (check_read <= 0) { + message = NULL; + extracting = 0; + } else { + extracting = 0; + } + } else { + extracting = 0; + } + } + + return message; +} void peer_respond_hello(peer *self) { char responding = 1; @@ -185,7 +213,7 @@ void peer_respond_hello(peer *self) { while (responding) { if (poll(&(self->pfd),1,-1) != -1 && self->pfd.revents & POLLIN) { - check_read = read(self->socket,&version,sizeof(double)); + check_read = read(self->socket,&version,LENGTH_OF_VERSION); if (check_read <= 0) { responding = 0; } else { @@ -196,6 +224,7 @@ void peer_respond_hello(peer *self) { } else { /** * TODO: File a mismatch version request. + * or do something. */ responding = 0; } @@ -219,7 +248,7 @@ void* BANG_read_peer_thread(void *self_info) { while (reading) { if (poll(&(self->pfd),1,-1) != -1 && self->pfd.revents & POLLIN) { - check_read = read(self->socket,&header,sizeof(unsigned int)); + check_read = read(self->socket,&header,LENGTH_OF_HEADERS); /** * Lookup header message and act accordingly. @@ -354,6 +383,7 @@ void BANG_remove_peer(int peer_id) { --current_peers; + peers = (peer**) realloc(peers,current_peers * sizeof(peer*)); keys = (int*) realloc(keys,current_peers * sizeof(int)); diff --git a/src/base/bang-com.h b/src/base/bang-com.h index ec8983d..f36ae79 100644 --- a/src/base/bang-com.h +++ b/src/base/bang-com.h @@ -66,7 +66,7 @@ typedef struct { } peer; /** - * \brief Intializes the bang-com part of BANG. + * \brief Initializes the bang-com part of BANG. */ void BANG_com_init(); diff --git a/src/base/bang-types.h b/src/base/bang-types.h index 034ac50..ae1241c 100644 --- a/src/base/bang-types.h +++ b/src/base/bang-types.h @@ -74,10 +74,10 @@ enum BANG_signals { * \page The Protocol * * \brief The protocol is specified, like this: - * - First, an unsigned int will be sent specifying what kind of communication it will be + * - First, an unsigned 4 bytes will be sent specifying what kind of communication it will be * - Then, everything else will follow * - * Following are possible unsigned ints to be sent. + * Following are possible unsigned to be sent. * * Communications currently can be done by this: * assuming peer1, peer2 @@ -91,6 +91,10 @@ enum BANG_signals { * */ +#define LENGTH_OF_HEADER 4 +#define LENGTH_OF_LENGTHS 4 +#define LENGTH_OF_VERSION 8 + enum BANG_headers { /** * message: @@ -102,20 +106,20 @@ enum BANG_headers { BANG_HELLO, /** * message: - * -BANG_DEBUG_MESSAGE (unsigned int) - * -length of message (unsigned int) + * -BANG_DEBUG_MESSAGE (unsigned 4 bytes) + * -length of message (unsigned 4 bytes) * -message (char*) */ BANG_DEBUG_MESSAGE, /** * message: - * -BANG_MISMATCH_VERSION (unsigned int) - * -our version (double) + * -BANG_MISMATCH_VERSION (unsigned 4 bytes) + * -our version (8 bytes) */ BANG_MISMATCH_VERSION, /** * message: - * -BANG_BYE (unsigned int) + * -BANG_BYE (unsigned 4 bytes) */ BANG_BYE, /**