2008-07-11 20:38:17 +00:00
|
|
|
#include "control.h"
|
|
|
|
|
2008-07-11 22:55:36 +00:00
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2008-07-12 00:17:29 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <poll.h>
|
2008-07-11 22:55:36 +00:00
|
|
|
|
|
|
|
static int opensocket(const char *hostname, const char *port) {
|
|
|
|
struct addrinfo *res = NULL, pref;
|
2008-07-12 00:17:29 +00:00
|
|
|
int ret, s, val;
|
2008-07-11 22:55:36 +00:00
|
|
|
|
|
|
|
pref.ai_flags = 0;
|
|
|
|
pref.ai_family = PF_UNSPEC;
|
|
|
|
pref.ai_socktype = SOCK_STREAM;
|
|
|
|
pref.ai_protocol = 0;
|
|
|
|
pref.ai_addrlen = 0;
|
|
|
|
pref.ai_addr = NULL;
|
|
|
|
pref.ai_canonname = NULL;
|
|
|
|
pref.ai_next = NULL;
|
|
|
|
|
|
|
|
if (0 != (ret = getaddrinfo(hostname, port, &pref, &res)) || !res) {
|
|
|
|
fprintf(stderr, "Couldn't find host/port '%s:%s': %s\n", hostname, port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-1 == (s = socket(res->ai_family, res->ai_socktype, res->ai_protocol))) {
|
|
|
|
fprintf(stderr, "Couldn't create socket: %s\n", strerror(errno));
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != connect(s, res->ai_addr, res->ai_addrlen)) {
|
|
|
|
fprintf(stderr, "Couldn't connect socket to '%s:%s': %s\n", hostname, port, strerror(errno));
|
|
|
|
freeaddrinfo(res);
|
2008-07-12 00:17:29 +00:00
|
|
|
close(s);
|
2008-07-11 22:55:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-07-12 00:17:29 +00:00
|
|
|
|
|
|
|
val = 1;
|
|
|
|
if (0 != setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val))) {
|
|
|
|
fprintf(stderr, "Couldn't set socket option TCP_NODELAY: %s\n", strerror(errno));
|
|
|
|
freeaddrinfo(res);
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-11 22:55:36 +00:00
|
|
|
freeaddrinfo(res);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-11 22:20:05 +00:00
|
|
|
static void telemetry_free(gpointer _te, gpointer x) {
|
|
|
|
telemetry *te = (telemetry*) _te;
|
2008-07-11 22:11:41 +00:00
|
|
|
UNUSED(x);
|
|
|
|
g_array_free(te->objects, TRUE);
|
2008-07-11 22:20:05 +00:00
|
|
|
g_slice_free(telemetry, te);
|
2008-07-11 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static telemetry* telemetry_new() {
|
|
|
|
telemetry *te = g_slice_new0(telemetry);
|
|
|
|
te->objects = g_array_new(FALSE, FALSE, sizeof(object));
|
|
|
|
return te;
|
|
|
|
}
|
|
|
|
|
2008-07-11 22:55:36 +00:00
|
|
|
trial *trial_new(const char *hostname, const char *port) {
|
|
|
|
trial *t;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
if (-1 == (sock = opensocket(hostname, port))) return NULL;
|
|
|
|
|
|
|
|
t = g_slice_new0(trial);
|
|
|
|
t->socket = sock;
|
2008-07-11 22:20:05 +00:00
|
|
|
g_queue_init(&t->telemetry);
|
2008-07-11 22:11:41 +00:00
|
|
|
t->map.solid_objects = g_array_new(FALSE, FALSE, sizeof(object));
|
2008-07-12 00:39:26 +00:00
|
|
|
control_parser_new(t);
|
2008-07-11 22:11:41 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void trial_reset_run(trial *t) {
|
2008-07-11 22:20:05 +00:00
|
|
|
g_queue_foreach(&t->telemetry, telemetry_free, NULL);
|
|
|
|
g_queue_clear(&t->telemetry);
|
2008-07-11 22:11:41 +00:00
|
|
|
t->last_ts = 0;
|
2008-07-12 00:39:26 +00:00
|
|
|
control_parser_reset(t);
|
2008-07-11 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void trial_wait_for_start(trial *t) {
|
2008-07-11 22:55:36 +00:00
|
|
|
while (0 == t->telemetry.length) {
|
|
|
|
trial_wait_for_input(t);
|
|
|
|
}
|
2008-07-11 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
2008-07-12 00:17:29 +00:00
|
|
|
static char buffer[16*1024];
|
|
|
|
|
2008-07-11 22:11:41 +00:00
|
|
|
void trial_check_input(trial *t) {
|
2008-07-12 00:17:29 +00:00
|
|
|
ssize_t len;
|
|
|
|
struct pollfd p;
|
|
|
|
p.fd = t->socket;
|
|
|
|
p.events = POLLIN;
|
|
|
|
switch (poll(&p, 1, 0)) {
|
|
|
|
case -1:
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "poll error: %s\n", strerror(errno));
|
|
|
|
exit(127);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
len = read(t->socket, buffer, sizeof(buffer));
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "read error: %s\n", strerror(errno));
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
g_string_append_len(t->parse_ctx->buffer, buffer, len);
|
|
|
|
control_parse(t);
|
2008-07-11 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void trial_wait_for_input(trial *t) {
|
2008-07-12 00:17:29 +00:00
|
|
|
ssize_t len = read(t->socket, buffer, sizeof(buffer));
|
|
|
|
if (len < 0) {
|
|
|
|
fprintf(stderr, "read error: %s\n", strerror(errno));
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
g_string_append_len(t->parse_ctx->buffer, buffer, len);
|
|
|
|
control_parse(t);
|
2008-07-11 22:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void trial_free(trial *t) {
|
2008-07-11 22:55:36 +00:00
|
|
|
shutdown(t->socket, SHUT_RDWR);
|
|
|
|
close(t->socket);
|
2008-07-11 22:20:05 +00:00
|
|
|
g_queue_foreach(&t->telemetry, telemetry_free, NULL);
|
|
|
|
g_queue_clear(&t->telemetry);
|
2008-07-11 22:11:41 +00:00
|
|
|
g_array_free(t->map.solid_objects, TRUE);
|
2008-07-12 00:39:26 +00:00
|
|
|
control_parser_free(t);
|
2008-07-11 22:20:05 +00:00
|
|
|
g_slice_free(trial, t);
|
2008-07-11 22:11:41 +00:00
|
|
|
}
|