aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--shared/makefile2
-rw-r--r--shared/protocol.c26
-rw-r--r--shared/protocol.h11
-rw-r--r--shared/test.c4
4 files changed, 31 insertions, 12 deletions
diff --git a/shared/makefile b/shared/makefile
index 2ca254c..2093f00 100644
--- a/shared/makefile
+++ b/shared/makefile
@@ -1,7 +1,7 @@
CC = gcc
LD = gcc
RM = rm -f
-CFLAGS = -g
+CFLAGS = -g -std=c11
LFLAGS =
TARGET = main
diff --git a/shared/protocol.c b/shared/protocol.c
index 49d1795..6e3bc73 100644
--- a/shared/protocol.c
+++ b/shared/protocol.c
@@ -46,16 +46,32 @@ void ws_protocol_parse_byte(ws_s_protocol_parser_state* state, char input) {
return;
}
+#define WS_CMD_MAP(parsed_cmd, name, code) \
+ if (strlen(parsed_cmd->argv[0]) == strlen(name) && strncmp(parsed_cmd->argv[0], name, strlen(name)) == 0) return code;
+
+static ws_e_protocol_cmd ws_protocol_get_cmd_code(ws_s_protocol_parsed_cmd* parsed_cmd) {
+ WS_CMD_MAP(parsed_cmd, "last-records", WS_PROTOCOL_CMD_LAST_RECORDS);
+
+ return WS_PROTOCOL_CMD_UNKNOWN;
+}
+
ws_s_protocol_response* ws_protocol_parse_finished(ws_s_protocol_parsed_cmd* parsed_cmd) {
ws_s_protocol_response* response = malloc(sizeof(ws_s_protocol_response));
+ response->success = WS_PROTOCOL_CMD_RETURN_ERROR;
+ response->msg = NULL;
- if (strncmp("last-records", parsed_cmd->argv[0], 12) == 0) {
- printf("last-records found!\n");
- }
+ ws_e_protocol_cmd cmd_code = ws_protocol_get_cmd_code(parsed_cmd);
+ if (cmd_code == WS_PROTOCOL_CMD_UNKNOWN) goto ws_protocol_parse_exit;
+ if (cmd_code >= WS_PROTOCOL_CMD_AMOUNT) goto ws_protocol_parse_exit;
+
+ void (*ws_protocol_res_handler)(ws_s_protocol_parsed_cmd*, ws_s_protocol_response*) =
+ g_ws_protocol_res_handlers[cmd_code];
+ if (ws_protocol_res_handler == NULL) goto ws_protocol_parse_exit;
+ (*ws_protocol_res_handler)(parsed_cmd, response);
- response->msg = ws_bin_s_alloc(50);
- strncpy((char*) response->msg->data, "hello world!\n\0", 14);
+ws_protocol_parse_exit:
+ if (response->msg == NULL) response->msg = ws_bin_s_alloc(0);
return response;
}
diff --git a/shared/protocol.h b/shared/protocol.h
index 3082af4..a19fe48 100644
--- a/shared/protocol.h
+++ b/shared/protocol.h
@@ -115,16 +115,19 @@ ws_s_bin* ws_protocol_req_last_records(unsigned int record_amount);
* command, and returns the response string
*
* @param parsed_cmd complete parsed command from ws_protocol_parse_*
- *
- * @return ws_s_bin containing response string
+ * @param response response struct with uninitialized pointer to msg
*/
-ws_s_bin* ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd);
+void ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd, ws_s_protocol_response* response);
+/** @brief cmd codes (used to call handlers) */
typedef enum {
+ WS_PROTOCOL_CMD_UNKNOWN = -1,
+
WS_PROTOCOL_CMD_LAST_RECORDS = 0,
} ws_e_protocol_cmd;
-static ws_s_bin* (*g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMOUNT])(ws_s_protocol_parsed_cmd* parsed_cmd) = {
+/** @brief response handlers, called when a command is parsed */
+static void (*g_ws_protocol_res_handlers[WS_PROTOCOL_CMD_AMOUNT])(ws_s_protocol_parsed_cmd*, ws_s_protocol_response*) = {
[WS_PROTOCOL_CMD_LAST_RECORDS] = &ws_protocol_res_last_records
};
diff --git a/shared/test.c b/shared/test.c
index 788dc94..5f2cd60 100644
--- a/shared/test.c
+++ b/shared/test.c
@@ -6,8 +6,8 @@
#include "protocol.h"
-ws_s_bin* ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd) {
- return NULL;
+void ws_protocol_res_last_records(ws_s_protocol_parsed_cmd* parsed_cmd, ws_s_protocol_response* response) {
+ printf("last-records detected!\n");
}
int main() {