Directory: | src/ |
---|---|
File: | src/client.c |
Date: | 2023-08-28 07:33:56 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 43 | 55 | 78.2% |
Branches: | 13 | 24 | 54.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com> | ||
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 | 9182 | int client_create(struct client **_client) | |
26 | { | ||
27 | 9182 | int ret = 0; | |
28 | |||
29 | 9182 | struct client *client = malloc(sizeof(struct client)); | |
30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9182 times.
|
9182 | if (!client) { |
31 | ✗ | log_errno("malloc"); | |
32 | ✗ | return -1; | |
33 | } | ||
34 | |||
35 | 9182 | *_client = client; | |
36 | 9182 | return ret; | |
37 | } | ||
38 | |||
39 | 9180 | void client_destroy(struct client *client) | |
40 | { | ||
41 | 9180 | free(client); | |
42 | 9180 | } | |
43 | |||
44 | 9182 | 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 9181 times.
|
9182 | 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 1 times.
|
9181 | if (!strcmp(argv[0], CMD_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_create(&run, 0, 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 = run_request_create(request, run); | |
61 | 9180 | run_destroy(run); | |
62 | 9180 | return ret; | |
63 | } | ||
64 | |||
65 | 1 | return -1; | |
66 | } | ||
67 | |||
68 | 9182 | int client_main(UNUSED const struct client *client, const struct settings *settings, int argc, | |
69 | const char **argv) | ||
70 | { | ||
71 | 9182 | int ret = 0; | |
72 | |||
73 | 9182 | struct jsonrpc_request *request = NULL; | |
74 | 9182 | ret = make_request(&request, argc, argv); | |
75 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9180 times.
|
9181 | if (ret < 0) { |
76 | 1 | exit_with_usage_err("invalid request"); | |
77 | ✗ | return ret; | |
78 | } | ||
79 | |||
80 | 9180 | ret = net_connect(settings->host, settings->port); | |
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
82 | ✗ | goto free_request; | |
83 | 9180 | int fd = ret; | |
84 | |||
85 | 9180 | ret = jsonrpc_request_send(request, fd); | |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
87 | ✗ | goto close; | |
88 | |||
89 | 9180 | struct jsonrpc_response *response = NULL; | |
90 | 9180 | ret = jsonrpc_response_recv(&response, fd); | |
91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
92 | ✗ | goto close; | |
93 | |||
94 |
1/2✓ Branch 1 taken 9180 times.
✗ Branch 2 not taken.
|
9180 | if (jsonrpc_response_is_error(response)) { |
95 | ✗ | log_err("server failed to process the request\n"); | |
96 | ✗ | ret = -1; | |
97 | ✗ | goto free_response; | |
98 | } | ||
99 | |||
100 | 9180 | free_response: | |
101 | 9180 | jsonrpc_response_destroy(response); | |
102 | |||
103 | 9180 | close: | |
104 | 9180 | net_close(fd); | |
105 | |||
106 | 9180 | free_request: | |
107 | 9180 | jsonrpc_request_destroy(request); | |
108 | |||
109 | 9180 | return ret; | |
110 | } | ||
111 |