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