Split out peer

This commit is contained in:
Ian Gulliver
2016-02-22 16:49:43 -08:00
parent b5219d9c1a
commit b5ab0cb0a6
12 changed files with 119 additions and 99 deletions

View File

@@ -3,7 +3,7 @@ CFLAGS ?= -Wall -Werror -O4 -g --std=gnu11 --pedantic-errors -fPIE -pie -fstack-
LDFLAGS ?= $(CFLAGS) -Wl,-z,relro -Wl,-z,now
LIBS ?= -ljansson
OBJ_NETWORK = incoming.o outgoing.o receive.o send.o
OBJ_NETWORK = incoming.o outgoing.o receive.o send.o peer.o
OBJ_PROTOCOL = airspy_adsb.o beast.o json.o raw.o stats.o
OBJ_UTIL = buf.o hex.o opts.o rand.o uuid.o wakeup.o common.o

View File

@@ -1,24 +1,22 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "incoming.h"
#include "outgoing.h"
#include "receive.h"
#include "send.h"
#include "beast.h"
#include "json.h"
#include "stats.h"
#include "common.h"
#include "hex.h"
#include "incoming.h"
#include "json.h"
#include "opts.h"
#include "outgoing.h"
#include "peer.h"
#include "rand.h"
#include "receive.h"
#include "send.h"
#include "stats.h"
#include "wakeup.h"
static void print_usage(const char *name) {

View File

@@ -14,76 +14,6 @@
#include "uuid.h"
#include "wakeup.h"
static char server_id[UUID_LEN];
static int peer_epoll_fd;
static int peer_cancel_fd;
static bool peer_canceled = false;
static void peer_cancel(int signal) {
assert(!close(peer_cancel_fd));
}
static void peer_cancel_handler(struct peer *peer) {
fprintf(stderr, "X %s: Shutting down\n", server_id);
assert(!close(peer->fd));
free(peer);
peer_canceled = true;
}
void peer_init() {
uuid_gen(server_id);
peer_epoll_fd = epoll_create1(0);
assert(peer_epoll_fd >= 0);
int cancel_fds[2];
assert(!pipe2(cancel_fds, O_NONBLOCK));
struct peer *cancel_peer = malloc(sizeof(*cancel_peer));
assert(cancel_peer);
cancel_peer->fd = cancel_fds[0];
cancel_peer->event_handler = peer_cancel_handler;
peer_epoll_add(cancel_peer, EPOLLRDHUP);
peer_cancel_fd = cancel_fds[1];
signal(SIGINT, peer_cancel);
}
void peer_epoll_add(struct peer *peer, uint32_t events) {
struct epoll_event ev = {
.events = events,
.data = {
.ptr = peer,
},
};
assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_ADD, peer->fd, &ev));
}
void peer_epoll_del(struct peer *peer) {
assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_DEL, peer->fd, NULL));
}
void peer_loop() {
fprintf(stderr, "X %s: Starting event loop\n", server_id);
while (!peer_canceled) {
#define MAX_EVENTS 10
struct epoll_event events[MAX_EVENTS];
int nfds = epoll_wait(peer_epoll_fd, events, MAX_EVENTS, wakeup_get_delay());
if (nfds == -1 && errno == EINTR) {
continue;
}
assert(nfds >= 0);
for (int n = 0; n < nfds; n++) {
struct peer *peer = events[n].data.ptr;
peer->event_handler(peer);
}
wakeup_dispatch();
}
assert(!close(peer_epoll_fd));
}
char *packet_type_names[] = {
"Mode-S short",

View File

@@ -1,22 +1,6 @@
#pragma once
#include <stdint.h>
#include <sys/epoll.h>
//////// peer
// All specific peer structs must be castable to this.
struct peer;
typedef void (*peer_event_handler)(struct peer *);
struct peer {
int fd;
peer_event_handler event_handler;
};
void peer_init();
void peer_epoll_add(struct peer *, uint32_t);
void peer_epoll_del(struct peer *);
void peer_loop();
//////// packet

View File

@@ -11,6 +11,7 @@
#include <unistd.h>
#include "common.h"
#include "peer.h"
#include "wakeup.h"
#include "uuid.h"

View File

@@ -9,6 +9,7 @@
#include <unistd.h>
#include "common.h"
#include "peer.h"
#include "wakeup.h"
#include "uuid.h"

86
adsbus/peer.c Normal file
View File

@@ -0,0 +1,86 @@
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <unistd.h>
#include "uuid.h"
#include "wakeup.h"
#include "peer.h"
static char server_id[UUID_LEN];
static int peer_epoll_fd;
static int peer_cancel_fd;
static bool peer_canceled = false;
static void peer_cancel(int signal) {
assert(!close(peer_cancel_fd));
}
static void peer_cancel_handler(struct peer *peer) {
fprintf(stderr, "X %s: Shutting down\n", server_id);
assert(!close(peer->fd));
free(peer);
peer_canceled = true;
}
void peer_init() {
uuid_gen(server_id);
peer_epoll_fd = epoll_create1(0);
assert(peer_epoll_fd >= 0);
int cancel_fds[2];
assert(!pipe2(cancel_fds, O_NONBLOCK));
struct peer *cancel_peer = malloc(sizeof(*cancel_peer));
assert(cancel_peer);
cancel_peer->fd = cancel_fds[0];
cancel_peer->event_handler = peer_cancel_handler;
peer_epoll_add(cancel_peer, EPOLLRDHUP);
peer_cancel_fd = cancel_fds[1];
signal(SIGINT, peer_cancel);
}
void peer_epoll_add(struct peer *peer, uint32_t events) {
struct epoll_event ev = {
.events = events,
.data = {
.ptr = peer,
},
};
assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_ADD, peer->fd, &ev));
}
void peer_epoll_del(struct peer *peer) {
assert(!epoll_ctl(peer_epoll_fd, EPOLL_CTL_DEL, peer->fd, NULL));
}
void peer_loop() {
fprintf(stderr, "X %s: Starting event loop\n", server_id);
while (!peer_canceled) {
#define MAX_EVENTS 10
struct epoll_event events[MAX_EVENTS];
int nfds = epoll_wait(peer_epoll_fd, events, MAX_EVENTS, wakeup_get_delay());
if (nfds == -1 && errno == EINTR) {
continue;
}
assert(nfds >= 0);
for (int n = 0; n < nfds; n++) {
struct peer *peer = events[n].data.ptr;
peer->event_handler(peer);
}
wakeup_dispatch();
}
assert(!close(peer_epoll_fd));
}

15
adsbus/peer.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include <sys/epoll.h>
// All specific peer structs must be castable to this.
struct peer;
typedef void (*peer_event_handler)(struct peer *);
struct peer {
int fd;
peer_event_handler event_handler;
};
void peer_init();
void peer_epoll_add(struct peer *, uint32_t);
void peer_epoll_del(struct peer *);
void peer_loop();

View File

@@ -7,6 +7,7 @@
#include "airspy_adsb.h"
#include "beast.h"
#include "buf.h"
#include "peer.h"
#include "raw.h"
#include "send.h"
#include "uuid.h"

View File

@@ -12,6 +12,7 @@
#include "beast.h"
#include "buf.h"
#include "json.h"
#include "peer.h"
#include "raw.h"
#include "stats.h"
#include "uuid.h"

View File

@@ -11,6 +11,7 @@
#include <sys/epoll.h>
#include "common.h"
#include "peer.h"
#include "rand.h"
#include "wakeup.h"

View File

@@ -1,5 +1,7 @@
#pragma once
#include <stdint.h>
struct peer;
void wakeup_init();