+ // Find all streams in the configuration file, and create inputs for them.
+ vector<Input *> inputs;
+ for (unsigned i = 0; i < config.size(); ++i) {
+ if (config[i].keyword != "stream") {
+ continue;
+ }
+ assert(config[i].arguments.size() == 1);
+ string stream_id = config[i].arguments[0];
+
+ if (config[i].parameters.count("src") == 0) {
+ fprintf(stderr, "WARNING: stream '%s' has no src= attribute, clients will not get any data.\n",
+ stream_id.c_str());
+ continue;
+ }
+
+ string src = config[i].parameters["src"];
+ Input *input = new Input(stream_id, src);
+ input->run();
+ inputs.push_back(input);
+ }
+
+ signal(SIGHUP, hup);
+
+ while (!hupped) {
+ usleep(100000);
+ }
+
+ // 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.
+ }
+
+ 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 (int i = 0; i < num_servers; ++i) {
+ servers->get_server(i)->stop();
+
+ CubemapStateProto local_state = servers->get_server(i)->serialize();
+
+ // The stream state should be identical between the servers, so we only store it once.
+ if (i == 0) {
+ state.mutable_streams()->MergeFrom(local_state.streams());
+ }
+ for (int j = 0; j < local_state.clients_size(); ++j) {
+ state.add_clients()->MergeFrom(local_state.clients(j));
+ }
+ }
+ delete servers;
+
+ fprintf(stderr, "Serializing state and re-execing...\n");
+ int state_fd = make_tempfile(state);
+
+ char buf[16];
+ sprintf(buf, "%d", state_fd);
+
+ for ( ;; ) {
+ execlp(argv[0], argv[0], config_filename.c_str(), "-state", buf, NULL);
+ perror("execlp");
+ fprintf(stderr, "PANIC: re-exec of %s failed. Waiting 0.2 seconds and trying again...\n", argv[0]);
+ usleep(200000);
+ }