From a3819b873938712fce7f9bcc93fe52b57b8e076b Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 23 Mar 2015 16:57:20 +1100 Subject: [PATCH] Add an API command to list all workers --- src/stratifier.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/stratifier.c b/src/stratifier.c index b542b71f..25548652 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -2437,6 +2437,18 @@ static void lazy_reconnect_client(sdata_t *sdata, stratum_instance_t *client) static user_instance_t *get_user(sdata_t *sdata, const char *username); +static json_t *userinfo(const user_instance_t *user) +{ + json_t *val; + + JSON_CPACK(val, "{ss,sI,si,sf,sf,sf,sf,sf,sf,si}", + "user", user->username, "id", user->id, "workers", user->workers, + "bestdiff", user->best_diff, "dsps1", user->dsps1, "dsps5", user->dsps5, + "dsps60", user->dsps60, "dsps1440", user->dsps1440, "dsps10080", user->dsps10080, + "lastshare", user->last_share.tv_sec); + return val; +} + static void getuser(sdata_t *sdata, const char *buf, int *sockd) { char *username = NULL; @@ -2458,11 +2470,7 @@ static void getuser(sdata_t *sdata, const char *buf, int *sockd) goto out; } user = get_user(sdata, username); - JSON_CPACK(val, "{ss,sI,si,sf,sf,sf,sf,sf,sf,si}", - "user", user->username, "id", user->id, "workers", user->workers, - "bestdiff", user->best_diff, "dsps1", user->dsps1, "dsps5", user->dsps5, - "dsps60", user->dsps60, "dsps1440", user->dsps1440, "dsps10080", user->dsps10080, - "lastshare", user->last_share.tv_sec); + val = userinfo(user); out: free(username); send_api_response(val, *sockd); @@ -2614,6 +2622,24 @@ static void getworkers(sdata_t *sdata, int *sockd) _Close(sockd); } +static void getusers(sdata_t *sdata, int *sockd) +{ + json_t *val = NULL, *user_array; + user_instance_t *user; + + user_array = json_array(); + + ck_rlock(&sdata->instance_lock); + for (user = sdata->user_instances; user; user = user->hh.next) { + json_array_append_new(user_array, userinfo(user)); + } + ck_runlock(&sdata->instance_lock); + + JSON_CPACK(val, "{so}", "users", user_array); + send_api_response(val, *sockd); + _Close(sockd); +} + static json_t *clientinfo(const stratum_instance_t *client) { json_t *val = json_object(); @@ -2983,6 +3009,10 @@ retry: getworkers(sdata, &sockd); goto retry; } + if (cmdmatch(buf, "users")) { + getusers(sdata, &sockd); + goto retry; + } if (cmdmatch(buf, "getclient")) { getclient(sdata, buf + 10, &sockd); goto retry;