From b143fa371731a24c0bea151253d14c9268e9e9e6 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 29 Jul 2014 16:41:22 +0200 Subject: mds-registry: restructure registry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-registry/registry.c | 199 +++++++++++++++++++++++--------------------- src/mds-registry/registry.h | 26 ------ 2 files changed, 105 insertions(+), 120 deletions(-) diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c index 026a5d5..98f5ad1 100644 --- a/src/mds-registry/registry.c +++ b/src/mds-registry/registry.c @@ -34,82 +34,6 @@ -/** - * Handle the received message containing ‘Command: register’-header–value - * - * @return Zero on success -1 on error or interruption, - * errno will be set accordingly - */ -static int handle_register_message(void) -{ - const char* recv_client_id = NULL; - const char* recv_message_id = NULL; - const char* recv_length = NULL; - const char* recv_action = NULL; - size_t i, length = 0; - -#define __get_header(storage, header) \ - (startswith(received.headers[i], header)) \ - storage = received.headers[i] + strlen(header) - - for (i = 0; i < received.header_count; i++) - { - if __get_header(recv_client_id, "Client ID: "); - else if __get_header(recv_message_id, "Message ID: "); - else if __get_header(recv_length, "Length: "); - else if __get_header(recv_action, "Action: "); - else - continue; - if (recv_client_id && recv_message_id && recv_length && recv_action) - break; - } - -#undef __get_header - - - if ((recv_client_id == NULL) || (strequals(recv_client_id, "0:0"))) - { - eprint("received message from anonymous sender, ignoring."); - return 0; - } - else if (strchr(recv_client_id, ':') == NULL) - { - eprint("received message from sender without a colon it its ID, ignoring, invalid ID."); - return 0; - } - else if ((recv_length == NULL) && ((recv_action == NULL) || !strequals(recv_action, "list"))) - { - eprint("received empty message without `Action: list`, ignoring, has no effect."); - return 0; - } - else if (recv_message_id == NULL) - { - eprint("received message with ID, ignoring, master server is misbehaving."); - return 0; - } - - - if (recv_length != NULL) - length = (size_t)atoll(recv_length); - if (recv_action != NULL) - recv_action = "add"; - -#define __registry_action(action) registry_action(length, action, recv_client_id, recv_message_id) - - if (strequals(recv_action, "add")) return __registry_action(1); - else if (strequals(recv_action, "remove")) return __registry_action(-1); - else if (strequals(recv_action, "wait")) return __registry_action(0); - else if (strequals(recv_action, "list")) return list_registry(recv_client_id, recv_message_id); - else - { - eprint("received invalid action, ignoring."); - return 0; - } - -#undef __registry_action -} - - /** * Handle the received message containing a ‘Client closed’-header * @@ -172,22 +96,6 @@ static int handle_close_message(void) } -/** - * Handle the received message - * - * @return Zero on success -1 on error or interruption, - * errno will be set accordingly - */ -int handle_message(void) -{ - size_t i; - for (i = 0; i < received.header_count; i++) - if (strequals(received.headers[i], "Command: register")) - return handle_register_message(); - return handle_close_message(); -} - - /** * Add a protocol to the registry * @@ -314,7 +222,7 @@ static int registry_action_act(char* command, int action, uint64_t client, hash_ * @return Zero on success -1 on error or interruption, * errno will be set accordingly */ -int registry_action(size_t length, int action, const char* recv_client_id, const char* recv_message_id) +static int registry_action(size_t length, int action, const char* recv_client_id, const char* recv_message_id) { char* payload = received.payload; uint64_t client = action ? parse_client_id(recv_client_id) : 0; @@ -390,7 +298,7 @@ int registry_action(size_t length, int action, const char* recv_client_id, const * @return Zero on success -1 on error or interruption, * errno will be set accordingly */ -int list_registry(const char* recv_client_id, const char* recv_message_id) +static int list_registry(const char* recv_client_id, const char* recv_message_id) { size_t ptr = 0, i; hash_entry_t* entry; @@ -448,3 +356,106 @@ int list_registry(const char* recv_client_id, const char* recv_message_id) return -1; } + +/** + * Handle the received message containing ‘Command: register’-header–value + * + * @return Zero on success -1 on error or interruption, + * errno will be set accordingly + */ +static int handle_register_message(void) +{ + /* Fetch message headers. */ + + const char* recv_client_id = NULL; + const char* recv_message_id = NULL; + const char* recv_length = NULL; + const char* recv_action = NULL; + size_t i, length = 0; + +#define __get_header(storage, header) \ + (startswith(received.headers[i], header)) \ + storage = received.headers[i] + strlen(header) + + for (i = 0; i < received.header_count; i++) + { + if __get_header(recv_client_id, "Client ID: "); + else if __get_header(recv_message_id, "Message ID: "); + else if __get_header(recv_length, "Length: "); + else if __get_header(recv_action, "Action: "); + else + continue; + + /* Stop if we got all headers we recognised. */ + if (recv_client_id && recv_message_id && recv_length && recv_action) + break; + } + +#undef __get_header + + + /* Validate headers. */ + + if ((recv_client_id == NULL) || (strequals(recv_client_id, "0:0"))) + { + eprint("received message from anonymous sender, ignoring."); + return 0; + } + else if (strchr(recv_client_id, ':') == NULL) + { + eprint("received message from sender without a colon it its ID, ignoring, invalid ID."); + return 0; + } + else if ((recv_length == NULL) && ((recv_action == NULL) || !strequals(recv_action, "list"))) + { + eprint("received empty message without `Action: list`, ignoring, has no effect."); + return 0; + } + else if (recv_message_id == NULL) + { + eprint("received message with ID, ignoring, master server is misbehaving."); + return 0; + } + + + /* Get message length, and make sure the action is defined. */ + + if (recv_length != NULL) + length = (size_t)atoll(recv_length); + if (recv_action != NULL) + recv_action = "add"; + + + /* Perform action. */ + +#define __registry_action(action) registry_action(length, action, recv_client_id, recv_message_id) + + if (strequals(recv_action, "add")) return __registry_action(1); + else if (strequals(recv_action, "remove")) return __registry_action(-1); + else if (strequals(recv_action, "wait")) return __registry_action(0); + else if (strequals(recv_action, "list")) return list_registry(recv_client_id, recv_message_id); + else + { + eprint("received invalid action, ignoring."); + return 0; + } + +#undef __registry_action +} + + +/** + * Handle the received message + * + * @return Zero on success -1 on error or interruption, + * errno will be set accordingly + */ +int handle_message(void) +{ + size_t i; + for (i = 0; i < received.header_count; i++) + if (strequals(received.headers[i], "Command: register")) + return handle_register_message(); + return handle_close_message(); +} + diff --git a/src/mds-registry/registry.h b/src/mds-registry/registry.h index f36749a..8d2a276 100644 --- a/src/mds-registry/registry.h +++ b/src/mds-registry/registry.h @@ -19,9 +19,6 @@ #define MDS_MDS_REGISTRY_REGISTRY_H -#include - - /** * Handle the received message * @@ -30,29 +27,6 @@ */ int handle_message(void); -/** - * Perform an action over the registry - * - * @param length The length of the received message - * @param action -1 to remove command, +1 to add commands, 0 to - * wait until the message commnds are registered - * @param recv_client_id The ID of the client - * @param recv_message_id The ID of the received message - * @return Zero on success -1 on error or interruption, - * errno will be set accordingly - */ -int registry_action(size_t length, int action, const char* recv_client_id, const char* recv_message_id); - -/** - * Send a list of all registered commands to a client - * - * @param recv_client_id The ID of the client - * @param recv_message_id The ID of the received message - * @return Zero on success -1 on error or interruption, - * errno will be set accordingly - */ -int list_registry(const char* recv_client_id, const char* recv_message_id); - #endif -- cgit v1.2.3-70-g09d2