]> git.sesse.net Git - cubemap/blobdiff - cubemap.cpp
Parse the HTTP header (more) properly, and send the headers on to any connecting...
[cubemap] / cubemap.cpp
index d5cfa010a1443782c37614903ae8fdeed905d7e6..1bcc07bd1e612573e85176749febdb204e9979f3 100644 (file)
@@ -3,12 +3,11 @@
 #include <stdint.h>
 #include <assert.h>
 #include <arpa/inet.h>
-#include <curl/curl.h>
 #include <sys/socket.h>
 #include <pthread.h>
 #include <sys/types.h>
 #include <sys/ioctl.h>
-#include <sys/epoll.h>
+#include <sys/poll.h>
 #include <signal.h>
 #include <errno.h>
 #include <ctype.h>
@@ -58,6 +57,12 @@ int create_server_socket(int port)
                exit(1);
        }
 
+       // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
+       if (ioctl(server_sock, FIONBIO, &one) == -1) {
+               perror("ioctl(FIONBIO)");
+               exit(1);
+       }
+
        sockaddr_in6 addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin6_family = AF_INET6;
@@ -79,8 +84,23 @@ int create_server_socket(int port)
 void *acceptor_thread_run(void *arg)
 {
        int server_sock = int(intptr_t(arg));
-       int num_accepted = 0;
-       for ( ;; ) {
+       while (!hupped) {
+               // Since we are non-blocking, we need to wait for the right state first.
+               // Wait up to 50 ms, then check hupped.
+               pollfd pfd;
+               pfd.fd = server_sock;
+               pfd.events = POLLIN;
+
+               int nfds = poll(&pfd, 1, 50);
+               if (nfds == 0 || (nfds == -1 && errno == EAGAIN)) {
+                       continue;
+               }
+               if (nfds == -1) {
+                       perror("poll");
+                       usleep(100000);
+                       continue;
+               }
+
                sockaddr_in6 addr;
                socklen_t addrlen = sizeof(addr);
 
@@ -91,7 +111,8 @@ void *acceptor_thread_run(void *arg)
                }
                if (sock == -1) {
                        perror("accept");
-                       exit(1);
+                       usleep(100000);
+                       continue;
                }
 
                // Set the socket as nonblocking.
@@ -103,8 +124,8 @@ void *acceptor_thread_run(void *arg)
 
                // Pick a server, round-robin, and hand over the socket to it.
                servers->add_client(sock);
-               ++num_accepted; 
        }
+       return NULL;
 }
 
 // Serialize the given state to a file descriptor, and return the (still open)
@@ -187,6 +208,7 @@ int main(int argc, char **argv)
 
        int server_sock = -1, old_port = -1;
        set<string> deserialized_stream_ids;
+       map<string, Input *> deserialized_inputs;
        if (argc == 4 && strcmp(argv[2], "-state") == 0) {
                fprintf(stderr, "Deserializing state from previous process... ");
                int state_fd = atoi(argv[3]);
@@ -204,6 +226,13 @@ int main(int argc, char **argv)
                        servers->add_client_from_serialized(loaded_state.clients(i));
                }
 
+               // Deserialize the inputs. Note that we don't actually add them to any state yet.
+               for (int i = 0; i < loaded_state.inputs_size(); ++i) {
+                       deserialized_inputs.insert(make_pair(
+                               loaded_state.inputs(i).stream_id(),
+                               new Input(loaded_state.inputs(i))));
+               } 
+
                // Deserialize the server socket.
                server_sock = loaded_state.server_sock();
                old_port = loaded_state.port();
@@ -233,9 +262,14 @@ int main(int argc, char **argv)
        for (set<string>::const_iterator stream_it = expecting_stream_ids.begin();
             stream_it != expecting_stream_ids.end();
             ++stream_it) {
+               string stream_id = *stream_it;
                fprintf(stderr, "WARNING: stream '%s' disappeared from the configuration file.\n",
-                       stream_it->c_str());
+                       stream_id.c_str());
                fprintf(stderr, "         It will not be deleted, but clients will not get any new inputs.\n");
+               if (deserialized_inputs.count(stream_id) != 0) {
+                       delete deserialized_inputs[stream_id];
+                       deserialized_inputs.erase(stream_id);
+               }
        }
 
        // Open a new server socket if we do not already have one, or if we changed ports.
@@ -269,11 +303,27 @@ int main(int argc, char **argv)
                }
 
                string src = config[i].parameters["src"];
-               Input *input = new Input(stream_id, src);
+               Input *input = NULL;
+               if (deserialized_inputs.count(stream_id) != 0) {
+                       input = deserialized_inputs[stream_id];
+                       if (input->get_url() != src) {
+                               fprintf(stderr, "INFO: Stream '%s' has changed URL from '%s' to '%s', restarting input.\n",
+                                       stream_id.c_str(), input->get_url().c_str(), src.c_str());
+                               delete input;
+                               input = NULL;
+                       }
+                       deserialized_inputs.erase(stream_id);
+               }
+               if (input == NULL) {
+                       input = new Input(stream_id, src);
+               }
                input->run();
                inputs.push_back(input);
        }
 
+       // All deserialized inputs should now have been taken care of, one way or the other.
+       assert(deserialized_inputs.empty());
+
        signal(SIGHUP, hup);
 
        while (!hupped) {
@@ -281,14 +331,20 @@ int main(int argc, char **argv)
        }
 
        // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
-       for (size_t i = 0; i < inputs.size(); ++i) {
-               inputs[i]->stop();
-               delete inputs[i];  // TODO: serialize instead of using libcurl.
+       if (pthread_join(acceptor_thread, NULL) == -1) {
+               perror("pthread_join");
+               exit(1);
        }
 
        CubemapStateProto state;
        state.set_server_sock(server_sock);
        state.set_port(port);
+
+       for (size_t i = 0; i < inputs.size(); ++i) {
+               inputs[i]->stop();
+               state.add_inputs()->MergeFrom(inputs[i]->serialize());
+       }
+
        for (int i = 0; i < num_servers; ++i) { 
                servers->get_server(i)->stop();