mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-27 21:19:31 +01:00
Irc: wrap (re)connecting in a while loop instead of calling it recursively
Also, remove unnecessary comments / ENDBURST hook from relay. Ping handling seems to work fine now! Closes #57. Closes #42.
This commit is contained in:
parent
99fd6060a7
commit
836d0e9701
51
main.py
51
main.py
@ -64,22 +64,29 @@ class Irc():
|
|||||||
def connect(self):
|
def connect(self):
|
||||||
ip = self.serverdata["ip"]
|
ip = self.serverdata["ip"]
|
||||||
port = self.serverdata["port"]
|
port = self.serverdata["port"]
|
||||||
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
|
while True:
|
||||||
try:
|
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
|
||||||
# Initial connection timeout is a lot smaller than the timeout after
|
try:
|
||||||
# we've connected; this is intentional.
|
# Initial connection timeout is a lot smaller than the timeout after
|
||||||
self.socket = socket.create_connection((ip, port), timeout=1)
|
# we've connected; this is intentional.
|
||||||
self.socket.setblocking(0)
|
self.socket = socket.create_connection((ip, port), timeout=1)
|
||||||
self.socket.settimeout(self.pingtimeout)
|
self.socket.setblocking(0)
|
||||||
self.proto.connect(self)
|
self.socket.settimeout(self.pingtimeout)
|
||||||
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
self.proto.connect(self)
|
||||||
log.warning('(%s) Failed to connect to IRC: %s: %s',
|
self.spawnMain()
|
||||||
self.name, type(e).__name__, str(e))
|
self.schedulePing()
|
||||||
self.disconnect()
|
self.run()
|
||||||
else:
|
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
||||||
self.spawnMain()
|
log.warning('(%s) Failed to connect to IRC: %s: %s',
|
||||||
self.schedulePing()
|
self.name, type(e).__name__, str(e))
|
||||||
self.run()
|
self.disconnect()
|
||||||
|
autoconnect = self.serverdata.get('autoconnect')
|
||||||
|
if autoconnect is not None and autoconnect >= 0:
|
||||||
|
log.info('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
||||||
|
time.sleep(autoconnect)
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
log.debug('(%s) Canceling pingTimer at %s due to disconnect() call', self.name, time.time())
|
log.debug('(%s) Canceling pingTimer at %s due to disconnect() call', self.name, time.time())
|
||||||
@ -89,11 +96,6 @@ class Irc():
|
|||||||
self.pingTimer.cancel()
|
self.pingTimer.cancel()
|
||||||
except: # Socket timed out during creation; ignore
|
except: # Socket timed out during creation; ignore
|
||||||
pass
|
pass
|
||||||
autoconnect = self.serverdata.get('autoconnect')
|
|
||||||
if autoconnect is not None and autoconnect >= 1110:
|
|
||||||
log.info('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
|
||||||
time.sleep(autoconnect)
|
|
||||||
self.connect()
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
buf = ""
|
buf = ""
|
||||||
@ -122,12 +124,7 @@ class Irc():
|
|||||||
data = data.replace('\n', ' ')
|
data = data.replace('\n', ' ')
|
||||||
data = data.encode("utf-8") + b"\n"
|
data = data.encode("utf-8") + b"\n"
|
||||||
log.debug("(%s) -> %s", self.name, data.decode("utf-8").strip("\n"))
|
log.debug("(%s) -> %s", self.name, data.decode("utf-8").strip("\n"))
|
||||||
try:
|
self.socket.send(data)
|
||||||
self.socket.send(data)
|
|
||||||
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
|
||||||
log.warning('(%s) Disconnected from IRC: %s: %s',
|
|
||||||
self.name, type(e).__name__, str(e))
|
|
||||||
raise ProtocolError
|
|
||||||
|
|
||||||
def schedulePing(self):
|
def schedulePing(self):
|
||||||
self.proto.pingServer(self)
|
self.proto.pingServer(self)
|
||||||
|
@ -18,6 +18,7 @@ relayusers = defaultdict(dict)
|
|||||||
def normalizeNick(irc, netname, nick, separator="/"):
|
def normalizeNick(irc, netname, nick, separator="/"):
|
||||||
# Block until we know the IRC network's nick length (after capabilities
|
# Block until we know the IRC network's nick length (after capabilities
|
||||||
# are sent)
|
# are sent)
|
||||||
|
log.debug('(%s) normalizeNick: waiting for irc.connected', irc.name)
|
||||||
irc.connected.wait()
|
irc.connected.wait()
|
||||||
|
|
||||||
orig_nick = nick
|
orig_nick = nick
|
||||||
@ -675,6 +676,7 @@ def delink(irc, source, args):
|
|||||||
utils.msg(irc, source, 'Error: no such relay %r.' % channel)
|
utils.msg(irc, source, 'Error: no such relay %r.' % channel)
|
||||||
|
|
||||||
def initializeAll(irc):
|
def initializeAll(irc):
|
||||||
|
log.debug('(%s) initializeAll: waiting for utils.started', irc.name)
|
||||||
utils.started.wait()
|
utils.started.wait()
|
||||||
for chanpair, entrydata in db.items():
|
for chanpair, entrydata in db.items():
|
||||||
network, channel = chanpair
|
network, channel = chanpair
|
||||||
@ -691,18 +693,3 @@ def main():
|
|||||||
thread = threading.Thread(target=scheduler.run)
|
thread = threading.Thread(target=scheduler.run)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
'''
|
|
||||||
for ircobj in utils.networkobjects.values():
|
|
||||||
initializeAll(irc)
|
|
||||||
|
|
||||||
# Same goes for all the other initialization stuff; we only
|
|
||||||
# want it to happen once.
|
|
||||||
for network, ircobj in utils.networkobjects.items():
|
|
||||||
if ircobj.name != irc.name:
|
|
||||||
irc.proto.spawnServer(irc, '%s.relay' % network)
|
|
||||||
'''
|
|
||||||
|
|
||||||
def handle_endburst(irc, numeric, command, args):
|
|
||||||
thread = threading.Thread(target=initializeAll, args=(irc,))
|
|
||||||
thread.start()
|
|
||||||
utils.add_hook(handle_endburst, "ENDBURST")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user