summaryrefslogtreecommitdiff
path: root/server_communication.c
blob: e8ab7b8682ad832debcef597055661f8c6852d9b (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "server_communication.h"
#include "music.h"

#include <gio/gio.h>
#include <string.h>

enum connection_type {
	SERVER_CONNECTION_TYPE_HTTP,
	SERVER_CONNECTION_TYPE_COMMAND,
};

static GSocket *server_connect(struct server *server, const enum connection_type type) {
	GError *error = NULL, *conn_error = NULL;
	GSocketConnectable *addr;
	GSocketAddressEnumerator *enumerator;
	GSocketAddress *sockaddr;
	GSocketConnection *conn = NULL;
	GSocketClient *client;

	guint16 port;
	switch(type) {
		case SERVER_CONNECTION_TYPE_HTTP:
			port = server->http_port;
			break;
		case SERVER_CONNECTION_TYPE_COMMAND:
			port = server->command_port;
			break;
	}

	addr = g_network_address_new(server->host, port);
	enumerator = g_socket_connectable_enumerate(addr);
	g_object_unref(addr);

	client = g_socket_client_new();

	while(conn == NULL && (sockaddr = g_socket_address_enumerator_next(enumerator, NULL, &error))) {
		conn = g_socket_client_connect(client, (GSocketConnectable*)sockaddr, NULL, &conn_error);
		g_object_unref(sockaddr);
	}

	if(sockaddr == NULL) {
		if(error != NULL) {
			g_warning(error->message);
			g_error_free(error);
		} else {
			g_warning("server_connect: no connectable addresses found for host %s",
					server->host);
		}
		return NULL;
	}

	if(conn == NULL) {
		g_warning(conn_error->message);
		g_error_free(conn_error);
		return NULL;
	}

	GSocket *socket = g_socket_connection_get_socket(conn);

	g_debug("connected");

	return socket;
}

static GSocket *server_connect_http(struct server *server) {
	return server_connect(server, SERVER_CONNECTION_TYPE_HTTP);
}

static GSocket *server_connect_command(struct server *server) {
	return server_connect(server, SERVER_CONNECTION_TYPE_COMMAND);
}

gboolean server_ping(struct server *server) {
	GTimer *timer = g_timer_new();
	GSocket *socket = server_connect_command(server);

	if(socket == NULL) {
		return FALSE;
	}

	gboolean result = FALSE;
	g_socket_send(socket, "ping\n", 5, NULL, NULL);
	gchar buffer[0x400];
	gssize size = g_socket_receive(socket, buffer, 0x400, NULL, NULL);
	if(size == 5 && strncmp(buffer, "pong", 4) == 0) {
		g_debug("ping response: %f ms", g_timer_elapsed(timer, NULL) * 1000);
		result = TRUE;
	}

	g_timer_destroy(timer);

	g_socket_close(socket, NULL);

	return result;
}

static gchar **server_get_stringlist(struct server *server, const gchar *cmd) {
	GSocket *socket = server_connect_command(server);

	if(socket == NULL) {
		return NULL;
	}

	g_socket_send(socket, cmd, strlen(cmd), NULL, NULL);

	gchar buffer[0x400];
	gssize size;
	GString *string = g_string_new(NULL);
	while((size = g_socket_receive(socket, buffer, 0x400, NULL, NULL)) > 0) {
		g_string_append_len(string, buffer, size);
	}

	g_socket_close(socket, NULL);

	gchar **data = g_strsplit(string->str, "\n", 0);
	g_string_free(string, TRUE);

	return data;
}

gchar **server_find(struct server *server, const gchar *type, const gchar *str) {
	gchar *cmd = g_strdup_printf("findl %s %s\nexit\n", type, str);
	gchar **data = server_get_stringlist(server, cmd);
	g_free(cmd);

	return data;
}

gchar **server_list(struct server *server, const gchar *directory) {
	gchar *cmd = g_strdup_printf("list %s\nexit\n", directory);
	gchar **data = server_get_stringlist(server, cmd);
	g_free(cmd);

	return data;
}

gboolean server_get(struct server *server, const gchar *type,
		const gchar *localfile, const gchar *remotefile) {
	GError *error = NULL;
	GSocket *socket = server_connect_http(server);

	if(socket == NULL) {
		return FALSE;
	}

	gchar *cmd = g_strdup_printf("GET /get_%s %s HTTP/1.1\r\n\r\n", type, remotefile);

	g_socket_send(socket, cmd, strlen(cmd), NULL, NULL);

	GSocketConnection *connection = g_socket_connection_factory_create_connection(socket);
	GDataInputStream *input = g_data_input_stream_new(g_io_stream_get_input_stream((GIOStream*)connection));
	g_data_input_stream_set_newline_type(input, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);

	gchar *line = NULL;
	gsize size;

	while((line = g_data_input_stream_read_line(input, &size, NULL, &error)) != NULL) {
		g_debug(line);
		if(size == 0) {
			break;
		}
	}

	if(error != NULL) {
		g_warning(error->message);
		g_error_free(error);
		return FALSE;
	}

	GFile *file = g_file_new_for_path(localfile);
	GFileOutputStream *output = g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);

	if(output == NULL) {
		g_warning(error->message);
		g_error_free(error);
		return FALSE;
	}

	g_output_stream_splice((GOutputStream*)output, (GInputStream*)input,
			G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
			NULL, &error);

	g_object_unref(input);
	g_object_unref(output);
	g_object_unref(file);

	if(error != NULL) {
		g_warning(error->message);
		g_error_free(error);
		return FALSE;
	}

	g_object_unref(connection);

	return TRUE;
}