From 23d6cca7eeeaffff75d8c933ef02787fb948fe15 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 23 Mar 2015 16:53:22 +1100 Subject: [PATCH] Add an API command to list all workers --- src/stratifier.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index acce72af..b542b71f 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2549,6 +2549,18 @@ out: static worker_instance_t *get_worker(sdata_t *sdata, user_instance_t *user, const char *workername); +static json_t *workerinfo(const user_instance_t *user, const worker_instance_t *worker) +{ + json_t *val; + + JSON_CPACK(val, "{ss,ss,sI,sf,sf,sf,sf,si,sf,si,sb}", + "user", user->username, "worker", worker->workername, "id", user->id, + "dsps1", worker->dsps1, "dsps5", worker->dsps5, "dsps60", worker->dsps60, + "dsps1440", worker->dsps1440, "lastshare", worker->last_share.tv_sec, + "bestdiff", worker->best_diff, "mindiff", worker->mindiff, "idle", worker->idle); + return val; +} + static void getworker(sdata_t *sdata, const char *buf, int *sockd) { char *tmp, *username, *workername = NULL; @@ -2574,17 +2586,34 @@ static void getworker(sdata_t *sdata, const char *buf, int *sockd) username = strsep(&tmp, "._"); user = get_user(sdata, username); worker = get_worker(sdata, user, workername); - JSON_CPACK(val, "{ss,ss,sI,sf,sf,sf,sf,si,sf,si,sb}", - "user", username, "worker", workername, "id", user->id, - "dsps1", worker->dsps1, "dsps5", worker->dsps5, "dsps60", worker->dsps60, - "dsps1440", worker->dsps1440, "lastshare", worker->last_share.tv_sec, - "bestdiff", worker->best_diff, "mindiff", worker->mindiff, "idle", worker->idle); + val = workerinfo(user, worker); out: free(workername); send_api_response(val, *sockd); _Close(sockd); } +static void getworkers(sdata_t *sdata, int *sockd) +{ + json_t *val = NULL, *worker_arr; + worker_instance_t *worker; + user_instance_t *user; + + worker_arr = json_array(); + + ck_rlock(&sdata->instance_lock); + for (user = sdata->user_instances; user; user = user->hh.next) { + DL_FOREACH(user->worker_instances, worker) { + json_array_append_new(worker_arr, workerinfo(user, worker)); + } + } + ck_runlock(&sdata->instance_lock); + + JSON_CPACK(val, "{so}", "workers", worker_arr); + send_api_response(val, *sockd); + _Close(sockd); +} + static json_t *clientinfo(const stratum_instance_t *client) { json_t *val = json_object(); @@ -2950,6 +2979,10 @@ retry: getclients(sdata, &sockd); goto retry; } + if (cmdmatch(buf, "workers")) { + getworkers(sdata, &sockd); + goto retry; + } if (cmdmatch(buf, "getclient")) { getclient(sdata, buf + 10, &sockd); goto retry;