static void net_worker_heartbeat(void) { time_t now; if (!locatorid || (locatorsvc == ST_MAX)) return; now = gettimer(); if (now > locatorhb) { locator_serverup(locatorid, locatorsvc); locatorhb = now + 60; } }
int main(int argc, char *argv[]) { char buf[1024]; int done = 0; char *res; if (argc < 2) { printf("Usage: %s IP:PORT\n", argv[0]); return 1; } if (locator_init(argv[1]) == -1) { printf("Locator ping failed\n"); return 1; } else { printf("Locator is available\n"); } while (!done) { char *p, *p1, *p2, *p3, *p4, *p5, *p6, *p7; char *extras; printf("Commands:\n"); printf(" r(egister) s servername type weight sticky\n"); printf(" r(egister) h servername type hostname\n"); printf(" d(own) servername type\n"); printf(" u(p) servername type\n"); printf(" f(orget) servername type\n"); printf(" q(uery) hostname type\n"); printf(" x(query) hostname type\n"); printf(" p(ing)\n"); printf(" s(ave state)\n"); printf(">"); fflush(stdout); done = (fgets(buf, sizeof(buf), stdin) == NULL); if (done) continue; p = strchr(buf, '\n'); if (p) *p = '\0'; p1 = p2 = p3 = p4 = p5 = p6 = p7 = NULL; p1 = strtok(buf, " "); if (p1) p2 = strtok(NULL, " "); if (p2) p3 = strtok(NULL, " "); if (p3) p4 = strtok(NULL, " "); if (p4) p5 = strtok(NULL, " "); if (p5) p6 = strtok(NULL, " "); if (p6) p7 = strtok(NULL, "\r\n"); switch (*p1) { case 'R': case 'r': if (*p2 == 's') { enum locator_servicetype_t svc; enum locator_sticky_t sticky; int weight; svc = get_servicetype(p4); weight = (p5 ? atoi(p5) : 1); sticky = ((p6 && (atoi(p6) == 1)) ? LOC_STICKY : LOC_ROAMING); printf("%s\n", locator_register_server(p3, svc, weight, sticky, p7) ? "Failed" : "OK"); } else if (*p2 == 'h') { printf("%s\n", locator_register_host(p5, get_servicetype(p4), p3) ? "Failed" : "OK"); } break; case 'D': case 'd': printf("%s\n", locator_serverdown(p2, get_servicetype(p3)) ? "Failed" : "OK"); break; case 'U': case 'u': printf("%s\n", locator_serverup(p2, get_servicetype(p3)) ? "Failed" : "OK"); break; case 'F': case 'f': printf("%s\n", locator_serverforget(p2, get_servicetype(p3)) ? "Failed" : "OK"); break; case 'Q': case 'q': case 'X': case 'x': extras = NULL; res = locator_query(p2, get_servicetype(p3), (*p1 == 'x') ? &extras : NULL); if (res) { printf("Result: %s\n", res); if (extras) printf(" Extras gave: %s\n", extras); } else { printf("Failed\n"); } break; case 'P': case 'p': p = locator_cmd("p"); if (p == NULL) printf("Failed\n"); else printf("OK: %s\n", p); break; case 'S': case 's': p = locator_cmd("@"); if (p == NULL) printf("Failed\n"); else printf("OK: %s\n", p); break; } } return 0; }