From fa900b4658b21294682760bdf84071cbb2b79aa4 Mon Sep 17 00:00:00 2001 From: Stephan Schwaer Date: Mon, 4 May 2015 17:23:42 +0200 Subject: Removed dnbd3_printClients, added clients to statistics json in rpc. --- src/server/rpc.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'src/server/rpc.c') diff --git a/src/server/rpc.c b/src/server/rpc.c index b2dc088..ae01a0e 100644 --- a/src/server/rpc.c +++ b/src/server/rpc.c @@ -1,18 +1,55 @@ #include +#include #include #include "rpc.h" +#include "server.h" +#include "net.h" +#include "uplink.h" +#include "log.h" +#include "locks.h" +#include "helper.h" + +static void clientsToJson(json_t *jsonClients); void rpc_sendStatsJson(int sock) { - int receivedBytes = 0; - int sentBytes = 1; - json_t *statisticsJson = json_pack( "{sisi}", "receivedBytes", receivedBytes, "sentBytes", sentBytes ); - char* jsonString = json_dumps(statisticsJson, 0); - char bla[500]; - snprintf(bla, sizeof bla, "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n", (int)strlen(jsonString)); - write( sock, bla, strlen(bla) ); - int n = write( sock, jsonString, strlen(jsonString)); - json_decref(statisticsJson); - free(jsonString); + uint64_t receivedBytes = uplink_getTotalBytesReceived(); + uint64_t sentBytes = net_getTotalBytesSent(); + + json_t *jsonClients = json_array(); + clientsToJson( jsonClients ); + + json_t *statisticsJson = json_pack( "{sisi}", "receivedBytes", (json_int_t) receivedBytes, "sentBytes", (json_int_t) sentBytes ); + json_object_set( statisticsJson, "clients", jsonClients ); + char *jsonString = json_dumps( statisticsJson, 0 ); + + char buffer[500]; + snprintf(buffer, sizeof(buffer), "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n", + (int) strlen( jsonString ) ); + write( sock, buffer, strlen( buffer ) ); + write( sock, jsonString, strlen( jsonString ) ); + json_decref( statisticsJson ); + json_decref( jsonClients ); + free( jsonString ); +} + +static void clientsToJson(json_t *jsonClients) +{ + json_t *clientStats; + int i; + char clientName[100]; + const char *imageName; + spin_lock( &_clients_lock ); + for (i = 0; i < _num_clients; ++i) { + if ( _clients[i] == NULL ) continue; + spin_lock( &_clients[i]->lock ); + host_to_string( &_clients[i]->host, clientName, sizeof(clientName) ); + imageName =_clients[i]->image != NULL ? _clients[i]->image->lower_name : "NULL"; + clientStats = json_pack( "{sssssi}", "client", clientName, "image", imageName , "bytesSent", (json_int_t) _clients[i]->bytesSent ); + json_array_append( jsonClients, clientStats ); + json_decref( clientStats ); + spin_unlock( &_clients[i]->lock ); + } + spin_unlock( &_clients_lock ); } -- cgit v1.2.3-55-g7522