18 vector<string> arguments;
19 map<string, string> parameters;
22 bool read_config(const string &filename, vector<ConfigLine> *lines)
24 FILE *fp = fopen(filename.c_str(), "r");
26 perror(filename.c_str());
32 if (fgets(buf, sizeof(buf), fp) == NULL) {
36 // Chop off the string at the first #, \r or \n.
37 buf[strcspn(buf, "#\r\n")] = 0;
39 // Remove all whitespace from the end of the string.
40 size_t len = strlen(buf);
41 while (len > 0 && isspace(buf[len - 1])) {
45 // If the line is now all blank, ignore it.
50 vector<string> tokens = split_tokens(buf);
51 assert(!tokens.empty());
54 line.keyword = tokens[0];
56 for (size_t i = 1; i < tokens.size(); ++i) {
57 // foo=bar is a parameter; anything else is an argument.
58 size_t equals_pos = tokens[i].find_first_of('=');
59 if (equals_pos == string::npos) {
60 line.arguments.push_back(tokens[i]);
62 string key = tokens[i].substr(0, equals_pos);
63 string value = tokens[i].substr(equals_pos + 1, string::npos);
64 line.parameters.insert(make_pair(key, value));
68 lines->push_back(line);
75 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
77 for (unsigned i = 0; i < config.size(); ++i) {
78 if (config[i].keyword != keyword) {
81 if (config[i].parameters.size() > 0 ||
82 config[i].arguments.size() != 1) {
83 fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
86 *value = config[i].arguments[0];
92 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
94 for (unsigned i = 0; i < config.size(); ++i) {
95 if (config[i].keyword != keyword) {
98 if (config[i].parameters.size() > 0 ||
99 config[i].arguments.size() != 1) {
100 fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
103 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
109 bool parse_port(const ConfigLine &line, Config *config)
111 if (line.arguments.size() != 1) {
112 fprintf(stderr, "ERROR: 'port' takes exactly one argument\n");
116 AcceptorConfig acceptor;
117 acceptor.port = atoi(line.arguments[0].c_str());
118 if (acceptor.port < 1 || acceptor.port >= 65536) {
119 fprintf(stderr, "ERROR: port %d is out of range (must be [1,65536>).\n", acceptor.port);
123 config->acceptors.push_back(acceptor);
127 int allocate_mark_pool(int from, int to, Config *config)
131 // Reuse mark pools if an identical one exists.
132 // Otherwise, check if we're overlapping some other mark pool.
133 for (size_t i = 0; i < config->mark_pools.size(); ++i) {
134 const MarkPoolConfig &pool = config->mark_pools[i];
135 if (from == pool.from && to == pool.to) {
137 } else if ((from >= pool.from && from < pool.to) ||
138 (to >= pool.from && to < pool.to)) {
139 fprintf(stderr, "WARNING: Mark pool %d-%d partially overlaps with %d-%d, you may get duplicate marks.\n",
140 from, to, pool.from, pool.to);
141 fprintf(stderr, " Mark pools must either be completely disjunct, or completely overlapping.\n");
145 if (pool_index != -1) {
149 // No match to existing pools.
153 config->mark_pools.push_back(pool);
155 return config->mark_pools.size() - 1;
158 bool parse_mark_pool(const string &mark_str, int *from, int *to)
160 size_t split = mark_str.find_first_of('-');
161 if (split == string::npos) {
162 fprintf(stderr, "ERROR: Invalid mark specification '%s' (expected 'X-Y').\n",
167 string from_str(mark_str.begin(), mark_str.begin() + split);
168 string to_str(mark_str.begin() + split + 1, mark_str.end());
169 *from = atoi(from_str.c_str());
170 *to = atoi(to_str.c_str());
172 if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
173 fprintf(stderr, "ERROR: Mark pool range %d-%d is outside legal range [1,65536>.\n",
181 bool parse_stream(const ConfigLine &line, Config *config)
183 if (line.arguments.size() != 1) {
184 fprintf(stderr, "ERROR: 'stream' takes exactly one argument\n");
189 stream.stream_id = line.arguments[0];
191 map<string, string>::const_iterator src_it = line.parameters.find("src");
192 if (src_it == line.parameters.end()) {
193 fprintf(stderr, "WARNING: stream '%s' has no src= attribute, clients will not get any data.\n",
194 stream.stream_id.c_str());
196 stream.src = src_it->second;
197 // TODO: Verify that the URL is parseable?
200 // Parse marks, if so desired.
201 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
202 if (mark_parm_it == line.parameters.end()) {
203 stream.mark_pool = -1;
206 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
209 stream.mark_pool = allocate_mark_pool(from, to, config);
212 config->streams.push_back(stream);
216 bool parse_config(const string &filename, Config *config)
218 vector<ConfigLine> lines;
219 if (!read_config(filename, &lines)) {
223 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
224 fprintf(stderr, "ERROR: Missing 'num_servers' statement in config file.\n");
227 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
228 fprintf(stderr, "ERROR: 'num_servers' is %d, needs to be in [1, 20000>.\n", config->num_servers);
232 // See if the user wants stats.
233 config->stats_interval = 60;
234 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
235 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
236 if (has_stats_interval && !has_stats_file) {
237 fprintf(stderr, "WARNING: 'stats_interval' given, but no 'stats_file'. No statistics will be written.\n");
240 for (size_t i = 0; i < lines.size(); ++i) {
241 const ConfigLine &line = lines[i];
242 if (line.keyword == "num_servers" ||
243 line.keyword == "stats_file" ||
244 line.keyword == "stats_interval") {
245 // Already taken care of, above.
246 } else if (line.keyword == "port") {
247 if (!parse_port(line, config)) {
250 } else if (line.keyword == "stream") {
251 if (!parse_stream(line, config)) {
255 fprintf(stderr, "ERROR: Unknown configuration keyword '%s'.\n",
256 line.keyword.c_str());