- if(!found) {
- logger(DEBUG_CONNECTIONS, LOG_INFO, "Cancelled outgoing connection to %s", o->name);
- /* The node variable is leaked in from using the list_each macro.
- The o variable could be used, but using node directly
- is more efficient.
- */
- list_delete_node(mesh->outgoings, node);
+ }
+
+ if(!found) {
+ logger(mesh, MESHLINK_DEBUG, "Autoconnecting to %s", connect_to->name);
+ outgoing_t *outgoing = xzalloc(sizeof(outgoing_t));
+ outgoing->mesh = mesh;
+ outgoing->name = xstrdup(connect_to->name);
+ list_insert_tail(mesh->outgoings, outgoing);
+ setup_outgoing_connection(mesh, outgoing);
+ } else
+ logger(mesh, MESHLINK_DEBUG, "* skip autoconnect since it is an outgoing connection already");
+ }
+
+
+ // disconnect suboptimal outgoing connections
+
+ if(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;
+ }
+
+ if(min_connects < connects) {
+ splay_tree_t *nodes = splay_alloc_tree(node_compare_devclass_desc, NULL);
+
+ for list_each(connection_t, c, mesh->connections) {
+ if(c->outgoing && c->node && c->node->devclass >= devclass)
+ splay_insert(nodes, c->node);
+ }
+
+ if(nodes->head) {
+ logger(mesh, MESHLINK_DEBUG, "* disconnect suboptimal outgoing connection");
+ disconnect_from = (node_t*)nodes->head->data;
+ }
+
+ splay_free_tree(nodes);
+ break;