Ejemplo n.º 1
0
 def serve(self):
     while self.continue_serving:
         try:
             self.conn, addr = self.sock.accept()
             print "Got connection from " + str(addr)
             self.conn.settimeout(SOCKET_TIMEOUT)
             if self.authorizeConnection():
                 while self.continue_serving:  # redundant check here to quit on Ctrl+C
                     try:
                         proto_bin = self.conn.recv(4096)
                     except timeout:
                         continue  # do nothing, the timeout is only set so we can poll for Ctrl+C
                     if not proto_bin:  # successfully recving no data implies conn closed
                         break  # so break out of the conn specific loop to await another conn
                     command = Command()
                     command.ParseFromString(proto_bin)
                     self.last_command = CommandWrapper(command)
                     if (command.HasField('intended_command')
                             and command.intended_command
                             == Command.DISCONNECT):
                         self.conn.close()
             else:
                 self.conn.close()
                 logger.warning("Invalid login attempt from " + str(addr))
         except timeout:
             pass  # this just means an accept has timed out, which is expected
         except Exception, e:
             logger.error("Controls Server error!", error=e)