2016-02-22 16:49:43 -08:00
|
|
|
#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>
|
|
|
|
|
|
2016-02-22 21:53:25 -08:00
|
|
|
#include "server.h"
|
2016-02-22 16:49:43 -08:00
|
|
|
#include "uuid.h"
|
|
|
|
|
#include "wakeup.h"
|
|
|
|
|
|
|
|
|
|
#include "peer.h"
|
|
|
|
|
|
2016-02-25 16:17:25 -08:00
|
|
|
uint32_t peer_count_in = 0, peer_count_out = 0;
|
|
|
|
|
|
2016-02-22 16:49:43 -08:00
|
|
|
static int peer_epoll_fd;
|
2016-02-24 20:15:09 -08:00
|
|
|
static int peer_shutdown_fd;
|
|
|
|
|
static bool peer_shutdown_flag = false;
|
2016-02-22 16:49:43 -08:00
|
|
|
|
2016-02-24 20:15:09 -08:00
|
|
|
static void peer_shutdown_handler(struct peer *peer) {
|
2016-02-22 16:49:43 -08:00
|
|
|
fprintf(stderr, "X %s: Shutting down\n", server_id);
|
|
|
|
|
assert(!close(peer->fd));
|
|
|
|
|
free(peer);
|
2016-02-24 20:15:09 -08:00
|
|
|
peer_shutdown_flag = true;
|
2016-02-22 16:49:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void peer_init() {
|
2016-02-23 22:24:03 -08:00
|
|
|
peer_epoll_fd = epoll_create1(EPOLL_CLOEXEC);
|
2016-02-22 16:49:43 -08:00
|
|
|
assert(peer_epoll_fd >= 0);
|
|
|
|
|
|
2016-02-24 20:15:09 -08:00
|
|
|
int shutdown_fds[2];
|
|
|
|
|
assert(!pipe2(shutdown_fds, O_CLOEXEC));
|
2016-02-22 16:49:43 -08:00
|
|
|
|
2016-02-24 20:15:09 -08:00
|
|
|
struct peer *shutdown_peer = malloc(sizeof(*shutdown_peer));
|
|
|
|
|
assert(shutdown_peer);
|
|
|
|
|
shutdown_peer->fd = shutdown_fds[0];
|
|
|
|
|
shutdown_peer->event_handler = peer_shutdown_handler;
|
|
|
|
|
peer_epoll_add(shutdown_peer, EPOLLRDHUP);
|
2016-02-22 16:49:43 -08:00
|
|
|
|
2016-02-24 20:15:09 -08:00
|
|
|
peer_shutdown_fd = shutdown_fds[1];
|
|
|
|
|
signal(SIGINT, peer_shutdown);
|
2016-02-22 16:49:43 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-23 13:49:23 -08:00
|
|
|
void peer_cleanup() {
|
|
|
|
|
assert(!close(peer_epoll_fd));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 23:37:37 -08:00
|
|
|
void peer_shutdown(int __attribute__((unused)) signal) {
|
2016-02-24 20:15:09 -08:00
|
|
|
assert(!close(peer_shutdown_fd));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 16:49:43 -08:00
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 20:15:09 -08:00
|
|
|
void peer_call(struct peer *peer) {
|
|
|
|
|
if (peer_shutdown_flag || !peer) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
peer->event_handler(peer);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-22 16:49:43 -08:00
|
|
|
void peer_loop() {
|
|
|
|
|
fprintf(stderr, "X %s: Starting event loop\n", server_id);
|
2016-02-24 20:15:09 -08:00
|
|
|
while (!peer_shutdown_flag) {
|
2016-02-25 16:33:58 -08:00
|
|
|
if (!peer_count_in) {
|
|
|
|
|
fprintf(stderr, "X %s: No remaining inputs\n", server_id);
|
|
|
|
|
peer_shutdown(0);
|
|
|
|
|
} else if (!peer_count_out) {
|
|
|
|
|
fprintf(stderr, "X %s: No remaining outputs\n", server_id);
|
|
|
|
|
peer_shutdown(0);
|
|
|
|
|
}
|
2016-02-22 16:49:43 -08:00
|
|
|
#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();
|
|
|
|
|
}
|
|
|
|
|
}
|