int loop(void){ int i; int status = 0; struct emwin_server *es; es = get_next_server(); if(es == NULL){ status = 1; log_errx("No servers available."); log_info("Waiting %d seconds.", g.retrysleep); for(i = 0; i <= g.retrysleep; ++i){ sleep(1); if(get_quit_flag() != 0) break; } log_info("Trying server list again."); }else if(es->fd == -1){ if(es->gai_code != 0){ log_errx("Cannot open connection to %s. %s", es->ip, gai_strerror(es->gai_code)); }else{ log_err2("Cannot open connection to", es->ip); } status = 1; }else if(es->fd == -2){ if(server_type_serial_device(es)) log_errx("Cannot configure or synchronize %s:%s", es->ip, es->port); else log_errx("Could not get packet from %s", es->ip); status = 1; }else{ log_info("Connected to %s @ %s", es->ip, es->port); } while((status == 0) && (get_quit_flag() == 0)){ if(g.f_server_enabled == 1) server_loop(); status = process_packets(es); periodic(); if(get_reload_servers_list_flag()) reload_servers_list(); if(get_send_bbclientid_flag()) bb_send_clientid(es); } if(get_quit_flag() != 0) log_info("Closing processor."); return(status); }
main(int argc, char **argv) { int conns; int i; struct srv *s; argc--; argv++; nsrv = argc; if (!nsrv) exit(1); srv = calloc(nsrv, sizeof(struct srv)); sw = 0; for (i = 0; i < nsrv; i++) { s = &srv[i]; s->num = i; s->w = atol(argv[i]); sw += s->w; } nsw = sw; init_tree = &tree_1; next_tree = &tree_2; /* and insert all the servers in the PREV tree */ /* note that it is required to insert them according to * the reverse order of their weights. */ printf("---------------:"); for (i = 0; i < nsrv; i++) { s = &srv[i]; queue_by_weight_0(init_tree, s); printf("%2d", s->w); } printf("\n"); p = sw; // time base of current tree conns = 0; while (1) { struct eb32_node *node; printf("%08d|%06d: ", conns, p); /* if we have en empty tree, let's first try to collect weights * which might have changed. */ if (!sw) { if (nsw) { sw = nsw; p = sw; /* do not switch trees, otherwise new servers (from init) * would end up in next. */ //switch_trees(); //printf("bla\n"); } else goto next_iteration; } s = get_next_server(); if (!s) { printf("----------- switch (empty) -- sw=%d -> %d ---------\n", sw, nsw); switch_trees(); s = get_next_server(); printf("%08d|%06d: ", conns, p); if (!s) goto next_iteration; } else if (s->next >= 2*sw) { printf("ARGGGGG! s[%d].next=%d, max=%d\n", s->num, s->next, 2*sw-1); } /* now we have THE server we want to put at this position */ for (i = 0; i < s->num; i++) { if (srv[i].w > 0) printf(". "); else printf("_ "); } printf("# "); for (i = s->num + 1; i < nsrv; i++) { if (srv[i].w > 0) printf(". "); else printf("_ "); } printf(" : "); printf("s=%02d v=%04d w=%03d n=%03d r=%03d ", s->num, s->node.key, s->w, s->next, s->rem); update_position(s); printf(" | next=%03d, rem=%03d ", s->next, s->rem); if (s->next >= sw * 2) { dequeue_srv(s); //queue_by_weight(next_tree, s); put_srv(s); printf(" => next (w=%d, n=%d) ", s->w, s->next); } else { printf(" => curr "); //s->node.key = s->next; /* we want to ensure that in case of conflicts, servers with * the highest weights will get served first. Also, we still * have the remainder to see where the entry expected to be * inserted. */ //s->node.key = 256 * s->next + 255 - s->w; //s->node.key = sw * s->next + sw / s->w; //s->node.key = sw * s->next + s->rem; /// seems best (check with filltab15) ! //s->node.key = (2 * sw * s->next) + s->rem + sw / s->w; /* FIXME: must be optimized */ dequeue_srv(s); put_srv(s); //eb32i_insert(&tree_0, &s->node); //s->tree = &tree_0; } next_iteration: p++; conns++; if (/*conns == 30*/ /**/random()%100 == 0/**/) { int w = /*20*//**/random()%4096/**/; int num = /*1*//**/random()%nsrv/**/; struct srv *s = &srv[num]; nsw = nsw - s->w + w; //sw=nsw; if (s->tree == init_tree) { printf(" -- chgwght1(%d): %d->%d, n=%d --", s->num, s->w, w, s->next); printf("(init)"); s->w = w; dequeue_srv(s); queue_by_weight_0(s->tree, s); } else if (s->tree == NULL) { printf(" -- chgwght2(%d): %d->%d, n=%d --", s->num, s->w, w, s->next); printf("(down)"); s->w = w; dequeue_srv(s); //queue_by_weight_0(init_tree, s); get_srv(s); s->next = p + (nsw + sw - p) / s->w; put_srv(s); } else { int oldnext; /* the server is either active or in the next queue */ get_srv(s); printf(" -- chgwght3(%d): %d->%d, n=%d, sw=%d, nsw=%d --", s->num, s->w, w, s->next, sw, nsw); oldnext = s->next; s->w = w; /* we must measure how far we are from the end of the current window * and try to fit their as many entries as should theorically be. */ //s->w = s->w * (2*sw - p) / sw; if (s->w > 0) { int step = (nsw /*+ sw - p*/) / s->w; s->next = s->last + step; s->rem = 0; if (s->next > oldnext) { s->next = oldnext; printf(" aaaaaaa "); } if (s->next < p + 2) { s->next = p + step; printf(" bbbbbb "); } } else { printf(" push -- "); /* push it into the next tree */ s->w = 0; s->next = p + sw; } dequeue_srv(s); printf(" n=%d", s->next); put_srv(s); } } printf("\n"); if (0 && conns % 50000 == 0) { printf("-------- %-5d : changing all weights ----\n", conns); for (i = 0; i < nsrv; i++) { int w = i + 1; s = &srv[i]; nsw = nsw - s->w + w; s->w = w; dequeue_srv(s); queue_by_weight_0(next_tree, s); // or init_tree ? } } } }