aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-05-13 11:11:59 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-05-13 11:37:46 +0200
commit09df279e8ef9cb43fd6c5e77524b74eed46bd464 (patch)
tree99f62e56feb685a8612b8bfaaf3395386ded3b48
parentbest practices & coding style fixes (diff)
downloadcimple-09df279e8ef9cb43fd6c5e77524b74eed46bd464.tar.gz
cimple-09df279e8ef9cb43fd6c5e77524b74eed46bd464.zip
command: refactoring
Diffstat (limited to '')
-rw-r--r--src/command.c72
-rw-r--r--src/command.h21
-rw-r--r--src/server.c13
-rw-r--r--src/worker.c11
4 files changed, 57 insertions, 60 deletions
diff --git a/src/command.c b/src/command.c
index b469ef6..8e24e7f 100644
--- a/src/command.c
+++ b/src/command.c
@@ -12,35 +12,35 @@
#include <stdlib.h>
#include <string.h>
-struct command_dispatcher {
- struct command_def *defs;
- size_t numof_defs;
+struct cmd_dispatcher {
+ struct cmd_desc *cmds;
+ size_t numof_cmds;
void *ctx;
};
-static int copy_def(struct command_def *dest, const struct command_def *src)
+static int copy_cmd(struct cmd_desc *dest, const struct cmd_desc *src)
{
dest->name = strdup(src->name);
if (!dest->name) {
log_errno("strdup");
return -1;
}
- dest->processor = src->processor;
+ dest->handler = src->handler;
return 0;
}
-static void free_def(struct command_def *def)
+static void free_cmd(struct cmd_desc *desc)
{
- free(def->name);
+ free(desc->name);
}
-static int copy_defs(struct command_def *dest, const struct command_def *src, size_t numof_defs)
+static int copy_cmds(struct cmd_desc *dest, const struct cmd_desc *src, size_t numof_cmds)
{
size_t numof_copied = 0;
int ret = 0;
- for (numof_copied = 0; numof_copied < numof_defs; ++numof_copied) {
- ret = copy_def(&dest[numof_copied], &src[numof_copied]);
+ for (numof_copied = 0; numof_copied < numof_cmds; ++numof_copied) {
+ ret = copy_cmd(&dest[numof_copied], &src[numof_copied]);
if (ret < 0)
goto free;
}
@@ -49,23 +49,23 @@ static int copy_defs(struct command_def *dest, const struct command_def *src, si
free:
for (size_t i = 0; i < numof_copied; ++i)
- free_def(&dest[numof_copied]);
+ free_cmd(&dest[numof_copied]);
return -1;
}
-static void free_defs(struct command_def *defs, size_t numof_defs)
+static void free_cmds(struct cmd_desc *cmds, size_t numof_cmds)
{
- for (size_t i = 0; i < numof_defs; ++i)
- free_def(&defs[i]);
+ for (size_t i = 0; i < numof_cmds; ++i)
+ free_cmd(&cmds[i]);
}
-int command_dispatcher_create(struct command_dispatcher **_dispatcher, struct command_def *defs,
- size_t numof_defs, void *ctx)
+int cmd_dispatcher_create(struct cmd_dispatcher **_dispatcher, struct cmd_desc *cmds,
+ size_t numof_cmds, void *ctx)
{
int ret = 0;
- struct command_dispatcher *dispatcher = malloc(sizeof(struct command_dispatcher));
+ struct cmd_dispatcher *dispatcher = malloc(sizeof(struct cmd_dispatcher));
if (!dispatcher) {
log_errno("malloc");
return -1;
@@ -73,22 +73,22 @@ int command_dispatcher_create(struct command_dispatcher **_dispatcher, struct co
dispatcher->ctx = ctx;
- dispatcher->defs = malloc(sizeof(struct command_def) * numof_defs);
- if (!dispatcher->defs) {
+ dispatcher->cmds = malloc(sizeof(struct cmd_desc) * numof_cmds);
+ if (!dispatcher->cmds) {
log_errno("malloc");
goto free;
}
- dispatcher->numof_defs = numof_defs;
+ dispatcher->numof_cmds = numof_cmds;
- ret = copy_defs(dispatcher->defs, defs, numof_defs);
+ ret = copy_cmds(dispatcher->cmds, cmds, numof_cmds);
if (ret < 0)
- goto free_defs;
+ goto free_cmds;
*_dispatcher = dispatcher;
return 0;
-free_defs:
- free(dispatcher->defs);
+free_cmds:
+ free(dispatcher->cmds);
free:
free(dispatcher);
@@ -96,15 +96,15 @@ free:
return -1;
}
-void command_dispatcher_destroy(struct command_dispatcher *dispatcher)
+void cmd_dispatcher_destroy(struct cmd_dispatcher *dispatcher)
{
- free_defs(dispatcher->defs, dispatcher->numof_defs);
- free(dispatcher->defs);
+ free_cmds(dispatcher->cmds, dispatcher->numof_cmds);
+ free(dispatcher->cmds);
free(dispatcher);
}
-int command_dispatcher_msg_handler(const struct command_dispatcher *dispatcher, int conn_fd,
- const struct msg *request)
+int cmd_dispatcher_handle_msg(const struct cmd_dispatcher *dispatcher, int conn_fd,
+ const struct msg *request)
{
struct msg *response = NULL;
int ret = 0;
@@ -115,13 +115,13 @@ int command_dispatcher_msg_handler(const struct command_dispatcher *dispatcher,
const char **words = msg_get_words(request);
- for (size_t i = 0; i < dispatcher->numof_defs; ++i) {
- struct command_def *def = &dispatcher->defs[i];
+ for (size_t i = 0; i < dispatcher->numof_cmds; ++i) {
+ struct cmd_desc *cmd = &dispatcher->cmds[i];
- if (strcmp(def->name, words[0]))
+ if (strcmp(cmd->name, words[0]))
continue;
- ret = def->processor(conn_fd, request, dispatcher->ctx, &response);
+ ret = cmd->handler(conn_fd, request, dispatcher->ctx, &response);
goto exit;
}
@@ -139,9 +139,9 @@ exit:
return ret;
}
-int command_dispatcher_conn_handler(int conn_fd, void *_dispatcher)
+int cmd_dispatcher_handle_conn(int conn_fd, void *_dispatcher)
{
- struct command_dispatcher *dispatcher = (struct command_dispatcher *)_dispatcher;
+ struct cmd_dispatcher *dispatcher = (struct cmd_dispatcher *)_dispatcher;
struct msg *request = NULL;
int ret = 0;
@@ -149,7 +149,7 @@ int command_dispatcher_conn_handler(int conn_fd, void *_dispatcher)
if (ret < 0)
return ret;
- ret = command_dispatcher_msg_handler(dispatcher, conn_fd, request);
+ ret = cmd_dispatcher_handle_msg(dispatcher, conn_fd, request);
msg_free(request);
return ret;
}
diff --git a/src/command.h b/src/command.h
index b4c3b40..dc798d2 100644
--- a/src/command.h
+++ b/src/command.h
@@ -12,24 +12,23 @@
#include <stdlib.h>
-typedef int (*command_processor)(int conn_fd, const struct msg *request, void *ctx,
- struct msg **response);
+typedef int (*cmd_handler)(int conn_fd, const struct msg *request, void *ctx,
+ struct msg **response);
-struct command_def {
+struct cmd_desc {
char *name;
- command_processor processor;
+ cmd_handler handler;
};
-struct command_dispatcher;
+struct cmd_dispatcher;
-int command_dispatcher_create(struct command_dispatcher **, struct command_def *, size_t numof_defs,
- void *ctx);
-void command_dispatcher_destroy(struct command_dispatcher *);
+int cmd_dispatcher_create(struct cmd_dispatcher **, struct cmd_desc *, size_t numof_defs,
+ void *ctx);
+void cmd_dispatcher_destroy(struct cmd_dispatcher *);
-int command_dispatcher_msg_handler(const struct command_dispatcher *, int conn_fd,
- const struct msg *);
+int cmd_dispatcher_handle_msg(const struct cmd_dispatcher *, int conn_fd, const struct msg *);
/* This is supposed to be used as an argument to tcp_server_accept. */
-int command_dispatcher_conn_handler(int conn_fd, void *dispatcher);
+int cmd_dispatcher_handle_conn(int conn_fd, void *dispatcher);
#endif
diff --git a/src/server.c b/src/server.c
index 1efbbb5..db9f9c3 100644
--- a/src/server.c
+++ b/src/server.c
@@ -292,7 +292,7 @@ static int msg_ci_run_handler(UNUSED int client_fd, const struct msg *request, v
return msg_success(response);
}
-static struct command_def commands[] = {
+static struct cmd_desc commands[] = {
{CMD_NEW_WORKER, msg_new_worker_handler},
{CMD_CI_RUN, msg_ci_run_handler},
};
@@ -323,23 +323,22 @@ unlock:
int server_main(struct server *server)
{
- struct command_dispatcher *dispatcher = NULL;
+ struct cmd_dispatcher *dispatcher = NULL;
int ret = 0;
ret = signal_install_global_handler();
if (ret < 0)
return ret;
- ret = command_dispatcher_create(&dispatcher, commands,
- sizeof(commands) / sizeof(commands[0]), server);
+ ret = cmd_dispatcher_create(&dispatcher, commands, sizeof(commands) / sizeof(commands[0]),
+ server);
if (ret < 0)
return ret;
while (!global_stop_flag) {
log("Waiting for new connections\n");
- ret = tcp_server_accept(server->tcp_server, command_dispatcher_conn_handler,
- dispatcher);
+ ret = tcp_server_accept(server->tcp_server, cmd_dispatcher_handle_conn, dispatcher);
if (ret < 0) {
if (errno == EINVAL && global_stop_flag)
ret = 0;
@@ -348,7 +347,7 @@ int server_main(struct server *server)
}
dispatcher_destroy:
- command_dispatcher_destroy(dispatcher);
+ cmd_dispatcher_destroy(dispatcher);
return server_set_stopping(server);
}
diff --git a/src/worker.c b/src/worker.c
index 8c731f2..3cbe626 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -128,13 +128,13 @@ static int msg_ci_run_handler(UNUSED int conn_fd, const struct msg *request, UNU
return msg_success(response);
}
-static struct command_def commands[] = {
+static struct cmd_desc cmds[] = {
{CMD_CI_RUN, msg_ci_run_handler},
};
int worker_main(struct worker *worker, UNUSED int argc, UNUSED char *argv[])
{
- struct command_dispatcher *dispatcher = NULL;
+ struct cmd_dispatcher *dispatcher = NULL;
int ret = 0;
ret = signal_install_global_handler();
@@ -145,8 +145,7 @@ int worker_main(struct worker *worker, UNUSED int argc, UNUSED char *argv[])
if (ret < 0)
return ret;
- ret = command_dispatcher_create(&dispatcher, commands,
- sizeof(commands) / sizeof(commands[0]), worker);
+ ret = cmd_dispatcher_create(&dispatcher, cmds, sizeof(cmds) / sizeof(cmds[0]), worker);
if (ret < 0)
return ret;
@@ -162,14 +161,14 @@ int worker_main(struct worker *worker, UNUSED int argc, UNUSED char *argv[])
goto dispatcher_destroy;
}
- ret = command_dispatcher_msg_handler(dispatcher, worker->fd, request);
+ ret = cmd_dispatcher_handle_msg(dispatcher, worker->fd, request);
msg_free(request);
if (ret < 0)
goto dispatcher_destroy;
}
dispatcher_destroy:
- command_dispatcher_destroy(dispatcher);
+ cmd_dispatcher_destroy(dispatcher);
return ret;
}