Navigation Menu

Skip to content

Commit

Permalink
Fixed up a bit of bang-com, added some documentation changed layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Samith Bysani committed Jan 20, 2009
1 parent 05edd53 commit 177f3c8
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 88 deletions.
181 changes: 101 additions & 80 deletions src/base/bang-com.c
Expand Up @@ -11,8 +11,7 @@
#include"bang-utils.h"
#include"bang-types.h"
#include<poll.h>
#include<pthread.h>
#include<semaphore.h>
#include<pthread.h> #include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Expand Down Expand Up @@ -85,6 +84,19 @@ typedef struct {
struct pollfd pfd;
} peer;

/**
* \brief Allocates and returns a new request node.
*/
static request_node* new_request_node();

/**
* \param head The head of the list to be freed.
*
* \brief Frees resources used by a request list started at
* head.
*/
static void free_request_list(request_node *head);

/**
* \return The head of the request node list starting at head.
*
Expand All @@ -101,54 +113,71 @@ static request_node* pop_request(request_node **head);
static void append_request(request_node **head, request_node *node);

/**
* \param head The head of the list to be freed.
* \param self The peer to be requested.
* \param request Request to be given to the peer.
*
* \brief Frees resources used by a request list started at
* head.
* \brief Adds a request to a peer structure.
*/
static void free_request_list(request_node *head);
static void request_peer(peer *self, BANG_request request);

/**
* \brief Allocates and returns a new request node.
* \brief Allocates and returns a new peer.
*/
static request_node* new_request_node();
static peer* new_peer();

/**
* \param self The peer to be requested.
* \param request Request to be given to the peer.
* \brief Removes and frees a peer and its resources.
*/
static void free_peer(peer *p);

/**
* \param new_peer A fully allocated and assemebled peer.
*
* \brief Adds a request to a peer structure.
* \brief Adds new_peer to the peers array.
*/
static void request_peer(peer *self, BANG_request request);
static void add_peer_to_peers(peer *new_peer);

/**
* \brief Removes and frees a peer and its resources.
* \param peer_id The id of a peer.
*
* \brief Gets the location of the peer in the in
* the peers array.
*/
static void free_peer(peer *p);
static int get_key_with_peer_id(int peer_id);

/**
* \brief Allocates and returns a new peer.
* \param self The peer to be closed.
*
* \brief A peer thread asks to close itself.
*/
static peer* new_peer();
static void read_peer_thread_self_close(peer *self);

/*
* \param requests The requests to be freed.
/**
* \param self The peer wanting to extract a message from the socket.
* \param length The length of the message to extract.
*
* \brief Frees a BANGRequests struct.
* \brief Extracts a message of length length.
*/
static void free_BANG_requests(BANG_requests *requests);
static void* extract_message(peer *self, unsigned int length);

/**
* \param self The peer responding to BANG_HELLO.
*
* \brief Acts on an incoming BANG_HELLO.
*/
static char peer_respond_hello(peer *self);

/*
* \return Returns an initialized BANGRequest pointer.
*/
static BANG_requests* new_BANG_requests();

/*
* \param self The peer to be closed.
* \param requests The requests to be freed.
*
* \brief A peer thread asks to close itself.
* \brief Frees a BANGRequests struct.
*/
static void read_peer_thread_self_close(peer *self);
static void free_BANG_requests(BANG_requests *requests);

/**
* A lock on peers structure and affiliate things. It can have multiple readers,
Expand Down Expand Up @@ -177,30 +206,17 @@ static peer **peers = NULL;
*/
static int *keys = NULL;

static void free_peer(peer *p) {
#ifdef BDEBUG_1
fprintf(stderr,"Freeing a peer with peer_id %d.\n",p->peer_id);
#endif
pthread_cancel(p->receive_thread);

/* We need to close the receive thread in a more roundabout way, since it may be waiting
* on a semaphore in which case it will never close */
BANG_request request;

request.type = BANG_CLOSE_REQUEST;
request.length = 0;
request.request = NULL;

request_peer(p,request);

pthread_join(p->receive_thread,NULL);
pthread_join(p->send_thread,NULL);

free_BANG_requests(p->requests);

close(p->socket);
static request_node* new_request_node() {
request_node *new_node = (request_node*) calloc(1,sizeof(request_node));
new_node->next = NULL;
return new_node;
}

free(p);
static void free_request_list(request_node *head) {
if (head == NULL) return;
if (head->next != NULL)
free_request_list(head->next);
free(head);
}

static request_node* pop_request(request_node **head) {
Expand All @@ -226,31 +242,30 @@ static void append_request(request_node **head, request_node *node) {
}
}

static void free_request_list(request_node *head) {
if (head == NULL) return;
if (head->next != NULL)
free_request_list(head->next);
free(head);
}
static void free_peer(peer *p) {
#ifdef BDEBUG_1
fprintf(stderr,"Freeing a peer with peer_id %d.\n",p->peer_id);
#endif
pthread_cancel(p->receive_thread);

static request_node* new_request_node() {
request_node *new_node = (request_node*) calloc(1,sizeof(request_node));
new_node->next = NULL;
return new_node;
}
/* We need to close the receive thread in a more roundabout way, since it may be waiting
* on a semaphore in which case it will never close */
BANG_request request;

static BANG_requests* new_BANG_requests() {
BANG_requests *requests = (BANG_requests*) calloc(1,sizeof(BANG_requests));
requests->head = NULL;
pthread_mutex_init(&(requests->lock),NULL);
sem_init(&(requests->num_requests),0,0);
return requests;
}
request.type = BANG_CLOSE_REQUEST;
request.length = 0;
request.request = NULL;

static void free_BANG_requests(BANG_requests *requests) {
pthread_mutex_destroy(&(requests->lock));
sem_destroy(&(requests->num_requests));
free_request_list(requests->head);
request_peer(p,request);

pthread_join(p->receive_thread,NULL);
pthread_join(p->send_thread,NULL);

free_BANG_requests(p->requests);

close(p->socket);

free(p);
}

static void read_peer_thread_self_close(peer *self) {
Expand All @@ -267,13 +282,6 @@ static void read_peer_thread_self_close(peer *self) {
pthread_exit(NULL);
}

/**
*
* \param self The peer wanting to extract a message from the socket.
* \param length The length of the message to extract.
*
* \brief Extracts a message of length length.
*/
static void* extract_message(peer *self, unsigned int length) {
void *message = (char*) calloc(length,1);
int check_read;
Expand Down Expand Up @@ -315,6 +323,21 @@ static void* extract_message(peer *self, unsigned int length) {
return message;
}

static BANG_requests* new_BANG_requests() {
BANG_requests *requests = (BANG_requests*) calloc(1,sizeof(BANG_requests));
requests->head = NULL;
pthread_mutex_init(&(requests->lock),NULL);
sem_init(&(requests->num_requests),0,0);
return requests;
}

static void free_BANG_requests(BANG_requests *requests) {
pthread_mutex_destroy(&(requests->lock));
sem_destroy(&(requests->num_requests));
free_request_list(requests->head);
}


static char peer_respond_hello(peer *self) {
unsigned char *version = (unsigned char*) extract_message(self,LENGTH_OF_VERSION);
/* TODO: Make this more elegant =P */
Expand Down Expand Up @@ -608,7 +631,7 @@ void BANG_request_peer_id(int peer_id, BANG_request request) {

BANG_read_lock(peers_lock);

id = BANG_get_key_with_peer_id(peer_id);
id = get_key_with_peer_id(peer_id);
request_peer(peers[id],request);

BANG_read_unlock(peers_lock);
Expand All @@ -618,15 +641,13 @@ static int intcmp(const void *i1, const void *i2) {
return *((int*) i1) - *((int*) i2);
}

int BANG_get_key_with_peer_id(int peer_id) {
static int get_key_with_peer_id(int peer_id) {
int pos = -1;
BANG_read_lock(peers_lock);

int *ptr = bsearch(&peer_id,keys,current_peers,sizeof(int),&intcmp);
if (ptr != NULL)
pos = keys - ptr;

BANG_read_unlock(peers_lock);
return pos;
}

Expand Down Expand Up @@ -696,7 +717,7 @@ void BANG_remove_peer(int peer_id) {

BANG_write_lock(peers_lock);

int pos = BANG_get_key_with_peer_id(peer_id);
int pos = get_key_with_peer_id(peer_id);
if (pos == -1) return;

free_peer(peers[pos]);
Expand Down
8 changes: 0 additions & 8 deletions src/base/bang-com.h
Expand Up @@ -79,12 +79,4 @@ void BANG_remove_peer(int peer_id);
* be able to catch this, remember to use a signal.
*/
void BANG_add_peer(int socket);

/**
* \param peer_id The id of the peer thread.
*
* \brief Gets the current key of the peer id (the place it is located in the keys array).
* Note: This is should be an internal function.
*/
int BANG_get_key_with_peer_id(int peer_id);
#endif

0 comments on commit 177f3c8

Please sign in to comment.