added untested reconnection code.

This commit is contained in:
jesopo 2016-03-30 19:32:14 +01:00
parent 87af05d4b6
commit d8cce800d1
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3

View file

@ -29,11 +29,11 @@ class Bot(object):
sys.stderr.write("Failed to connect to %s\n" % str(server)) sys.stderr.write("Failed to connect to %s\n" % str(server))
traceback.print_exc() traceback.print_exc()
return False return False
self.poll.register(server.fileno(), select.EPOLLOUT)
return True return True
def connect_all(self): def connect_all(self):
for server in self.servers.values(): for server in self.servers.values():
if self.connect(server): self.connect(server)
self.poll.register(server.fileno(), select.EPOLLOUT)
def add_timer(self, function, delay, *args, **kwargs): def add_timer(self, function, delay, *args, **kwargs):
timer = Timer.Timer(function, delay, *args, **kwargs) timer = Timer.Timer(function, delay, *args, **kwargs)
@ -64,6 +64,15 @@ class Bot(object):
def since_last_read(self, server): def since_last_read(self, server):
return time.time()-server.last_read return time.time()-server.last_read
def disconnect(self, server):
self.poll.unregister(server.fileno())
del self.servers[server.fileno()]
def reconnect(self, server):
IRCServer.Server.__init__(server)
if self.connect(server):
self.servers[server.fileno()] = server
def run(self): def run(self):
while self.running: while self.running:
self.lock.acquire() self.lock.acquire()
@ -92,10 +101,13 @@ class Bot(object):
print("pingout from %s" % str(server)) print("pingout from %s" % str(server))
server.disconnect() server.disconnect()
if not server.connected: if not server.connected:
self.poll.unregister(server.fileno()) self.disconnect(server)
del self.servers[server.fileno()]
# add reconnect here reconnect_delay = self.config.get("reconnect-delay", 10)
print("disconnected from %s" % str(server)) self.add_timer(self.reconnect, reconnect_delay, server)
print("disconnected from %s, reconnecting in %d seconds" % (
str(server), reconnect_delay))
elif server.waiting_send(): elif server.waiting_send():
self.register_both(server) self.register_both(server)
self.lock.release() self.lock.release()