summaryrefslogtreecommitdiff
path: root/server_communication.c
blob: 72e33863eebadd05cc1de614cb4f3069df4392cf (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
#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) {
	GError *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(conn != NULL) {
		if(conn_error != NULL) {
			g_error_free(conn_error);
		}
		return g_socket_connection_get_socket(conn);
	} else if(*error != NULL) {
		if(conn_error != NULL) {
			g_error_free(conn_error);
		}
		return NULL;
	} else {
		g_propagate_error(error, conn_error);
		return NULL;
	}
}

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

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

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

	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, GError **error) {
	GSocket *socket = server_connect_command(server, error);

	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, GError **error) {
	gchar *cmd = g_strdup_printf("findl %s %s\nexit\n", type, str);
	gchar **data = server_get_stringlist(server, cmd, error);
	g_free(cmd);

	return data;
}

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

	return data;
}

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

	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) {
		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) {
		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);
	g_object_unref(connection);
	g_object_unref(socket);

	if(*error != NULL) {
		return FALSE;
	}

	return TRUE;
}