#include "input.h"
#include "state.pb.h"
-#define NUM_SERVERS 4
#define STREAM_ID "stream"
#define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
return ret;
}
+// Note: Limits are inclusive.
+int fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int min_limit, int max_limit)
+{
+ bool value_found = false;
+ int value = -1;
+ for (unsigned i = 0; i < config.size(); ++i) {
+ if (config[i].keyword != keyword) {
+ continue;
+ }
+ if (config[i].parameters.size() > 0 ||
+ config[i].arguments.size() != 1) {
+ fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
+ exit(1);
+ }
+ value_found = true;
+ value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
+ }
+ if (!value_found) {
+ fprintf(stderr, "ERROR: Missing '%s' statement in config file.\n",
+ keyword.c_str());
+ exit(1);
+ }
+ if (value < min_limit || value > max_limit) {
+ fprintf(stderr, "ERROR: '%s' is set to %d, must be in [%d,%d]\n",
+ keyword.c_str(), value, min_limit, max_limit);
+ exit(1);
+ }
+ return value;
+}
+
int main(int argc, char **argv)
{
fprintf(stderr, "\nCubemap starting.\n");
string config_filename = (argc == 1) ? "cubemap.config" : argv[1];
vector<ConfigLine> config = parse_config(config_filename);
- // Go through each (parsed) configuration line.
- int port = -1;
- for (unsigned i = 0; i < config.size(); ++i) {
- if (config[i].keyword == "port") {
- if (config[i].parameters.size() > 0 ||
- config[i].arguments.size() != 1) {
- fprintf(stderr, "ERROR: 'port' takes one argument and no parameters\n");
- exit(1);
- }
- port = atoi(config[i].arguments[0].c_str());
- }
- }
- if (port <= 0 || port > 65535) {
- fprintf(stderr, "ERROR: Missing or invalid 'port' statement in config file\n");
- exit(1);
- }
+ int port = fetch_config_int(config, "port", 1, 65535);
+ int num_servers = fetch_config_int(config, "num_servers", 1, 20000); // Insanely high max limit.
- servers = new ServerPool(NUM_SERVERS);
+ servers = new ServerPool(num_servers);
int server_sock = -1, old_port = -1;
if (argc == 4 && strcmp(argv[2], "-state") == 0) {
CubemapStateProto state;
state.set_server_sock(server_sock);
state.set_port(port);
- for (int i = 0; i < NUM_SERVERS; ++i) {
+ for (int i = 0; i < num_servers; ++i) {
servers->get_server(i)->stop();
CubemapStateProto local_state = servers->get_server(i)->serialize();