diff --git a/classes.py b/classes.py index 67f2822..04b1f3d 100644 --- a/classes.py +++ b/classes.py @@ -56,9 +56,9 @@ class Irc(): # HACK: Don't thread if we're running tests. self.connect() else: - self.connection_thread = threading.Thread(target = self.connect) + self.connection_thread = threading.Thread(target=self.connect, + name="Listener for %s" % self.name) self.connection_thread.start() - self.pingTimer = None def initVars(self): """ @@ -128,6 +128,8 @@ class Irc(): self.uplink = None self.start_ts = int(time.time()) + self.ping_thread = None + def connect(self): """ Runs the connect loop for the IRC object. This is usually called by @@ -211,8 +213,14 @@ class Irc(): # and run the listen loop. self.proto.connect() self.spawnMain() - log.info('(%s) Starting ping schedulers....', self.name) - self.schedulePing() + + # Spawn the ping loop as a separate thread. + log.info('(%s) Starting ping loop...', self.name) + self.ping_thread = threading.Thread(target=self.schedulePing, + name="Ping loop for %s" % self.name) + self.ping_thread.daemon = True + self.ping_thread.start() + log.info('(%s) Server ready; listening for data.', self.name) self.run() else: # Configuration error :( @@ -280,13 +288,12 @@ class Irc(): def _disconnect(self): """Handle disconnects from the remote server.""" - log.debug('(%s) Canceling pingTimer at %s due to _disconnect() call', self.name, time.time()) self.connected.clear() try: self.socket.close() - self.pingTimer.cancel() except: # Socket timed out during creation; ignore pass + # Internal hook signifying that a network has disconnected. self.callHooks([None, 'PYLINK_DISCONNECT', {}]) @@ -390,11 +397,15 @@ class Irc(): def schedulePing(self): """Schedules periodic pings in a loop.""" - self.proto.pingServer() - self.pingTimer = threading.Timer(self.pingfreq, self.schedulePing) - self.pingTimer.daemon = True - self.pingTimer.start() - log.debug('(%s) Ping scheduled at %s', self.name, time.time()) + + while not self.aborted.is_set(): + log.debug('(%s) Ping sent at %s', self.name, time.time()) + self.proto.pingServer() + + # Sleep for the time (frequency) between pings. + time.sleep(self.pingfreq) + + log.debug('(%s) Canceling ping_thread at %s due to self.aborted being set.', self.name, time.time()) def spawnMain(self): """Spawns the main PyLink client.""" @@ -451,17 +462,20 @@ class IrcServer(): name: The name of the server. internal: Whether the server is an internal PyLink PseudoServer. """ + def __init__(self, uplink, name, internal=False, desc="(None given)"): self.uplink = uplink self.users = set() self.internal = internal self.name = name.lower() self.desc = desc + def __repr__(self): return repr(self.__dict__) class IrcChannel(): """PyLink IRC channel class.""" + def __init__(self): # Initialize variables, such as the topic, user list, TS, who's opped, etc. self.users = set()