diff options
author | Simon Rettberg | 2015-05-08 10:53:06 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-05-08 10:53:06 +0200 |
commit | 2ab53f3a518dcd8b703e515ffd123adce181044c (patch) | |
tree | 96e52eddf2c9808ace02cfc5d4bb0457e8153ae0 | |
parent | [SERVER] Fix premature connection close for RPC replies (diff) | |
download | dnbd3-2ab53f3a518dcd8b703e515ffd123adce181044c.tar.gz dnbd3-2ab53f3a518dcd8b703e515ffd123adce181044c.tar.xz dnbd3-2ab53f3a518dcd8b703e515ffd123adce181044c.zip |
[SERVER] Add image ID for easier handling in RPC, export RID via RPC, make names of the byte count fields in RPC consistent
-rw-r--r-- | src/server/globals.h | 1 | ||||
-rw-r--r-- | src/server/image.c | 7 | ||||
-rw-r--r-- | src/server/net.c | 2 | ||||
-rw-r--r-- | src/server/rpc.c | 18 |
4 files changed, 17 insertions, 11 deletions
diff --git a/src/server/globals.h b/src/server/globals.h index 9f6a46d..9024a46 100644 --- a/src/server/globals.h +++ b/src/server/globals.h @@ -108,6 +108,7 @@ struct _dnbd3_image int cacheFd; // used to write to the image, in case it is relayed. ONLY USE FROM UPLINK THREAD! int rid; // revision of image int users; // clients currently using this image + int id; // Unique ID of this image. Only unique in the context of this running instance of DNBD3-Server time_t atime; // last access time bool working; // true if image exists and completeness is == 100% or a working upstream proxy is connected pthread_spinlock_t lock; diff --git a/src/server/image.c b/src/server/image.c index 920f15d..1b2ce07 100644 --- a/src/server/image.c +++ b/src/server/image.c @@ -500,6 +500,7 @@ static bool image_load_all_internal(char *base, char *path) static bool image_load(char *base, char *path, int withUplink) { + static int imgIdCounter = 0; // Used to assign unique numeric IDs to images int i, revision; struct stat st; uint8_t *cache_map = NULL; @@ -682,6 +683,8 @@ static bool image_load(char *base, char *path, int withUplink) // ### Reaching this point means loading succeeded // Add to images array spin_lock( &_images_lock ); + // Now we're locked, assign unique ID to image (unique for this running server instance!) + image->id = ++imgIdCounter; for (i = 0; i < _num_images; ++i) { if ( _images[i] != NULL ) continue; _images[i] = image; @@ -1144,8 +1147,8 @@ json_t* image_fillJson() for (i = 0; i < _num_images; ++i) { if ( _images[i] == NULL ) continue; spin_lock( &_images[i]->lock ); - image = json_pack( "{sssIsI}", "image", _images[i]->lower_name, "users", (json_int_t) _images[i]->users, - "complete", (json_int_t) image_getCompletenessEstimate( _images[i] ) ); + image = json_pack( "{sisssisIsi}", "id", _images[i]->id, "image", _images[i]->lower_name, "rid", (int) _images[i]->rid, "users", (json_int_t) _images[i]->users, + "complete", image_getCompletenessEstimate( _images[i] ) ); if ( _images[i]->uplink != NULL ) { host_to_string( &_images[i]->uplink->currentServer, buffer, sizeof(buffer) ); json_object_set_new( image, "uplinkServer", json_string( buffer ) ); diff --git a/src/server/net.c b/src/server/net.c index 9dc1442..a7bdf86 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -63,7 +63,7 @@ static inline bool recv_request_header(int sock, dnbd3_request_t *request) // Make sure all bytes are in the right order (endianness) fixup_request( *request ); if ( request->magic != dnbd3_packet_magic ) { - logadd( LOG_DEBUG1, "Magic in client request incorrect (cmd: %d, len: %d)\n", (int)request->cmd, (int)request->size ); + logadd( LOG_DEBUG2, "Magic in client request incorrect (cmd: %d, len: %d)\n", (int)request->cmd, (int)request->size ); return false; } // Payload sanity check diff --git a/src/server/rpc.c b/src/server/rpc.c index d02ff92..ea1eccf 100644 --- a/src/server/rpc.c +++ b/src/server/rpc.c @@ -17,14 +17,14 @@ static void clientsToJson(json_t *jsonClients); void rpc_sendStatsJson(int sock) { - const uint64_t receivedBytes = uplink_getTotalBytesReceived(); - const uint64_t sentBytes = net_getTotalBytesSent(); + const uint64_t bytesReceived = uplink_getTotalBytesReceived(); + const uint64_t bytesSent = net_getTotalBytesSent(); const int uptime = dnbd3_serverUptime(); json_t *jsonClients = json_array(); clientsToJson( jsonClients ); - json_t *statisticsJson = json_pack( "{sIsI}", "receivedBytes", (json_int_t) receivedBytes, "sentBytes", (json_int_t) sentBytes ); + json_t *statisticsJson = json_pack( "{sIsI}", "bytesReceived", (json_int_t) bytesReceived, "bytesSent", (json_int_t) bytesSent ); json_object_set_new( statisticsJson, "clients", jsonClients ); json_object_set_new( statisticsJson, "images", image_fillJson() ); json_object_set_new( statisticsJson, "uptime", json_integer( uptime ) ); @@ -47,15 +47,17 @@ 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_new( jsonClients, clientStats ); + if ( _clients[i]->image != NULL ) { + if ( !host_to_string( &_clients[i]->host, clientName, sizeof(clientName) ) ) { + strcpy( clientName, "???" ); + } + clientStats = json_pack( "{sssisI}", "client", clientName, "image", _clients[i]->image->id, "bytesSent", (json_int_t)_clients[i]->bytesSent ); + json_array_append_new( jsonClients, clientStats ); + } spin_unlock( &_clients[i]->lock ); } spin_unlock( &_clients_lock ); |