Skip to content

Commit

Permalink
Created an extract function, added some things to utils.
Browse files Browse the repository at this point in the history
git-svn-id: https://subversion.cs.uiuc.edu/svn/bang/eoh2009@24 69d76c3e-0761-0410-948c-9895a8bb34fc
  • Loading branch information
nbysani2 committed Jan 24, 2009
1 parent dc434af commit 2292591
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
74 changes: 69 additions & 5 deletions src/base/bang-com.c
Expand Up @@ -6,10 +6,12 @@
* \brief Implements "the master of slave peer threads" model.
*/
#include"bang-com.h"
#include"bang-module-api.h"
#include"bang-net.h"
#include"bang-routing.h"
#include"bang-signals.h"
#include"bang-utils.h"
#include"bang-types.h"
#include"bang-utils.h"
#include<poll.h>
#include<pthread.h>
#include<semaphore.h>
Expand Down Expand Up @@ -623,7 +625,7 @@ void BANG_com_close() {
static char peer_respond_hello(peer *self) {
unsigned char *version = (unsigned char*) extract_message(self,LENGTH_OF_VERSION);
/* TODO: Make this more elegant =P */
if (version == NULL || version[0] != BANG_MAJOR_VERSION || version[1] != BANG_MIDDLE_VERSION || version[2] != BANG_MINOR_VERSION) {
if (BANG_version_cmp(version,BANG_LIBRARY_VERSION) == 0) {
free(version);
return 0;
}
Expand Down Expand Up @@ -675,6 +677,58 @@ static char read_module_message(peer *self) {
return 1;
}

static void extract_uuid(peer *self,uuid_t uuid) {
uuid_t *uuid_ptr = (uuid_t*) extract_message(self,sizeof(uuid_t));
if (uuid_ptr) {
uuid_copy(uuid,*uuid_ptr);
free(uuid_ptr);
} else {
uuid_clear(uuid);
}
}

static char read_job_message(peer *self) {
uuid_t auth, peer;

extract_uuid(self,auth);
if (uuid_is_null(auth)) {
return 0;
}

extract_uuid(self,peer);
if (uuid_is_null(peer)) {
return 0;
}

BANG_job job;

/* A MAGIC NUMBER */
int *job_number = (int*) extract_message(self,4);
if (job_number == NULL) {
return 0;
}

job.job_number = *job_number;
free(job_number);

unsigned int *job_length = (unsigned int*) extract_message(self,LENGTH_OF_LENGTHS);
if (job_length == NULL) {
return 0;
}

job.length = *job_length;
free(job_length);

job.data = extract_message(self,job.length);
if (job.data == NULL) {
return 0;
}

BANG_route_job(auth,peer,&job);

return 1;
}

/**
* \param self The thread sending the module.
* \param request The module path to send.
Expand Down Expand Up @@ -798,29 +852,37 @@ static void* extract_message(peer *self, unsigned int length) {
void* BANG_read_peer_thread(void *self_info) {
peer *self = (peer*)self_info;

unsigned int *header;
BANG_header *header;

char reading = 1;

while (reading) {
if ((header = (unsigned int*) extract_message(self,LENGTH_OF_HEADER)) != NULL) {
if ((header = (BANG_header*) extract_message(self,LENGTH_OF_HEADER)) != NULL) {
switch (*header) {
case BANG_HELLO:
reading = peer_respond_hello(self);
break;

case BANG_DEBUG_MESSAGE:
reading = read_debug_message(self);
break;

case BANG_SEND_MODULE:
/* I guess we'll take it... */
reading = read_module_message(self);
break;

case BANG_WANT_MODULE:
/* TODO: Someone is asking us if we want a module... send out a signal! */
break;

case BANG_REQUEST_MODULE:
/* TODO: This may be pretty hard to do. */
break;

case BANG_SEND_JOB:
reading = read_job_message(self);

case BANG_MISMATCH_VERSION:
case BANG_BYE:
reading = 0;
Expand All @@ -835,6 +897,7 @@ void* BANG_read_peer_thread(void *self_info) {
reading = 0;
break;
}

free(header);
} else {
reading = 0;
Expand All @@ -853,7 +916,8 @@ void* BANG_write_peer_thread(void *self_info) {
peer *self = (peer*)self_info;
request_node *current;
char sending = 1;
unsigned int header;
BANG_header header;

while (sending) {
sem_wait(&(self->requests->num_requests));
pthread_mutex_lock(&(self->requests->lock));
Expand Down
6 changes: 4 additions & 2 deletions src/base/bang-routing.c
Expand Up @@ -57,15 +57,17 @@ void BANG_route_job(uuid_t authority, uuid_t peer, BANG_job *job) {
* Probably should move this to bang-com.c
*/
request.length = LENGTH_OF_HEADER +sizeof(uuid_t) * 2 +
4 /* A MAGIC NUMBER! */ +
LENGTH_OF_LENGTHS +
4 /* A MAGIC NUMBER! */ +
job->length;
request.request = malloc(request.length);
unsigned int header = BANG_SEND_JOB;
BANG_header header = BANG_SEND_JOB;

memcpy(request.request,&header,LENGTH_OF_HEADER);
memcpy(request.request,authority,sizeof(uuid_t));
memcpy(request.request,peer,sizeof(uuid_t));
memcpy(request.request,&(job->job_number),4);
memcpy(request.request,&(job->length),LENGTH_OF_LENGTHS);
memcpy(request.request,job->data,job->length);

BANG_request_peer_id(sqlite3_column_int(get_peer_route,3),request);
Expand Down
12 changes: 10 additions & 2 deletions src/base/bang-types.h
Expand Up @@ -8,6 +8,7 @@
#ifndef __BANG_TYPES_H
#define __BANG_TYPES_H
#include"bang-module-api.h"
#include<stdint.h>

/**
* The major version of the library.
Expand All @@ -25,6 +26,11 @@
*/
#define BANG_MINOR_VERSION 1

static const unsigned char BANG_LIBRARY_VERSION[3] = {
BANG_MAJOR_VERSION,
BANG_MIDDLE_VERSION,
BANG_MINOR_VERSION };

/**
* The structure that represents a module for the program.
*/
Expand Down Expand Up @@ -193,7 +199,9 @@ enum BANG_signals {
/**
* Length of the headers.
*/
#define LENGTH_OF_HEADER 4
typedef uint32_t BANG_header;
#define LENGTH_OF_HEADER sizeof(BANG_header)

/**
* Length of any lengths sent.
*/
Expand Down Expand Up @@ -233,6 +241,7 @@ enum BANG_headers {
*/
BANG_SEND_MODULE,
/**
* Requests a module from the remote end
* message:
* -BANG_REQUEST_MODULE (unsigned 4 bytes)
* -length
Expand All @@ -257,7 +266,6 @@ enum BANG_headers {
* message:
* -BANG_SEND_JOB
* -uuid (16 bytes)
* -job stuff
*/
BANG_SEND_JOB,
/**
Expand Down
18 changes: 13 additions & 5 deletions src/base/bang-utils.c
Expand Up @@ -38,13 +38,21 @@ void free_BANG_rw_syncro(BANG_rw_syncro *lck) {
free(lck);
}

int BANG_version_cmp(const unsigned char *v1, const unsigned char *v2) {
int cmp = 0;

cmp += v1[0] - v2[0];
cmp += v1[1] - v2[1];
cmp += v1[2] - v2[2];

return cmp;
}

int BANG_module_name_cmp(const char *m1, const char *m2) {
int cmp = strcmp(m1,m2);
int length = strlen(m1),
length2 = strlen(m2);
cmp += m1[length + 1] - m2[length2 + 1];
cmp += m1[length + 2] - m2[length2 + 2];
cmp += m1[length + 3] - m2[length2 + 3];
int length = strlen(m1) + 1,
length2 = strlen(m2) + 1;
cmp += BANG_version_cmp(((unsigned char*)m1) + length, ((unsigned char*)m2) + length2);
return cmp;
}

Expand Down
10 changes: 10 additions & 0 deletions src/base/bang-utils.h
Expand Up @@ -17,6 +17,16 @@ typedef struct {
int readers;
} BANG_rw_syncro;

/**
* \param v1 A bang version.
* \param v2 Another bang version.
*
* \return A comparsion of the two versions.
*
* \brief Compares the two versions, 0 if equal, - if less than, + if greater than.
*/
int BANG_version_cmp(const unsigned char *v1, const unsigned char *v2);

/**
* \param m1 The first module name to compare.
* \param m2 The second module name to compare.
Expand Down

0 comments on commit 2292591

Please sign in to comment.