def run(self): """ Continuously verify for the correct operation of the registered clients """ import time while True: #sleep for a while at every loop time.sleep(DirectoryChecker.LOOP_WAIT) #generate the list of registered users users = [i[0] for i in self.__directory.directory_query()] self.__logger.debug("%d users are active" % len(users)) for username in users: #retrieve the bind information for each user res = self.__directory.directory_query(username) username, address, port = res[0] if len(res) == 1 else ( username, None, None) if address != None and port != None: try: self.__logger.debug("connecting to %s:%d" % (address, port)) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((address, port)) #use the protocol wrapper to simplify socket handling proto = ProtocolWrapper(sock, (address, port)) #send a ping, and wait for a pong self.__logger.debug("sending ping") proto.send(p.T_PING) msg = proto.recv() if msg != None and msg.type == p.T_PONG: self.__logger.info("USER %s OK" % username) proto.close() else: raise Exception, "no PONG received" except Exception, e: #client is misbehaving, deregister it self.__logger.error("USER %s ERROR (%s)" % (username, str(e))) self.__directory.directory_deregister(username)
def __init__(self, directory, clisock, addrinfo): """ The thread takes as input three arguments: 1) the instance of the directory object that accepted the connection 2) the connected socket associated with the client 3) the address info tuple """ threading.Thread.__init__(self) self.username = None self.password = None self.bind_address = None self.bind_port = None self.__directory = directory self.__protocol = ProtocolWrapper(clisock, addrinfo) #we are not interested in joining these threads self.daemon = True #the logger self.__logger = logging.getLogger("client")