]> git.meshlink.io Git - meshlink/commitdiff
various connect order bugfixes; storing / reading devclass bugfix
authorNiklas Hofmann <niklas.hofmann@everbase.net>
Wed, 13 Aug 2014 14:52:47 +0000 (16:52 +0200)
committerNiklas Hofmann <niklas.hofmann@everbase.net>
Wed, 13 Aug 2014 14:52:47 +0000 (16:52 +0200)
src/net.c
src/net_setup.c
src/node.c

index 80e866a0ad461fc49bce1794379044ca97e2e33c..146f95f38753a5f5164213c5f7208dd9d94fb9be 100644 (file)
--- 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);
                }
index fd4386746c2b1f626a35bb273e456ce3f08a80b0..1ba98077046b3d709b7d7db09ecef73bdfc798ea 100644 (file)
@@ -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);
index f43619a2a4b2f38fd2cc6e1c2d311eb7c8cef423..c6213a17016b61d755973be24a0d0518cf8e7630 100644 (file)
@@ -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;
 }