From: Steinar H. Gunderson Date: Wed, 10 Apr 2013 22:06:29 +0000 (+0200) Subject: Some small refactoring of main(). X-Git-Tag: 1.0.0~154 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=7c55e615d7b03fc92b93701ddc3a5d2e6b7087d0 Some small refactoring of main(). --- diff --git a/main.cpp b/main.cpp index 69f0e9b..278c51d 100644 --- a/main.cpp +++ b/main.cpp @@ -153,6 +153,49 @@ MarkPool *parse_mark_pool(map, MarkPool *> *mark_pools, const str return get_mark_pool(mark_pools, from, to); } + +// Find all streams in the configuration file, and create inputs for them. +vector create_inputs(const vector &config, + map *deserialized_inputs) +{ + vector 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]; + + map::const_iterator src_it = + config[i].parameters.find("src"); + if (src_it == config[i].parameters.end()) { + fprintf(stderr, "WARNING: stream '%s' has no src= attribute, clients will not get any data.\n", + stream_id.c_str()); + continue; + } + + string src = src_it->second; + Input *input = NULL; + map::iterator deserialized_input_it = + deserialized_inputs->find(stream_id); + if (deserialized_input_it != deserialized_inputs->end()) { + input = deserialized_input_it->second; + 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(deserialized_input_it); + } + if (input == NULL) { + input = new Input(stream_id, src); + } + input->run(); + inputs.push_back(input); + } + return inputs; +} int main(int argc, char **argv) { @@ -258,49 +301,13 @@ int main(int argc, char **argv) if (stats_interval != -1 && stats_file.empty()) { fprintf(stderr, "WARNING: 'stats_interval' given, but no 'stats_file'. No statistics will be written.\n"); } - StatsThread *stats_thread = NULL; - if (!stats_file.empty()) { - stats_thread = new StatsThread(stats_file, stats_interval); - } servers->run(); AcceptorThread acceptor_thread(server_sock); acceptor_thread.run(); - // Find all streams in the configuration file, and create inputs for them. - vector 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); - } + vector inputs = create_inputs(config, &deserialized_inputs); // All deserialized inputs should now have been taken care of, one way or the other. assert(deserialized_inputs.empty()); @@ -315,7 +322,9 @@ int main(int argc, char **argv) } // Start writing statistics. - if (stats_thread != NULL) { + StatsThread *stats_thread = NULL; + if (!stats_file.empty()) { + stats_thread = new StatsThread(stats_file, stats_interval); stats_thread->run(); }