diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-11 12:47:21 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-11 12:58:07 +0200 |
commit | 04547fe158fe05f97ccf584332319da3e6b344d8 (patch) | |
tree | 904a8716d3261c53c199a967fbc651b33430db2b /src | |
parent | worker_queue: fix worker_queue_add_last (diff) | |
download | cimple-04547fe158fe05f97ccf584332319da3e6b344d8.tar.gz cimple-04547fe158fe05f97ccf584332319da3e6b344d8.zip |
msg: rework some APIs
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 11 | ||||
-rw-r--r-- | src/msg.c | 45 | ||||
-rw-r--r-- | src/msg.h | 11 | ||||
-rw-r--r-- | src/server.c | 12 | ||||
-rw-r--r-- | src/worker.c | 13 |
5 files changed, 52 insertions, 40 deletions
diff --git a/src/client.c b/src/client.c index 8cfb1d1..d7c1b58 100644 --- a/src/client.c +++ b/src/client.c @@ -39,17 +39,13 @@ void client_destroy(struct client *client) int client_main(UNUSED const struct client *client, const struct settings *settings, const char **argv) { - struct msg *request = NULL, *response = NULL; + struct msg *response = NULL; int ret = 0; - ret = msg_from_argv(&request, argv); + ret = msg_connect_and_talk_argv(settings->host, settings->port, argv, &response); if (ret < 0) return ret; - ret = msg_connect_and_communicate(settings->host, settings->port, request, &response); - if (ret < 0) - goto free_request; - if (!msg_is_success(response)) { log_err("Server failed to process the request\n"); msg_dump(response); @@ -60,8 +56,5 @@ int client_main(UNUSED const struct client *client, const struct settings *setti free_response: msg_free(response); -free_request: - msg_free(request); - return ret; } @@ -170,7 +170,7 @@ destroy_buf: return ret; } -int msg_send_from_argv(int fd, const char **argv) +int msg_send_argv(int fd, const char **argv) { struct msg *msg = NULL; int ret = 0; @@ -224,7 +224,7 @@ destroy_buf: return ret; } -int msg_communicate(int fd, const struct msg *request, struct msg **response) +int msg_talk(int fd, const struct msg *request, struct msg **response) { int ret = 0; @@ -248,8 +248,25 @@ int msg_communicate(int fd, const struct msg *request, struct msg **response) return ret; } -int msg_connect_and_communicate(const char *host, const char *port, const struct msg *request, - struct msg **response) +int msg_talk_argv(int fd, const char **argv, struct msg **response) +{ + struct msg *request = NULL; + int ret = 0; + + ret = msg_from_argv(&request, argv); + if (ret < 0) + return ret; + + ret = msg_talk(fd, request, response); + msg_free(request); + if (ret < 0) + return ret; + + return ret; +} + +int msg_connect_and_talk(const char *host, const char *port, const struct msg *request, + struct msg **response) { int fd = -1, ret = 0; @@ -257,7 +274,7 @@ int msg_connect_and_communicate(const char *host, const char *port, const struct if (fd < 0) return fd; - ret = msg_communicate(fd, request, response); + ret = msg_talk(fd, request, response); if (ret < 0) goto close; @@ -267,6 +284,24 @@ close: return ret; } +int msg_connect_and_talk_argv(const char *host, const char *port, const char **argv, + struct msg **response) +{ + struct msg *request = NULL; + int ret = 0; + + ret = msg_from_argv(&request, argv); + if (ret < 0) + return ret; + + ret = msg_connect_and_talk(host, port, request, response); + msg_free(request); + if (ret < 0) + return ret; + + return ret; +} + void msg_dump(const struct msg *msg) { log("Message[%zu]:\n", msg->argc); @@ -28,13 +28,16 @@ int msg_is_success(const struct msg *); int msg_is_error(const struct msg *); int msg_recv(int fd, struct msg **); + int msg_send(int fd, const struct msg *); +int msg_send_argv(int fd, const char **argv); -int msg_send_from_argv(int fd, const char **argv); +int msg_talk(int fd, const struct msg *, struct msg **response); +int msg_talk_argv(int fd, const char **argv, struct msg **response); -int msg_communicate(int fd, const struct msg *, struct msg **response); -int msg_connect_and_communicate(const char *host, const char *port, const struct msg *, - struct msg **); +int msg_connect_and_talk(const char *host, const char *port, const struct msg *, struct msg **); +int msg_connect_and_talk_argv(const char *host, const char *port, const char **argv, + struct msg **response); void msg_dump(const struct msg *); diff --git a/src/server.c b/src/server.c index ff5b9e1..a5319e0 100644 --- a/src/server.c +++ b/src/server.c @@ -154,17 +154,7 @@ static int server_assign_run(struct server *server) const char *argv[] = {CMD_RUN, run_get_url(run), run_get_rev(run), NULL}; - struct msg *request = NULL; - - ret = msg_from_argv(&request, argv); - if (ret < 0) { - worker_queue_add_first(&server->worker_queue, worker); - run_queue_add_first(&server->run_queue, run); - return ret; - } - - ret = msg_communicate(worker_get_fd(worker), request, NULL); - msg_free(request); + ret = msg_talk_argv(worker_get_fd(worker), argv, NULL); if (ret < 0) { /* Failed to communicate with the worker, requeue the run * and forget about the worker. */ diff --git a/src/worker.c b/src/worker.c index ab06563..d7ad310 100644 --- a/src/worker.c +++ b/src/worker.c @@ -61,22 +61,13 @@ void worker_destroy(struct worker *worker) static int worker_send_to_server(const struct settings *settings, const struct msg *request, struct msg **response) { - return msg_connect_and_communicate(settings->host, settings->port, request, response); + return msg_connect_and_talk(settings->host, settings->port, request, response); } static int worker_send_to_server_argv(const struct settings *settings, const char **argv, struct msg **response) { - struct msg *msg = NULL; - int ret = 0; - - ret = msg_from_argv(&msg, argv); - if (ret < 0) - return ret; - - ret = worker_send_to_server(settings, msg, response); - msg_free(msg); - return ret; + return msg_connect_and_talk_argv(settings->host, settings->port, argv, response); } static int worker_send_new_worker(const struct settings *settings, struct msg **task) |