summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-02-08 15:21:19 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2017-02-08 15:21:19 +0100
commit9ca43c73ba29d6b42cd771f1567074418c883c3e (patch)
tree1e3560f218c80fe1e343a647c1d0f177d2d16b0f
parentee26b8ff850add4f83b912635a71dbde06f268d1 (diff)
Changes knowledge structure, defines protocol.
-rw-r--r--doc/protocol.txt185
-rw-r--r--src/knowledge/knowledge.c12
-rw-r--r--src/knowledge/knowledge.h5
-rw-r--r--src/main.c2
-rw-r--r--src/parameters/parameters.c28
-rw-r--r--src/parameters/parameters.h12
-rw-r--r--src/pipe/pipe.h4
-rw-r--r--src/sequence/sequence_to_string.c2
-rw-r--r--src/server/server.c4
-rw-r--r--src/server/server.h2
-rw-r--r--src/server/server_types.h10
-rw-r--r--src/server/server_worker.c31
-rw-r--r--src/server/server_worker_receive.c20
13 files changed, 258 insertions, 59 deletions
diff --git a/doc/protocol.txt b/doc/protocol.txt
index a940e69..f29885c 100644
--- a/doc/protocol.txt
+++ b/doc/protocol.txt
@@ -1,41 +1,144 @@
-Create 2 POSIX named pipes:
- - REQ_PIPE, used to send requests.
- - REPLY_PIPE, used to get replies to the requests.
-
-Open the pipes:
- - REQ_PIPE as write_only.
- - REPLY_PIPE as read_only.
-
-Open the POSIX named message queue in write_only mode.
-Send a message to this queue with the following format:
- {
- 'C',
- unsigned int requested_protocol_version;
- char req_pipe_name[255],
- char reply_pipe_name[255]
- }
-
-Read from the reply pipe name, with a timeout. When the server has handled
-your connection request, you will receive "ZoO_protocol_version X\n",
-with X a positive integer indicating the version of the protocol that will be
-used. If you do not support this protocol, proceed to destroying all the pipes,
-otherwise you are now able to send requests.
-Disconnecting is done by destroying the pipes. This goes both way: if you did
-not initiate the destroying, assume that the server went offline.
-
-Once connected, send your request on REQ_PIPE using the "CMD STRING\n" format,
-in which:
- - CMD is one of:
- - "L" -> Learn STRING, do not reply, do not add to storage.
- - "LS" -> Learn STRING, do not reply, add to storage.
- - "LSR" -> Learn STRING, reply, add to storage.
- - "R" -> Do not learn STRING, reply, do not add to storage.
- - STRING must not contain the '\n' character.
-
-The reply will come from REPLY_PIPE, using the "TYPE STRING\n" format, in which:
- - TYPE is one of:
- - "E" -> STRING contains an error message.
- - "D" -> STRING contains the requested data.
- - "C" -> All replies related to the request have been sent.
-At most one "D" reply per request, exactly one "C" reply per request, any
-number of "E" replies per request.
+################################################################################
+## Zero of One - PROTOCOL VERSION 1 ############################################
+################################################################################
+Communications are done through bi-directional sockets.
+
+All communications are text based. Messages are separated by the '\n'
+character.
+
+#### SUPPORTED MESSAGES ########################################################
+Messages use the "<TAG>[ <CONTENT>]\n" format. Neither <TAG> nor <CONTENT> are
+allowed to contain the character '\n'.
+
+###### Requests ################################################################
+######## Request Protocol Version ##############################################
+########## Syntax
+"<TAG>": "?RPV"
+"<CONTENT>": list of comma separated unsigned integers.
+
+########## 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.
+
+######## Request Learn #########################################################
+########## Syntax
+"<TAG>": "?RL"
+"<CONTENT>": Any character string (does not expect null termination).
+
+########## Semantic
+Requests the learning of <CONTENT>. Response should be either a Positive
+message, or a Negative message.
+
+######## Request Learn & Store #################################################
+########## Syntax
+"<TAG>": "?RLS"
+"<CONTENT>": Any character string (does not expect null termination).
+
+########## Semantic
+Requests the learning of <CONTENT>, followed by the storing of <CONTENT> in
+the storage file. Response should be either a Positive message, or a Negative
+message.
+
+######## Request Learn & Store & Reply #########################################
+########## Syntax
+"<TAG>": "?RLSR"
+"<CONTENT>": Any character string (does not expect null termination).
+
+########## Semantic
+Requests the learning of <CONTENT>, followed by the storing of <CONTENT> in
+the storage file, as well as the generation of a reply.
+Response should be either a Positive message preceded by a Generated Reply
+message, or a Negative message.
+
+######## Request Reply #########################################################
+########## Syntax
+"<TAG>": "?RR"
+"<CONTENT>": Any character string (does not expect null termination).
+
+########## Semantic
+Requests the generation of a reply to <CONTENT>.
+Response should be either a Positive message preceded by a Generated Reply
+message, or a Negative message.
+
+###### Replies #################################################################
+######## Confirm Protocol Version ##############################################
+########## Syntax
+"<TAG>": "!CPV"
+"<CONTENT>": unsigned integer.
+
+########## Semantic
+Indicates the Zero of One protocol version that will henceforth be used.
+
+######## Positive ##############################################################
+########## Syntax
+"<TAG>": "!P"
+"<CONTENT>": None.
+
+########## Semantic
+Previous request should be considered as being completed (no more messages
+related to it will be sent).
+
+######## Negative ##############################################################
+########## Syntax
+"<TAG>": "!N"
+"<CONTENT>": None.
+
+########## Semantic
+Previous request should be considered as having failed (no more messages related
+to it will be sent).
+
+######## Generated Reply #######################################################
+########## Syntax
+"<TAG>": "!GR"
+"<CONTENT>": Any character string (without null termination).
+
+########## Semantic
+This is a string that has been generated following a request.
+
+###### Errors ##################################################################
+######## Debug Message #########################################################
+########## Syntax
+"<TAG>": "[D]"
+"<CONTENT>": Any character string (without null termination).
+
+########## Semantic
+Debug message that has been generated during the handling of the previous
+request.
+
+######## Warning Message #######################################################
+########## Syntax
+"<TAG>": "[W]"
+"<CONTENT>": Any character string (without null termination).
+
+########## Semantic
+Warning message that has been generated during the handling of the previous
+request.
+
+######## Error Message #########################################################
+########## Syntax
+"<TAG>": "[E]"
+"<CONTENT>": Any character string (without null termination).
+
+########## Semantic
+Error message that has been generated during the handling of the previous
+request.
+
+######## Programming fault Message #############################################
+########## Syntax
+"<TAG>": "[P]"
+"<CONTENT>": Any character string (without null termination).
+
+########## Semantic
+A serious programming fault occurred during the handling of the previous
+request.
+
+######## Fatal error Message #############################################
+########## Syntax
+"<TAG>": "[F]"
+"<CONTENT>": Any character string (without null termination).
+
+########## Semantic
+A fatal error occurred during the handling of the previous request.
diff --git a/src/knowledge/knowledge.c b/src/knowledge/knowledge.c
index c9bfc2a..5ce6917 100644
--- a/src/knowledge/knowledge.c
+++ b/src/knowledge/knowledge.c
@@ -56,3 +56,15 @@ void ZoO_knowledge_unlock_access
{
pthread_mutex_unlock(&(k->mutex));
}
+
+void ZoO_knowledge_get_word
+(
+ const struct ZoO_knowledge k [const static 1],
+ const ZoO_index word_ref,
+ const ZoO_char * word [const restrict static 1],
+ ZoO_index word_length [const restrict static 1]
+)
+{
+ *word = k->words[word_ref].word;
+ *word_length = k->words[word_ref].word_length;
+}
diff --git a/src/knowledge/knowledge.h b/src/knowledge/knowledge.h
index 7f1bb42..e97d84e 100644
--- a/src/knowledge/knowledge.h
+++ b/src/knowledge/knowledge.h
@@ -61,13 +61,12 @@ int ZoO_knowledge_learn_markov_sequence
const struct ZoO_pipe io [const restrict static 1]
);
-int ZoO_knowledge_get_word
+void ZoO_knowledge_get_word
(
const struct ZoO_knowledge k [const static 1],
const ZoO_index word_ref,
const ZoO_char * word [const restrict static 1],
- ZoO_index word_size [const restrict static 1],
- const struct ZoO_pipe io [const restrict static 1]
+ ZoO_index word_length [const restrict static 1]
);
/*
diff --git a/src/main.c b/src/main.c
index a2a9330..7b9f8e7 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,7 +18,7 @@ static void print_help ()
"\nParameters:\n"
" SESSION_NAME: valid POSIX message queue filename.\n"
" MARKOV_ORDER: non-null positive integer.\n"
- " STORAGE_FILE: file in which the knowledge will be stored.",
+ " STORAGE_FILE: file in which the knowledge will be stored.\n",
ZoO_SERVER_VERSION,
ZoO_PROTOCOL_VERSION
);
diff --git a/src/parameters/parameters.c b/src/parameters/parameters.c
index 82ac9d1..0e38e5e 100644
--- a/src/parameters/parameters.c
+++ b/src/parameters/parameters.c
@@ -29,9 +29,9 @@ static int parse_markov_order
fprintf
(
stderr,
- "[F] Invalid or value for parameter 'MARKOV_ORDER', accepted"
+ "[F] Invalid or value for parameter 'MARKOV_ORDER', accepted "
"range is "
- "[1, %lli] (integer).",
+ "[1, %lli] (integer).\n",
(long long int) ZoO_INDEX_MAX
);
@@ -82,3 +82,27 @@ enum ZoO_invocation_objective ZoO_parameters_initialize
return ZoO_PRINTS_HELP;
}
}
+
+const char * ZoO_parameters_get_session_name
+(
+ const struct ZoO_parameters param [const restrict static 1]
+)
+{
+ return param->session;
+}
+
+ZoO_index ZoO_parameters_get_markov_order
+(
+ const struct ZoO_parameters param [const restrict static 1]
+)
+{
+ return param->markov_order;
+}
+
+const char * ZoO_parameters_get_storage_filename
+(
+ const struct ZoO_parameters param [const restrict static 1]
+)
+{
+ return param->storage;
+}
diff --git a/src/parameters/parameters.h b/src/parameters/parameters.h
index 7927ef7..b6d516c 100644
--- a/src/parameters/parameters.h
+++ b/src/parameters/parameters.h
@@ -3,7 +3,17 @@
#include "parameters_types.h"
-char * ZoO_parameters_get_session_name
+const char * ZoO_parameters_get_session_name
+(
+ const struct ZoO_parameters param [const restrict static 1]
+);
+
+ZoO_index ZoO_parameters_get_markov_order
+(
+ const struct ZoO_parameters param [const restrict static 1]
+);
+
+const char * ZoO_parameters_get_storage_filename
(
const struct ZoO_parameters param [const restrict static 1]
);
diff --git a/src/pipe/pipe.h b/src/pipe/pipe.h
index 9b9445c..4616050 100644
--- a/src/pipe/pipe.h
+++ b/src/pipe/pipe.h
@@ -39,7 +39,7 @@
#endif
#define ZoO_PRINT_STDERR(pipe, symbol, str, ...)\
- fprintf(pipe->out, "E [" symbol "]" ZoO_LOCATION " " str "\n", __VA_ARGS__);
+ fprintf(pipe->out, "[" symbol "] " ZoO_LOCATION " " str "\n", __VA_ARGS__);
/*
* Given that we use preprocessor contants as flags, we can expect the compilers
@@ -96,7 +96,7 @@
/* For outputs without dynamic content (static). ******************************/
#define ZoO_PRINT_S_STDERR(pipe, symbol, str)\
- fprintf(pipe->out, "E [" symbol "]" ZoO_LOCATION " " str "\n");
+ fprintf(pipe->out, "[" symbol "] " ZoO_LOCATION " " str "\n");
#define ZoO_S_DEBUG(pipe, flag, str)\
ZoO_ISOLATE\
diff --git a/src/sequence/sequence_to_string.c b/src/sequence/sequence_to_string.c
index cec3af8..96bd521 100644
--- a/src/sequence/sequence_to_string.c
+++ b/src/sequence/sequence_to_string.c
@@ -110,7 +110,7 @@ static int add_word
size_t insertion_point;
(void) ZoO_knowledge_lock_access(k, io);
- ZoO_knowledge_get_word(k, word_id, &word, &word_size, io);
+ ZoO_knowledge_get_word(k, word_id, &word, &word_size);
(void) ZoO_knowledge_unlock_access(k, io);
insertion_point = *destination_length;
diff --git a/src/server/server.c b/src/server/server.c
index 874f5eb..197faf6 100644
--- a/src/server/server.c
+++ b/src/server/server.c
@@ -26,7 +26,7 @@ int ZoO_server_main
while (ZoO_server_is_running())
{
- switch (ZoO_server_wait_for_new_event(&server))
+ switch (ZoO_server_wait_for_event(&server))
{
case 0: /* Timed out or signal'd. */
ZoO_server_handle_joining_threads(&server);
@@ -60,7 +60,7 @@ int ZoO_server_main
/* Waiting for the threads to join... */
while (server.workers.currently_running > 0)
{
- switch (ZoO_server_wait_for_new_event(&server))
+ switch (ZoO_server_wait_for_event(&server))
{
case 0: /* Timed out. */
case 1: /* New client attempted connection. */
diff --git a/src/server/server.h b/src/server/server.h
index 36ec4ce..90103ed 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -28,7 +28,7 @@ int ZoO_server_main
void ZoO_server_finalize (struct ZoO_server [const restrict static 1]);
-int ZoO_server_wait_for_new_event
+int ZoO_server_wait_for_event
(
struct ZoO_server server [const restrict static 1]
);
diff --git a/src/server/server_types.h b/src/server/server_types.h
index fb78b77..1ba9f96 100644
--- a/src/server/server_types.h
+++ b/src/server/server_types.h
@@ -7,7 +7,11 @@
#include <pthread.h>
#endif
-#include "../core/index.h"
+#include "../core/index_types.h"
+
+#include "../knowledge/knowledge_types.h"
+
+#include "../parameters/parameters_types.h"
#include "../pipe/pipe_types.h"
@@ -54,16 +58,18 @@ struct ZoO_server_thread_parameters
{
struct ZoO_server_thread_collection * thread_collection;
const struct ZoO_parameters * server_params;
+ struct ZoO_knowledge * knowledge;
ZoO_index thread_id;
int socket;
};
struct ZoO_server_worker
{
+ struct ZoO_server_thread_parameters params;
char * buffer;
size_t buffer_capacity;
size_t buffer_length;
- struct ZoO_server_thread_parameters params;
+ FILE * socket_as_file;
};
struct ZoO_server
diff --git a/src/server/server_worker.c b/src/server/server_worker.c
index f6378e0..bd3b5d1 100644
--- a/src/server/server_worker.c
+++ b/src/server/server_worker.c
@@ -1,10 +1,10 @@
-#include <signal.h>
+#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "server.h"
-static void initialize
+static int initialize
(
struct ZoO_server_worker worker [const restrict static 1],
void * input
@@ -22,6 +22,16 @@ static void initialize
worker->buffer = (char *) NULL;
worker->buffer_capacity = 0;
worker->buffer_length = 0;
+
+ worker->socket_as_file = fdopen(worker->params.socket, "w+");
+
+ if (worker->socket_as_file == (FILE *) NULL)
+ {
+ /* TODO: error message? */
+ return -1;
+ }
+
+ return 0;
}
static void finalize
@@ -29,6 +39,23 @@ static void finalize
struct ZoO_server_worker worker [const restrict static 1]
)
{
+ if (worker->socket_as_file != (FILE *) NULL)
+ {
+ fclose(worker->socket_as_file);
+
+ worker->socket_as_file = NULL;
+ }
+
+ if (worker->buffer != (char *) NULL)
+ {
+ free((void *) worker->buffer);
+
+ worker->buffer = (char *) NULL;
+ }
+
+ worker->buffer_capacity = 0;
+ worker->buffer_length = 0;
+
pthread_mutex_lock(&(worker->params.thread_collection->mutex));
worker->params.thread_collection->threads[worker->params.thread_id].state =
diff --git a/src/server/server_worker_receive.c b/src/server/server_worker_receive.c
index 6e70159..4dc2fc7 100644
--- a/src/server/server_worker_receive.c
+++ b/src/server/server_worker_receive.c
@@ -1,11 +1,29 @@
#include "server.h"
+
int ZoO_server_worker_receive
(
struct ZoO_server_worker worker [const restrict static 1]
)
{
- /* TODO */
+ ssize_t received;
+
+ received =
+ getline
+ (
+ &(worker->buffer),
+ &(worker->buffer_capacity),
+ worker->socket_as_file
+ );
+
+ if (received == -1)
+ {
+ /* TODO: error message? */
+
+ return -1;
+ }
+
+ worker->buffer_length = (size_t) received;
return 0;
}