- if(c->last_ping_time + pingtimeout < now) {
- if(c->status.active) {
- if(c->status.pinged) {
- ifdebug(CONNECTIONS) logger(LOG_INFO, _("%s (%s) didn't respond to PING in %ld seconds"),
- c->name, c->hostname, now - c->last_ping_time);
- c->status.timeout = true;
- terminate_connection(c, true);
- } else if(c->last_ping_time + pinginterval < now) {
- send_ping(c);
- }
- } else {
- if(c->status.remove) {
- logger(LOG_WARNING, _("Old connection_t for %s (%s) status %04x still lingering, deleting..."),
- c->name, c->hostname, c->status.value);
- connection_del(c);
- continue;
+ /* Check if there are too many contradicting ADD_EDGE and DEL_EDGE messages.
+ This usually only happens when another node has the same Name as this node.
+ If so, sleep for a short while to prevent a storm of contradicting messages.
+ */
+
+ if(mesh->contradicting_del_edge > 100 && mesh->contradicting_add_edge > 100) {
+ logger(mesh, MESHLINK_WARNING, "Possible node with same Name as us! Sleeping %d seconds.", mesh->sleeptime);
+ usleep(mesh->sleeptime * 1000000LL);
+ mesh->sleeptime *= 2;
+ if(mesh->sleeptime < 0)
+ mesh->sleeptime = 3600;
+ } else {
+ mesh->sleeptime /= 2;
+ if(mesh->sleeptime < 10)
+ mesh->sleeptime = 10;
+ }
+
+ mesh->contradicting_add_edge = 0;
+ mesh->contradicting_del_edge = 0;
+
+ int timeout = 5;
+
+ /* Check if we need to make or break connections. */
+
+ if(mesh->nodes->count > 1) {
+
+ logger(mesh, MESHLINK_DEBUG, "--- autoconnect begin ---");
+
+ int retry_timeout = min(mesh->nodes->count * 5, 60);
+
+ logger(mesh, MESHLINK_DEBUG, "* devclass = %d", mesh->devclass);
+ logger(mesh, MESHLINK_DEBUG, "* nodes = %d", mesh->nodes->count);
+ logger(mesh, MESHLINK_DEBUG, "* retry_timeout = %d", retry_timeout);
+
+
+ // connect disconnect nodes
+
+ node_t* connect_to = NULL;
+ node_t* disconnect_from = NULL;
+
+
+ // get cur_connects
+
+ int cur_connects = 0;
+
+ for list_each(connection_t, c, mesh->connections)
+ {
+ if(c->status.active)
+ {
+ cur_connects += 1;
+ }
+ }
+
+ logger(mesh, MESHLINK_DEBUG, "* cur_connects = %d", cur_connects);
+ logger(mesh, MESHLINK_DEBUG, "* outgoings = %d", mesh->outgoings->count);
+
+ // get min_connects and max_connects
+
+ assert(mesh->devclass >= 0 && mesh->devclass <= _DEV_CLASS_MAX);
+
+ int min_connects = dev_class_traits[mesh->devclass].min_connects;
+ int max_connects = dev_class_traits[mesh->devclass].max_connects;
+
+ logger(mesh, MESHLINK_DEBUG, "* min_connects = %d", min_connects);
+ logger(mesh, MESHLINK_DEBUG, "* max_connects = %d", max_connects);
+
+
+ // find the best one for initial connect
+
+ if(cur_connects < min_connects)
+ {
+ splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_asc_lsc_desc, NULL);
+
+ for splay_each(node_t, n, mesh->nodes)
+ {
+ logger(mesh, MESHLINK_DEBUG, "* 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); }
+ }
+
+ if(nodes->head)
+ {
+ logger(mesh, MESHLINK_DEBUG, "* found best one for initial connect");
+
+ //timeout = 0;
+ connect_to = (node_t*)nodes->head->data;
+ }
+ else
+ { logger(mesh, MESHLINK_DEBUG, "* could not find node for initial connect"); }
+
+ splay_free_tree(nodes);
+ }
+
+
+ // find better nodes to connect to
+
+ if(!connect_to && min_connects <= cur_connects && cur_connects < max_connects)
+ {
+ unsigned int connects = 0;
+
+ for(int devclass = 0; devclass <= mesh->devclass; ++devclass)
+ {
+ for list_each(connection_t, c, mesh->connections)
+ {
+ if(c->status.active && c->node && c->node->devclass == devclass)
+ { connects += 1; }