From 1a2bcf26b785169f22c6065a1b28674c3843adaf Mon Sep 17 00:00:00 2001 From: nbysani2 Date: Mon, 26 Jan 2009 00:14:58 +0000 Subject: [PATCH] Created a remove_mapping function. git-svn-id: https://subversion.cs.uiuc.edu/svn/bang/eoh2009@58 69d76c3e-0761-0410-948c-9895a8bb34fc --- src/base/bang-routing.c | 39 ++++++++++++++++++++++++++++++++------- src/base/bang-routing.h | 7 +++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/base/bang-routing.c b/src/base/bang-routing.c index cccb3d1..c0f4d9f 100644 --- a/src/base/bang-routing.c +++ b/src/base/bang-routing.c @@ -14,9 +14,10 @@ #include #define CREATE_MAPPINGS "CREATE TABLE mappings(route_uuid blob unique primary key, remote integer, peer_id int, module blob, name text, version blob)" -#define INSERT_STATEMENT "INSERT INTO mappings(route_uuid,remote,module,peer_id,name,text) VALUES (?,?,?,?,?)" -#define SELECT_STATEMENT "SELECT remote,module,peer_id,name,version FROM mappings WHERE ? = route_uuid" +#define INSERT_MAPPING "INSERT INTO mappings(route_uuid,remote,module,peer_id,name,text) VALUES (?,?,?,?,?)" +#define SELECT_MAPPING "SELECT remote,module,peer_id,name,version FROM mappings WHERE ? = route_uuid" #define SELECT_UUID "SELECT route_uuid WHERE ? = peer_id" +#define REMOVE_MAPPING "DELETE FROM mappings WHERE ? = route_uuid" #define CREATE_PEER_LIST "CREATE TABLE peers(id int)" #define INSERT_PEER "INSERT INTO peers(id) VALUES (?)" @@ -41,6 +42,8 @@ static void mem_append(void *dst, void *src, int length, int *pos); static void insert_mapping(uuid_t uuid, int remote, BANG_module *module, int peer_id, char *module_name, unsigned char *module_version); +static void remove_mapping(uuid_t uuid); + static sqlite3_stmt* prepare_select_statement(uuid_t uuid); static BANG_linked_list* select_routes_from_id(int id); @@ -304,6 +307,12 @@ void BANG_register_peer_route(uuid_t uuid, int peer, char *module_name, unsigned insert_mapping(uuid,REMOTE_ROUTE,NULL,peer,module_name,module_version); } +void BANG_deregister_route(uuid_t uuid) { + assert(!uuid_is_null(uuid)); + + remove_mapping(uuid); +} + void BANG_route_close() { #ifdef BDEBUG_1 fprintf(stderr,"BANG route closing.\n"); @@ -377,9 +386,9 @@ static void insert_mapping(uuid_t uuid, int remote, BANG_module *module, int pee sqlite3_stmt *insert; #ifdef NEW_SQLITE - sqlite3_prepare_v2(db,INSERT_STATMENT,-1,&insert,NULL); + sqlite3_prepare_v2(db,INSERT_MAPPING,-1,&insert,NULL); #else - sqlite3_prepare(db,INSERT_STATEMENT,-1,&insert,NULL); + sqlite3_prepare(db,INSERT_MAPPING,-1,&insert,NULL); #endif sqlite3_bind_blob(insert,1,uuid,sizeof(uuid_t),SQLITE_STATIC); sqlite3_bind_int(insert,2,remote); @@ -392,15 +401,31 @@ static void insert_mapping(uuid_t uuid, int remote, BANG_module *module, int pee sqlite3_finalize(insert); } +static void remove_mapping(uuid_t uuid) { + assert(!uuid_is_null(uuid)); + + sqlite3_stmt *remove; + +#ifdef NEW_SQLITE + sqlite3_prepare_v2(db,REMOVE_MAPPING,-1,&remove,NULL); +#else + sqlite3_prepare(db,REMOVE_MAPPING,-1,&remove,NULL); +#endif + + sqlite3_bind_blob(remove,1,uuid,sizeof(uuid_t),SQLITE_STATIC); + sqlite3_step(remove); + sqlite3_finalize(remove); +} + static sqlite3_stmt* prepare_select_statement(uuid_t uuid) { assert(!uuid_is_null(uuid)); sqlite3_stmt *get_peer_route; /* God dammit, ews needs to update sqlite */ #ifdef NEW_SQLITE - sqlite3_prepare_v2(db,SELECT_STATEMENT,-1,&get_peer_route,NULL); + sqlite3_prepare_v2(db,SELECT_MAPPING,-1,&get_peer_route,NULL); #else - sqlite3_prepare(db,SELECT_STATEMENT,90,&get_peer_route,NULL); + sqlite3_prepare(db,SELECT_MAPPING,-1,&get_peer_route,NULL); #endif sqlite3_bind_blob(get_peer_route,1,uuid,sizeof(uuid_t),SQLITE_STATIC); @@ -563,7 +588,6 @@ static void catch_peer_removed(int signal, int num_peers, void **p) { lst = select_routes_from_id(*(peers[i])); while ((route = BANG_linked_list_pop(lst)) != NULL) { - /* TODO: tell them that route is being removed. */ route_list = select_route(*route); while ((remote_route = BANG_linked_list_pop(route_list)) != NULL) { @@ -571,6 +595,7 @@ static void catch_peer_removed(int signal, int num_peers, void **p) { free(remote_route); } + BANG_deregister_route(*route); free(route); } diff --git a/src/base/bang-routing.h b/src/base/bang-routing.h index d7398e2..5734a98 100644 --- a/src/base/bang-routing.h +++ b/src/base/bang-routing.h @@ -114,6 +114,13 @@ void BANG_register_module_route(BANG_module *module); */ void BANG_register_peer_route(uuid_t uuid, int peer, char *module_name, unsigned char* module_version); +/** + * \param uuid The identifier to deregister. + * + * \brief Deregisters a uuid. + */ +void BANG_deregister_route(uuid_t uid); + /** * \brief Starts the routing part of the library. */