16 #define DEFAULT_BACKLOG_SIZE 1048576
20 vector<string> arguments;
21 map<string, string> parameters;
24 bool read_config(const string &filename, vector<ConfigLine> *lines)
26 FILE *fp = fopen(filename.c_str(), "r");
28 perror(filename.c_str());
34 if (fgets(buf, sizeof(buf), fp) == NULL) {
38 // Chop off the string at the first #, \r or \n.
39 buf[strcspn(buf, "#\r\n")] = 0;
41 // Remove all whitespace from the end of the string.
42 size_t len = strlen(buf);
43 while (len > 0 && isspace(buf[len - 1])) {
47 // If the line is now all blank, ignore it.
52 vector<string> tokens = split_tokens(buf);
53 assert(!tokens.empty());
56 line.keyword = tokens[0];
58 for (size_t i = 1; i < tokens.size(); ++i) {
59 // foo=bar is a parameter; anything else is an argument.
60 size_t equals_pos = tokens[i].find_first_of('=');
61 if (equals_pos == string::npos) {
62 line.arguments.push_back(tokens[i]);
64 string key = tokens[i].substr(0, equals_pos);
65 string value = tokens[i].substr(equals_pos + 1, string::npos);
66 line.parameters.insert(make_pair(key, value));
70 lines->push_back(line);
77 bool fetch_config_string(const vector<ConfigLine> &config, const string &keyword, string *value)
79 for (unsigned i = 0; i < config.size(); ++i) {
80 if (config[i].keyword != keyword) {
83 if (config[i].parameters.size() > 0 ||
84 config[i].arguments.size() != 1) {
85 fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
88 *value = config[i].arguments[0];
94 bool fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int *value)
96 for (unsigned i = 0; i < config.size(); ++i) {
97 if (config[i].keyword != keyword) {
100 if (config[i].parameters.size() > 0 ||
101 config[i].arguments.size() != 1) {
102 fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
105 *value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
111 bool parse_port(const ConfigLine &line, Config *config)
113 if (line.arguments.size() != 1) {
114 fprintf(stderr, "ERROR: 'port' takes exactly one argument\n");
118 AcceptorConfig acceptor;
119 acceptor.port = atoi(line.arguments[0].c_str());
120 if (acceptor.port < 1 || acceptor.port >= 65536) {
121 fprintf(stderr, "ERROR: port %d is out of range (must be [1,65536>).\n", acceptor.port);
125 config->acceptors.push_back(acceptor);
129 int allocate_mark_pool(int from, int to, Config *config)
133 // Reuse mark pools if an identical one exists.
134 // Otherwise, check if we're overlapping some other mark pool.
135 for (size_t i = 0; i < config->mark_pools.size(); ++i) {
136 const MarkPoolConfig &pool = config->mark_pools[i];
137 if (from == pool.from && to == pool.to) {
139 } else if ((from >= pool.from && from < pool.to) ||
140 (to >= pool.from && to < pool.to)) {
141 fprintf(stderr, "WARNING: Mark pool %d-%d partially overlaps with %d-%d, you may get duplicate marks.\n",
142 from, to, pool.from, pool.to);
143 fprintf(stderr, " Mark pools must either be completely disjunct, or completely overlapping.\n");
147 if (pool_index != -1) {
151 // No match to existing pools.
155 config->mark_pools.push_back(pool);
157 return config->mark_pools.size() - 1;
160 bool parse_mark_pool(const string &mark_str, int *from, int *to)
162 size_t split = mark_str.find_first_of('-');
163 if (split == string::npos) {
164 fprintf(stderr, "ERROR: Invalid mark specification '%s' (expected 'X-Y').\n",
169 string from_str(mark_str.begin(), mark_str.begin() + split);
170 string to_str(mark_str.begin() + split + 1, mark_str.end());
171 *from = atoi(from_str.c_str());
172 *to = atoi(to_str.c_str());
174 if (*from <= 0 || *from >= 65536 || *to <= 0 || *to >= 65536) {
175 fprintf(stderr, "ERROR: Mark pool range %d-%d is outside legal range [1,65536>.\n",
183 bool parse_stream(const ConfigLine &line, Config *config)
185 if (line.arguments.size() != 1) {
186 fprintf(stderr, "ERROR: 'stream' takes exactly one argument\n");
191 stream.stream_id = line.arguments[0];
193 map<string, string>::const_iterator src_it = line.parameters.find("src");
194 if (src_it == line.parameters.end()) {
195 fprintf(stderr, "WARNING: stream '%s' has no src= attribute, clients will not get any data.\n",
196 stream.stream_id.c_str());
198 stream.src = src_it->second;
199 // TODO: Verify that the URL is parseable?
202 map<string, string>::const_iterator backlog_it = line.parameters.find("backlog_size");
203 if (backlog_it == line.parameters.end()) {
204 stream.backlog_size = DEFAULT_BACKLOG_SIZE;
206 stream.backlog_size = atoi(backlog_it->second.c_str());
209 // Parse marks, if so desired.
210 map<string, string>::const_iterator mark_parm_it = line.parameters.find("mark");
211 if (mark_parm_it == line.parameters.end()) {
212 stream.mark_pool = -1;
215 if (!parse_mark_pool(mark_parm_it->second, &from, &to)) {
218 stream.mark_pool = allocate_mark_pool(from, to, config);
221 config->streams.push_back(stream);
225 bool parse_config(const string &filename, Config *config)
227 vector<ConfigLine> lines;
228 if (!read_config(filename, &lines)) {
232 if (!fetch_config_int(lines, "num_servers", &config->num_servers)) {
233 fprintf(stderr, "ERROR: Missing 'num_servers' statement in config file.\n");
236 if (config->num_servers < 1 || config->num_servers >= 20000) { // Insanely high max limit.
237 fprintf(stderr, "ERROR: 'num_servers' is %d, needs to be in [1, 20000>.\n", config->num_servers);
241 // See if the user wants stats.
242 config->stats_interval = 60;
243 bool has_stats_file = fetch_config_string(lines, "stats_file", &config->stats_file);
244 bool has_stats_interval = fetch_config_int(lines, "stats_interval", &config->stats_interval);
245 if (has_stats_interval && !has_stats_file) {
246 fprintf(stderr, "WARNING: 'stats_interval' given, but no 'stats_file'. No statistics will be written.\n");
249 for (size_t i = 0; i < lines.size(); ++i) {
250 const ConfigLine &line = lines[i];
251 if (line.keyword == "num_servers" ||
252 line.keyword == "stats_file" ||
253 line.keyword == "stats_interval") {
254 // Already taken care of, above.
255 } else if (line.keyword == "port") {
256 if (!parse_port(line, config)) {
259 } else if (line.keyword == "stream") {
260 if (!parse_stream(line, config)) {
264 fprintf(stderr, "ERROR: Unknown configuration keyword '%s'.\n",
265 line.keyword.c_str());