+ 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
+
+ unsigned int min_connects = mesh->dev_class_traits[mesh->devclass].min_connects;
+ unsigned int max_connects = mesh->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, "* %s->devclass = %d", n->name, n->devclass);
+
+ if(n != mesh->self && n->devclass <= mesh->devclass && !n->connection && !n->status.blacklisted && (n->last_connect_try == 0 || (mesh->loop.now.tv_sec - n->last_connect_try) > retry_timeout)) {
+ splay_insert(nodes, n);
+ }
+ }
+
+ if(nodes->head) {
+ //timeout = 0;
+ connect_to = (node_t *)nodes->head->data;
+
+ logger(mesh, MESHLINK_DEBUG, "* found best one for initial connect: %s", connect_to->name);
+ } else {
+ logger(mesh, MESHLINK_DEBUG, "* could not find node for initial connect");
+ }
+
+ splay_delete_tree(nodes);