+
+// Find all port statements in the configuration file, and create acceptors for htem.
+vector<Acceptor *> create_acceptors(
+ const vector<ConfigLine> &config,
+ map<int, Acceptor *> *deserialized_acceptors)
+{
+ vector<Acceptor *> acceptors;
+ for (unsigned i = 0; i < config.size(); ++i) {
+ if (config[i].keyword != "port") {
+ continue;
+ }
+ if (config[i].arguments.size() != 1) {
+ fprintf(stderr, "ERROR: 'port' takes exactly one argument\n");
+ exit(1);
+ }
+ int port = atoi(config[i].arguments[0].c_str());
+ if (port < 1 || port >= 65536) {
+ fprintf(stderr, "WARNING: port %d is out of range (must be [1,65536>), ignoring\n", port);
+ continue;
+ }
+
+ Acceptor *acceptor = NULL;
+ map<int, Acceptor *>::iterator deserialized_acceptor_it =
+ deserialized_acceptors->find(port);
+ if (deserialized_acceptor_it != deserialized_acceptors->end()) {
+ acceptor = deserialized_acceptor_it->second;
+ deserialized_acceptors->erase(deserialized_acceptor_it);
+ } else {
+ int server_sock = create_server_socket(port, TCP_SOCKET);
+ acceptor = new Acceptor(server_sock, port);
+ }
+ acceptor->run();
+ acceptors.push_back(acceptor);
+ }
+
+ // Close all acceptors that are no longer in the configuration file.
+ for (map<int, Acceptor *>::iterator acceptor_it = deserialized_acceptors->begin();
+ acceptor_it != deserialized_acceptors->end();
+ ++acceptor_it) {
+ acceptor_it->second->close_socket();
+ delete acceptor_it->second;
+ }
+
+ return acceptors;
+}
+