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-17 01:16:11 +00:00
|
|
|
#include <string.h>
|
2016-02-17 13:56:13 -08:00
|
|
|
#include <signal.h>
|
2016-02-14 02:12:35 +00:00
|
|
|
|
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-17 13:41:33 -08:00
|
|
|
#include "incoming.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-17 08:30:32 +00:00
|
|
|
#include "stats.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-17 01:16:11 +00:00
|
|
|
static void print_usage(char *argv[]) {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Usage: %s [OPTION]...\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Options:\n"
|
|
|
|
|
"\t--help\n"
|
2016-02-17 11:10:01 -08:00
|
|
|
"\t--backend=HOST/PORT\n"
|
2016-02-17 01:16:11 +00:00
|
|
|
"\t--dump=FORMAT\n"
|
2016-02-17 13:41:33 -08:00
|
|
|
"\t--incoming=[HOST/]PORT\n"
|
2016-02-17 13:56:13 -08:00
|
|
|
"\t--listen=FORMAT=[HOST/]PORT\n"
|
2016-02-17 01:16:11 +00:00
|
|
|
, argv[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool parse_opts(int argc, char *argv[], int epoll_fd) {
|
|
|
|
|
static struct option long_options[] = {
|
2016-02-17 13:41:33 -08:00
|
|
|
{"backend", required_argument, 0, 'b'},
|
|
|
|
|
{"dump", required_argument, 0, 'd'},
|
|
|
|
|
{"incoming", required_argument, 0, 'i'},
|
2016-02-17 13:56:13 -08:00
|
|
|
{"listen", required_argument, 0, 'l'},
|
2016-02-17 13:41:33 -08:00
|
|
|
{"help", no_argument, 0, 'h'},
|
2016-02-17 01:16:11 +00:00
|
|
|
};
|
|
|
|
|
|
2016-02-14 02:12:35 +00:00
|
|
|
int opt;
|
2016-02-17 13:56:13 -08:00
|
|
|
char *delim1, *delim2;
|
2016-02-17 01:16:11 +00:00
|
|
|
while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) {
|
2016-02-14 02:12:35 +00:00
|
|
|
switch (opt) {
|
2016-02-17 01:16:11 +00:00
|
|
|
case 'b':
|
|
|
|
|
// It would be really nice if libc had a standard way to split host:port.
|
2016-02-17 13:56:13 -08:00
|
|
|
delim1 = strrchr(optarg, '/');
|
|
|
|
|
if (delim1 == NULL) {
|
2016-02-17 01:16:11 +00:00
|
|
|
print_usage(argv);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-17 13:56:13 -08:00
|
|
|
*delim1 = '\0';
|
|
|
|
|
delim1++;
|
2016-02-17 01:16:11 +00:00
|
|
|
|
2016-02-17 13:56:13 -08:00
|
|
|
backend_new(optarg, delim1, epoll_fd);
|
2016-02-17 01:16:11 +00:00
|
|
|
break;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2016-02-17 01:16:11 +00:00
|
|
|
case 'h':
|
|
|
|
|
print_usage(argv);
|
|
|
|
|
return false;
|
|
|
|
|
|
2016-02-17 13:41:33 -08:00
|
|
|
case 'i':
|
2016-02-17 13:56:13 -08:00
|
|
|
delim1 = strrchr(optarg, '/');
|
|
|
|
|
if (delim1 == NULL) {
|
|
|
|
|
incoming_new(NULL, optarg, epoll_fd, backend_new_fd, NULL);
|
2016-02-17 13:41:33 -08:00
|
|
|
} else {
|
2016-02-17 13:56:13 -08:00
|
|
|
*delim1 = '\0';
|
|
|
|
|
delim1++;
|
|
|
|
|
incoming_new(optarg, delim1, epoll_fd, backend_new_fd, NULL);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
|
delim1 = strchr(optarg, '=');
|
|
|
|
|
if (delim1 == NULL) {
|
|
|
|
|
print_usage(argv);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
*delim1 = '\0';
|
|
|
|
|
delim1++;
|
|
|
|
|
struct serializer *serializer = client_get_serializer(optarg);
|
|
|
|
|
if (!serializer) {
|
|
|
|
|
fprintf(stderr, "Unknown format: %s\n", optarg);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delim2 = strrchr(delim1, '/');
|
|
|
|
|
if (delim2 == NULL) {
|
|
|
|
|
incoming_new(NULL, delim1, epoll_fd, client_new_fd, serializer);
|
|
|
|
|
} else {
|
|
|
|
|
*delim2 = '\0';
|
|
|
|
|
delim2++;
|
|
|
|
|
incoming_new(delim1, delim2, epoll_fd, client_new_fd, serializer);
|
2016-02-17 13:41:33 -08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2016-02-14 02:12:35 +00:00
|
|
|
default:
|
2016-02-17 01:16:11 +00:00
|
|
|
print_usage(argv);
|
2016-02-16 02:28:05 +00:00
|
|
|
return false;
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-17 01:16:11 +00:00
|
|
|
|
|
|
|
|
if (optind != argc) {
|
|
|
|
|
fprintf(stderr, "Not a flag: %s\n", argv[optind]);
|
|
|
|
|
print_usage(argv);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2016-02-17 08:05:18 +00:00
|
|
|
peer->event_handler(peer, epoll_fd);
|
2016-02-14 02:12:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2016-02-17 13:56:13 -08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
|
|
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-17 08:30:32 +00:00
|
|
|
stats_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-17 01:16:11 +00:00
|
|
|
if (!parse_opts(argc, argv, epoll_fd)) {
|
2016-02-16 02:28:05 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|