diff options
author | Yorhel <git@yorhel.nl> | 2013-04-27 19:52:17 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2013-04-27 19:52:17 +0200 |
commit | 663fd1ae491116ffa2e0f30a6a4d675503d77b0d (patch) | |
tree | da73800da665c9815a61a1f66a82355fcf7a95b8 | |
parent | df4d258ab8ee914dc4f4bf8441e423a07b4ab88a (diff) |
hub/manager: Use new vector macros for the list of hubs
-rw-r--r-- | src/global.h | 1 | ||||
-rw-r--r-- | src/hub/manager.c | 81 |
2 files changed, 23 insertions, 59 deletions
diff --git a/src/global.h b/src/global.h index bd8b4b1..1160643 100644 --- a/src/global.h +++ b/src/global.h @@ -77,6 +77,7 @@ #include <util/netstream.h> #include <util/netutil.h> #include <util/nmdc.h> +#include <util/vec.h> #include <main.h> #include <app.h> diff --git a/src/hub/manager.c b/src/hub/manager.c index 3eb96c1..8276bc3 100644 --- a/src/hub/manager.c +++ b/src/hub/manager.c @@ -30,10 +30,7 @@ typedef struct { dbo_t dbo; dbo_hubm_properties_t props; - /* This list forms an unordered set of hub pointers. Incides [0..hubnum-1] - * contain valid pointers, [hubnum..hublen-1] are empty slots. */ - int hubnum, hublen; - hub_t **hubs; + vec_t(hub_t *) hubs; /* Ordered by h->id */ } obj_t; static obj_t manager; @@ -41,43 +38,6 @@ static obj_t manager; -static hub_t *hub_find(int id) { - int i; - for(i=0; i<manager.hubnum; i++) - if(manager.hubs[i]->id == id) - return manager.hubs[i]; - return NULL; -} - - -static void hub_insert(hub_t *h) { - if(manager.hubnum == manager.hublen) { - manager.hublen = manager.hublen < 8 ? 8 : manager.hublen * 2; - manager.hubs = realloc(manager.hubs, manager.hublen*sizeof(hub_t *)); - } - manager.hubs[manager.hubnum++] = h; -} - - -static void hub_remove(int id) { - int i; - for(i=0; i<manager.hubnum; i++) - if(manager.hubs[i]->id == id) - break; - manager.hubs[i] = manager.hubs[--manager.hubnum]; -} - - -static int hub_newid() { - int i, id = 1; - for(i=0; i<manager.hubnum; i++) - if(manager.hubs[i]->id >= id) - id = manager.hubs[i]->id+1; - return id; -} - - - #define LOADVALUES\ V(AutoConnect)\ V(ConnectAddress)\ @@ -133,14 +93,15 @@ static void hub_create(int id, sqlasync_result_t *r) { #undef INTVAL - hub_insert(h); + size_t idx; + vec_search_insert(manager.hubs, idx, manager.hubs.a[i]->id - h->id); + vec_insert_order(manager.hubs, idx, h); } static void hub_destroy(hub_t *h) { yinfo("Destroying hub object: %d", h->id); - hub_remove(h->id); sqlasync_sql(db_sql, NULL, SQLASYNC_STATIC, "DELETE FROM hub_vars WHERE hub = ?", 1, sqlasync_int(h->id)); hub_conn_destroy(h); @@ -153,7 +114,7 @@ static void hub_destroy(hub_t *h) { static void Create(obj_t *o, DBusMessage *msg) { - int id = hub_newid(); + int id = manager.hubs.n > 0 ? manager.hubs.a[manager.hubs.n-1]->id+1 : 1; hub_create(id, NULL); dbo_hubm_Create_reply(msg, id); dbo_hubm_Created_signal(o, id); @@ -161,11 +122,13 @@ static void Create(obj_t *o, DBusMessage *msg) { static void Delete(obj_t *o, DBusMessage *msg, uint32_t id) { - hub_t *h = hub_find(id); - if(!h) + size_t idx = (size_t)-1; + vec_search(manager.hubs, manager.hubs.a[i]->id - id, idx=i); + if(idx == (size_t)-1) dbo_sendfree(dbus_message_new_error_printf(msg, DBUS_ERROR_INVALID_ARGS, "Invalid hub identifier")); else { - hub_destroy(h); + hub_destroy(manager.hubs.a[idx]); + vec_remove_order(manager.hubs, idx); dbo_hubm_Delete_reply(msg); dbo_hubm_Deleted_signal(o, id); } @@ -177,9 +140,9 @@ static void List(obj_t *o, DBusMessage *msg) { DBusMessage *r = dbus_message_new_method_return(msg); dbus_message_iter_init_append(r, &iter); dbus_message_iter_open_container(&iter, 'a', "u", &sub); - int i; - for(i=0; i<o->hubnum; i++) { - uint32_t n = o->hubs[i]->id; + size_t i; + for(i=0; i<o->hubs.n; i++) { + uint32_t n = o->hubs.a[i]->id; dbus_message_iter_append_basic(&sub, 'u', &n); } dbus_message_iter_close_container(&iter, &sub); @@ -189,9 +152,9 @@ static void List(obj_t *o, DBusMessage *msg) { static void hub_manager_infchange(EV_P_ ev_idle *w, int revents) { ev_idle_stop(EV_A_ w); - int i; - for(i=0; i<manager.hubnum; i++) - hub_hub_infchange(manager.hubs[i], false); + size_t i; + for(i=0; i<manager.hubs.n; i++) + hub_hub_infchange(manager.hubs.a[i], false); } @@ -272,19 +235,19 @@ void hub_manager_create(bool autoconnect) { dbo_register(&manager.dbo, MANAGER_PATH, ®, 1); hub_manager_load(); - int i; + size_t i; if(autoconnect) - for(i=0; i<manager.hubnum; i++) - hub_conn_autoconnect(manager.hubs[i]); + for(i=0; i<manager.hubs.n; i++) + hub_conn_autoconnect(manager.hubs.a[i]); } void hub_manager_shutdown() { /* Force disconnect all hubs. It'd be "cleaner" to do a graceful * disconnect, but we shouldn't take too long to shut down... */ - int i; - for(i=0; i<manager.hubnum; i++) { - hub_t *h = manager.hubs[i]; + size_t i; + for(i=0; i<manager.hubs.n; i++) { + hub_t *h = manager.hubs.a[i]; hub_conn_disconnect(h, HUBCD_FORCE, "net.blicky.Globster.Disconnected", "Shutting down"); hub_users_shutdown(h); } |