From a53d210d4d66eef43abcfddfd9ee437e8e74e24e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 14 Aug 2018 22:30:17 +0200 Subject: [SERVER] rpc: Distinguish between client and server (proxy) connections --- src/server/net.c | 19 ++++++++++++++----- src/server/net.h | 2 +- src/server/rpc.c | 5 +++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/server/net.c b/src/server/net.c index 67908cf..864d260 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -547,7 +547,7 @@ struct json_t* net_getListAsJson() { json_t *jsonClients = json_array(); json_t *clientStats; - int imgId; + int imgId, isServer; uint64_t bytesSent; char host[HOSTNAMELEN]; host[HOSTNAMELEN-1] = '\0'; @@ -564,11 +564,13 @@ struct json_t* net_getListAsJson() spin_unlock( &_clients_lock ); strncpy( host, client->hostName, HOSTNAMELEN - 1 ); imgId = client->image->id; + isServer = (int)client->isServer; bytesSent = client->bytesSent; spin_unlock( &client->lock ); - clientStats = json_pack( "{sssisI}", + clientStats = json_pack( "{sssisisI}", "address", host, "imageId", imgId, + "isServer", isServer, "bytesSent", (json_int_t)bytesSent ); json_array_append_new( jsonClients, clientStats ); spin_lock( &_clients_lock ); @@ -582,9 +584,9 @@ struct json_t* net_getListAsJson() * we don't unlock the list while iterating or we might get an * incorrect result if a client is disconnecting while iterating. */ -void net_getStats(int *clientCount, uint64_t *bytesSent) +void net_getStats(int *clientCount, int *serverCount, uint64_t *bytesSent) { - int cc = 0; + int cc = 0, sc = 0; uint64_t bs = 0; spin_lock( &_clients_lock ); @@ -592,13 +594,20 @@ void net_getStats(int *clientCount, uint64_t *bytesSent) const dnbd3_client_t * const client = _clients[i]; if ( client == NULL || client->image == NULL ) continue; - cc += 1; + if ( client->isServer ) { + sc += 1; + } else { + cc += 1; + } bs += client->bytesSent; } spin_unlock( &_clients_lock ); if ( clientCount != NULL ) { *clientCount = cc; } + if ( serverCount != NULL ) { + *serverCount = sc; + } if ( bytesSent != NULL ) { *bytesSent = totalBytesSent + bs; } diff --git a/src/server/net.h b/src/server/net.h index e91395c..6813b49 100644 --- a/src/server/net.h +++ b/src/server/net.h @@ -31,7 +31,7 @@ void* net_handleNewConnection(void *clientPtr); struct json_t* net_getListAsJson(); -void net_getStats(int *clientCount, uint64_t *bytesSent); +void net_getStats(int *clientCount, int *serverCount, uint64_t *bytesSent); void net_disconnectAll(); diff --git a/src/server/rpc.c b/src/server/rpc.c index 2e1fca0..340f5c0 100644 --- a/src/server/rpc.c +++ b/src/server/rpc.c @@ -292,14 +292,15 @@ static bool handleStatus(int sock, int permissions, struct field *fields, size_t json_t *statisticsJson; if ( stats ) { - int clientCount; + int clientCount, serverCount; uint64_t bytesSent; const uint64_t bytesReceived = uplink_getTotalBytesReceived(); - net_getStats( &clientCount, &bytesSent ); + net_getStats( &clientCount, &serverCount, &bytesSent ); statisticsJson = json_pack( "{sIsIsisIsI}", "bytesReceived", (json_int_t) bytesReceived, "bytesSent", (json_int_t) bytesSent, "clientCount", clientCount, + "serverCount", serverCount, "uptime", (json_int_t) dnbd3_serverUptime(), "runId", randomRunId ); } else { -- cgit v1.2.3-55-g7522