nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit a8d9b5ce9b1e01d8be260d6d2df59516eb0f8473
parent 76c8c714cdf91c1841ab2a50e2bc2de5399aa820
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon, 16 Dec 2019 20:05:07 -0500

nserver: Update call_function.

* nserver/src/ncmd.c (call_function): Add arg out. Return integer.
(process): Skeleton commented out.
* nserver/src/ncmd.h (call_function): Update function declaration.
(process): New function declaration.
* nserver/tests/ncmd_tests.c
(test_call_function): Update test.

Diffstat:
nserver/src/ncmd.c | 124++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
nserver/src/ncmd.h | 5++++-
nserver/tests/ncmd_tests.c | 40+++++++++++++++++++++-------------------
3 files changed, 103 insertions(+), 66 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -156,116 +156,148 @@ int check_args(struct bstrList *cmd_parts, int argc) return -1; } -char *call_function(int func, struct bstrList *cmd_parts) +int call_function(int func, struct bstrList *cmd_parts, char *out) { - char *msg = NULL; - int msg_sz = 200; - msg = (char *) calloc(msg_sz + 1, sizeof(char)); - check_mem(msg); + check(out != NULL, "out invalid"); if (func < 0 || cmd_parts == NULL || cmd_parts->qty < 1) { - strncpy(msg, "error: args invalid\n", msg_sz); + strncpy(out, "error: args invalid\n", RSP_SIZE); - return msg; + return -1; } switch (func) { case NS_CREATE: if(check_args(cmd_parts, 2) != 0) { - strncpy(msg, "error: command invalid\n", msg_sz); - break; + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; } if (sscreate(bdata(cmd_parts->entry[1])) != 0) { - strncpy(msg, "error: create failed\n", msg_sz); - break; + strncpy(out, "error: create failed\n", RSP_SIZE); + + return -1; } - strncpy(msg, "OK\n", msg_sz); + strncpy(out, "OK\n", RSP_SIZE); break; case NS_SAMPLE: if(check_args(cmd_parts, 3) != 0) { - strncpy(msg, "error: command invalid\n", msg_sz); - break; + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; } double sample = strtod(bdata(cmd_parts->entry[2]), NULL); if (sssample(bdata(cmd_parts->entry[1]), sample) != 0) { - strncpy(msg, "error: sample failed\n", msg_sz); - break; + strncpy(out, "error: sample failed\n", RSP_SIZE); + + return -1; } - strncpy(msg, "OK\n", msg_sz); + strncpy(out, "OK\n", RSP_SIZE); break; case NS_MEAN: if(check_args(cmd_parts, 2) != 0) { - strncpy(msg, "error: command invalid\n", msg_sz); - break; + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; } double mean = ssmean(bdata(cmd_parts->entry[1])); if (mean < 0) { - strncpy(msg, "error: mean failed\n", msg_sz); - break; + strncpy(out, "error: mean failed\n", RSP_SIZE); + + return -1; } - if (sprintf(msg, "Mean: %.2f\n", mean) < 0) { - strncpy(msg, "error: mean failed\n", msg_sz); + if (sprintf(out, "Mean: %.2f\n", mean) < 0) { + strncpy(out, "error: mean failed\n", RSP_SIZE); + + return -1; } break; case NS_DUMP: if(check_args(cmd_parts, 2) != 0) { - strncpy(msg, "error: command invalid\n", msg_sz); - break; + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; } char *dump = ssdump(bdata(cmd_parts->entry[1])); if (dump == NULL) { - strncpy(msg, "error: dump failed\n", msg_sz); - break; + strncpy(out, "error: dump failed\n", RSP_SIZE); + + return -1; } + strncpy(out, dump, RSP_SIZE); - // Clean up msg. - free(msg); + // Clean up dump + free(dump); - msg = dump; break; case NS_DELETE: if(check_args(cmd_parts, 2) != 0) { - strncpy(msg, "error: command invalid\n", msg_sz); - break; + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; } if (ssdelete(bdata(cmd_parts->entry[1])) != 0) { - strncpy(msg, "error: delete failed\n", msg_sz); - break; + strncpy(out, "error: delete failed\n", RSP_SIZE); + + return -1; } - strncpy(msg, "OK\n", msg_sz); + strncpy(out, "OK\n", RSP_SIZE); break; case NS_LIST: if(check_args(cmd_parts, 1) != 0) { - strncpy(msg, "error: command invalid\n", msg_sz); - break; + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; } char *list = sslist(); if (list == NULL) { - strncpy(msg, "error: list failed\n", msg_sz); - break; + strncpy(out, "error: list failed\n", RSP_SIZE); + + return -1; } + strncpy(out, list, RSP_SIZE); - // Clean up msg. - free(msg); + // Clean up list. + free(list); - msg = list; break; default: - strncpy(msg, "error: function not found\n", msg_sz); - break; + strncpy(out, "error: function not found\n", RSP_SIZE); + + return -1; } - return msg; + return 0; error: - return NULL; + return -1; } +/* +int process(char *cmd, int sock) +{ + // split cmd into parts. + struct bstrList *parts = cmd_parts(cmd); + check(parts != NULL, "cmd_parts failed"); + check(parts->qty > 0, "bstrList qty check failed"); + + // call find_function. + + // call call_function + + // barf result back to sock + + // done + + return 0; + error: + return -1; } +*/ diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -9,6 +9,7 @@ #define CMD_MIN_SIZE 5 #define CMD_MAX_SIZE 120 +#define RSP_SIZE 200 enum FUNCTIONS { NS_CREATE, @@ -24,8 +25,10 @@ int sanitize(char *cmd); char *check_cmd(char *cmd); struct bstrList *cmd_parts(char *cmd); + int find_function(struct bstrList *cmd_parts); -char *call_function(int func, struct bstrList *cmd_parts); +int call_function(int func, struct bstrList *cmd_parts, char *out); +int process(char *cmd, int sock); #endif diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -212,72 +212,74 @@ char *test_find_function() char *test_call_function() { struct bstrList *parts = NULL; - char *msg = 0; + + char *msg = (char *) calloc(RSP_SIZE + 1, sizeof(char)); + mu_assert(msg != NULL, "msg invalid"); char *bacon = "/create bacon"; parts = cmd_parts(bacon); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_CREATE, parts); + int rc = call_function(NS_CREATE, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - free(msg); char *ham = "/create ham"; parts = cmd_parts(ham); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_CREATE, parts); + rc = call_function(NS_CREATE, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - free(msg); char *beef = "/create beef"; parts = cmd_parts(beef); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_CREATE, parts); + rc = call_function(NS_CREATE, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - free(msg); char *bacon_sample = "/sample bacon 4.2"; parts = cmd_parts(bacon_sample); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_SAMPLE, parts); + rc = call_function(NS_SAMPLE, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - free(msg); bacon_sample = "/Sample bacon 6.9"; parts = cmd_parts(bacon_sample); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_SAMPLE, parts); + rc = call_function(NS_SAMPLE, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - free(msg); char *bacon_mean = "/mean bacon"; parts = cmd_parts(bacon_mean); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_MEAN, parts); + rc = call_function(NS_MEAN, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "Mean: 5.55\n") == 0, "call function failed"); - free(msg); char *bacon_dump = "/dump bacon"; parts = cmd_parts(bacon_dump); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_DUMP, parts); + rc = call_function(NS_DUMP, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "sum: 11.100000, sumsq: 65.250000, n: 2, min: 4.200000, max: 6.900000, mean: 5.550000, stddev: 1.909188\n") == 0, "call function failed"); - free(msg); char *bacon_delete = "/delete bacon"; parts = cmd_parts(bacon_delete); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_DELETE, parts); + rc = call_function(NS_DELETE, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - free(msg); char *list = "/list"; parts = cmd_parts(list); mu_assert(parts != NULL, "cmd_parts failed"); - msg = call_function(NS_LIST, parts); + rc = call_function(NS_LIST, parts, msg); + mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "ham\nbeef\n") == 0 || strcmp(msg, "beef\nham\n") == 0, "call function failed"); - free(msg); return NULL; }