+ // 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 = 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) {
+ usleep(100000);
+ }
+
+ // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
+ 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();
+
+ 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);
+ }