22 #include "serverpool.h"
30 ServerPool *servers = NULL;
31 volatile bool hupped = false;
38 CubemapStateProto collect_state(const timeval &serialize_start,
39 const vector<Acceptor *> acceptors,
40 const vector<Input *> inputs,
43 CubemapStateProto state = servers->serialize(); // Fills streams() and clients().
44 state.set_serialize_start_sec(serialize_start.tv_sec);
45 state.set_serialize_start_usec(serialize_start.tv_usec);
47 for (size_t i = 0; i < acceptors.size(); ++i) {
48 state.add_acceptors()->MergeFrom(acceptors[i]->serialize());
51 for (size_t i = 0; i < inputs.size(); ++i) {
52 state.add_inputs()->MergeFrom(inputs[i]->serialize());
58 // Find all port statements in the configuration file, and create acceptors for htem.
59 vector<Acceptor *> create_acceptors(
61 map<int, Acceptor *> *deserialized_acceptors)
63 vector<Acceptor *> acceptors;
64 for (unsigned i = 0; i < config.acceptors.size(); ++i) {
65 const AcceptorConfig &acceptor_config = config.acceptors[i];
66 Acceptor *acceptor = NULL;
67 map<int, Acceptor *>::iterator deserialized_acceptor_it =
68 deserialized_acceptors->find(acceptor_config.port);
69 if (deserialized_acceptor_it != deserialized_acceptors->end()) {
70 acceptor = deserialized_acceptor_it->second;
71 deserialized_acceptors->erase(deserialized_acceptor_it);
73 int server_sock = create_server_socket(acceptor_config.port, TCP_SOCKET);
74 acceptor = new Acceptor(server_sock, acceptor_config.port);
77 acceptors.push_back(acceptor);
80 // Close all acceptors that are no longer in the configuration file.
81 for (map<int, Acceptor *>::iterator acceptor_it = deserialized_acceptors->begin();
82 acceptor_it != deserialized_acceptors->end();
84 acceptor_it->second->close_socket();
85 delete acceptor_it->second;
91 // Find all streams in the configuration file, and create inputs for them.
92 vector<Input *> create_inputs(const Config &config,
93 map<string, Input *> *deserialized_inputs)
95 vector<Input *> inputs;
96 for (unsigned i = 0; i < config.streams.size(); ++i) {
97 const StreamConfig &stream_config = config.streams[i];
98 if (stream_config.src.empty()) {
102 string stream_id = stream_config.stream_id;
103 string src = stream_config.src;
106 map<string, Input *>::iterator deserialized_input_it =
107 deserialized_inputs->find(stream_id);
108 if (deserialized_input_it != deserialized_inputs->end()) {
109 input = deserialized_input_it->second;
110 if (input->get_url() != src) {
111 fprintf(stderr, "INFO: Stream '%s' has changed URL from '%s' to '%s', restarting input.\n",
112 stream_id.c_str(), input->get_url().c_str(), src.c_str());
113 input->close_socket();
117 deserialized_inputs->erase(deserialized_input_it);
120 input = create_input(stream_id, src);
122 fprintf(stderr, "ERROR: did not understand URL '%s', clients will not get any data.\n",
128 inputs.push_back(input);
133 void create_streams(const Config &config,
134 const set<string> &deserialized_stream_ids,
135 map<string, Input *> *deserialized_inputs)
137 vector<MarkPool *> mark_pools; // FIXME: leak
138 for (unsigned i = 0; i < config.mark_pools.size(); ++i) {
139 const MarkPoolConfig &mp_config = config.mark_pools[i];
140 mark_pools.push_back(new MarkPool(mp_config.from, mp_config.to));
143 set<string> expecting_stream_ids = deserialized_stream_ids;
144 for (unsigned i = 0; i < config.streams.size(); ++i) {
145 const StreamConfig &stream_config = config.streams[i];
146 if (deserialized_stream_ids.count(stream_config.stream_id) == 0) {
147 servers->add_stream(stream_config.stream_id, stream_config.backlog_size);
149 servers->set_backlog_size(stream_config.stream_id, stream_config.backlog_size);
151 expecting_stream_ids.erase(stream_config.stream_id);
153 if (stream_config.mark_pool != -1) {
154 servers->set_mark_pool(stream_config.stream_id,
155 mark_pools[stream_config.mark_pool]);
159 // Warn about any servers we've lost.
160 // TODO: Make an option (delete=yes?) to actually shut down streams.
161 for (set<string>::const_iterator stream_it = expecting_stream_ids.begin();
162 stream_it != expecting_stream_ids.end();
164 string stream_id = *stream_it;
165 fprintf(stderr, "WARNING: stream '%s' disappeared from the configuration file.\n",
167 fprintf(stderr, " It will not be deleted, but clients will not get any new inputs.\n");
168 if (deserialized_inputs->count(stream_id) != 0) {
169 delete (*deserialized_inputs)[stream_id];
170 deserialized_inputs->erase(stream_id);
175 bool dry_run_config(const std::string &argv0, const std::string &config_filename)
177 char *argv0_copy = strdup(argv0.c_str());
178 char *config_filename_copy = strdup(config_filename.c_str());
185 free(config_filename_copy);
189 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, NULL);
198 free(config_filename_copy);
203 err = waitpid(pid, &status, 0);
204 } while (err == -1 && errno == EINTR);
211 return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
214 int main(int argc, char **argv)
220 bool test_config = false;
222 static const option long_options[] = {
223 { "state", required_argument, 0, 's' },
224 { "test-config", no_argument, 0, 't' },
226 int option_index = 0;
227 int c = getopt_long (argc, argv, "s:t", long_options, &option_index);
234 state_fd = atoi(optarg);
244 string config_filename = "cubemap.config";
246 config_filename = argv[optind++];
250 if (!parse_config(config_filename, &config)) {
258 fprintf(stderr, "\nCubemap " SERVER_VERSION " starting.\n");
259 servers = new ServerPool(config.num_servers);
261 CubemapStateProto loaded_state;
262 struct timeval serialize_start;
263 set<string> deserialized_stream_ids;
264 map<string, Input *> deserialized_inputs;
265 map<int, Acceptor *> deserialized_acceptors;
266 if (state_fd != -1) {
267 fprintf(stderr, "Deserializing state from previous process... ");
269 if (!read_tempfile(state_fd, &serialized)) {
272 if (!loaded_state.ParseFromString(serialized)) {
273 fprintf(stderr, "ERROR: Failed deserialization of state.\n");
277 serialize_start.tv_sec = loaded_state.serialize_start_sec();
278 serialize_start.tv_usec = loaded_state.serialize_start_usec();
280 // Deserialize the streams.
281 for (int i = 0; i < loaded_state.streams_size(); ++i) {
282 servers->add_stream_from_serialized(loaded_state.streams(i));
283 deserialized_stream_ids.insert(loaded_state.streams(i).stream_id());
286 // Deserialize the inputs. Note that we don't actually add them to any state yet.
287 for (int i = 0; i < loaded_state.inputs_size(); ++i) {
288 deserialized_inputs.insert(make_pair(
289 loaded_state.inputs(i).stream_id(),
290 create_input(loaded_state.inputs(i))));
293 // Deserialize the acceptors.
294 for (int i = 0; i < loaded_state.acceptors_size(); ++i) {
295 deserialized_acceptors.insert(make_pair(
296 loaded_state.acceptors(i).port(),
297 new Acceptor(loaded_state.acceptors(i))));
300 fprintf(stderr, "done.\n");
303 // Find all streams in the configuration file, and create them.
304 create_streams(config, deserialized_stream_ids, &deserialized_inputs);
308 vector<Acceptor *> acceptors = create_acceptors(config, &deserialized_acceptors);
309 vector<Input *> inputs = create_inputs(config, &deserialized_inputs);
311 // All deserialized inputs should now have been taken care of, one way or the other.
312 assert(deserialized_inputs.empty());
314 // Put back the existing clients. It doesn't matter which server we
315 // allocate them to, so just do round-robin. However, we need to add
316 // them after the mark pools have been set up.
317 for (int i = 0; i < loaded_state.clients_size(); ++i) {
318 servers->add_client_from_serialized(loaded_state.clients(i));
321 // Start writing statistics.
322 StatsThread *stats_thread = NULL;
323 if (!config.stats_file.empty()) {
324 stats_thread = new StatsThread(config.stats_file, config.stats_interval);
328 struct timeval server_start;
329 gettimeofday(&server_start, NULL);
330 if (state_fd != -1) {
331 // Measure time from we started deserializing (below) to now, when basically everything
332 // is up and running. This is, in other words, a conservative estimate of how long our
333 // “glitch” period was, not counting of course reconnects if the configuration changed.
334 double glitch_time = server_start.tv_sec - serialize_start.tv_sec +
335 1e-6 * (server_start.tv_usec - serialize_start.tv_usec);
336 fprintf(stderr, "Re-exec happened in approx. %.0f ms.\n", glitch_time * 1000.0);
343 // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
344 gettimeofday(&serialize_start, NULL);
346 if (stats_thread != NULL) {
347 stats_thread->stop();
349 for (size_t i = 0; i < acceptors.size(); ++i) {
350 acceptors[i]->stop();
352 for (size_t i = 0; i < inputs.size(); ++i) {
357 fprintf(stderr, "Serializing state and re-execing...\n");
358 CubemapStateProto state = collect_state(
359 serialize_start, acceptors, inputs, servers);
361 state.SerializeToString(&serialized);
362 state_fd = make_tempfile(serialized);
363 if (state_fd == -1) {
368 if (!dry_run_config(argv[0], config_filename)) {
369 fprintf(stderr, "ERROR: %s --test-config failed. Restarting old version instead of new.\n", argv[0]);
375 sprintf(buf, "%d", state_fd);
378 execlp(argv[0], argv[0], config_filename.c_str(), "--state", buf, NULL);
380 fprintf(stderr, "PANIC: re-exec of %s failed. Waiting 0.2 seconds and trying again...\n", argv[0]);