aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/client.c
blob: 8c6c597e98e6abd2090c7d9ea0a5b6b482fb603c (plain) (blame)
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
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * 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_RUN)) {
		if (argc != 3)
			return -1;

		struct run *run = NULL;
		int ret = run_create(&run, 0, argv[1], argv[2]);
		if (ret < 0)
			return ret;

		ret = run_request_create(request, run);
		run_destroy(run);
		return ret;
	}

	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;

	if (jsonrpc_response_is_error(response)) {
		log_err("server failed to process the request\n");
		ret = -1;
		goto free_response;
	}

free_response:
	jsonrpc_response_destroy(response);

close:
	net_close(fd);

free_request:
	jsonrpc_request_destroy(request);

	return ret;
}