Ejemplo n.º 1
0
    def main(self):

        config_data = ConfigUtils()
        if not config_data.validate():
            print "Error- invalid data in config file"
            logging.error("Error- invalid data in config file")

            sys.exit(1)

        self.supported_machines_list = config_data.supported_machines
        self.max_machines_limit = config_data.max_machines

        # Set up the listening socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = socket.gethostname()  # local
        try:
            s.bind((host, config_data.port))
        except socket.error as err:
            print "Error: socket error\n" + err.message
            logging.error("Error: socket error\n" + err.message)
            sys.exit(1)
        print "Server is listening on port {p}...".format(p=config_data.port)

        s.listen(30)

        # accept connections in a loop
        while True:
            (connection, address) = s.accept()
            print "Got connection"
            self.connection_lock.acquire()

            if len(self.connections) >= config_data.max_clients:
                try:
                    connection.send("Sorry, too many clients connected")
                    logging.error("Sorry, too many clients connected")
                    connection.close()
                except:
                    print "Error on closing connection"
                    logging.error("Error on closing connection")
            else:
                connection.send("Connection succeeded")
                logging.info("Connection succeeded")
                self.connections.add(connection)
                self.client_connections_map[connection] = self.clients_id_rise
                self.clients_id_rise += 1
                self.connection_lock.release()
                threading.Thread(target=self.receive, args=[connection]).start()
Ejemplo n.º 2
0
    def main(self):

        config_data = ConfigUtils()
        if not config_data.validate():
            print "Error- invalid data in config file"
            logging.error("Error- invalid data in config file")

            sys.exit(1)

        self.supported_machines_list = config_data.supported_machines
        self.max_machines_limit = config_data.max_machines

        # Set up the listening socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = socket.gethostname()  # local
        try:
            s.bind((host, config_data.port))
        except socket.error as err:
            print "Error: socket error\n" + err.message
            logging.error("Error: socket error\n" + err.message)
            sys.exit(1)
        print "Server is listening on port {p}...".format(p=config_data.port)

        s.listen(30)

        # accept connections in a loop
        while True:
            (connection, address) = s.accept()
            print "Got connection"
            self.connection_lock.acquire()

            if len(self.connections) >= config_data.max_clients:
                try:
                    connection.send("Sorry, too many clients connected")
                    logging.error("Sorry, too many clients connected")
                    connection.close()
                except:
                    print "Error on closing connection"
                    logging.error("Error on closing connection")
            else:
                connection.send("Connection succeeded")
                logging.info("Connection succeeded")
                self.connections.add(connection)
                self.client_connections_map[connection] = self.clients_id_rise
                self.clients_id_rise += 1
                self.connection_lock.release()
                threading.Thread(target=self.receive, args=[connection]).start()