| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-02-09 20:03:33 +0100 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-02-09 20:03:33 +0100 |
| commit | 7af295b2ec22f06b24079bf895ac97079f64b6d7 (patch) | |
| tree | 84a554fc2c169956e3ee975152332c39f6c3615a | |
| parent | 9ca43c73ba29d6b42cd771f1567074418c883c3e (diff) | |
It's starting to "properly" reply...
The ACSL coverage is far behind though.
37 files changed, 1148 insertions, 387 deletions
diff --git a/doc/protocol.txt b/doc/protocol.txt index f29885c..bceaa97 100644 --- a/doc/protocol.txt +++ b/doc/protocol.txt @@ -14,14 +14,13 @@ allowed to contain the character '\n'. ######## Request Protocol Version ############################################## ########## Syntax "<TAG>": "?RPV" -"<CONTENT>": list of comma separated unsigned integers. +"<CONTENT>": list of comma separated unsigned integers, in a ascending order. ########## Semantic Upon reception of one such message, one should start using the latest Zero of One protocol version listed in <CONTENT> that one can. If none are -supported, communication sockets should be closed (error messages may be sent -prior to closing the socket). Otherwise, a Confirm Protocol Version message -should be sent. +supported, a Negative reply is sent. Otherwise, a Confirm Protocol Version +message should be sent. ######## Request Learn ######################################################### ########## Syntax diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 25cacc2..33a89cf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ add_subdirectory(core) +add_subdirectory(error) add_subdirectory(knowledge) add_subdirectory(parameters) -add_subdirectory(pipe) add_subdirectory(sequence) add_subdirectory(server) add_subdirectory(storage) diff --git a/src/core/char.c b/src/core/char.c index 819cd57..a6b3c8d 100644 --- a/src/core/char.c +++ b/src/core/char.c @@ -18,9 +18,30 @@ int ZoO_word_cmp ( const ZoO_char word_a [const static 1], const size_t word_a_size, - const ZoO_char word_b [const static 1] + const ZoO_char word_b [const static 1], + const size_t word_b_size ) { - return strncmp((const char *) word_a, (const char *) word_b, word_a_size); + int result; + size_t min_size; + + if (word_a_size < word_b_size) + { + result = + strncmp((const char *) word_a, (const char *) word_b, word_a_size); + + return (result == 0) ? -1 : result; + } + else if (word_b_size < word_a_size) + { + result = + strncmp((const char *) word_a, (const char *) word_b, word_b_size); + + return (result == 0) ? 1 : result; + } + else + { + return strncmp((const char *) word_a, (const char *) word_b, word_a_size); + } } diff --git a/src/core/char.h b/src/core/char.h index 7039563..69606a0 100644 --- a/src/core/char.h +++ b/src/core/char.h @@ -13,7 +13,8 @@ int ZoO_word_cmp ( const ZoO_char word_a [const static 1], const size_t word_a_size, - const ZoO_char word_b [const static 1] + const ZoO_char word_b [const static 1], + const size_t word_b_size ); /* diff --git a/src/core/state_types.h b/src/core/state_types.h deleted file mode 100644 index 89c814e..0000000 --- a/src/core/state_types.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef _ZoO_CORE_STATE_TYPES_H_ -#define _ZoO_CORE_STATE_TYPES_H_ - -#include "../io/parameters_types.h" -#include "../io/network_types.h" - -#include "knowledge_types.h" - -struct ZoO_state -{ - struct ZoO_parameters param; - struct ZoO_knowledge knowledge; - struct ZoO_network network; -}; - -#endif diff --git a/src/pipe/CMakeLists.txt b/src/error/CMakeLists.txt index fa07534..fa07534 100644 --- a/src/pipe/CMakeLists.txt +++ b/src/error/CMakeLists.txt diff --git a/src/pipe/pipe.h b/src/error/error.h index 4616050..9a7fe04 100644 --- a/src/pipe/pipe.h +++ b/src/error/error.h @@ -1,12 +1,10 @@ -#ifndef _ZoO_PIPE_PIPE_H_ -#define _ZoO_PIPE_PIPE_H_ +#ifndef _ZoO_ERROR_ERROR_H_ +#define _ZoO_ERROR_ERROR_H_ #include <stdio.h> #include "../pervasive.h" -#include "pipe_types.h" - #ifndef ZoO_DEBUG_PROGRAM_FLOW #define ZoO_DEBUG_PROGRAM_FLOW (0 || ZoO_DEBUG_ALL) #endif @@ -33,13 +31,13 @@ #define ZoO_ENABLE_FATAL_ERROR_OUTPUT 1 #ifdef ZoO_ENABLE_ERROR_LOCATION - #define ZoO_LOCATION "[" __FILE__ "][" ZoO_TO_STRING(__LINE__) "]" + #define ZoO_LOCATION " [" __FILE__ "][" ZoO_TO_STRING(__LINE__) "]" #else #define ZoO_LOCATION "" #endif -#define ZoO_PRINT_STDERR(pipe, symbol, str, ...)\ - fprintf(pipe->out, "[" symbol "] " ZoO_LOCATION " " str "\n", __VA_ARGS__); +#define ZoO_PRINT_STDERR(io, symbol, str, ...)\ + fprintf(io, "[" symbol "]" ZoO_LOCATION " " str "\n", __VA_ARGS__); /* * Given that we use preprocessor contants as flags, we can expect the compilers @@ -47,99 +45,99 @@ * allowing many debug options. */ -#define ZoO_DEBUG(pipe, flag, str, ...)\ +#define ZoO_DEBUG(io, flag, str, ...)\ ZoO_ISOLATE\ (\ if (flag)\ {\ - ZoO_PRINT_STDERR(pipe, "D", str, __VA_ARGS__);\ + ZoO_PRINT_STDERR(io, "D", str, __VA_ARGS__);\ }\ ) -#define ZoO_WARNING(pipe, str, ...)\ +#define ZoO_WARNING(io, str, ...)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_WARNINGS_OUTPUT)\ {\ - ZoO_PRINT_STDERR(pipe, "W", str, __VA_ARGS__);\ + ZoO_PRINT_STDERR(io, "W", str, __VA_ARGS__);\ }\ ) -#define ZoO_ERROR(pipe, str, ...)\ +#define ZoO_ERROR(io, str, ...)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT)\ {\ - ZoO_PRINT_STDERR(pipe, "E", str, __VA_ARGS__);\ + ZoO_PRINT_STDERR(io, "E", str, __VA_ARGS__);\ }\ ) -#define ZoO_PROG_ERROR(pipe, str, ...)\ +#define ZoO_PROG_ERROR(io, str, ...)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\ {\ - ZoO_PRINT_STDERR(pipe, "P", str, __VA_ARGS__);\ + ZoO_PRINT_STDERR(io, "P", str, __VA_ARGS__);\ }\ ) -#define ZoO_FATAL(pipe, str, ...)\ +#define ZoO_FATAL(io, str, ...)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_FATAL_ERROR_OUTPUT)\ {\ - ZoO_PRINT_STDERR(pipe, "F", str, __VA_ARGS__);\ + ZoO_PRINT_STDERR(io, "F", str, __VA_ARGS__);\ }\ ) /* For outputs without dynamic content (static). ******************************/ -#define ZoO_PRINT_S_STDERR(pipe, symbol, str)\ - fprintf(pipe->out, "[" symbol "] " ZoO_LOCATION " " str "\n"); +#define ZoO_PRINT_S_STDERR(io, symbol, str)\ + fprintf(io, "[" symbol "]" ZoO_LOCATION " " str "\n"); -#define ZoO_S_DEBUG(pipe, flag, str)\ +#define ZoO_S_DEBUG(io, flag, str)\ ZoO_ISOLATE\ (\ if (flag)\ {\ - ZoO_PRINT_S_STDERR(pipe, "D", str);\ + ZoO_PRINT_S_STDERR(io, "D", str);\ }\ ) -#define ZoO_S_WARNING(pipe, str)\ +#define ZoO_S_WARNING(io, str)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_WARNINGS_OUTPUT)\ {\ - ZoO_PRINT_S_STDERR(pipe, "W", str);\ + ZoO_PRINT_S_STDERR(io, "W", str);\ }\ ) -#define ZoO_S_ERROR(pipe, str)\ +#define ZoO_S_ERROR(io, str)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_RUNTIME_ERRORS_OUTPUT)\ {\ - ZoO_PRINT_S_STDERR(pipe, "E", str);\ + ZoO_PRINT_S_STDERR(io, "E", str);\ }\ ) -#define ZoO_S_PROG_ERROR(pipe, str)\ +#define ZoO_S_PROG_ERROR(io, str)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_PROGRAMMING_ERRORS_OUTPUT)\ {\ - ZoO_PRINT_S_STDERR(pipe, "P", str);\ + ZoO_PRINT_S_STDERR(io, "P", str);\ }\ ) -#define ZoO_S_FATAL(pipe, str)\ +#define ZoO_S_FATAL(io, str)\ ZoO_ISOLATE\ (\ if (ZoO_ENABLE_FATAL_ERROR_OUTPUT)\ {\ - ZoO_PRINT_S_STDERR(pipe, "F", str);\ + ZoO_PRINT_S_STDERR(io, "F", str);\ }\ ) #endif diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c index 5ce6917..e7d1cd9 100644 --- a/src/knowledge/knowledge.c +++ b/src/knowledge/knowledge.c @@ -3,7 +3,7 @@ #include <stdio.h> #include <stdint.h> /* defines SIZE_MAX */ -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -13,6 +13,7 @@ int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]) { int error; + ZoO_index reserved_word_id; k->words = (struct ZoO_knowledge_word *) NULL; k->words_length = 0; @@ -36,25 +37,83 @@ int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]) return -1; } + if + ( + ( + ZoO_knowledge_learn_word + ( + k, + "[SoS]", + 5, + &reserved_word_id, + stderr + ) < 0 + ) + || + ( + ZoO_knowledge_learn_word + ( + k, + "[EoS]", + 5, + &reserved_word_id, + stderr + ) < 0 + ) + ) + { + ZoO_S_FATAL(stderr, "Unable to learn reserved words."); + + return -1; + } + return 0; } int ZoO_knowledge_lock_access ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { - return pthread_mutex_lock(&(k->mutex)); + int err; + + err = pthread_mutex_lock(&(k->mutex)); + + if (err != 0) + { + ZoO_ERROR + ( + io, + "Unable to get exclusive access to knowledge: %s", + strerror(err) + ); + + return -1; + } + + return 0; } void ZoO_knowledge_unlock_access ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { - pthread_mutex_unlock(&(k->mutex)); + int err; + + err = pthread_mutex_unlock(&(k->mutex)); + + if (err != 0) + { + ZoO_ERROR + ( + io, + "Unable to release exclusive access to knowledge: %s", + strerror(err) + ); + } } void ZoO_knowledge_get_word @@ -68,3 +127,37 @@ void ZoO_knowledge_get_word *word = k->words[word_ref].word; *word_length = k->words[word_ref].word_length; } + +int ZoO_knowledge_rarest_word +( + const struct ZoO_knowledge k [const static 1], + const ZoO_index sequence [const restrict static 1], + const size_t sequence_length, + ZoO_index word_id [const restrict static 1] +) +{ + ZoO_index current_max_score; + size_t i; + int success; + + current_max_score = ZoO_INDEX_MAX; + + success = -1; + + for (i = 0; i < sequence_length; ++i) + { + if + ( + (k->words[sequence[i]].occurrences <= current_max_score) + /* Otherwise we might just have learned it, or it must not be used. */ + && (k->words[sequence[i]].occurrences > 1) + ) + { + current_max_score = k->words[sequence[i]].occurrences; + *word_id = sequence[i]; + success = 0; + } + } + + return success; +} diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h index e97d84e..876dbe4 100644 --- a/src/knowledge/knowledge.h +++ b/src/knowledge/knowledge.h @@ -4,20 +4,20 @@ #include "../core/char_types.h" #include "../core/index_types.h" -#include "../pipe/pipe_types.h" +#include "../error/error.h" #include "knowledge_types.h" int ZoO_knowledge_lock_access ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); void ZoO_knowledge_unlock_access ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_knowledge_initialize (struct ZoO_knowledge k [const restrict static 1]); @@ -40,7 +40,7 @@ int ZoO_knowledge_learn_word const ZoO_char word [const restrict static 1], const size_t word_length, ZoO_index result [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_knowledge_learn_sequence @@ -49,7 +49,7 @@ int ZoO_knowledge_learn_sequence const ZoO_index sequence [const restrict static 1], const size_t sequence_length, const ZoO_index markov_order, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_knowledge_learn_markov_sequence @@ -58,7 +58,7 @@ int ZoO_knowledge_learn_markov_sequence const ZoO_index sequence [const restrict static 1], const ZoO_index markov_order, /* Pre (> markov_order 1) */ ZoO_index sequence_id [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); void ZoO_knowledge_get_word @@ -95,6 +95,14 @@ int ZoO_knowledge_find_sequence ZoO_index sequence_id [const restrict static 1] ); +int ZoO_knowledge_rarest_word +( + const struct ZoO_knowledge k [const static 1], + const ZoO_index sequence [const restrict static 1], + const size_t sequence_length, + ZoO_index word_id [const restrict static 1] +); + int ZoO_knowledge_find_markov_sequence ( const ZoO_index sequence_id, @@ -131,7 +139,7 @@ int ZoO_knowledge_copy_random_swt_sequence ZoO_index sequence [const restrict static 1], const ZoO_index word_id, const ZoO_index markov_order, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_knowledge_strengthen_swt @@ -140,7 +148,7 @@ int ZoO_knowledge_strengthen_swt const ZoO_index sequence_id, const ZoO_index word_id, const ZoO_index target_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_knowledge_strengthen_tws @@ -149,7 +157,7 @@ int ZoO_knowledge_strengthen_tws const ZoO_index target_id, const ZoO_index word_id, const ZoO_index sequence_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); /* @@ -162,7 +170,7 @@ int ZoO_knowledge_weaken_swt const ZoO_index sequence_id, const ZoO_index word_id, const ZoO_index target_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_knowledge_weaken_tws @@ -171,7 +179,7 @@ int ZoO_knowledge_weaken_tws const ZoO_index target_id, const ZoO_index word_id, const ZoO_index sequence_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); */ #endif diff --git a/src/knowledge/knowledge_finalize.c b/src/knowledge/knowledge_finalize.c index 6c249f0..37548d4 100644 --- a/src/knowledge/knowledge_finalize.c +++ b/src/knowledge/knowledge_finalize.c @@ -2,7 +2,6 @@ #include "knowledge.h" - static void knowledge_sequence_data_finalize ( struct ZoO_knowledge_sequence_data sd [const restrict static 1] diff --git a/src/knowledge/knowledge_get_random_sequence.c b/src/knowledge/knowledge_get_random_sequence.c index 9055d31..92800a4 100644 --- a/src/knowledge/knowledge_get_random_sequence.c +++ b/src/knowledge/knowledge_get_random_sequence.c @@ -2,9 +2,10 @@ #include "../core/char.h" #include "../core/index.h" + #include "../sequence/sequence.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -52,7 +53,7 @@ int ZoO_knowledge_copy_random_swt_sequence ZoO_index sequence [const restrict static 1], const ZoO_index word_id, const ZoO_index markov_order, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sequence_id; diff --git a/src/knowledge/knowledge_get_random_target.c b/src/knowledge/knowledge_get_random_target.c index d9bf8a9..99fbc9b 100644 --- a/src/knowledge/knowledge_get_random_target.c +++ b/src/knowledge/knowledge_get_random_target.c @@ -2,9 +2,10 @@ #include "../core/char.h" #include "../core/index.h" -#include "../sequence/sequence.h" -#include "../pipe/pipe.h" +#include "../error/error.h" + +#include "../sequence/sequence.h" #include "knowledge.h" diff --git a/src/knowledge/knowledge_learn_markov_sequence.c b/src/knowledge/knowledge_learn_markov_sequence.c index 35cc123..54357c0 100644 --- a/src/knowledge/knowledge_learn_markov_sequence.c +++ b/src/knowledge/knowledge_learn_markov_sequence.c @@ -4,7 +4,7 @@ #include "../sequence/sequence.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -40,12 +40,12 @@ static void set_nth_sequence static int reallocate_sequences_list ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index ** new_sequences; - if ((SIZE_MAX / sizeof(ZoO_index *)) > (size_t) k->sequences_length) + if ((SIZE_MAX / sizeof(ZoO_index *)) < (size_t) k->sequences_length) { ZoO_S_ERROR ( @@ -83,12 +83,12 @@ static int reallocate_sequences_list static int reallocate_sequences_sorted_list ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index * new_sequences_sorted; - if ((SIZE_MAX / sizeof(ZoO_index)) > (size_t) k->sequences_length) + if ((SIZE_MAX / sizeof(ZoO_index)) < (size_t) k->sequences_length) { ZoO_S_ERROR ( @@ -104,7 +104,7 @@ static int reallocate_sequences_sorted_list (ZoO_index *) realloc ( (void *) k->sequences_sorted, - ((size_t) k->sequences_length) * sizeof(ZoO_index) + (((size_t) k->sequences_length) * sizeof(ZoO_index)) ); if (new_sequences_sorted == (ZoO_index *) NULL) @@ -144,7 +144,7 @@ static ZoO_index * copy_sequence ( const ZoO_index base [const restrict static 1], const ZoO_index destination_length, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index i, diff; @@ -189,7 +189,7 @@ static int add_sequence const ZoO_index markov_order, /* Pre (> markov_order 1) */ const ZoO_index sequence_id, const ZoO_index sorted_sequence_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index * stored_sequence; @@ -213,6 +213,30 @@ static int add_sequence return -1; } + if (ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE) + { + ZoO_index i; + + ZoO_S_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE, + "Learning new sequence." + ); + + for (i = 0; i < (markov_order - 1); ++i) + { + ZoO_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE, + "markov_sequence[%u]: %u", + i, + stored_sequence[i] + ); + } + } + k->sequences_length += 1; if (reallocate_sequences_list(k, io) < 0) @@ -271,16 +295,15 @@ int ZoO_knowledge_find_sequence current_min = 0; current_max -= 1; - for (;;) + while (current_min <= current_max) { i = (current_min + ((current_max - current_min) / 2)); cmp = ZoO_sequence_cmp ( - k->sequences[k->sequences_sorted[i]], - markov_sequence_length, sequence, + k->sequences[k->sequences_sorted[i]], markov_sequence_length ); @@ -297,9 +320,9 @@ int ZoO_knowledge_find_sequence } else if (cmp < 0) { - if ((current_min > current_max) || (i == 0)) + if ((current_min >= current_max) || (i == 0)) { - *sequence_id = i; + *sequence_id = current_min; return -1; } @@ -313,6 +336,10 @@ int ZoO_knowledge_find_sequence return 0; } } + + *sequence_id = current_min; + + return -1; } /******************************************************************************/ @@ -325,11 +352,35 @@ int ZoO_knowledge_learn_markov_sequence const ZoO_index sequence [const restrict static 1], const ZoO_index markov_order, /* Pre (> markov_order 1) */ ZoO_index sequence_id [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sorted_id; + if (ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE) + { + ZoO_index i; + + ZoO_S_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE, + "Studying markov sequence..." + ); + + for (i = 0; i < (markov_order - 1); ++i) + { + ZoO_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE, + "markov_sequence[%u]: %u", + i, + sequence[i] + ); + } + } + if ( ZoO_knowledge_find_sequence @@ -341,6 +392,14 @@ int ZoO_knowledge_learn_markov_sequence ) == 0 ) { + ZoO_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE, + "Markov sequence is known. ID: %u", + *sequence_id + ); + return 0; } diff --git a/src/knowledge/knowledge_learn_sequence.c b/src/knowledge/knowledge_learn_sequence.c index 6a666bd..b127bfb 100644 --- a/src/knowledge/knowledge_learn_sequence.c +++ b/src/knowledge/knowledge_learn_sequence.c @@ -4,7 +4,7 @@ #include "../sequence/sequence.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -26,8 +26,6 @@ static void parse_swt_sequence for (j = 0; j < buffer_length; ++j) { - index_offset = (buffer_length - j); - if (index >= index_offset) { buffer[j] = sequence[index - index_offset]; @@ -36,6 +34,8 @@ static void parse_swt_sequence { buffer[j] = ZoO_START_OF_SEQUENCE_ID; } + + --index_offset; } } @@ -48,7 +48,7 @@ static int add_swt_sequence const ZoO_index markov_order, ZoO_index buffer [const restrict static 1], const ZoO_index buffer_length, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sequence_id; @@ -64,7 +64,7 @@ static int add_swt_sequence (buffer_length + 1), &sequence_id, io - ) + ) < 0 ) { return -1; @@ -110,15 +110,14 @@ static void parse_tws_sequence { size_t j; size_t index_offset; - const size_t remaining_items = (sequence_length - index); for (j = 0; j < buffer_length; ++j) { - index_offset = (j + 1); + index_offset = (j + 1) + index; - if (remaining_items > index_offset) + if (sequence_length > index_offset) { - buffer[j] = sequence[index + index_offset]; + buffer[j] = sequence[index_offset]; } else { @@ -136,7 +135,7 @@ static int add_tws_sequence const ZoO_index markov_order, ZoO_index buffer [const restrict static 1], const ZoO_index buffer_length, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sequence_id; @@ -152,7 +151,7 @@ static int add_tws_sequence (buffer_length + 1), &sequence_id, io - ) + ) < 0 ) { return -1; @@ -193,7 +192,7 @@ int ZoO_knowledge_learn_sequence const ZoO_index sequence [const restrict static 1], const size_t sequence_length, const ZoO_index markov_order, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index * buffer; @@ -220,30 +219,43 @@ int ZoO_knowledge_learn_sequence for (i = 0; i < sequence_length; ++i) { - /* TODO: handle failure. */ - add_tws_sequence + if ( - k, - sequence, - i, - sequence_length, - markov_order, - buffer, - buffer_length, - io - ); + add_swt_sequence + ( + k, + sequence, + i, + sequence_length, + markov_order, + buffer, + buffer_length, + io + ) < 0 + ) + { + return -1; + } - add_swt_sequence + /* TODO: handle failure. */ + if ( - k, - sequence, - i, - sequence_length, - markov_order, - buffer, - buffer_length, - io - ); + add_tws_sequence + ( + k, + sequence, + i, + sequence_length, + markov_order, + buffer, + buffer_length, + io + ) < 0 + ) + { + return -1; + } + k->words[sequence[i]].occurrences += 1; } diff --git a/src/knowledge/knowledge_learn_word.c b/src/knowledge/knowledge_learn_word.c index 8430bc0..605af8e 100644 --- a/src/knowledge/knowledge_learn_word.c +++ b/src/knowledge/knowledge_learn_word.c @@ -2,7 +2,7 @@ #include <string.h> #include <stdint.h> /* defines SIZE_MAX */ -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -40,7 +40,7 @@ static ZoO_char * copy_word ( const ZoO_char original [const restrict static 1], const ZoO_index original_length, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_char * result; @@ -67,13 +67,13 @@ static ZoO_char * copy_word (((size_t) original_length) * sizeof(ZoO_char)) ); - return 0; + return result; } static int reallocate_words_list ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { struct ZoO_knowledge_word * new_words; @@ -81,7 +81,7 @@ static int reallocate_words_list if ( (SIZE_MAX / sizeof(struct ZoO_knowledge_word)) - > (size_t) k->words_length + < (size_t) k->words_length ) { ZoO_S_ERROR @@ -120,7 +120,7 @@ static int reallocate_words_list static int reallocate_words_sorted_list ( struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index * new_words_sorted; @@ -130,7 +130,7 @@ static int reallocate_words_sorted_list * whose size is bigger than a ZoO_index. * */ /* - if ((SIZE_MAX / sizeof(ZoO_index)) > k->words_length) + if ((SIZE_MAX / sizeof(ZoO_index)) < k->words_length) { ZoO_S_ERROR ( @@ -197,7 +197,7 @@ static int add_word const ZoO_index word_length, const ZoO_index word_id, const ZoO_index sorted_word_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_char * stored_word; @@ -244,7 +244,7 @@ static int add_word set_nth_word(k, sorted_word_id, word_id); - return -1; + return 0; } /******************************************************************************/ @@ -257,7 +257,7 @@ int ZoO_knowledge_learn_word const ZoO_char word [const restrict static 1], const size_t word_length, ZoO_index word_id [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sorted_id; @@ -280,11 +280,30 @@ int ZoO_knowledge_learn_word ) == 0 ) { + ZoO_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_WORD, + "Word of size %u is already known (id: %u).", + (ZoO_index) word_length, + *word_id + ); + return 0; } sorted_id = *word_id; *word_id = k->words_length; + ZoO_DEBUG + ( + io, + ZoO_DEBUG_KNOWLEDGE_LEARN_WORD, + "Learning new word of size %u (id: %u, sorted_id: %u).", + (ZoO_index) word_length, + *word_id, + sorted_id + ); + return add_word(k, word, (ZoO_index) word_length, *word_id, sorted_id, io); } diff --git a/src/knowledge/knowledge_search.c b/src/knowledge/knowledge_search.c index cc1934e..1163f61 100644 --- a/src/knowledge/knowledge_search.c +++ b/src/knowledge/knowledge_search.c @@ -4,7 +4,7 @@ #include "../core/index.h" #include "../sequence/sequence.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -36,11 +36,18 @@ int ZoO_knowledge_find_word_id current_min = 0; current_max -= 1; - for (;;) + while (current_min <= current_max) { i = (current_min + ((current_max - current_min) / 2)); - cmp = ZoO_word_cmp(word, word_length, k->words[k->words_sorted[i]].word); + cmp = + ZoO_word_cmp + ( + word, + word_length, + k->words[k->words_sorted[i]].word, + k->words[k->words_sorted[i]].word_length + ); if (cmp > 0) { @@ -55,7 +62,7 @@ int ZoO_knowledge_find_word_id } else if (cmp < 0) { - if ((current_min > current_max) || (i == 0)) + if ((current_min >= current_max) || (i == 0)) { *result = current_min; @@ -71,6 +78,10 @@ int ZoO_knowledge_find_word_id return 0; } } + + *result = current_min; + + return -1; } int ZoO_knowledge_find_markov_sequence @@ -94,7 +105,7 @@ int ZoO_knowledge_find_markov_sequence current_min = 0; current_max = (sc->sequences_ref_length - 1); - for (;;) + while (current_min <= current_max) { i = (current_min + ((current_max - current_min) / 2)); @@ -118,7 +129,7 @@ int ZoO_knowledge_find_markov_sequence } else if (cmp < 0) { - if ((current_min > current_max) || (i == 0)) + if ((current_min >= current_max) || (i == 0)) { *result = current_min; @@ -134,6 +145,10 @@ int ZoO_knowledge_find_markov_sequence return 0; } } + + *result = current_min; + + return -1; } int ZoO_knowledge_find_sequence_target @@ -157,7 +172,7 @@ int ZoO_knowledge_find_sequence_target current_min = 0; current_max = (sd->targets_length - 1); - for (;;) + while (current_min <= current_max) { i = (current_min + ((current_max - current_min) / 2)); @@ -176,7 +191,7 @@ int ZoO_knowledge_find_sequence_target } else if (cmp < 0) { - if ((current_min > current_max) || (i == 0)) + if ((current_min >= current_max) || (i == 0)) { *result = current_min; @@ -192,4 +207,8 @@ int ZoO_knowledge_find_sequence_target return 0; } } + + *result = current_min; + + return -1; } diff --git a/src/knowledge/knowledge_swt_tws_modifications.c b/src/knowledge/knowledge_swt_tws_modifications.c index 40e2e3b..1a8048d 100644 --- a/src/knowledge/knowledge_swt_tws_modifications.c +++ b/src/knowledge/knowledge_swt_tws_modifications.c @@ -2,7 +2,7 @@ #include "../core/index.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "knowledge.h" @@ -12,7 +12,7 @@ static int add_target const ZoO_index target_id, const ZoO_index s_index, const ZoO_index t_index, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { struct ZoO_knowledge_target * new_p; @@ -68,7 +68,7 @@ static int add_sequence struct ZoO_knowledge_sequence_collection sc [const restrict static 1], const ZoO_index sequence_id, ZoO_index s_index [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { struct ZoO_knowledge_sequence_data * new_p; @@ -149,7 +149,7 @@ static int add_sequence sc->sequences_ref[*s_index].targets = (struct ZoO_knowledge_target *) NULL; sc->sequences_ref[*s_index].targets_length = 0; - return -1; + return 0; } int ZoO_knowledge_strengthen_swt @@ -158,7 +158,7 @@ int ZoO_knowledge_strengthen_swt const ZoO_index sequence_id, const ZoO_index word_id, const ZoO_index target_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index s_index, t_index; @@ -248,7 +248,7 @@ int ZoO_knowledge_strengthen_tws const ZoO_index target_id, const ZoO_index word_id, const ZoO_index sequence_id, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index s_index, t_index; @@ -278,7 +278,6 @@ int ZoO_knowledge_strengthen_tws } } - if ( ZoO_knowledge_find_sequence_target @@ -330,5 +329,5 @@ int ZoO_knowledge_strengthen_tws k->words[word_id].tws.sequences_ref[s_index].occurrences += 1; k->words[word_id].tws.sequences_ref[s_index].targets[t_index].occurrences += 1; - return -1; + return 0; } diff --git a/src/pervasive.h b/src/pervasive.h index 6677c7e..d7b213f 100644 --- a/src/pervasive.h +++ b/src/pervasive.h @@ -14,6 +14,24 @@ #define ZoO_DEBUG_ALL 0 #endif +#define ZoO_DEBUG_KNOWLEDGE (0 | ZoO_DEBUG_ALL) +#define ZoO_DEBUG_KNOWLEDGE_LEARN_SEQUENCE \ + (ZoO_DEBUG_KNOWLEDGE & (0 | ZoO_DEBUG_ALL)) +#define ZoO_DEBUG_KNOWLEDGE_LEARN_WORD \ + (ZoO_DEBUG_KNOWLEDGE & (0 | ZoO_DEBUG_ALL)) + +#define ZoO_DEBUG_SEQUENCE (0 | ZoO_DEBUG_ALL) +#define ZoO_DEBUG_SEQUENCE_FROM_STRING \ + (ZoO_DEBUG_SEQUENCE & (0 | ZoO_DEBUG_ALL)) + +#define ZoO_DEBUG_SEQUENCE_CREATION \ + (ZoO_DEBUG_SEQUENCE & (0 | ZoO_DEBUG_ALL)) + +#define ZoO_DEBUG_SEQUENCE_CREATION_INIT \ + (ZoO_DEBUG_SEQUENCE_CREATION & (0 | ZoO_DEBUG_ALL)) + +#define ZoO_DEBUG_CORE (0 | ZoO_DEBUG_ALL) + #define ZoO__TO_STRING(x) #x #define ZoO_TO_STRING(x) ZoO__TO_STRING(x) #define ZoO_ISOLATE(a) do {a} while (0) diff --git a/src/pipe/pipe_types.h b/src/pipe/pipe_types.h deleted file mode 100644 index 91cf0c3..0000000 --- a/src/pipe/pipe_types.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _ZoO_PIPE_PIPE_TYPES_H_ -#define _ZoO_PIPE_PIPE_TYPES_H_ - -#define ZoO_PIPE_NAME_LENGTH 255 - -#include <stdio.h> - -struct ZoO_pipe -{ - FILE * out; - FILE * in; -}; - -struct ZoO_pipe_names -{ - char request_pipe[ZoO_PIPE_NAME_LENGTH]; - char reply_pipe[ZoO_PIPE_NAME_LENGTH]; -}; -// const struct ZoO_pipe io [const restrict static 1] - -#endif diff --git a/src/sequence/sequence.c b/src/sequence/sequence.c index f3c3b46..78a5aad 100644 --- a/src/sequence/sequence.c +++ b/src/sequence/sequence.c @@ -5,74 +5,6 @@ #include "sequence.h" -/* - * Bypass rendundant ZoO_START_OF_SEQUENCE_ID at the start of a sequence. - */ -/*@ - requires - ( - \valid(sequence+ (0 .. sequence_length)) - || (sequence_length == 0) - ); - requires \separated(sequence, sequence_offset); - - assigns (*sequence_offset); - - ensures (*sequence_offset < sequence_length); - - ensures - ( - (*sequence_offset == 0) - || (sequence[0 .. *sequence_offset] == ZoO_START_OF_SEQUENCE_ID) - ); - - ensures - ( - (*sequence_offset == sequence_length) - || (sequence[*sequence_offset + 1] != ZoO_START_OF_SEQUENCE_ID) - ); - -@*/ -static void bypass_redundant_sos -( - const ZoO_index sequence [const restrict], - const size_t sequence_length, - size_t sequence_offset [const restrict static 1] -) -{ - ZoO_index i; - - *sequence_offset = 0; - - /*@ - loop invariant 0 <= i <= sequence_length; - loop invariant (*sequence_offset <= i); - loop invariant - ( - (*sequence_offset == 0) - || (sequence[*sequence_offset] == ZoO_START_OF_SEQUENCE_ID) - ); - loop invariant - ( - (i == 0) || (sequence[0 .. (i - 1)] == ZoO_START_OF_SEQUENCE_ID) - ); - loop assigns i, *sequence_offset; - loop variant (sequence_length - i); - @*/ - for (i = 0; i < sequence_length; ++i) - { - if (sequence[i] != ZoO_START_OF_SEQUENCE_ID) - { - return; - } - else if (sequence[i] == ZoO_START_OF_SEQUENCE_ID) - { - *sequence_offset = i; - } - } -} - - /* See "sequence.h" */ /*@ requires @@ -91,80 +23,28 @@ static void bypass_redundant_sos @*/ int ZoO_sequence_cmp ( - const ZoO_index sequence_a [const], - size_t sequence_a_length, - const ZoO_index sequence_b [const], - size_t sequence_b_length + const ZoO_index sequence_a [const restrict static 1], + const ZoO_index sequence_b [const restrict static 1], + const ZoO_index length ) { - size_t min_length, a, b; - size_t a_offset, b_offset; - size_t i; - /*@ ghost size_t actual_a_length, actual_b_length; @*/ - - bypass_redundant_sos(sequence_a, sequence_a_length, &a_offset); - bypass_redundant_sos(sequence_b, sequence_b_length, &b_offset); + ZoO_index i, a, b; - /*@ ghost actual_a_length = sequence_a_length; @*/ - /*@ ghost actual_b_length = sequence_b_length; @*/ - - /*@ assert (a_offset <= sequence_a_length); @*/ - sequence_a_length -= a_offset; - /*@ assert (b_offset <= sequence_b_length); @*/ - sequence_b_length -= b_offset; - - if (sequence_a_length < sequence_b_length) - { - min_length = sequence_a_length; - } - else + for (i = 0; i < length; ++i) { - min_length = sequence_b_length; - } - - /*@ assert (min_length <= sequence_a_length); @*/ - /*@ assert (min_length <= sequence_b_length); @*/ - - /*@ assert (min_length + a_offset <= actual_a_length); @*/ - /*@ assert (min_length + b_offset <= actual_b_length); @*/ - - /*@ - loop invariant 0 <= i <= min_length; - loop assigns i; - loop variant (min_length - i); - @*/ - for (i = 0; i < min_length; ++i) - { - /*@ assert ((i + a_offset) < actual_a_length); @*/ - a = sequence_a[i + a_offset]; - /*@ assert ((i + b_offset) < actual_b_length); @*/ - b = sequence_b[i + b_offset]; + a = sequence_a[i]; + b = sequence_b[i]; if (a < b) { return -1; } - else if (b > a) + else if (a > b) { return 1; } - else if ((a == ZoO_END_OF_SEQUENCE_ID) && (b == ZoO_END_OF_SEQUENCE_ID)) - { - return 0; - } } - if (sequence_a_length > sequence_b_length) - { - return 1; - } - else if (sequence_a_length < sequence_b_length) - { - return -1; - } - else - { - return 0; - } + return 0; } diff --git a/src/sequence/sequence.h b/src/sequence/sequence.h index 129c457..9239336 100644 --- a/src/sequence/sequence.h +++ b/src/sequence/sequence.h @@ -7,7 +7,7 @@ #include "../core/char_types.h" #include "../core/index_types.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "../knowledge/knowledge_types.h" @@ -15,16 +15,32 @@ /*@ requires \valid(sequence); - requires \valid(*sequence); + requires (\block_length(sequence) >= 1); requires \valid(sequence_capacity); + requires (\block_length(sequence) >= 1); requires \valid(io); requires (((*sequence_capacity) * sizeof(ZoO_index)) <= SIZE_MAX); requires ((sequence_required_capacity * sizeof(ZoO_index)) <= SIZE_MAX); - requires \separated(sequence, *sequence, sequence_capacity, io); + requires + \separated + ( + (sequence+ (0 .. \block_length(sequence))), + ((*sequence)+ (0 .. \block_length(*sequence))), + (sequence_capacity+ (0 ..\block_length(sequence_capacity))), + (io+ (0 ..\block_length(io))) + ); + + ensures + \separated + ( + (sequence+ (0 .. \block_length(sequence))), + ((*sequence)+ (0 .. \block_length(*sequence))), + (sequence_capacity+ (0 ..\block_length(sequence_capacity))), + (io+ (0 ..\block_length(io))) + ); - ensures \separated(sequence, *sequence, sequence_capacity, io); ensures (((*sequence_capacity) * sizeof(ZoO_index)) <= SIZE_MAX); ensures ((sequence_required_capacity * sizeof(ZoO_index)) <= SIZE_MAX); ensures \valid(sequence); @@ -70,7 +86,7 @@ int ZoO_sequence_ensure_capacity ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], const size_t sequence_required_capacity, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); int ZoO_sequence_from_undercase_string @@ -81,7 +97,7 @@ int ZoO_sequence_from_undercase_string ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); /* @@ -114,7 +130,7 @@ int ZoO_sequence_create_from ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); /*@ @@ -125,7 +141,15 @@ int ZoO_sequence_create_from requires \valid(io); requires (((*sequence_length) * sizeof(ZoO_index)) <= SIZE_MAX); requires (((*sequence_capacity) * sizeof(ZoO_index)) <= SIZE_MAX); - requires \separated(sequence, *sequence, sequence_capacity, sequence_length, io); + requires + \separated + ( + (sequence+ (0 .. \block_length(sequence))), + ((*sequence)+ (0 .. \block_length(*sequence))), + (sequence_capacity+ (0 ..\block_length(sequence_capacity))), + (sequence_length+ (0 ..\block_length(sequence_length))), + (io+ (0 ..\block_length(io))) + ); assigns (*sequence_length); assigns (*sequence[0]); @@ -138,7 +162,15 @@ int ZoO_sequence_create_from ensures \valid(io); ensures (((*sequence_length) * sizeof(ZoO_index)) <= SIZE_MAX); ensures (((*sequence_capacity) * sizeof(ZoO_index)) <= SIZE_MAX); - ensures \separated(sequence, *sequence, sequence_capacity, sequence_length, io); + ensures + \separated + ( + (sequence+ (0 .. \block_length(sequence))), + ((*sequence)+ (0 .. \block_length(*sequence))), + (sequence_capacity+ (0 ..\block_length(sequence_capacity))), + (sequence_length+ (0 ..\block_length(sequence_length))), + (io+ (0 ..\block_length(io))) + ); ensures ((\result == 0) || (\result == -1)); @@ -157,7 +189,7 @@ int ZoO_sequence_create_from ensures ((\result == 0) ==> (*sequence_length > \old(*sequence_length))); ensures ((\result == -1) ==> ((*sequence_length) == \old(*sequence_length))); - ensures ((\result == -1) ==> ((*sequence[0]) == \old(*sequence[0]))); + ensures ((\result == -1) ==> (((*sequence)[0]) == \old((*sequence)[0]))); ensures ( (\result == -1) ==> @@ -178,7 +210,7 @@ int ZoO_sequence_append_left ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); /*@ @@ -189,10 +221,18 @@ int ZoO_sequence_append_left requires \valid(io); requires (((*sequence_length) * sizeof(ZoO_index)) <= SIZE_MAX); requires (((*sequence_capacity) * sizeof(ZoO_index)) <= SIZE_MAX); - requires \separated(sequence, *sequence, sequence_capacity, sequence_length, io); + requires + \separated + ( + (sequence+ (0 .. \block_length(sequence))), + ((*sequence)+ (0 .. \block_length(*sequence))), + (sequence_capacity+ (0 ..\block_length(sequence_capacity))), + (sequence_length+ (0 ..\block_length(sequence_length))), + (io+ (0 ..\block_length(io))) + ); assigns (*sequence_length); - assigns (*sequence[0]); + assigns ((*sequence)[0]); assigns (*sequence_capacity); ensures \valid(sequence); @@ -202,7 +242,15 @@ int ZoO_sequence_append_left ensures \valid(io); ensures (((*sequence_length) * sizeof(ZoO_index)) <= SIZE_MAX); ensures (((*sequence_capacity) * sizeof(ZoO_index)) <= SIZE_MAX); - ensures \separated(sequence, *sequence, sequence_capacity, sequence_length, io); + ensures + \separated + ( + (sequence+ (0 .. \block_length(sequence))), + ((*sequence)+ (0 .. \block_length(*sequence))), + (sequence_capacity+ (0 ..\block_length(sequence_capacity))), + (sequence_length+ (0 ..\block_length(sequence_length))), + (io+ (0 ..\block_length(io))) + ); ensures ((\result == 0) || (\result == -1)); @@ -221,7 +269,7 @@ int ZoO_sequence_append_left ensures ((\result == 0) ==> (*sequence_length > \old(*sequence_length))); ensures ((\result == -1) ==> ((*sequence_length) == \old(*sequence_length))); - ensures ((\result == -1) ==> ((*sequence[0]) == \old(*sequence[0]))); + ensures ((\result == -1) ==> (((*sequence)[0]) == \old((*sequence)[0]))); ensures ( (\result == -1) ==> @@ -242,7 +290,7 @@ int ZoO_sequence_append_right const ZoO_index word_id, size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); /* @@ -265,9 +313,19 @@ int ZoO_sequence_append_right int ZoO_sequence_cmp ( const ZoO_index sequence_a [const], - size_t sequence_a_length, const ZoO_index sequence_b [const], - size_t sequence_b_length + const ZoO_index length +); + +int ZoO_sequence_to_undercase_string +( + const ZoO_index sequence [const restrict static 1], + const size_t sequence_length, + struct ZoO_knowledge k [const restrict static 1], + ZoO_char * destination [const restrict static 1], + size_t destination_capacity [const restrict static 1], + size_t destination_length [const restrict static 1], + FILE io [const restrict static 1] ); #endif diff --git a/src/sequence/sequence_append.c b/src/sequence/sequence_append.c index 604d251..81dfa99 100644 --- a/src/sequence/sequence_append.c +++ b/src/sequence/sequence_append.c @@ -5,7 +5,7 @@ #include "../core/index.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "sequence.h" @@ -16,10 +16,14 @@ /*@ requires \valid(required_capacity); requires \valid(io); - requires \separated(required_capacity, io); + requires + \separated + ( + (required_capacity+ (0 .. \block_length(required_capacity))), + (io+ (0 .. \block_length(io))) + ); assigns \result; - assigns (*required_capacity); ensures ((\result == 0) || (\result == -1)); @@ -27,7 +31,12 @@ ensures \valid(required_capacity); ensures \valid(io); - ensures \separated(required_capacity, io); + ensures + \separated + ( + (required_capacity+ (0 .. \block_length(required_capacity))), + (io+ (0 .. \block_length(io))) + ); ensures ( @@ -51,7 +60,7 @@ static int increment_required_capacity ( size_t required_capacity [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { if @@ -92,7 +101,7 @@ int ZoO_sequence_ensure_capacity ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], const size_t sequence_required_capacity, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index * new_sequence; @@ -146,7 +155,7 @@ int ZoO_sequence_append_left ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { if (increment_required_capacity(sequence_length, io) < 0) @@ -171,17 +180,17 @@ int ZoO_sequence_append_left return -1; } - /*@ assert *sequence_length >= 0; @*/ + /*@ assert (*sequence_length) >= 0; @*/ - if (*sequence_length > 1) + if ((*sequence_length) > 1) { /*@ assert(((*sequence_length) * sizeof(ZoO_index)) <= SIZE_MAX); @*/ #ifndef ZoO_RUNNING_FRAMA_C memmove ( - (void *) (*sequence + 1), - (const void *) sequence, + (void *) ((*sequence) + 1), + (const void *) (*sequence), (((*sequence_length) - 1) * sizeof(ZoO_index)) ); #endif @@ -198,7 +207,7 @@ int ZoO_sequence_append_right const ZoO_index word_id, size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { if (increment_required_capacity(sequence_length, io) < 0) @@ -206,7 +215,7 @@ int ZoO_sequence_append_right return -1; } - /*@ assert ((*sequence_length * sizeof(ZoO_index)) <= SIZE_MAX); @*/ + /*@ assert (((*sequence_length) * sizeof(ZoO_index)) <= SIZE_MAX); @*/ if ( @@ -224,9 +233,9 @@ int ZoO_sequence_append_right return -1; } - /*@ assert (*sequence_length >= 1); @*/ - (*sequence)[*sequence_length - 1] = word_id; - /*@ assert (*sequence_length >= 1); @*/ + /*@ assert ((*sequence_length) >= 1); @*/ + (*sequence)[(*sequence_length) - 1] = word_id; + /*@ assert ((*sequence_length) >= 1); @*/ return 0; } diff --git a/src/sequence/sequence_creation.c b/src/sequence/sequence_creation.c index 1f20262..9556693 100644 --- a/src/sequence/sequence_creation.c +++ b/src/sequence/sequence_creation.c @@ -5,7 +5,7 @@ #include "../core/index.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "../knowledge/knowledge.h" @@ -40,7 +40,7 @@ static int extend_left size_t sequence_length [const restrict static 1], const ZoO_index markov_order, struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sequence_id, word_id; @@ -62,7 +62,8 @@ static int extend_left { (void) ZoO_knowledge_unlock_access(k, io); - /* TODO: Err message. */ + ZoO_S_ERROR(io, "Could not find matching TWS sequence."); + return -1; } @@ -83,7 +84,7 @@ static int extend_left { (void) ZoO_knowledge_unlock_access(k, io); - /* TODO: Err message. */ + ZoO_S_ERROR(io, "Could not find matching TWS target."); return -1; } @@ -138,7 +139,7 @@ static int complete_left_part_of_sequence const ZoO_index markov_order, size_t credits [const restrict], struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { for (;;) @@ -159,7 +160,7 @@ static int complete_left_part_of_sequence ) { /* We are sure *sequence[0] is defined. */ - if (*sequence[0] == ZoO_START_OF_SEQUENCE_ID) + if ((*sequence)[0] == ZoO_START_OF_SEQUENCE_ID) { /* * We failed to add a word, but it was because none should have @@ -176,7 +177,7 @@ static int complete_left_part_of_sequence else { /* No more credits available, the sequence will have to start here. */ - *sequence[0] = ZoO_START_OF_SEQUENCE_ID; + (*sequence)[0] = ZoO_START_OF_SEQUENCE_ID; return 0; } @@ -187,7 +188,7 @@ static int complete_left_part_of_sequence } /* We are sure *sequence[0] is defined. */ - switch (*sequence[0]) + switch ((*sequence)[0]) { case ZoO_END_OF_SEQUENCE_ID: ZoO_S_WARNING @@ -196,7 +197,7 @@ static int complete_left_part_of_sequence "END OF LINE was added at the left part of an sequence." ); - *sequence[0] = ZoO_START_OF_SEQUENCE_ID; + (*sequence)[0] = ZoO_START_OF_SEQUENCE_ID; return 0; case ZoO_START_OF_SEQUENCE_ID: @@ -237,7 +238,7 @@ static int extend_right size_t sequence_length [const restrict static 1], const ZoO_index markov_order, struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index sequence_id, word_id; @@ -342,7 +343,7 @@ static int complete_right_part_of_sequence const ZoO_index markov_order, size_t credits [const restrict], struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { for (;;) @@ -363,7 +364,7 @@ static int complete_right_part_of_sequence ) { /* Safe: (> sequence_length 1) */ - if (*sequence[(*sequence_length - 1)] == ZoO_END_OF_SEQUENCE_ID) + if ((*sequence)[(*sequence_length - 1)] == ZoO_END_OF_SEQUENCE_ID) { /* * We failed to add a word, but it was because none should have @@ -380,7 +381,7 @@ static int complete_right_part_of_sequence else { /* No more credits available, we end the sequence. */ - *sequence[(*sequence_length - 1)] = ZoO_END_OF_SEQUENCE_ID; + (*sequence)[((*sequence_length) - 1)] = ZoO_END_OF_SEQUENCE_ID; return 0; } @@ -391,7 +392,7 @@ static int complete_right_part_of_sequence } /* Safe: (> sequence_length 1) */ - switch (*sequence[(*sequence_length - 1)]) + switch ((*sequence)[((*sequence_length) - 1)]) { case ZoO_START_OF_SEQUENCE_ID: ZoO_S_WARNING @@ -400,7 +401,7 @@ static int complete_right_part_of_sequence "END OF LINE was added at the right part of an sequence." ); - *sequence[(*sequence_length - 1)] = ZoO_END_OF_SEQUENCE_ID; + (*sequence)[((*sequence_length) - 1)] = ZoO_END_OF_SEQUENCE_ID; return 0; case ZoO_END_OF_SEQUENCE_ID: @@ -438,7 +439,7 @@ static int initialize_sequence const ZoO_index initial_word, const ZoO_index markov_order, struct ZoO_knowledge k [const static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { sequence[(markov_order - 1)] = initial_word; @@ -467,6 +468,23 @@ static int initialize_sequence return -1; } + if (ZoO_DEBUG_SEQUENCE_CREATION_INIT) + { + ZoO_index i; + + for (i = 0; i < markov_order; ++i) + { + ZoO_DEBUG + ( + io, + ZoO_DEBUG_SEQUENCE_CREATION_INIT, + "sequence[%u]: %u", + i, + sequence[i] + ); + } + } + (void) ZoO_knowledge_unlock_access(k, io); return 0; @@ -486,9 +504,11 @@ int ZoO_sequence_create_from ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { + size_t i; + if ( ZoO_sequence_ensure_capacity @@ -517,6 +537,8 @@ int ZoO_sequence_create_from ) < 0 ) { + ZoO_S_ERROR(io, "Failed to create start of new sequence."); + *sequence_length = 0; return -2; @@ -538,6 +560,8 @@ int ZoO_sequence_create_from ) < 0 ) { + ZoO_S_ERROR(io, "Failed to create right part of sequence."); + *sequence_length = 0; return -3; @@ -557,6 +581,8 @@ int ZoO_sequence_create_from ) < 0 ) { + ZoO_S_ERROR(io, "Failed to create left part of sequence."); + *sequence_length = 0; return -4; diff --git a/src/sequence/sequence_from_string.c b/src/sequence/sequence_from_string.c index 6acfdc2..e493bbc 100644 --- a/src/sequence/sequence_from_string.c +++ b/src/sequence/sequence_from_string.c @@ -6,7 +6,7 @@ #include "../core/char.h" #include "../core/index.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "../knowledge/knowledge.h" @@ -25,17 +25,12 @@ static int add_word_to_sequence size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], struct ZoO_knowledge k [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_index word_id; ZoO_char * stored_word; - if (word_length == 0) - { - return 0; - } - (void) ZoO_knowledge_lock_access(k, io); if @@ -105,11 +100,6 @@ static int find_word i += 1; } - if (i >= string_length) - { - return -1; - } - *word_length = (i - *word_start); return 0; @@ -128,7 +118,7 @@ int ZoO_sequence_from_undercase_string ZoO_index * sequence [const restrict static 1], size_t sequence_capacity [const restrict static 1], size_t sequence_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { size_t word_start, word_length; @@ -136,9 +126,16 @@ int ZoO_sequence_from_undercase_string i = 0; - *sequence = (ZoO_index *) NULL; *sequence_length = 0; + ZoO_DEBUG + ( + io, + ZoO_DEBUG_SEQUENCE_FROM_STRING, + "Converting string of size %lu to sequence.", + string_length + ); + if ( ZoO_sequence_append_right @@ -151,16 +148,34 @@ int ZoO_sequence_from_undercase_string ) < 0 ) { + *sequence_length = 0; + return -1; } + ZoO_S_DEBUG + ( + io, + ZoO_DEBUG_SEQUENCE_FROM_STRING, + "[SOS] added to sequence." + ); + while (i < string_length) { - if (find_word(string, i, string_length, &word_start, &word_length) < 0) + if (find_word(string, string_length, i, &word_start, &word_length) < 0) { break; } + ZoO_DEBUG + ( + io, + ZoO_DEBUG_SEQUENCE_FROM_STRING, + "Word of size %lu found in string at index %lu.", + word_length, + word_start + ); + if ( add_word_to_sequence @@ -176,8 +191,6 @@ int ZoO_sequence_from_undercase_string ) < 0 ) { - free((void *) *sequence); - *sequence = (ZoO_index *) NULL; *sequence_length = 0; return -1; @@ -198,9 +211,6 @@ int ZoO_sequence_from_undercase_string ) < 0 ) { - free((void *) *sequence); - - *sequence = (ZoO_index *) NULL; *sequence_length = 0; return -1; diff --git a/src/sequence/sequence_to_string.c b/src/sequence/sequence_to_string.c index 96bd521..919ef0b 100644 --- a/src/sequence/sequence_to_string.c +++ b/src/sequence/sequence_to_string.c @@ -6,7 +6,7 @@ #include "../core/char.h" #include "../core/index.h" -#include "../pipe/pipe.h" +#include "../error/error.h" #include "../knowledge/knowledge.h" @@ -20,7 +20,7 @@ static int ensure_string_capacity ZoO_char * string [const restrict static 1], size_t string_capacity [const restrict static 1], const size_t string_required_capacity, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { ZoO_char * new_string; @@ -61,10 +61,10 @@ static int increment_required_capacity ( size_t current_capacity [const restrict static 1], const size_t increase_factor, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { - if ((ZoO_INDEX_MAX - increase_factor) > *current_capacity) + if ((ZoO_INDEX_MAX - increase_factor) < *current_capacity) { ZoO_S_ERROR ( @@ -78,7 +78,7 @@ static int increment_required_capacity *current_capacity += increase_factor; - if ((SIZE_MAX / sizeof(ZoO_char)) > *current_capacity) + if ((SIZE_MAX / sizeof(ZoO_char)) < *current_capacity) { *current_capacity -= increase_factor; @@ -102,13 +102,18 @@ static int add_word ZoO_char * destination [const restrict static 1], size_t destination_capacity [const restrict static 1], size_t destination_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { const ZoO_char * word; ZoO_index word_size; size_t insertion_point; + if (word_id < ZoO_RESERVED_IDS_COUNT) + { + return 0; + } + (void) ZoO_knowledge_lock_access(k, io); ZoO_knowledge_get_word(k, word_id, &word, &word_size); (void) ZoO_knowledge_unlock_access(k, io); @@ -155,15 +160,17 @@ int ZoO_sequence_to_undercase_string ( const ZoO_index sequence [const restrict static 1], const size_t sequence_length, - ZoO_char * destination [const restrict static 1], struct ZoO_knowledge k [const restrict static 1], + ZoO_char * destination [const restrict static 1], size_t destination_capacity [const restrict static 1], size_t destination_length [const restrict static 1], - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { size_t i; + *destination_length = 0; + for (i = 0; i < sequence_length; ++i) { if diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index ca476fb..3b64500 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -11,6 +11,7 @@ set( ${CMAKE_CURRENT_SOURCE_DIR}/server_worker.c ${CMAKE_CURRENT_SOURCE_DIR}/server_worker_handle_request.c ${CMAKE_CURRENT_SOURCE_DIR}/server_worker_receive.c + ${CMAKE_CURRENT_SOURCE_DIR}/server_worker_send.c ) set(SRC_FILES ${SRC_FILES} PARENT_SCOPE) diff --git a/src/server/server.c b/src/server/server.c index 197faf6..edd1bd7 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -84,3 +84,4 @@ int ZoO_server_main return 0; } + diff --git a/src/server/server.h b/src/server/server.h index 90103ed..b8c0bc8 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -55,4 +55,24 @@ int ZoO_server_worker_handle_request struct ZoO_server_worker worker [const restrict static 1] ); +int ZoO_server_worker_send_confirm_protocol_version +( + struct ZoO_server_worker worker [const restrict static 1] +); + +int ZoO_server_worker_send_positive +( + struct ZoO_server_worker worker [const restrict static 1] +); + +int ZoO_server_worker_send_negative +( + struct ZoO_server_worker worker [const restrict static 1] +); + +int ZoO_server_worker_send_generated_reply +( + struct ZoO_server_worker worker [const restrict static 1] +); + #endif diff --git a/src/server/server_initialize.c b/src/server/server_initialize.c index 9213156..e0632a4 100644 --- a/src/server/server_initialize.c +++ b/src/server/server_initialize.c @@ -4,6 +4,8 @@ #include "../parameters/parameters.h" +#include "../knowledge/knowledge.h" + #include "server.h" static int initialize_worker_collection @@ -67,6 +69,7 @@ void initialize_thread_parameters { server->thread_params.thread_collection = &(server->workers); server->thread_params.server_params = params; + server->thread_params.knowledge = &(server->k); server->thread_params.socket = -1; } @@ -81,6 +84,11 @@ int ZoO_server_initialize return -1; } + if (ZoO_knowledge_initialize(&(server->k)) < 0) + { + return -1; + } + if ( ZoO_server_socket_open diff --git a/src/server/server_new_connection.c b/src/server/server_new_connection.c index 5392de5..46b38fc 100644 --- a/src/server/server_new_connection.c +++ b/src/server/server_new_connection.c @@ -89,7 +89,7 @@ static int get_new_thread (struct ZoO_server server [const restrict static 1]) new_threads = (struct ZoO_server_thread_data *) realloc ( - &(server->workers.threads), + server->workers.threads, ( sizeof(struct ZoO_server_thread_data) * ((size_t) server->workers.threads_capacity) diff --git a/src/server/server_types.h b/src/server/server_types.h index 1ba9f96..a6fb875 100644 --- a/src/server/server_types.h +++ b/src/server/server_types.h @@ -13,7 +13,7 @@ #include "../parameters/parameters_types.h" -#include "../pipe/pipe_types.h" +#include "../error/error.h" #define ZoO_SERVER_MAX_RETRIES 10 #define ZoO_SERVER_BUFFER_SIZE 0 @@ -66,10 +66,15 @@ struct ZoO_server_thread_parameters struct ZoO_server_worker { struct ZoO_server_thread_parameters params; + FILE * socket_as_file; + char * buffer; size_t buffer_capacity; size_t buffer_length; - FILE * socket_as_file; + + ZoO_index * sequence_buffer; + size_t sequence_buffer_capacity; + size_t sequence_buffer_length; }; struct ZoO_server @@ -77,6 +82,7 @@ struct ZoO_server struct ZoO_server_thread_collection workers; struct ZoO_server_socket socket; struct ZoO_server_thread_parameters thread_params; + struct ZoO_knowledge k; }; #endif diff --git a/src/server/server_worker.c b/src/server/server_worker.c index bd3b5d1..c96f92e 100644 --- a/src/server/server_worker.c +++ b/src/server/server_worker.c @@ -23,6 +23,10 @@ static int initialize worker->buffer_capacity = 0; worker->buffer_length = 0; + worker->sequence_buffer = (ZoO_index *) NULL; + worker->sequence_buffer_capacity = 0; + worker->sequence_buffer_length = 0; + worker->socket_as_file = fdopen(worker->params.socket, "w+"); if (worker->socket_as_file == (FILE *) NULL) diff --git a/src/server/server_worker_handle_request.c b/src/server/server_worker_handle_request.c index 0c97091..a1450df 100644 --- a/src/server/server_worker_handle_request.c +++ b/src/server/server_worker_handle_request.c @@ -1,11 +1,423 @@ +#include "../pervasive.h" + +#include "../error/error.h" + +#include "../sequence/sequence.h" + +#include "../knowledge/knowledge.h" + +#include "../parameters/parameters.h" + +#include "../storage/storage.h" + #include "server.h" -int ZoO_server_worker_handle_request +static int load_reply +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + ZoO_index rarest_word_id; + + if + ( + ZoO_knowledge_lock_access + ( + worker->params.knowledge, + worker->socket_as_file + ) < 0 + ) + { + return -1; + } + + if + ( + ZoO_knowledge_rarest_word + ( + worker->params.knowledge, + worker->sequence_buffer, + worker->sequence_buffer_length, + &rarest_word_id + ) < 0 + ) + { + ZoO_S_ERROR(worker->socket_as_file, "Could not find rarest word."); + + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + return -1; + } + + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + ZoO_DEBUG + ( + worker->socket_as_file, + 1, + "Word selected as pillar: %u", + rarest_word_id + ); + + if + ( + ZoO_sequence_create_from + ( + rarest_word_id, + (size_t *) NULL, + worker->params.knowledge, + ZoO_parameters_get_markov_order(worker->params.server_params), + &(worker->sequence_buffer), + &(worker->sequence_buffer_capacity), + &(worker->sequence_buffer_length), + worker->socket_as_file + ) < 0 + ) + { + ZoO_S_ERROR(worker->socket_as_file, "Could not create reply from selected word."); + + return -1; + } + + if + ( + ZoO_sequence_to_undercase_string + ( + worker->sequence_buffer, + worker->sequence_buffer_length, + worker->params.knowledge, + &(worker->buffer), + &(worker->buffer_capacity), + &(worker->buffer_length), + worker->socket_as_file + ) < 0 + ) + { + ZoO_S_ERROR(worker->socket_as_file, "Could not convert reply sequence to string."); + + return -1; + } + + return 0; +} + +static int handle_rpv ( struct ZoO_server_worker worker [const restrict static 1] ) { /* TODO */ + return -1; +} + +static int handle_rl +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + if + ( + ZoO_sequence_from_undercase_string + ( + (const ZoO_char *) (worker->buffer + 4), + (worker->buffer_length - 5), + worker->params.knowledge, + &(worker->sequence_buffer), + &(worker->sequence_buffer_capacity), + &(worker->sequence_buffer_length), + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if + ( + ZoO_knowledge_lock_access + ( + worker->params.knowledge, + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if + ( + ZoO_knowledge_learn_sequence + ( + worker->params.knowledge, + worker->sequence_buffer, + worker->sequence_buffer_length, + ZoO_parameters_get_markov_order(worker->params.server_params), + worker->socket_as_file + ) < 0 + ) + { + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + return ZoO_server_worker_send_negative(worker); + } + + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + return ZoO_server_worker_send_positive(worker); +} + +static int handle_rls +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + if + ( + ZoO_sequence_from_undercase_string + ( + (const ZoO_char *) (worker->buffer + 5), + (worker->buffer_length - 6), + worker->params.knowledge, + &(worker->sequence_buffer), + &(worker->sequence_buffer_capacity), + &(worker->sequence_buffer_length), + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if + ( + ZoO_knowledge_lock_access + ( + worker->params.knowledge, + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if + ( + ZoO_knowledge_learn_sequence + ( + worker->params.knowledge, + worker->sequence_buffer, + worker->sequence_buffer_length, + ZoO_parameters_get_markov_order(worker->params.server_params), + worker->socket_as_file + ) < 0 + ) + { + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + return ZoO_server_worker_send_negative(worker); + } + + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + if + ( + ZoO_storage_write_line + ( + ZoO_parameters_get_storage_filename(worker->params.server_params), + (worker->buffer + 5), + (worker->buffer_length - 5), /* Keep the \n this time. */ + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + return ZoO_server_worker_send_positive(worker); +} + +static int handle_rlsr +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + if + ( + ZoO_sequence_from_undercase_string + ( + (const ZoO_char *) (worker->buffer + 6), + (worker->buffer_length - 7), + worker->params.knowledge, + &(worker->sequence_buffer), + &(worker->sequence_buffer_capacity), + &(worker->sequence_buffer_length), + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if + ( + ZoO_knowledge_lock_access + ( + worker->params.knowledge, + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if + ( + ZoO_knowledge_learn_sequence + ( + worker->params.knowledge, + worker->sequence_buffer, + worker->sequence_buffer_length, + ZoO_parameters_get_markov_order(worker->params.server_params), + worker->socket_as_file + ) < 0 + ) + { + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + return ZoO_server_worker_send_negative(worker); + } + + ZoO_knowledge_unlock_access + ( + worker->params.knowledge, + worker->socket_as_file + ); + + if + ( + ZoO_storage_write_line + ( + ZoO_parameters_get_storage_filename(worker->params.server_params), + (worker->buffer + 6), + (worker->buffer_length - 6), /* Keep the \n this time. */ + worker->socket_as_file + ) < 0 + ) + { + return ZoO_server_worker_send_negative(worker); + } + + if (load_reply(worker) < 0) + { + return ZoO_server_worker_send_negative(worker); + } + + if (ZoO_server_worker_send_generated_reply(worker) < 0) + { + return -1; + } + + return ZoO_server_worker_send_positive(worker); +} + +static int handle_rr +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + if + ( + ZoO_sequence_from_undercase_string + ( + (const ZoO_char *) (worker->buffer + 4), + (worker->buffer_length - 5), + worker->params.knowledge, + &(worker->sequence_buffer), + &(worker->sequence_buffer_capacity), + &(worker->sequence_buffer_length), + worker->socket_as_file + ) < 0 + ) + { + ZoO_S_DEBUG(worker->socket_as_file, 1, "?RR failed at string to sequence."); + + return ZoO_server_worker_send_negative(worker); + } + + if (load_reply(worker) < 0) + { + ZoO_S_DEBUG(worker->socket_as_file, 1, "?RR failed at load reply."); + return ZoO_server_worker_send_negative(worker); + } + + if (ZoO_server_worker_send_generated_reply(worker) < 0) + { + return -1; + } + + return ZoO_server_worker_send_positive(worker); +} + +int ZoO_server_worker_handle_request +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + if (ZoO_IS_PREFIX("?RPV ", worker->buffer)) + { + return handle_rpv(worker); + } + else if (ZoO_IS_PREFIX("?RL ", worker->buffer)) + { + return handle_rl(worker); + } + else if (ZoO_IS_PREFIX("?RLS ", worker->buffer)) + { + return handle_rls(worker); + } + else if (ZoO_IS_PREFIX("?RLSR ", worker->buffer)) + { + return handle_rlsr(worker); + } + else if (ZoO_IS_PREFIX("?RR ", worker->buffer)) + { + return handle_rr(worker); + } + else + { + ZoO_S_ERROR(worker->socket_as_file, "Unsupported request received."); + + return ZoO_server_worker_send_negative(worker); + } + return 0; } diff --git a/src/server/server_worker_receive.c b/src/server/server_worker_receive.c index 4dc2fc7..10944a9 100644 --- a/src/server/server_worker_receive.c +++ b/src/server/server_worker_receive.c @@ -1,6 +1,5 @@ #include "server.h" - int ZoO_server_worker_receive ( struct ZoO_server_worker worker [const restrict static 1] diff --git a/src/server/server_worker_send.c b/src/server/server_worker_send.c new file mode 100644 index 0000000..6ba6d67 --- /dev/null +++ b/src/server/server_worker_send.c @@ -0,0 +1,106 @@ +#include "../pervasive.h" + +#include "server.h" + +int ZoO_server_worker_send_confirm_protocol_version +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + int err; + + err = fprintf(worker->socket_as_file, "!CPV 1\n"); + + if (err <= 0) + { + /* TODO: error message? */ + + return -1; + } + + return 0; +} + +int ZoO_server_worker_send_positive +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + int err; + + err = fprintf(worker->socket_as_file, "!P\n"); + + if (err <= 0) + { + /* TODO: error message? */ + + return -1; + } + + return 0; +} + +int ZoO_server_worker_send_negative +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + int err; + + err = fprintf(worker->socket_as_file, "!N\n"); + + if (err <= 0) + { + /* TODO: error message? */ + + return -1; + } + + return 0; +} + +int ZoO_server_worker_send_generated_reply +( + struct ZoO_server_worker worker [const restrict static 1] +) +{ + int err; + + /* TODO */ + err = fputs + ( + "!GR ", + worker->socket_as_file + ); + + if (err <= 0) + { + /* TODO: error message? */ + + return -1; + } + + err = + fwrite + ( + worker->buffer, + sizeof(ZoO_char), + worker->buffer_length, + worker->socket_as_file + ); + + err = fputs + ( + "\n", + worker->socket_as_file + ); + + if (err <= 0) + { + /* TODO: error message? */ + + return -1; + } + + return 0; +} diff --git a/src/storage/storage.c b/src/storage/storage.c index 22c5c49..40185a3 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -6,7 +6,7 @@ #include <stdint.h> /* defines SIZE_MAX */ #include <stdio.h> -#include "../pipe/pipe.h" +#include "../error/error.h" #include "storage.h" @@ -15,12 +15,17 @@ int ZoO_storage_write_line const char filename [const restrict static 1], char line [const restrict static 1], size_t const line_size, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ) { const int old_errno = errno; FILE * file; + if (filename == (const char *) NULL) + { + return 0; + } + file = fopen(filename, "a"); if (file == (FILE *) NULL) @@ -53,8 +58,7 @@ int ZoO_storage_write_line ZoO_ERROR ( io, - "Could not store line '%s' in %s.", - line, + "Could not store line in storage file %s.", filename ); diff --git a/src/storage/storage.h b/src/storage/storage.h index c287b23..9eff281 100644 --- a/src/storage/storage.h +++ b/src/storage/storage.h @@ -1,14 +1,14 @@ #ifndef _ZoO_STORAGE_STORAGE_H_ #define _ZoO_STORAGE_STORAGE_H_ -#include "../pipe/pipe_types.h" +#include <stdio.h> int ZoO_storage_write_line ( const char filename [const restrict static 1], char line [const restrict static 1], size_t const line_size, - const struct ZoO_pipe io [const restrict static 1] + FILE io [const restrict static 1] ); #endif |


