mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-28 05:29:25 +01:00
main: thread Irc.connect() and begin work on autoreconnection
This commit is contained in:
parent
bbe71e22ab
commit
293bfff813
23
main.py
23
main.py
@ -44,24 +44,35 @@ class Irc():
|
|||||||
self.sid = self.serverdata["sid"]
|
self.sid = self.serverdata["sid"]
|
||||||
self.botdata = conf['bot']
|
self.botdata = conf['bot']
|
||||||
self.proto = proto
|
self.proto = proto
|
||||||
self.connect()
|
connection_thread = threading.Thread(target = self.connect)
|
||||||
|
connection_thread.start()
|
||||||
|
|
||||||
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)
|
log.info("Connecting to network %r on %s:%s", self.name, ip, port)
|
||||||
self.socket = socket.socket()
|
# Initial connection timeout is a lot smaller than the timeout after
|
||||||
|
# we've connected; this is intentional.
|
||||||
|
self.socket = socket.create_connection((ip, port), timeout=10)
|
||||||
self.socket.setblocking(0)
|
self.socket.setblocking(0)
|
||||||
self.socket.settimeout(180)
|
self.socket.settimeout(180)
|
||||||
self.socket.connect((ip, port))
|
try:
|
||||||
self.proto.connect(self)
|
self.proto.connect(self)
|
||||||
reading_thread = threading.Thread(target = self.run)
|
except (socket.error, socket.timeout):
|
||||||
|
log.error('(%s) Failed to connect to IRC: %s: %s',
|
||||||
|
self.name, type(e).__name__, str(e))
|
||||||
|
self.disconnect()
|
||||||
self.connected = True
|
self.connected = True
|
||||||
reading_thread.start()
|
self.run()
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
self.connected = False
|
self.connected = False
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
|
autoconnect = self.serverdata.get('autoconnect')
|
||||||
|
if autoconnect is not None and autoconnect >= 0:
|
||||||
|
log.warning('(%s) Going to auto-reconnect in %s seconds.', self.name, autoconnect)
|
||||||
|
time.sleep(autoconnect)
|
||||||
|
self.connect()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
buf = ""
|
buf = ""
|
||||||
@ -77,7 +88,7 @@ class Irc():
|
|||||||
log.debug("(%s) <- %s", self.name, line)
|
log.debug("(%s) <- %s", self.name, line)
|
||||||
proto.handle_events(self, line)
|
proto.handle_events(self, line)
|
||||||
except (socket.error, classes.ProtocolError) as e:
|
except (socket.error, classes.ProtocolError) as e:
|
||||||
log.error('Disconnected from network %r: %s: %s, exiting.',
|
log.error('(%s) Disconnected from IRC: %s: %s',
|
||||||
self.name, type(e).__name__, str(e))
|
self.name, type(e).__name__, str(e))
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user