diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-04-23 16:03:40 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-04-23 16:03:40 +0200 |
commit | 88c7815d5fa18f51ed4ac318155822b8a688fdf5 (patch) | |
tree | c7f36d3b44fdb1ecc13dcbd8400c1c46ee029ac8 | |
parent | add value remapping to hash table unmarshaling (diff) | |
download | mds-88c7815d5fa18f51ed4ac318155822b8a688fdf5.tar.gz mds-88c7815d5fa18f51ed4ac318155822b8a688fdf5.tar.bz2 mds-88c7815d5fa18f51ed4ac318155822b8a688fdf5.tar.xz |
m fixes + store client information in a hash table and in a linked list
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | src/libmdsserver/hash-table.h | 2 | ||||
-rw-r--r-- | src/libmdsserver/linked-list.h | 4 | ||||
-rw-r--r-- | src/mds-server.c | 82 | ||||
-rw-r--r-- | src/mds-server.h | 20 |
4 files changed, 101 insertions, 7 deletions
diff --git a/src/libmdsserver/hash-table.h b/src/libmdsserver/hash-table.h index 2525da0..cf0af02 100644 --- a/src/libmdsserver/hash-table.h +++ b/src/libmdsserver/hash-table.h @@ -165,7 +165,7 @@ int hash_table_create_fine_tuned(hash_table_t* restrict this, size_t initial_cap * @return :int Non-zero on error, `errno` will have been set accordingly */ #define hash_table_create_tuned(this, initial_capacity) \ - hash_table_create_fine_tuned(this, initial_capacity, 0.75) + hash_table_create_fine_tuned(this, initial_capacity, 0.75f) /** * Create a hash table diff --git a/src/libmdsserver/linked-list.h b/src/libmdsserver/linked-list.h index 3427fa6..e99040f 100644 --- a/src/libmdsserver/linked-list.h +++ b/src/libmdsserver/linked-list.h @@ -227,7 +227,7 @@ void linked_list_remove(linked_list_t* restrict this, ssize_t node); * `LINKED_LIST_UNUSED` on error, `errno` will be set accordingly */ #define linked_list_insert_end(this, value) \ - (linked_list_insert_before(this, value, this->edge)) + (linked_list_insert_before((this), (value), (this)->edge)) /** * Remove the node at the end of the list @@ -236,7 +236,7 @@ void linked_list_remove(linked_list_t* restrict this, ssize_t node); * @return :ssize_t The node that has been removed */ #define linked_list_remove_end(this) \ - (linked_list_remove_before(this, this->edge)) + (linked_list_remove_before((this), (this)->edge)) /** * Calculate the buffer size need to marshal a linked list diff --git a/src/mds-server.c b/src/mds-server.c index 1ef31c0..b0314f8 100644 --- a/src/mds-server.c +++ b/src/mds-server.c @@ -18,6 +18,10 @@ #include "mds-server.h" #include "config.h" +#include <libmdsserver/linked-list.h> +#include <libmdsserver/hash-table.h> +#include <libmdsserver/hash-help.h> + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -54,15 +58,25 @@ static volatile int running = 1; static int running_slaves = 0; /** - * Mutex for slave counter + * Mutex for slave data */ static pthread_mutex_t slave_mutex; /** - * Condition for slave counter + * Condition for slave data */ static pthread_cond_t slave_cond; +/** + * Map from client socket file descriptor to all information (client_t) + */ +static hash_table_t client_map; + +/** + * List of client information (client_t) + */ +static linked_list_t client_list; + /* TODO make the server update without all slaves dying on SIGUSR1 */ @@ -192,6 +206,21 @@ int main(int argc_, char** argv_) } + /* Create list and table of clients. */ + if (hash_table_create(&client_map)) + { + perror(*argv); + hash_table_destroy(&client_map, NULL, NULL); + return 1; + } + if (linked_list_create(&client_list, 32)) + { + perror(*argv); + linked_list_destroy(&client_list); + return 1; + } + + /* Create mutex and condition for slave counter. */ pthread_mutex_init(&slave_mutex, NULL); pthread_cond_init(&slave_cond, NULL); @@ -249,6 +278,11 @@ int main(int argc_, char** argv_) pthread_cond_wait(&slave_cond, &slave_mutex); pthread_mutex_unlock(&slave_mutex); + + /* Release resources. */ + hash_table_destroy(&client_map, NULL, NULL); + linked_list_destroy(&client_list); + return 0; } @@ -262,14 +296,54 @@ int main(int argc_, char** argv_) void* slave_loop(void* data) { int socket_fd = (int)(intptr_t)data; + ssize_t entry = LINKED_LIST_UNUSED; + client_t* information; + size_t tmp; + + /* Create information table. */ + information = malloc(sizeof(client_t)); + if (information == NULL) + { + perror(*argv); + goto fail; + } + + /* Add to list of clients. */ + pthread_mutex_lock(&slave_mutex); + entry = linked_list_insert_end(&client_list, (size_t)(void*)information); + if (entry == LINKED_LIST_UNUSED) + { + perror(*argv); + pthread_mutex_unlock(&slave_mutex); + goto fail; + } + + /* Add client to hash table. */ + tmp = hash_table_put(&client_map, (size_t)socket_fd, (size_t)(void*)information); + pthread_mutex_unlock(&slave_mutex); + if ((tmp == 0) && errno) + { + perror(*argv); + goto fail; + } + + /* Fill information table. */ + information->list_entry = entry; + information->socket_fd = socket_fd; /* TODO */ - /* Close socket. */ + fail: + /* Close socket and free resources. */ close(socket_fd); + if (information != NULL) + free(information); + hash_table_remove(&client_map, (size_t)socket_fd); - /* Decrease the slave count. */ + /* Unlist client and decrease the slave count. */ pthread_mutex_lock(&slave_mutex); + if (entry != LINKED_LIST_UNUSED) + linked_list_remove(&client_list, entry); running_slaves--; pthread_cond_signal(&slave_cond); pthread_mutex_unlock(&slave_mutex); diff --git a/src/mds-server.h b/src/mds-server.h index cad1fb1..5b2ab30 100644 --- a/src/mds-server.h +++ b/src/mds-server.h @@ -19,6 +19,26 @@ #define MDS_MDS_SERVER_H +#include <stdlib.h> + + +/** + * Client information structure + */ +typedef struct client +{ + /** + * The client's entry in the list of clients + */ + ssize_t list_entry; + + /** + * The socket file descriptor for the socket connected to the client + */ + int socket_fd; + +} client_t; + /** * Master function for slave threads |