| summaryrefslogtreecommitdiff |
diff options
25 files changed, 360 insertions, 141 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d180c5..7929ab0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,18 +9,18 @@ add_definitions(-D_POSIX_SOURCE) add_definitions(-D_POSIX_C_SOURCE=200809L) if(CMAKE_COMPILER_IS_GNUCC) - message(STATUS "GNUCC detected. Adding '-02' parameter.") + # message(STATUS "GNUCC detected. Adding '-03' parameter.") # set(CMAKE_C_FLAGS "-O2") - set(CMAKE_C_FLAGS "-O3") + set(CMAKE_C_FLAGS " -g") endif() # ${SRC_FILES} is recursively defined in the subdirectories. # Each subdirectory only adds the source files that are present at its level. -add_executable(zero_of_one_server ${SRC_FILES}) -set_property(TARGET zero_of_one_server PROPERTY C_STANDARD 99) -set_property(TARGET zero_of_one_server PROPERTY C_STANDARD_REQUIRED ON) -#find_package(Threads) -#target_link_libraries(cborg ${CMAKE_THREAD_LIBS_INIT}) +add_executable(ZoO_server ${SRC_FILES}) +set_property(TARGET ZoO_server PROPERTY C_STANDARD 99) +set_property(TARGET ZoO_server PROPERTY C_STANDARD_REQUIRED ON) +find_package(Threads) +target_link_libraries(ZoO_server ${CMAKE_THREAD_LIBS_INIT}) ## OPTION HANDLING ############################################################# # TODO @@ -1,68 +1,49 @@ -# zero-of-one -(Yet another) Markov chain based IRC reply bot. +# zero-of-one-server +Server for Zero of One, a K-order Markov chain reply bot. + +## WARNING +This is an alpha version, both the software and the protocol are subject to +changes. There is currently no stable version. + +## Description +This is a multi-threaded K-order Markov chain reply bot. It uses its own +text-based protocol over UNIX sockets as only interface, meaning that it cannot +directly connect to IRC/Discord/Twitch/etc servers. Instead, it relies on +gateway applications to be the actual clients. This allows the same Zero of One +server to talk to any number of gateways (e.g. you can use the same instance of +the bot on both IRC and Discord). Moreover, it can be used while loading input +files, as those are simply considered to be gateways. + +Following the UNIX philosophy, it allows for any number of filter programs to be +set up between any number of gateway and the server, as long as they use the +protocol. + +This particular implementation of a Zero of One server keeps all its knowledge +in RAM and stores learned inputs into a text file. + +Learning multiple times the exact same input is supported, it strengthens the +links between the involved words. ## Dependencies - POSIX compliant OS. - C compiler (with C99 support). - CMake. -## Quick setup guide -``` -$ git clone https://github.com/nsensfel/zero-of-one.git -$ cd zero-of-one -$ mkdir build -$ cd build -$ cmake .. -$ make -$ ./zero_of_one -h -Usage: ./zero_of_one [option_1 option_2 ...] NICKNAME [ALIAS_1 ALIAS_2 ...] -NICKNAME is used as the IRC nickname value. -If NICKNAME or any ALIAS is found in an event, the program will reply. +## How to use? +The ZoO protocol uses text based exchanges over UNIX sockets. If you were to +use this server for an IRC bot, you'd have to at least get a Zero of One to IRC +gateway, then connect it to the server. You may also want to add filter programs +between the gateway and the server. -Available options: - [--data-filename | -df] FILENAME - Learn content from FILENAME before connecting. - Default: ./memory.txt. - [--new-data-filename | -ndf] FILENAME - Store new data learned in FILENAME. - Default: value of the --data-filename param. - [--irc-server-addr | -isa] IRC_SERVER_ADDR - Connect to this server address. - Default: irc.foonetic.net. - [--irc-server-port | -isp] IRC_SERVER_PORT - Connect to this server port. - Default: 6667. - [--irc-server-channel | -isc] IRC_SERVER_CHANNEL - Connect to this server's channel. - Default: #theborghivemind. - [--irc-username | -iu] USERNAME - Connect using this as 'username' (shown in WHOIS). - Default: zeroofone. - [--irc-realname | -ir] REALNAME - Connect using this as 'realname' (shown in WHOIS). - Default: Zero of One (bot). - [--reply-rate | -rr] REPLY_RATE - Chance to reply to an event (integer, range [0, 100]). - Default: 8. +### Starting the server. +### Adding filters. +### Adding a gateway. +### Loading data. +The recommended method of loading previously learned data into the server is to +use the Zero of One CLI gateway: +```$ cat my_storage_file | sed -e 's/^/?RL /' | ./ZoO_cli SERVER_SOCKER_NAME +``` +To load new data, it would be preferable to go through the filters instead, +and to also request the storing of the newly learned lines: +```$ cat my_new_file | sed -e 's/^/?RLS /' | ./ZoO_cli FILTER_SOCKER_NAME ``` -Note that if the NICKNAME has a uppercase character, it will never be matched by -any inputs, as all inputs are converted to lowercase. A simple solution is to -have a lowercase version of NICKNAME in the ALIAS list. - -## Main Objectives -- POSIX compliance. -- Paranoia levels of error checking. -- Low RAM usage. -- Giggles. - -## Behavior -- Replies when it reads messages containing a word matching its NICKNAME or one - of its ALIASes. -- Replies to messages with a REPLY_RATE percent chance. The word used to - construct the reply is the less known one. -- Reacts to user joining in. If the username is in enough samples, it is used - to construct the greeting, otherwise a random word is selected as starting - point. -- Repetition is taken into account and augments the strength of the links. -- Some punctuation symbols are identified (e.g. "example." will be understood - as "example" followed by "."). diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c index e7d1cd9..ec6e098 100644 --- a/src/knowledge/knowledge.c +++ b/src/knowledge/knowledge.c @@ -23,14 +23,19 @@ int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]) k->sequences_length = 0; k->sequences_sorted = (ZoO_index *) NULL; +#ifndef ZoO_RUNNING_FRAMA_C error = pthread_mutex_init(&(k->mutex), (const pthread_mutexattr_t *) NULL); +#else + k->mutex = 1; + error = 0; +#endif if (error != 0) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to initialize knowledge mutex: %s.\n", + "Unable to initialize knowledge mutex: %s.", strerror(error) ); @@ -78,7 +83,13 @@ int ZoO_knowledge_lock_access { int err; +#ifndef ZoO_RUNNING_FRAMA_C err = pthread_mutex_lock(&(k->mutex)); +#else + /*@ assert (k->mutex == 1); @*/ + k->mutex = 0; + err = 0; +#endif if (err != 0) { @@ -103,7 +114,13 @@ void ZoO_knowledge_unlock_access { int err; +#ifndef ZoO_RUNNING_FRAMA_C err = pthread_mutex_unlock(&(k->mutex)); +#else + /*@ assert (k->mutex == 0); @*/ + k->mutex = 1; + err = 0; +#endif if (err != 0) { diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h index 876dbe4..9d028ab 100644 --- a/src/knowledge/knowledge.h +++ b/src/knowledge/knowledge.h @@ -8,18 +8,67 @@ #include "knowledge_types.h" +/*@ + requires \valid(k); + requires \separated(k, io); + +// Do not use if lock is already yours. + requires (k->mutex == 1); + +// Returns zero on success, -1 on failure. + assigns \result; + ensures ((\result == 0) || (\result == -1)); + +// On success, lock is acquired. + ensures ((\result == 0) ==> (k->mutex == 0)); + +// Changes the status of the lock. + assigns (k->mutex); +@*/ int ZoO_knowledge_lock_access ( struct ZoO_knowledge k [const restrict static 1], FILE io [const restrict static 1] ); +/*@ + requires \valid(k); + requires \separated(k, io); + +// Do not use if lock is not yours. + requires (k->mutex == 0); + +// Lock is released. + ensures (k->mutex == 1); + +// Changes the status of the lock. + assigns (k->mutex); +@*/ void ZoO_knowledge_unlock_access ( struct ZoO_knowledge k [const restrict static 1], FILE io [const restrict static 1] ); +/*@ + requires (\block_length(k) >= 1); + +// Returns zero on success, -1 on failure. + assigns \result; + ensures ((\result == 0) || (\result == -1)); + +// On success, all fields of {*k} are set. + ensures ((\result == 0) ==> \valid(k)); + +// On success, the two reserved words are learnt. + ensures ((\result == 0) ==> (k->words_length == 2)); + +// On success, the mutex is initialized and unlocked. + ensures ((\result == 0) ==> (k->mutex == 1)); + +// At least some fields of k are set in any case. + assigns (*k); +@*/ int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]); void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1]); diff --git a/src/knowledge/knowledge_finalize.c b/src/knowledge/knowledge_finalize.c index 37548d4..cec0ff6 100644 --- a/src/knowledge/knowledge_finalize.c +++ b/src/knowledge/knowledge_finalize.c @@ -2,6 +2,9 @@ #include "knowledge.h" +/*@ + requires \valid(sd); +@*/ static void knowledge_sequence_data_finalize ( struct ZoO_knowledge_sequence_data sd [const restrict static 1] @@ -27,7 +30,6 @@ static void knowledge_sequence_collection_finalize { ZoO_index i; - for (i = 0; i < c->sequences_ref_length; ++i) { knowledge_sequence_data_finalize(c->sequences_ref + i); @@ -36,12 +38,14 @@ static void knowledge_sequence_collection_finalize if (c->sequences_ref != (struct ZoO_knowledge_sequence_data *) NULL) { free((void *) c->sequences_ref); + c->sequences_ref = (struct ZoO_knowledge_sequence_data *) NULL; } if (c->sequences_ref_sorted != (ZoO_index *) NULL) { free((void *) c->sequences_ref_sorted); + c->sequences_ref_sorted = (ZoO_index *) NULL; } diff --git a/src/knowledge/knowledge_get_random_sequence.c b/src/knowledge/knowledge_get_random_sequence.c index 92800a4..39fbd50 100644 --- a/src/knowledge/knowledge_get_random_sequence.c +++ b/src/knowledge/knowledge_get_random_sequence.c @@ -68,8 +68,13 @@ int ZoO_knowledge_copy_random_swt_sequence ) < 0 ) { - /* TODO: Err message. */ - + ZoO_S_PROG_ERROR + ( + io, + "Knowledge inconsistency; there are no acceptable markov sequences " + "linked to a word that has been picked as being an acceptable pillar." + ) + ; return -1; } diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c index 54357c0..ce38b06 100644 --- a/src/knowledge/knowledge_learn_markov_sequence.c +++ b/src/knowledge/knowledge_learn_markov_sequence.c @@ -50,7 +50,7 @@ static int reallocate_sequences_list ZoO_S_ERROR ( io, - "Unable to store the size of the sequences list, as it would overflow" + "Unable to store the size of the sequences list, as it would overflow " "size_t variables." ); @@ -93,8 +93,8 @@ static int reallocate_sequences_sorted_list ZoO_S_ERROR ( io, - "Unable to store the size of the sorted sequences list, as it would" - " overflow size_t variables." + "Unable to store the size of the sorted sequences list, as it would " + "overflow size_t variables." ); return -1; @@ -112,8 +112,8 @@ static int reallocate_sequences_sorted_list ZoO_S_ERROR ( io, - "Unable to allocate the memory required for the new sorted sequences" - " list." + "Unable to allocate the memory required for the new sorted sequences " + "list." ); return -1; diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c index 605af8e..8af3ef0 100644 --- a/src/knowledge/knowledge_learn_word.c +++ b/src/knowledge/knowledge_learn_word.c @@ -87,7 +87,7 @@ static int reallocate_words_list ZoO_S_ERROR ( io, - "Unable to store the size of the words list, as it would overflow" + "Unable to store the size of the words list, as it would overflow " "size_t variables." ); @@ -134,8 +134,8 @@ static int reallocate_words_sorted_list { ZoO_S_ERROR ( - "Unable to store the size of the sorted words list, as it would" - " overflow size_t variables." + "Unable to store the size of the sorted words list, as it would " + "overflow size_t variables." ); return -1; diff --git a/src/knowledge/knowledge_types.h b/src/knowledge/knowledge_types.h index 53330ce..a314927 100644 --- a/src/knowledge/knowledge_types.h +++ b/src/knowledge/knowledge_types.h @@ -44,15 +44,19 @@ struct ZoO_knowledge_word struct ZoO_knowledge { +#ifndef ZoO_RUNNING_FRAMA_C + pthread_mutex_t mutex; +#else + int mutex; +#endif + struct ZoO_knowledge_word * words; ZoO_index words_length; ZoO_index * words_sorted; + ZoO_index ** sequences; ZoO_index sequences_length; ZoO_index * sequences_sorted; -#ifndef ZoO_RUNNING_FRAMA_C - pthread_mutex_t mutex; -#endif }; #endif diff --git a/src/parameters/parameters.c b/src/parameters/parameters.c index 0e38e5e..0f01381 100644 --- a/src/parameters/parameters.c +++ b/src/parameters/parameters.c @@ -4,6 +4,8 @@ #include "../core/index.h" +#include "../error/error.h" + #include "parameters.h" static int parse_markov_order @@ -26,12 +28,12 @@ static int parse_markov_order || (input < 1) ) { - fprintf + ZoO_FATAL ( stderr, - "[F] Invalid or value for parameter 'MARKOV_ORDER', accepted " + "Invalid or value for parameter 'MARKOV_ORDER', accepted " "range is " - "[1, %lli] (integer).\n", + "[1, %lli] (integer).", (long long int) ZoO_INDEX_MAX ); diff --git a/src/pervasive.h b/src/pervasive.h index d7b213f..fccc75b 100644 --- a/src/pervasive.h +++ b/src/pervasive.h @@ -6,9 +6,11 @@ #define ZoO_SERVER_VERSION 1 #define ZoO_PROTOCOL_VERSION 1 -//#define ZoO_RUNNING_FRAMA_C 1 +#ifdef __FRAMAC__ + #define ZoO_RUNNING_FRAMA_C 1 +#endif -#define ZoO_DEBUG_ALL 1 +#define ZoO_DEBUG_ALL 0 #ifndef ZoO_DEBUG_ALL #define ZoO_DEBUG_ALL 0 diff --git a/src/sequence/sequence_append.c b/src/sequence/sequence_append.c index 81dfa99..d69db51 100644 --- a/src/sequence/sequence_append.c +++ b/src/sequence/sequence_append.c @@ -75,8 +75,8 @@ static int increment_required_capacity ZoO_S_ERROR ( io, - "Sequence capacity increment aborted, as the new size would not fit" - " in a size_t variable." + "Sequence capacity increment aborted, as the new size would not fit " + "in a size_t variable." ); #endif diff --git a/src/sequence/sequence_creation.c b/src/sequence/sequence_creation.c index 9556693..06c391f 100644 --- a/src/sequence/sequence_creation.c +++ b/src/sequence/sequence_creation.c @@ -260,7 +260,13 @@ static int extend_right { (void) ZoO_knowledge_unlock_access(k, io); - /* TODO: Err message. */ + ZoO_S_PROG_ERROR + ( + io, + "Knowledge consistency error: generated markov sequence could not be " + "found." + ); + return -1; } @@ -281,7 +287,12 @@ static int extend_right { (void) ZoO_knowledge_unlock_access(k, io); - /* TODO: Err message. */ + ZoO_S_PROG_ERROR + ( + io, + "Knowledge consistency error: generated markov sequence had no known " + "targets." + ); return -1; } @@ -468,6 +479,8 @@ static int initialize_sequence return -1; } + (void) ZoO_knowledge_unlock_access(k, io); + if (ZoO_DEBUG_SEQUENCE_CREATION_INIT) { ZoO_index i; @@ -485,8 +498,6 @@ static int initialize_sequence } } - (void) ZoO_knowledge_unlock_access(k, io); - return 0; } diff --git a/src/sequence/sequence_to_string.c b/src/sequence/sequence_to_string.c index 919ef0b..8666204 100644 --- a/src/sequence/sequence_to_string.c +++ b/src/sequence/sequence_to_string.c @@ -69,8 +69,8 @@ static int increment_required_capacity ZoO_S_ERROR ( io, - "String capacity increment aborted, as the new capacity would not" - " fit in a ZoO_index variable." + "String capacity increment aborted, as the new capacity would not " + "fit in a ZoO_index variable." ); return -1; @@ -85,8 +85,8 @@ static int increment_required_capacity ZoO_S_ERROR ( io, - "String capacity increment aborted, as the new size would not fit" - " in a size_t variable." + "String capacity increment aborted, as the new size would not fit " + "in a size_t variable." ); return -2; diff --git a/src/server/server.c b/src/server/server.c index edd1bd7..30c6bff 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -29,6 +29,7 @@ int ZoO_server_main switch (ZoO_server_wait_for_event(&server)) { case 0: /* Timed out or signal'd. */ + ZoO_S_DEBUG(stderr, 1, "Timed out..."); ZoO_server_handle_joining_threads(&server); retries = 0; @@ -36,6 +37,7 @@ int ZoO_server_main break; case 1: /* New client attempted connection. */ + ZoO_S_DEBUG(stderr, 1, "New connection."); ZoO_server_handle_joining_threads(&server); (void) ZoO_server_handle_new_connection(&server); @@ -54,6 +56,15 @@ int ZoO_server_main } break; + + default: + ZoO_S_PROG_ERROR + ( + stderr, + "Unexpected wait_for_event return value." + ); + + break; } } diff --git a/src/server/server_create_socket.c b/src/server/server_create_socket.c index 77e55b7..d7b17b6 100644 --- a/src/server/server_create_socket.c +++ b/src/server/server_create_socket.c @@ -7,6 +7,8 @@ #include <sys/socket.h> #include <sys/un.h> +#include "../error/error.h" + #include "server.h" static int create_socket (int result [const restrict static 1]) @@ -18,10 +20,10 @@ static int create_socket (int result [const restrict static 1]) if (*result == -1) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to create server socket: %s.\n", + "Unable to create server socket: %s.", strerror(errno) ); @@ -64,10 +66,10 @@ static int bind_socket ) != 0 ) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to bind server socket to %s: %s.\n", + "Unable to bind server socket to %s: %s.", socket_name, strerror(errno) ); @@ -91,10 +93,10 @@ static int set_socket_to_unblocking (const int socket) if (current_flags == -1) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to get server socket properties: %s.\n", + "Unable to get server socket properties: %s.", strerror(errno) ); @@ -109,10 +111,10 @@ static int set_socket_to_unblocking (const int socket) if (current_flags == -1) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to set server socket properties: %s.\n", + "Unable to set server socket properties: %s.", strerror(errno) ); @@ -132,10 +134,10 @@ static int set_socket_as_listener (const int socket) if (listen(socket, ZoO_SERVER_SOCKET_LISTEN_BACKLOG) != 0) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to set server socket properties: %s.\n", + "Unable to set server socket properties: %s.", strerror(errno) ); diff --git a/src/server/server_initialize.c b/src/server/server_initialize.c index e0632a4..c4800cd 100644 --- a/src/server/server_initialize.c +++ b/src/server/server_initialize.c @@ -28,10 +28,10 @@ static int initialize_worker_collection if (error != 0) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to initialize worker collection's mutex: %s.\n", + "Unable to initialize worker collection's mutex: %s.", strerror(error) ); @@ -48,10 +48,10 @@ static int initialize_worker_collection if (error != 0) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to initialize worker collection's barrier: %s.\n", + "[F] Unable to initialize worker collection's barrier: %s.", strerror(error) ); diff --git a/src/server/server_joining_threads.c b/src/server/server_joining_threads.c index 48b5ac6..6db056c 100644 --- a/src/server/server_joining_threads.c +++ b/src/server/server_joining_threads.c @@ -24,6 +24,8 @@ void ZoO_server_handle_joining_threads { if (server->workers.threads[i].state == ZoO_SERVER_JOINING_THREAD) { + ZoO_DEBUG(stderr, 1, "Joining thread %u", i); + pthread_join(server->workers.threads[i].posix_id, (void **) NULL); server->workers.threads[i].state = ZoO_SERVER_NO_THREAD; diff --git a/src/server/server_new_connection.c b/src/server/server_new_connection.c index 46b38fc..c29b896 100644 --- a/src/server/server_new_connection.c +++ b/src/server/server_new_connection.c @@ -25,10 +25,10 @@ static int get_new_socket (struct ZoO_server server [const restrict static 1]) if (server->thread_params.socket == -1) { - fprintf + ZoO_ERROR ( stderr, - "[E] Unable to accept on the server's socket: %s.\n", + "Unable to accept on the server's socket: %s.", strerror(errno) ); @@ -71,11 +71,10 @@ static int get_new_thread (struct ZoO_server server [const restrict static 1]) ) ) { - fprintf + ZoO_S_ERROR ( stderr, - "[E] Maximum number of concurrent threads attained, unable to add" - " more.\n" + "Maximum number of concurrent threads attained, unable to add more." ); pthread_mutex_unlock(&(server->workers.mutex)); @@ -98,10 +97,10 @@ static int get_new_thread (struct ZoO_server server [const restrict static 1]) if (new_threads == ((struct ZoO_server_thread_data *) NULL)) { - fprintf + ZoO_S_ERROR ( stderr, - "[E] Reallocation of the threads' data list failed.\n" + "Reallocation of the threads' data list failed." ); pthread_mutex_unlock(&(server->workers.mutex)); @@ -134,10 +133,10 @@ static int spawn_thread (struct ZoO_server server [const restrict static 1]) if (error != 0) { - fprintf + ZoO_ERROR ( stderr, - "[E] Unable to spawn thread: %s.\n", + "Unable to spawn thread: %s.", strerror(error) ); diff --git a/src/server/server_types.h b/src/server/server_types.h index a6fb875..29f9bbb 100644 --- a/src/server/server_types.h +++ b/src/server/server_types.h @@ -18,7 +18,7 @@ #define ZoO_SERVER_MAX_RETRIES 10 #define ZoO_SERVER_BUFFER_SIZE 0 -#define ZoO_SERVER_SOCKET_ACCEPT_TIMEOUT_SEC 60 +#define ZoO_SERVER_SOCKET_ACCEPT_TIMEOUT_SEC 5 #define ZoO_SERVER_SOCKET_LISTEN_BACKLOG 5 enum ZoO_server_thread_state diff --git a/src/server/server_wait_for_event.c b/src/server/server_wait_for_event.c index b229e6b..015592a 100644 --- a/src/server/server_wait_for_event.c +++ b/src/server/server_wait_for_event.c @@ -4,6 +4,8 @@ #include <stdio.h> #include <string.h> +#include "../error/error.h" + #include "server.h" int ZoO_server_wait_for_event @@ -13,21 +15,28 @@ int ZoO_server_wait_for_event { int ready_fds; const int old_errno = errno; + fd_set ready_to_read; + + ready_to_read = server->socket.as_a_set; /* call to select may alter timeout */ memset((void *) &(server->socket.timeout), 0, sizeof(struct timeval)); server->socket.timeout.tv_sec = ZoO_SERVER_SOCKET_ACCEPT_TIMEOUT_SEC; + errno = 0; + ready_fds = select ( (server->socket.file_descriptor + 1), - &(server->socket.as_a_set), + &ready_to_read, (fd_set *) NULL, (fd_set *) NULL, &(server->socket.timeout) ); + ZoO_DEBUG(stderr, 1, "SELECT returned: %i, errno is %i.", ready_fds, errno); + if (errno == EINTR) { ready_fds = 0; @@ -35,10 +44,10 @@ int ZoO_server_wait_for_event if (ready_fds == -1) { - fprintf + ZoO_FATAL ( stderr, - "[F] Unable to wait on server socket: %s.\n", + "Unable to wait on server socket: %s.", strerror(errno) ); diff --git a/src/server/server_worker.c b/src/server/server_worker.c index c96f92e..e1118e8 100644 --- a/src/server/server_worker.c +++ b/src/server/server_worker.c @@ -1,6 +1,7 @@ #include <stdlib.h> #include <string.h> #include <stdio.h> +#include <errno.h> #include "server.h" @@ -10,6 +11,8 @@ static int initialize void * input ) { + const int old_errno = errno; + memcpy ( (void *) &(worker->params), @@ -29,12 +32,24 @@ static int initialize worker->socket_as_file = fdopen(worker->params.socket, "w+"); + errno = 0; + if (worker->socket_as_file == (FILE *) NULL) { - /* TODO: error message? */ + ZoO_ERROR + ( + stderr, + "Unable to open client socket as a file stream: %s.", + strerror(errno) + ); + + errno = old_errno; + return -1; } + errno = old_errno; + return 0; } diff --git a/src/server/server_worker_handle_request.c b/src/server/server_worker_handle_request.c index a1450df..09d08f4 100644 --- a/src/server/server_worker_handle_request.c +++ b/src/server/server_worker_handle_request.c @@ -42,14 +42,14 @@ static int load_reply ) < 0 ) { - ZoO_S_ERROR(worker->socket_as_file, "Could not find rarest word."); - ZoO_knowledge_unlock_access ( worker->params.knowledge, worker->socket_as_file ); + ZoO_S_ERROR(worker->socket_as_file, "Could not find rarest word."); + return -1; } diff --git a/src/server/server_worker_receive.c b/src/server/server_worker_receive.c index 10944a9..5b4edc3 100644 --- a/src/server/server_worker_receive.c +++ b/src/server/server_worker_receive.c @@ -1,3 +1,9 @@ +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include "../error/error.h" + #include "server.h" int ZoO_server_worker_receive @@ -5,8 +11,11 @@ int ZoO_server_worker_receive struct ZoO_server_worker worker [const restrict static 1] ) { + const int old_errno = errno; ssize_t received; + errno = 0; + received = getline ( @@ -17,11 +26,20 @@ int ZoO_server_worker_receive if (received == -1) { - /* TODO: error message? */ + ZoO_ERROR + ( + stderr, + "Thread could not receive from socket: %s.", + strerror(errno) + ); + + errno = old_errno; return -1; } + errno = old_errno; + worker->buffer_length = (size_t) received; return 0; diff --git a/src/server/server_worker_send.c b/src/server/server_worker_send.c index 6ba6d67..4662fa9 100644 --- a/src/server/server_worker_send.c +++ b/src/server/server_worker_send.c @@ -1,4 +1,7 @@ -#include "../pervasive.h" +#include <stdio.h> +#include <string.h> + +#include "../error/error.h" #include "server.h" @@ -11,9 +14,24 @@ int ZoO_server_worker_send_confirm_protocol_version err = fprintf(worker->socket_as_file, "!CPV 1\n"); - if (err <= 0) + if (err == 0) + { + ZoO_S_ERROR + ( + stderr, + "Thread could not write anything to socket." + ); + + return -1; + } + else if (err < 0) { - /* TODO: error message? */ + ZoO_ERROR + ( + stderr, + "Thread could not write to socket: %s.", + strerror(err) + ); return -1; } @@ -30,9 +48,24 @@ int ZoO_server_worker_send_positive err = fprintf(worker->socket_as_file, "!P\n"); - if (err <= 0) + if (err == 0) + { + ZoO_S_ERROR + ( + stderr, + "Thread could not write anything to socket." + ); + + return -1; + } + else if (err < 0) { - /* TODO: error message? */ + ZoO_ERROR + ( + stderr, + "Thread could not write to socket: %s.", + strerror(err) + ); return -1; } @@ -49,9 +82,24 @@ int ZoO_server_worker_send_negative err = fprintf(worker->socket_as_file, "!N\n"); - if (err <= 0) + if (err == 0) + { + ZoO_S_ERROR + ( + stderr, + "Thread could not write anything to socket." + ); + + return -1; + } + else if (err < 0) { - /* TODO: error message? */ + ZoO_ERROR + ( + stderr, + "Thread could not write to socket: %s.", + strerror(err) + ); return -1; } @@ -66,16 +114,30 @@ int ZoO_server_worker_send_generated_reply { int err; - /* TODO */ err = fputs ( "!GR ", worker->socket_as_file ); - if (err <= 0) + if (err == 0) + { + ZoO_S_ERROR + ( + stderr, + "Thread could not write anything to socket." + ); + + return -1; + } + else if (err < 0) { - /* TODO: error message? */ + ZoO_ERROR + ( + stderr, + "Thread could not write to socket: %s.", + strerror(err) + ); return -1; } @@ -89,15 +151,41 @@ int ZoO_server_worker_send_generated_reply worker->socket_as_file ); + if (err == 0) + { + ZoO_S_ERROR + ( + stderr, + "Thread could not write anything to socket." + ); + + return -1; + } + else if (err < 0) + { + ZoO_ERROR + ( + stderr, + "Thread could not write to socket: %s.", + strerror(err) + ); + + return -1; + } + err = fputs ( "\n", worker->socket_as_file ); - if (err <= 0) + if (err == 0) { - /* TODO: error message? */ + ZoO_S_ERROR + ( + stderr, + "Thread could not write anything to socket." + ); return -1; } |


