2016-02-14 06:20:17 +00:00
|
|
|
#include <assert.h>
|
2016-02-14 02:12:35 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
|
2016-02-14 06:20:17 +00:00
|
|
|
#include "common.h"
|
2016-02-16 02:28:05 +00:00
|
|
|
#include "backend.h"
|
|
|
|
|
#include "client.h"
|
2016-02-14 06:20:17 +00:00
|
|
|
#include "airspy_adsb.h"
|
2016-02-16 03:42:41 +00:00
|
|
|
#include "beast.h"
|
2016-02-16 02:28:05 +00:00
|
|
|
#include "json.h"
|
2016-02-14 06:20:17 +00:00
|
|
|
|
2016-02-14 02:12:35 +00:00
|
|
|
|
2016-02-16 02:28:05 +00:00
|
|
|
static bool add_dump(char *format) {
|
|
|
|
|
struct serializer *serializer = client_get_serializer(format);
|
|
|
|
|
if (!serializer) {
|
|
|
|
|
fprintf(stderr, "Unknown dump format: %s\n", format);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
client_add(1, serializer);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-02-14 02:12:35 +00:00
|
|
|
|
2016-02-16 02:28:05 +00:00
|
|
|
static bool parse_opts(int argc, char *argv[]) {
|
2016-02-14 02:12:35 +00:00
|
|
|
int opt;
|
2016-02-16 02:28:05 +00:00
|
|
|
while ((opt = getopt(argc, argv, "d:")) != -1) {
|
2016-02-14 02:12:35 +00:00
|
|
|
switch (opt) {
|
2016-02-15 21:12:26 +00:00
|
|
|
case 'd':
|
2016-02-16 02:28:05 +00:00
|
|
|
if (!add_dump(optarg)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-14 02:12:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2016-02-16 02:28:05 +00:00
|
|
|
return false;
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 02:28:05 +00:00
|
|
|
return true;
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-15 20:01:48 +00:00
|
|
|
static int loop(int epoll_fd) {
|
2016-02-14 02:12:35 +00:00
|
|
|
while (1) {
|
|
|
|
|
#define MAX_EVENTS 10
|
|
|
|
|
struct epoll_event events[MAX_EVENTS];
|
2016-02-15 20:01:48 +00:00
|
|
|
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
|
2016-02-14 02:12:35 +00:00
|
|
|
if (nfds == -1) {
|
|
|
|
|
perror("epoll_wait");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int n = 0; n < nfds; n++) {
|
2016-02-15 06:47:40 +00:00
|
|
|
struct peer *peer = events[n].data.ptr;
|
|
|
|
|
switch (peer->type) {
|
2016-02-15 20:01:48 +00:00
|
|
|
case PEER_BACKEND:
|
|
|
|
|
if (!backend_read((struct backend *) peer)) {
|
2016-02-15 06:47:40 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "Unpossible: unknown peer type.\n");
|
2016-02-14 02:12:35 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2016-02-17 00:21:28 +00:00
|
|
|
server_init();
|
2016-02-14 22:26:34 +00:00
|
|
|
hex_init();
|
2016-02-15 06:47:40 +00:00
|
|
|
airspy_adsb_init();
|
2016-02-16 03:42:41 +00:00
|
|
|
beast_init();
|
2016-02-16 02:28:05 +00:00
|
|
|
json_init();
|
2016-02-14 02:12:35 +00:00
|
|
|
|
2016-02-15 20:01:48 +00:00
|
|
|
int epoll_fd = epoll_create1(0);
|
|
|
|
|
if (epoll_fd == -1) {
|
|
|
|
|
perror("epoll_create1");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 02:28:05 +00:00
|
|
|
if (!parse_opts(argc, argv) ||
|
|
|
|
|
argc - optind < 2 ||
|
|
|
|
|
(argc - optind) % 2 != 0) {
|
|
|
|
|
fprintf(stderr, "Usage: %s [ -d format ] localhost 30006 [ remotehost 30002 ... ]\n", argv[0]);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-15 21:12:26 +00:00
|
|
|
int nbackends = (argc - optind) / 2;
|
|
|
|
|
struct backend backends[nbackends];
|
|
|
|
|
for (int i = 0, j = optind; i < nbackends && j < argc; i++, j += 2) {
|
|
|
|
|
backend_init(&backends[i]);
|
|
|
|
|
if (!backend_connect(argv[j], argv[j + 1], &backends[i], epoll_fd)) {
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-02-15 20:01:48 +00:00
|
|
|
loop(epoll_fd);
|
|
|
|
|
close(epoll_fd);
|
2016-02-14 02:12:35 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|