Directory: | src/ |
---|---|
File: | src/protocol.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 90 | 131 | 68.7% |
Branches: | 24 | 48 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2023 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 "protocol.h" | ||
9 | #include "base64.h" | ||
10 | #include "compiler.h" | ||
11 | #include "const.h" | ||
12 | #include "json.h" | ||
13 | #include "json_rpc.h" | ||
14 | #include "process.h" | ||
15 | #include "run_queue.h" | ||
16 | |||
17 | #include <stddef.h> | ||
18 | #include <stdint.h> | ||
19 | #include <stdlib.h> | ||
20 | |||
21 | static const char *const run_key_id = "id"; | ||
22 | static const char *const run_key_url = "url"; | ||
23 | static const char *const run_key_rev = "rev"; | ||
24 | |||
25 | 9180 | int request_create_queue_run(struct jsonrpc_request **request, const struct run *run) | |
26 | { | ||
27 | 9180 | int ret = 0; | |
28 | |||
29 | 9180 | ret = jsonrpc_request_create(request, jsonrpc_generate_request_id(), CMD_QUEUE_RUN, NULL); | |
30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
31 | ✗ | return ret; | |
32 | 9180 | ret = jsonrpc_request_set_param_string(*request, run_key_url, run_get_repo_url(run)); | |
33 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
34 | ✗ | goto free_request; | |
35 | 9180 | ret = jsonrpc_request_set_param_string(*request, run_key_rev, run_get_repo_rev(run)); | |
36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
37 | ✗ | goto free_request; | |
38 | |||
39 | 9180 | return ret; | |
40 | |||
41 | ✗ | free_request: | |
42 | ✗ | jsonrpc_request_destroy(*request); | |
43 | |||
44 | ✗ | return ret; | |
45 | } | ||
46 | |||
47 | 9180 | int request_parse_queue_run(const struct jsonrpc_request *request, struct run **run) | |
48 | { | ||
49 | 9180 | int ret = 0; | |
50 | |||
51 | 9180 | const char *url = NULL; | |
52 | 9180 | ret = jsonrpc_request_get_param_string(request, run_key_url, &url); | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
54 | ✗ | return ret; | |
55 | 9180 | const char *rev = NULL; | |
56 | 9180 | ret = jsonrpc_request_get_param_string(request, run_key_rev, &rev); | |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
58 | ✗ | return ret; | |
59 | |||
60 | 9180 | return run_queued(run, url, rev); | |
61 | } | ||
62 | |||
63 | 9234 | int request_create_new_worker(struct jsonrpc_request **request) | |
64 | { | ||
65 | 9234 | return jsonrpc_notification_create(request, CMD_NEW_WORKER, NULL); | |
66 | } | ||
67 | |||
68 | ✗ | int request_parse_new_worker(UNUSED const struct jsonrpc_request *request) | |
69 | { | ||
70 | ✗ | return 0; | |
71 | } | ||
72 | |||
73 | 9180 | int request_create_start_run(struct jsonrpc_request **request, const struct run *run) | |
74 | { | ||
75 | 9180 | int ret = 0; | |
76 | |||
77 | 9180 | ret = jsonrpc_notification_create(request, CMD_START_RUN, NULL); | |
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
79 | ✗ | return ret; | |
80 | 9180 | ret = jsonrpc_request_set_param_int(*request, run_key_id, run_get_id(run)); | |
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
82 | ✗ | goto free_request; | |
83 | 9180 | ret = jsonrpc_request_set_param_string(*request, run_key_url, run_get_repo_url(run)); | |
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
85 | ✗ | goto free_request; | |
86 | 9180 | ret = jsonrpc_request_set_param_string(*request, run_key_rev, run_get_repo_rev(run)); | |
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
88 | ✗ | goto free_request; | |
89 | |||
90 | 9180 | return ret; | |
91 | |||
92 | ✗ | free_request: | |
93 | ✗ | jsonrpc_request_destroy(*request); | |
94 | |||
95 | ✗ | return ret; | |
96 | } | ||
97 | |||
98 | 9180 | int request_parse_start_run(const struct jsonrpc_request *request, struct run **run) | |
99 | { | ||
100 | 9180 | int ret = 0; | |
101 | |||
102 | 9180 | int64_t id = 0; | |
103 | 9180 | ret = jsonrpc_request_get_param_int(request, run_key_id, &id); | |
104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
105 | ✗ | return ret; | |
106 | 9180 | const char *url = NULL; | |
107 | 9180 | ret = jsonrpc_request_get_param_string(request, run_key_url, &url); | |
108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
109 | ✗ | return ret; | |
110 | 9180 | const char *rev = NULL; | |
111 | 9180 | ret = jsonrpc_request_get_param_string(request, run_key_rev, &rev); | |
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
113 | ✗ | return ret; | |
114 | |||
115 | 9180 | return run_created(run, (int)id, url, rev); | |
116 | } | ||
117 | |||
118 | static const char *const finished_key_run_id = "run_id"; | ||
119 | static const char *const finished_key_ec = "exit_code"; | ||
120 | static const char *const finished_key_data = "output"; | ||
121 | |||
122 | 9180 | int request_create_finished_run(struct jsonrpc_request **request, int run_id, | |
123 | const struct proc_output *output) | ||
124 | { | ||
125 | 9180 | int ret = 0; | |
126 | |||
127 | 9180 | ret = jsonrpc_notification_create(request, CMD_FINISHED_RUN, NULL); | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
129 | ✗ | return ret; | |
130 | 9180 | ret = jsonrpc_request_set_param_int(*request, finished_key_run_id, run_id); | |
131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
132 | ✗ | goto free_request; | |
133 | 9180 | ret = jsonrpc_request_set_param_int(*request, finished_key_ec, output->ec); | |
134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
135 | ✗ | goto free_request; | |
136 | |||
137 | 9180 | char *b64data = NULL; | |
138 | 9180 | ret = base64_encode(output->data, output->data_size, &b64data); | |
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
140 | ✗ | goto free_request; | |
141 | |||
142 | 9180 | ret = jsonrpc_request_set_param_string(*request, finished_key_data, b64data); | |
143 | 9180 | free(b64data); | |
144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
145 | ✗ | goto free_request; | |
146 | |||
147 | 9180 | return ret; | |
148 | |||
149 | ✗ | free_request: | |
150 | ✗ | jsonrpc_request_destroy(*request); | |
151 | |||
152 | ✗ | return ret; | |
153 | } | ||
154 | |||
155 | 9180 | int request_parse_finished_run(const struct jsonrpc_request *request, int *_run_id, | |
156 | struct proc_output **_output) | ||
157 | { | ||
158 | 9180 | int ret = 0; | |
159 | |||
160 | 9180 | struct proc_output *output = NULL; | |
161 | 9180 | ret = proc_output_create(&output); | |
162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
163 | ✗ | return ret; | |
164 | |||
165 | 9180 | int64_t run_id = 0; | |
166 | 9180 | ret = jsonrpc_request_get_param_int(request, finished_key_run_id, &run_id); | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
168 | ✗ | goto free_output; | |
169 | |||
170 | 9180 | int64_t ec = -1; | |
171 | 9180 | ret = jsonrpc_request_get_param_int(request, finished_key_ec, &ec); | |
172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
173 | ✗ | goto free_output; | |
174 | 9180 | output->ec = (int)ec; | |
175 | |||
176 | 9180 | const char *b64data = NULL; | |
177 | 9180 | ret = jsonrpc_request_get_param_string(request, finished_key_data, &b64data); | |
178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
179 | ✗ | goto free_output; | |
180 | |||
181 | 9180 | ret = base64_decode(b64data, &output->data, &output->data_size); | |
182 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
183 | ✗ | goto free_output; | |
184 | |||
185 | 9180 | *_run_id = (int)run_id; | |
186 | 9180 | *_output = output; | |
187 | 9180 | return ret; | |
188 | |||
189 | ✗ | free_output: | |
190 | ✗ | proc_output_destroy(output); | |
191 | |||
192 | ✗ | return ret; | |
193 | } | ||
194 | |||
195 | 26 | int request_create_get_runs(struct jsonrpc_request **request) | |
196 | { | ||
197 | 26 | return jsonrpc_request_create(request, jsonrpc_generate_request_id(), CMD_GET_RUNS, NULL); | |
198 | } | ||
199 | |||
200 | 26 | int request_parse_get_runs(UNUSED const struct jsonrpc_request *request) | |
201 | { | ||
202 | 26 | return 0; | |
203 | } | ||
204 | |||
205 | 26 | int response_create_get_runs(struct jsonrpc_response **response, | |
206 | const struct jsonrpc_request *request, const struct run_queue *runs) | ||
207 | { | ||
208 | 26 | struct json_object *runs_json = NULL; | |
209 | 26 | int ret = 0; | |
210 | |||
211 | 26 | ret = run_queue_to_json(runs, &runs_json); | |
212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (ret < 0) |
213 | ✗ | return ret; | |
214 | |||
215 | 26 | ret = jsonrpc_response_create(response, request, runs_json); | |
216 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | if (ret < 0) |
217 | ✗ | goto free_json; | |
218 | |||
219 | 26 | return ret; | |
220 | |||
221 | ✗ | free_json: | |
222 | ✗ | libjson_free(runs_json); | |
223 | |||
224 | ✗ | return ret; | |
225 | } | ||
226 |