Directory: | src/ |
---|---|
File: | src/client.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 50 | 62 | 80.6% |
Branches: | 17 | 30 | 56.7% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2022 Egor Tensin <egor@tensin.name> | ||
3 | * This file is part of the "cimple" project. | ||
4 | * For details, see https://github.com/egor-tensin/cimple. | ||
5 | * Distributed under the MIT License. | ||
6 | */ | ||
7 | |||
8 | #include "client.h" | ||
9 | #include "cmd_line.h" | ||
10 | #include "compiler.h" | ||
11 | #include "const.h" | ||
12 | #include "json_rpc.h" | ||
13 | #include "log.h" | ||
14 | #include "net.h" | ||
15 | #include "protocol.h" | ||
16 | #include "run_queue.h" | ||
17 | |||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | struct client { | ||
22 | int dummy; | ||
23 | }; | ||
24 | |||
25 | 9208 | int client_create(struct client **_client) | |
26 | { | ||
27 | 9208 | int ret = 0; | |
28 | |||
29 | 9208 | struct client *client = malloc(sizeof(struct client)); | |
30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9208 times.
|
9208 | if (!client) { |
31 | ✗ | log_errno("malloc"); | |
32 | ✗ | return -1; | |
33 | } | ||
34 | |||
35 | 9208 | *_client = client; | |
36 | 9208 | return ret; | |
37 | } | ||
38 | |||
39 | 9206 | void client_destroy(struct client *client) | |
40 | { | ||
41 | 9206 | free(client); | |
42 | 9206 | } | |
43 | |||
44 | 9208 | static int make_request(struct jsonrpc_request **request, int argc, const char **argv) | |
45 | { | ||
46 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9207 times.
|
9208 | if (argc < 1) { |
47 | 1 | exit_with_usage_err("no action specified"); | |
48 | ✗ | return -1; | |
49 | } | ||
50 | |||
51 |
2/2✓ Branch 0 taken 9180 times.
✓ Branch 1 taken 27 times.
|
9207 | if (!strcmp(argv[0], CMD_QUEUE_RUN)) { |
52 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (argc != 3) |
53 | ✗ | return -1; | |
54 | |||
55 | 9180 | struct run *run = NULL; | |
56 | 9180 | int ret = run_queued(&run, argv[1], argv[2]); | |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
58 | ✗ | return ret; | |
59 | |||
60 | 9180 | ret = request_create_queue_run(request, run); | |
61 | 9180 | run_destroy(run); | |
62 | 9180 | return ret; | |
63 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1 times.
|
27 | } else if (!strcmp(argv[0], CMD_GET_RUNS)) { |
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (argc != 1) |
65 | ✗ | return -1; | |
66 | 26 | return request_create_get_runs(request); | |
67 | } | ||
68 | |||
69 | 1 | return -1; | |
70 | } | ||
71 | |||
72 | 9208 | int client_main(UNUSED const struct client *client, const struct settings *settings, int argc, | |
73 | const char **argv) | ||
74 | { | ||
75 | 9208 | int ret = 0; | |
76 | |||
77 | 9208 | struct jsonrpc_request *request = NULL; | |
78 | 9208 | ret = make_request(&request, argc, argv); | |
79 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9206 times.
|
9207 | if (ret < 0) { |
80 | 1 | exit_with_usage_err("invalid request"); | |
81 | ✗ | return ret; | |
82 | } | ||
83 | |||
84 | 9206 | ret = net_connect(settings->host, settings->port); | |
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9206 times.
|
9206 | if (ret < 0) |
86 | ✗ | goto free_request; | |
87 | 9206 | int fd = ret; | |
88 | |||
89 | 9206 | ret = jsonrpc_request_send(request, fd); | |
90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9206 times.
|
9206 | if (ret < 0) |
91 | ✗ | goto close; | |
92 | |||
93 | 9206 | struct jsonrpc_response *response = NULL; | |
94 | 9206 | ret = jsonrpc_response_recv(&response, fd); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9206 times.
|
9206 | if (ret < 0) |
96 | ✗ | goto close; | |
97 | |||
98 | 9206 | const char *response_str = jsonrpc_response_to_string(response); | |
99 |
1/2✓ Branch 0 taken 9206 times.
✗ Branch 1 not taken.
|
9206 | if (response_str) { |
100 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9206 times.
|
9206 | if (jsonrpc_response_is_error(response)) |
101 | ✗ | ret = -1; | |
102 | 9206 | printf("%s", response_str); | |
103 | } else { | ||
104 | ✗ | log_err("no response\n"); | |
105 | } | ||
106 | 9206 | goto free_response; | |
107 | |||
108 | 9206 | free_response: | |
109 | 9206 | jsonrpc_response_destroy(response); | |
110 | |||
111 | 9206 | close: | |
112 | 9206 | net_close(fd); | |
113 | |||
114 | 9206 | free_request: | |
115 | 9206 | jsonrpc_request_destroy(request); | |
116 | |||
117 | 9206 | return ret; | |
118 | } | ||
119 |