7 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
23 #include "serverpool.h"
27 #define STREAM_ID "stream"
28 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
32 ServerPool *servers = NULL;
33 volatile bool hupped = false;
40 int create_server_socket(int port)
42 int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
43 if (server_sock == -1) {
49 if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
50 perror("setsockopt(SO_REUSEADDR)");
54 // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
56 if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
57 perror("setsockopt(IPV6_V6ONLY)");
62 memset(&addr, 0, sizeof(addr));
63 addr.sin6_family = AF_INET6;
64 addr.sin6_port = htons(port);
66 if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
71 if (listen(server_sock, 128) == -1) {
79 void *acceptor_thread_run(void *arg)
81 int server_sock = int(intptr_t(arg));
84 socklen_t addrlen = sizeof(addr);
87 int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
88 if (sock == -1 && errno == EINTR) {
96 // Set the socket as nonblocking.
98 if (ioctl(sock, FIONBIO, &one) == -1) {
103 // Pick a server, round-robin, and hand over the socket to it.
104 servers->add_client(sock);
108 // Serialize the given state to a file descriptor, and return the (still open)
110 int make_tempfile(const CubemapStateProto &state)
112 char tmpl[] = "/tmp/cubemapstate.XXXXXX";
113 int state_fd = mkstemp(tmpl);
114 if (state_fd == -1) {
120 state.SerializeToString(&serialized);
122 const char *ptr = serialized.data();
123 size_t to_write = serialized.size();
124 while (to_write > 0) {
125 ssize_t ret = write(state_fd, ptr, to_write);
138 // Read the state back from the file descriptor made by make_tempfile,
140 CubemapStateProto read_tempfile(int state_fd)
142 if (lseek(state_fd, 0, SEEK_SET) == -1) {
150 ssize_t ret = read(state_fd, buf, sizeof(buf));
160 serialized.append(string(buf, buf + ret));
163 close(state_fd); // Implicitly deletes the file.
165 CubemapStateProto state;
166 if (!state.ParseFromString(serialized)) {
167 fprintf(stderr, "PANIC: Failed deserialization of state.\n");
174 int main(int argc, char **argv)
176 fprintf(stderr, "\nCubemap starting.\n");
178 string config_filename = (argc == 1) ? "cubemap.config" : argv[1];
179 vector<ConfigLine> config = parse_config(config_filename);
181 int port = fetch_config_int(config, "port", 1, 65535);
182 int num_servers = fetch_config_int(config, "num_servers", 1, 20000); // Insanely high max limit.
184 servers = new ServerPool(num_servers);
186 int server_sock = -1, old_port = -1;
187 set<string> deserialized_stream_ids;
188 if (argc == 4 && strcmp(argv[2], "-state") == 0) {
189 fprintf(stderr, "Deserializing state from previous process... ");
190 int state_fd = atoi(argv[3]);
191 CubemapStateProto loaded_state = read_tempfile(state_fd);
193 // Deserialize the streams.
194 for (int i = 0; i < loaded_state.streams_size(); ++i) {
195 servers->add_stream_from_serialized(loaded_state.streams(i));
196 deserialized_stream_ids.insert(loaded_state.streams(i).stream_id());
199 // Put back the existing clients. It doesn't matter which server we
200 // allocate them to, so just do round-robin.
201 for (int i = 0; i < loaded_state.clients_size(); ++i) {
202 servers->add_client_from_serialized(loaded_state.clients(i));
205 // Deserialize the server socket.
206 server_sock = loaded_state.server_sock();
207 old_port = loaded_state.port();
209 fprintf(stderr, "done.\n");
212 // Find all streams in the configuration file, and create them.
213 set<string> expecting_stream_ids = deserialized_stream_ids;
214 for (unsigned i = 0; i < config.size(); ++i) {
215 if (config[i].keyword != "stream") {
218 if (config[i].arguments.size() != 1) {
219 fprintf(stderr, "ERROR: 'stream' takes exactly one argument\n");
222 string stream_id = config[i].arguments[0];
223 if (deserialized_stream_ids.count(stream_id) == 0) {
224 servers->add_stream(stream_id);
226 expecting_stream_ids.erase(stream_id);
229 // Warn about any servers we've lost.
230 // TODO: Make an option (delete=yes?) to actually shut down streams.
231 for (set<string>::const_iterator stream_it = expecting_stream_ids.begin();
232 stream_it != expecting_stream_ids.end();
234 fprintf(stderr, "WARNING: stream '%s' disappeared from the configuration file.\n",
236 fprintf(stderr, " It will not be deleted, but clients will not get any new inputs.\n");
239 // Open a new server socket if we do not already have one, or if we changed ports.
240 if (server_sock != -1 && port != old_port) {
241 fprintf(stderr, "NOTE: Port changed from %d to %d; opening new socket.\n", old_port, port);
245 if (server_sock == -1) {
246 server_sock = create_server_socket(port);
251 pthread_t acceptor_thread;
252 pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
254 // Find all streams in the configuration file, and create inputs for them.
255 vector<Input *> inputs;
256 for (unsigned i = 0; i < config.size(); ++i) {
257 if (config[i].keyword != "stream") {
260 assert(config[i].arguments.size() == 1);
261 string stream_id = config[i].arguments[0];
263 if (config[i].parameters.count("src") == 0) {
264 fprintf(stderr, "WARNING: stream '%s' has no src= attribute, clients will not get any data.\n",
269 string src = config[i].parameters["src"];
270 Input *input = new Input(stream_id, src);
272 inputs.push_back(input);
281 // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
282 for (size_t i = 0; i < inputs.size(); ++i) {
284 delete inputs[i]; // TODO: serialize instead of using libcurl.
287 CubemapStateProto state;
288 state.set_server_sock(server_sock);
289 state.set_port(port);
290 for (int i = 0; i < num_servers; ++i) {
291 servers->get_server(i)->stop();
293 CubemapStateProto local_state = servers->get_server(i)->serialize();
295 // The stream state should be identical between the servers, so we only store it once.
297 state.mutable_streams()->MergeFrom(local_state.streams());
299 for (int j = 0; j < local_state.clients_size(); ++j) {
300 state.add_clients()->MergeFrom(local_state.clients(j));
305 fprintf(stderr, "Serializing state and re-execing...\n");
306 int state_fd = make_tempfile(state);
309 sprintf(buf, "%d", state_fd);
312 execlp(argv[0], argv[0], config_filename.c_str(), "-state", buf, NULL);
314 fprintf(stderr, "PANIC: re-exec of %s failed. Waiting 0.2 seconds and trying again...\n", argv[0]);