Skip to content

Commit

Permalink
Temporary work, trying to get things sorted out.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Samith Bysani committed Dec 26, 2008
1 parent 5ef57d6 commit 55abb02
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
36 changes: 33 additions & 3 deletions src/base/bang-com.c
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -196,6 +224,7 @@ void peer_respond_hello(peer *self) {
} else {
/**
* TODO: File a mismatch version request.
* or do something.
*/
responding = 0;
}
Expand All @@ -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.
Expand Down Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/base/bang-com.h
Expand Up @@ -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();

Expand Down
18 changes: 11 additions & 7 deletions src/base/bang-types.h
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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,
/**
Expand Down

0 comments on commit 55abb02

Please sign in to comment.