]> git.sesse.net Git - cubemap/blob - cubemap.cpp
When we get a header, retroactively send it out to all clients that have received...
[cubemap] / cubemap.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <curl/curl.h>
7 #include <sys/socket.h>
8 #include <pthread.h>
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
12 #include <signal.h>
13 #include <errno.h>
14 #include <vector>
15 #include <string>
16 #include <map>
17
18 #include "metacube.h"
19 #include "server.h"
20 #include "input.h"
21 #include "state.pb.h"
22
23 #define NUM_SERVERS 4
24 #define STREAM_ID "stream"
25 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
26 #define PORT 9094
27
28 using namespace std;
29
30 Server *servers = NULL;
31 volatile bool hupped = false;
32
33 void hup(int ignored)
34 {
35         hupped = true;
36 }
37
38 int create_server_socket(int port)
39 {
40         int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
41         if (server_sock == -1) {
42                 perror("socket");
43                 exit(1);
44         }
45
46         int one = 1;
47         if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
48                 perror("setsockopt(SO_REUSEADDR)");
49                 exit(1);
50         }
51
52         // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
53         int zero = 0;
54         if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
55                 perror("setsockopt(IPV6_V6ONLY)");
56                 exit(1);
57         }
58
59         sockaddr_in6 addr;
60         memset(&addr, 0, sizeof(addr));
61         addr.sin6_family = AF_INET6;
62         addr.sin6_port = htons(port);
63
64         if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
65                 perror("bind");
66                 exit(1);
67         }
68
69         if (listen(server_sock, 128) == -1) {
70                 perror("listen");
71                 exit(1);
72         }
73
74         return server_sock;
75 }
76
77 void *acceptor_thread_run(void *arg)
78 {
79         int server_sock = int(intptr_t(arg));
80         int num_accepted = 0;
81         for ( ;; ) {
82                 sockaddr_in6 addr;
83                 socklen_t addrlen = sizeof(addr);
84
85                 // Get a new socket.
86                 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
87                 if (sock == -1 && errno == EINTR) {
88                         continue;
89                 }
90                 if (sock == -1) {
91                         perror("accept");
92                         exit(1);
93                 }
94
95                 // Set the socket as nonblocking.
96                 int one = 1;
97                 if (ioctl(sock, FIONBIO, &one) == -1) {
98                         perror("FIONBIO");
99                         exit(1);
100                 }
101
102                 // Pick a server, round-robin, and hand over the socket to it.
103                 servers[num_accepted % NUM_SERVERS].add_client(sock);
104                 ++num_accepted; 
105         }
106 }
107
108 int main(int argc, char **argv)
109 {
110         servers = new Server[NUM_SERVERS];
111         for (int i = 0; i < NUM_SERVERS; ++i) {
112                 servers[i].add_stream(STREAM_ID);
113                 servers[i].run();
114         }
115
116         int server_sock = create_server_socket(PORT);
117
118         pthread_t acceptor_thread;
119         pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
120
121         Input input(STREAM_ID, STREAM_URL);
122         input.run();
123
124         signal(SIGHUP, hup);
125
126         while (!hupped) {
127                 usleep(100000);
128         }
129
130         input.stop();
131
132         CubemapStateProto state;
133         for (int i = 0; i < NUM_SERVERS; ++i) {
134                 servers[i].stop();
135
136                 CubemapStateProto local_state = servers[i].serialize();
137
138                 // The stream state should be identical between the servers, so we only store it once.
139                 if (i == 0) {
140                         state.mutable_streams()->MergeFrom(local_state.streams());
141                 }
142                 for (int j = 0; j < local_state.clients_size(); ++j) {
143                         state.add_clients()->MergeFrom(local_state.clients(j));
144                 }
145         }
146         delete[] servers;
147
148         printf("SERIALIZED: [%s]\n", state.DebugString().c_str());
149 }