From: Niklas Hofmann Date: Wed, 13 Aug 2014 14:52:47 +0000 (+0200) Subject: various connect order bugfixes; storing / reading devclass bugfix X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=963cca19035a2084a385541cb35f338bb2d740a8;p=meshlink various connect order bugfixes; storing / reading devclass bugfix --- diff --git a/src/net.c b/src/net.c index 80e866a0..146f95f3 100644 --- a/src/net.c +++ b/src/net.c @@ -145,10 +145,16 @@ static int node_compare_devclass_asc_last_connect_try_desc(const void *a, const if(na->last_connect_try == nb->last_connect_try) return 0; + if(na->last_connect_try == 0 || na->last_connect_try > nb->last_connect_try) + return -1; + if(nb->last_connect_try == 0 || na->last_connect_try < nb->last_connect_try) + return 1; + + if(na < nb) return -1; - if(na->last_connect_try == 0 || na->last_connect_try > nb->last_connect_try) + if(na > nb) return 1; return 0; @@ -162,10 +168,16 @@ static int node_compare_last_connect_try_desc(const void *a, const void *b) if(na->last_connect_try == nb->last_connect_try) return 0; + if(na->last_connect_try == 0 || na->last_connect_try > nb->last_connect_try) + return -1; + if(nb->last_connect_try == 0 || na->last_connect_try < nb->last_connect_try) + return 1; + + if(na < nb) return -1; - if(na->last_connect_try == 0 || na->last_connect_try > nb->last_connect_try) + if(na > nb) return 1; return 0; @@ -182,13 +194,18 @@ static int node_compare_devclass_desc(const void *a, const void *b) if(na->devclass > nb->devclass) { return 1; } + if(na < nb) + return -1; + + if(na > nb) + return 1; + return 0; } /* - autoconnect() { timeout = 5 @@ -278,8 +295,7 @@ disconnect } - - */ +*/ static void periodic_handler(event_loop_t *loop, void *data) { @@ -313,9 +329,13 @@ static void periodic_handler(event_loop_t *loop, void *data) { logger(mesh, MESHLINK_INFO, "--- autoconnect begin ---"); - int retry_timeout = min(mesh->nodes->count * 5, 60); + logger(mesh, MESHLINK_INFO, "* devclass = %d", mesh->devclass); + logger(mesh, MESHLINK_INFO, "* nodes = %d", mesh->nodes->count); + logger(mesh, MESHLINK_INFO, "* retry_timeout = %d", retry_timeout); + + // connect disconnect nodes node_t* connect_to = NULL; @@ -335,7 +355,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { } logger(mesh, MESHLINK_INFO, "* cur_connects = %d", cur_connects); - + logger(mesh, MESHLINK_INFO, "* outgoings = %d", mesh->outgoings->count); // get min_connects and max_connects @@ -356,7 +376,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { for splay_each(node_t, n, mesh->nodes) { - if(n->devclass <= mesh->devclass && !n->connection && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) + logger(mesh, MESHLINK_INFO, "* n->devclass = %d", n->devclass); + if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) { splay_insert(nodes, n); } } @@ -367,6 +388,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { //timeout = 0; connect_to = (node_t*)nodes->head->data; } + else + { logger(mesh, MESHLINK_INFO, "* could not find node for initial connect"); } splay_free_tree(nodes); } @@ -392,7 +415,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { for splay_each(node_t, n, mesh->nodes) { - if(n->devclass == devclass && !n->connection && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) + if(n != mesh->self && n->devclass == devclass && !n->connection && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) { splay_insert(nodes, n); } } @@ -410,6 +433,9 @@ static void periodic_handler(event_loop_t *loop, void *data) { else { break; } } + + if(!connect_to) + { logger(mesh, MESHLINK_INFO, "* could not find better nodes"); } } @@ -421,7 +447,7 @@ static void periodic_handler(event_loop_t *loop, void *data) { for splay_each(node_t, n, mesh->nodes) { - if(n->devclass <= mesh->devclass && !n->status.reachable && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) + if(n != mesh->self && n->devclass <= mesh->devclass && !n->status.reachable && (n->last_connect_try == 0 || (time(NULL) - n->last_connect_try) > retry_timeout)) { splay_insert(nodes, n); } } @@ -430,6 +456,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { logger(mesh, MESHLINK_INFO, "* try to heal partition"); connect_to = (node_t*)nodes->head->data; } + else + { logger(mesh, MESHLINK_INFO, "* could not find nodes for partition healing"); } splay_free_tree(nodes); } @@ -439,10 +467,14 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(connect_to && !connect_to->connection) { + connect_to->last_connect_try = time(NULL); + /* check if there is already a connection attempt to this node */ bool found = false; - for list_each(outgoing_t, outgoing, mesh->outgoings) { - if(!strcmp(outgoing->name, connect_to->name)) { + for list_each(outgoing_t, outgoing, mesh->outgoings) + { + if(!strcmp(outgoing->name, connect_to->name)) + { found = true; break; } @@ -457,6 +489,8 @@ static void periodic_handler(event_loop_t *loop, void *data) { list_insert_tail(mesh->outgoings, outgoing); setup_outgoing_connection(mesh, outgoing); } + else + { logger(mesh, MESHLINK_INFO, "* skip autoconnect since it is an outgoing connection already"); } } @@ -494,6 +528,9 @@ static void periodic_handler(event_loop_t *loop, void *data) { break; } } + + if(!disconnect_from) + { logger(mesh, MESHLINK_INFO, "* no suboptimal outgoing connections"); } } @@ -511,11 +548,13 @@ static void periodic_handler(event_loop_t *loop, void *data) { if(nodes->head) { - logger(mesh, MESHLINK_INFO, "* disconnect connection (too many connections"); + logger(mesh, MESHLINK_INFO, "* disconnect connection (too many connections)"); //timeout = 0; disconnect_from = (node_t*)nodes->head->data; } + else + { logger(mesh, MESHLINK_INFO, "* no node we want to disconnect, even though we have too many connections"); } splay_free_tree(nodes); } diff --git a/src/net_setup.c b/src/net_setup.c index fd438674..1ba98077 100644 --- a/src/net_setup.c +++ b/src/net_setup.c @@ -123,9 +123,7 @@ static bool read_invitation_key(meshlink_handle_t *mesh) { } bool node_read_devclass(meshlink_handle_t *mesh, node_t *n) { - if(n->devclass != 0) - return true; - + splay_tree_t *config_tree; char *p; @@ -149,7 +147,7 @@ exit: bool node_write_devclass(meshlink_handle_t *mesh, node_t *n) { - if(n->devclass == 0) + if(n->devclass < 0 || n->devclass > _DEV_CLASS_MAX) return false; bool result = false; @@ -380,6 +378,8 @@ bool setup_myself(meshlink_handle_t *mesh) { mesh->self->via = mesh->self; mesh->self->status.reachable = true; mesh->self->last_state_change = mesh->loop.now.tv_sec; + + node_write_devclass(mesh, mesh->self); node_add(mesh, mesh->self); graph(mesh); diff --git a/src/node.c b/src/node.c index f43619a2..c6213a17 100644 --- a/src/node.c +++ b/src/node.c @@ -54,6 +54,7 @@ node_t *new_node(void) { n->edge_tree = new_edge_tree(); n->mtu = MTU; n->maxmtu = MTU; + n->devclass = _DEV_CLASS_MAX; return n; }