summaryrefslogtreecommitdiffstats
path: root/src/server/rpc.c
diff options
context:
space:
mode:
authorStephan Schwaer2015-05-04 17:23:42 +0200
committerStephan Schwaer2015-05-04 17:23:42 +0200
commitfa900b4658b21294682760bdf84071cbb2b79aa4 (patch)
treea5ed190f304b88638c7440eadb44cf40fb16b2f2 /src/server/rpc.c
parent[SERVER] PoC RPC interface (diff)
downloaddnbd3-fa900b4658b21294682760bdf84071cbb2b79aa4.tar.gz
dnbd3-fa900b4658b21294682760bdf84071cbb2b79aa4.tar.xz
dnbd3-fa900b4658b21294682760bdf84071cbb2b79aa4.zip
Removed dnbd3_printClients, added clients to statistics json in rpc.
Diffstat (limited to 'src/server/rpc.c')
-rw-r--r--src/server/rpc.c57
1 files changed, 47 insertions, 10 deletions
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 <unistd.h>
+#include <string.h>
#include <jansson.h>
#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 );
}