Skip to content

Commit

Permalink
Added some of the routing functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Samith Bysani committed Jan 19, 2009
1 parent 0c2f688 commit d7619ee
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/base/bang-module-api.c
Expand Up @@ -69,8 +69,12 @@ void BANG_debug_on_all_peers(BANG_module_info *info, char *message) {
void BANG_get_me_peers(BANG_module_info *info) {
/* This does not use routing...! */
uuid_t *valid_routes = get_valid_routes(info);
/* TODO: THIS! */
/*int **peers_to_bug =*/ BANG_not_route_get_peer_id(valid_routes);
unsigned int length = 0;
while (!uuid_is_null(valid_routes[length])) {
++length;
}

/*int **peers_to_bug =*/ BANG_not_route_get_peer_id(valid_routes,length);

free(valid_routes);
}
Expand Down
60 changes: 57 additions & 3 deletions src/base/bang-routing.c
Expand Up @@ -51,9 +51,9 @@ 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 +
job->length;
4 /* A MAGIC NUMBER! */ +
LENGTH_OF_LENGTHS +
job->length;
request.request = malloc(request.length);
unsigned int header = BANG_SEND_JOB;
memcpy(request.request,&header,LENGTH_OF_HEADER);
Expand Down Expand Up @@ -139,6 +139,60 @@ int BANG_route_get_peer_id(uuid_t uuid) {
return - 1;
}

static char* create_not_select_string(unsigned int nots) {
if (nots == 0) return NULL;

unsigned int string_length = 34 + (21 * nots) + (nots - 1) * 3 + 1,
size = 0,
i = 0;

char *statement = malloc(string_length * sizeof(char));

strcpy(statement + size,"SELECT peer_id FROM mappings WHERE");
size += 34;
for (i = 0; i < nots; ++i) {
strcpy(statement + size," NOT remote_uuid = ? ");
size += 21;

if (i + 1 < nots) {
strcpy(statement ,"AND");
size += 3;
}
}

statement[string_length] = '\0';

return statement;
}

int** BANG_not_route_get_peer_id(uuid_t *uuids, unsigned int length) {
if (length == 0) return NULL;
unsigned int i;
char *select_string = create_not_select_string(length);

sqlite3_stmt *select_statement;
sqlite3_prepare_v2(db,select_string,-1,&select_statement,NULL);
free(select_string);

for (i = 1; i <= length; ++i) {
sqlite3_bind_blob(select_statement,i,uuids[i],sizeof(uuid_t),SQLITE_STATIC);
}

int **peer_ids = NULL;
i = 0;

while (sqlite3_step(select_statement) == SQLITE_ROW) {
peer_ids = realloc(peer_ids,i++ + 1 * sizeof(int));
peer_ids[i] = malloc(sizeof(int));
*(peer_ids[i]) = sqlite3_column_int(select_statement,i);

}

peer_ids[i + 1] = NULL;

return peer_ids;
}

static void insert_route(uuid_t uuid, int remote, BANG_module *module, int peer_id, char *module_name, unsigned char *module_version) {
assert(!uuid_is_null(uuid));
assert(remote == LOCAL_ROUTE || remote == REMOTE_ROUTE);
Expand Down
3 changes: 2 additions & 1 deletion src/base/bang-routing.h
Expand Up @@ -72,13 +72,14 @@ int BANG_route_get_peer_id(uuid_t uuid);

/**
* \param uuids The routes to peers.
* \param length The number of routes
*
* \return NULL terminated array of peer ids.
*
* \brief Returns peer_ids not on the list.
* memory: You must take care of it.
*/
int** BANG_not_route_get_peer_id(uuid_t *uuids);
int** BANG_not_route_get_peer_id(uuid_t *uuids, unsigned int length);

/**
* \param module The module to register with the uuid.
Expand Down

0 comments on commit d7619ee

Please sign in to comment.