aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/command.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-05-15 15:31:33 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-05-15 15:32:17 +0200
commit7cd83e15139447156ca915ce2d9d19295c146d56 (patch)
tree277f35dcc6c59d93cf5ef0232daa525079342f97 /src/command.c
parentcommand: adjust order of parameters to handlers (diff)
downloadcimple-7cd83e15139447156ca915ce2d9d19295c146d56.tar.gz
cimple-7cd83e15139447156ca915ce2d9d19295c146d56.zip
rework server-worker communication
OK, this is a major rework. * tcp_server: connection threads are not detached anymore, the caller has to clean them up. This was done so that the server can clean up the threads cleanly. * run_queue: simple refactoring, run_queue_entry is called just run now. * server: worker threads are now killed when a run is assigned to a worker. * worker: the connection to server is no longer persistent. A worker sends "new-worker", waits for a task, closes the connection, and when it's done, sends the "complete" message and waits for a new task. This is supposed to improve resilience, since the worker-server connections don't have to be maintained while the worker is doing a CI run.
Diffstat (limited to '')
-rw-r--r--src/command.c65
1 files changed, 39 insertions, 26 deletions
diff --git a/src/command.c b/src/command.c
index 3c75e69..581b319 100644
--- a/src/command.c
+++ b/src/command.c
@@ -103,17 +103,10 @@ void cmd_dispatcher_destroy(struct cmd_dispatcher *dispatcher)
free(dispatcher);
}
-int cmd_dispatcher_handle_msg(const struct cmd_dispatcher *dispatcher, int conn_fd,
- const struct msg *request)
+int cmd_dispatcher_handle(const struct cmd_dispatcher *dispatcher, const struct msg *command,
+ struct msg **result)
{
- struct msg *response = NULL;
- int ret = 0;
-
- size_t numof_words = msg_get_length(request);
- if (numof_words == 0)
- goto unknown;
-
- const char *actual_cmd = msg_get_first_word(request);
+ const char *actual_cmd = msg_get_first_word(command);
for (size_t i = 0; i < dispatcher->numof_cmds; ++i) {
struct cmd_desc *cmd = &dispatcher->cmds[i];
@@ -121,35 +114,55 @@ int cmd_dispatcher_handle_msg(const struct cmd_dispatcher *dispatcher, int conn_
if (strcmp(cmd->name, actual_cmd))
continue;
- ret = cmd->handler(conn_fd, request, &response, dispatcher->ctx);
- goto exit;
+ return cmd->handler(command, result, dispatcher->ctx);
}
-unknown:
log_err("Received an unknown command\n");
- ret = -1;
- msg_dump(request);
- goto exit;
-
-exit:
- if (ret < 0 && !response)
- msg_error(&response);
- if (response)
- return msg_send(conn_fd, response);
- return ret;
+ msg_dump(command);
+ return -1;
}
int cmd_dispatcher_handle_conn(int conn_fd, void *_dispatcher)
{
struct cmd_dispatcher *dispatcher = (struct cmd_dispatcher *)_dispatcher;
- struct msg *request = NULL;
+ struct msg *request = NULL, *response = NULL;
int ret = 0;
+ struct cmd_conn_ctx *new_ctx = malloc(sizeof(struct cmd_conn_ctx));
+ if (!new_ctx) {
+ log_errno("malloc");
+ return -1;
+ }
+
+ new_ctx->fd = conn_fd;
+ new_ctx->arg = dispatcher->ctx;
+
ret = msg_recv(conn_fd, &request);
if (ret < 0)
- return ret;
+ goto free_ctx;
+
+ void *old_ctx = dispatcher->ctx;
+ dispatcher->ctx = new_ctx;
+ ret = cmd_dispatcher_handle(dispatcher, request, &response);
+ dispatcher->ctx = old_ctx;
+
+ if (ret < 0)
+ goto free_response;
+
+ if (response) {
+ ret = msg_send(conn_fd, response);
+ if (ret < 0)
+ goto free_response;
+ }
+
+free_response:
+ if (response)
+ msg_free(response);
- ret = cmd_dispatcher_handle_msg(dispatcher, conn_fd, request);
msg_free(request);
+
+free_ctx:
+ free(new_ctx);
+
return ret;
}