From 6c2873ba2bedf239fcfe6587702b1f34691cb405 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 2 Aug 2014 20:46:31 +0200 Subject: mds-registry: notify client 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 | 4 ++-- src/mds-registry/slave.c | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/mds-registry/registry.c b/src/mds-registry/registry.c index b05d1a1..80bfbe3 100644 --- a/src/mds-registry/registry.c +++ b/src/mds-registry/registry.c @@ -385,10 +385,10 @@ static int list_registry(const char* recv_client_id, const char* recv_message_id /* Construct message headers. */ sprintf(send_buffer + ptr, "To: %s\nIn response to: %s\nMessage ID: %" PRIi32 "\nLength: %" PRIu64 "\n\n", - recv_message_id, recv_client_id, message_id, ptr); + recv_client_id, recv_message_id, message_id, ptr); /* Increase message ID. */ - message_id = message_id == INT32_MAX ? 0 : (message_id + 1); + with_mutex (slave_mutex, message_id = message_id == INT32_MAX ? 0 : (message_id + 1);); /* Send message. */ if (full_send(send_buffer + ptr, strlen(send_buffer + ptr))) diff --git a/src/mds-registry/slave.c b/src/mds-registry/slave.c index 6afc69f..6c5406d 100644 --- a/src/mds-registry/slave.c +++ b/src/mds-registry/slave.c @@ -22,14 +22,49 @@ #include "../mds-base.h" +#include #include #include #include #include +#include +/** + * Notify the waiting client that it may resume + * + * @param slave The slave + * @return Non-zero, `errno` will be set accordingly + */ +static int slave_notify_client(slave_t* slave) +{ + char buf[sizeof("To: %s\nIn response to: %s\nMessage ID: %" PRIi32 "\n\n") / sizeof(char) + 40]; + size_t ptr = 0, sent, left; + + /* Construct message headers. */ + sprintf(buf, "To: %s\nIn response to: %s\nMessage ID: %" PRIi32 "\n\n", + slave->client_id, slave->message_id, message_id); + + /* Increase message ID. */ + message_id = message_id == INT32_MAX ? 0 : (message_id + 1); + + /* Send message to client. */ + left = strlen(buf); + while (left > 0) + { + sent = send_message(socket_fd, buf + ptr, left); + if ((sent < left) && errno && (errno != EINTR)) + return -1; + left -= sent; + ptr += sent; + } + + return 0; +} + + /** * Master function for slave threads * @@ -55,10 +90,10 @@ static void* slave_loop(void* data) pthread_cond_wait(&slave_cond, &slave_mutex); } - pthread_mutex_unlock(&slave_mutex); + if (!(slave->closed) && (slave->wait_set->size == 0)) + slave_notify_client(slave); - if (!(slave->closed) && slave->wait_set->size) - ; /* FIXME send inside slave_mutex */ + pthread_mutex_unlock(&slave_mutex); goto done; -- cgit v1.2.3-70-g09d2