aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/client.c
blob: 6f1bb4729caee09ef45ba96e5128af5424a33ff3 (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
#include "client.h"
#include "log.h"
#include "msg.h"
#include "net.h"

#include <unistd.h>

int client_create(struct client *client, const struct settings *settings)
{
	client->fd = net_connect(settings->host, settings->port);
	if (client->fd < 0)
		return client->fd;

	return 0;
}

void client_destroy(const struct client *client)
{
	log_errno_if(close(client->fd), "close");
}

int client_main(const struct client *client, int argc, char *argv[])
{
	struct msg request = {argc, argv};
	struct msg response;
	int ret = 0;

	ret = msg_send_and_wait(client->fd, &request, &response);
	if (ret < 0)
		return ret;

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

free_response:
	msg_free(&response);

	return ret;
}