- /* Count number of unreachable nodes */
- int num_unreachable = 0;
- for splay_each(node_t, n, mesh->nodes) {
- if(!n->status.reachable)
- num_unreachable++;
+ logger(mesh, MESHLINK_INFO, "* cur_connects = %d", cur_connects);
+
+
+ // get min_connects and max_connects
+
+ int min_connects = dev_class_traits[mesh->devclass].min_connects;
+ int max_connects = dev_class_traits[mesh->devclass].max_connects;
+
+ logger(mesh, MESHLINK_INFO, "* min_connects = %d", min_connects);
+ logger(mesh, MESHLINK_INFO, "* 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_last_connect_try_desc, NULL);
+
+ 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))
+ { splay_insert(nodes, n); }
+ }
+
+ if(nodes->head)
+ {
+ logger(mesh, MESHLINK_INFO, "* found best one for initial connect");
+
+ timeout = 0;
+ connect_to = (node_t*)nodes->head->data;
+ }
+
+ splay_free_tree(nodes);