From 014dd0f4682363a37e33aa8ea6f46fd1109fd7a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Tue, 17 May 2016 17:27:51 +0300 Subject: [PATCH] xmpp reconnection --- opt/ahenk/ahenkd.py | 9 +++++---- opt/ahenk/base/messaging/Messager.py | 16 ++-------------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index f881c79..1150b4e 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -98,8 +98,8 @@ class AhenkDeamon(BaseDaemon): def init_messager(self): messenger = Messager() - messanger_thread = threading.Thread(target=messenger.connect_to_server) - messanger_thread.start() + messenger_thread = threading.Thread(target=messenger.connect_to_server) + messenger_thread.start() while messenger.is_connected() is False: time.sleep(1) @@ -219,12 +219,13 @@ class AhenkDeamon(BaseDaemon): signal.signal(signal.SIGALRM, self.signal_handler) logger.info('[AhenkDeamon] Signal handler is set up') except Exception as e: - logger.error('[AhenkDeamon] Signal handler could not set up :' + e.errno + '-' + e.strerror) + logger.error('[AhenkDeamon] Signal handler could not set up. Error Message: {} '.format(str(e))) messager.send_direct_message('test') while True: - if messager.ping_lider() is False: + if messager.is_connected() is False: + logger.debug('reconnecting') Scope.getInstance().getLogger().warning('[AhenkDeamon] Connection is lost. Ahenk is trying for reconnection') messager = self.init_messager() time.sleep(1) diff --git a/opt/ahenk/base/messaging/Messager.py b/opt/ahenk/base/messaging/Messager.py index 4516721..1ea73ae 100644 --- a/opt/ahenk/base/messaging/Messager.py +++ b/opt/ahenk/base/messaging/Messager.py @@ -41,18 +41,6 @@ class Messager(slixmpp.ClientXMPP): self.register_extensions() self.add_listeners() - def ping_lider(self): - try: - result = self['xep_0199'].send_ping(jid=self.receiver,timeout=10) - if result is False: - self.logger.debug('[Messager] Couldn\'t send ping to lider.') - return False - else: - return True - except Exception as e: - self.logger.error('[Messager] A problem occurred while pinging to lider. Error Message:{}'.format(str(e))) - return False - def add_listeners(self): self.add_event_handler('session_start', self.session_start) self.add_event_handler('session_end', self.session_end) @@ -133,7 +121,7 @@ class Messager(slixmpp.ClientXMPP): j = json.loads(str(msg['body'])) self.logger.debug('[Messager] Received message: {}'.format(str(msg['body']))) message_type = j['type'] - self.logger.debug('[Messager] Fired event is: {}' .format(message_type)) + self.logger.debug('[Messager] Fired event is: {}'.format(message_type)) print('----->' + str(msg['body'])) self.event_manger.fireEvent(message_type, str(msg['body'])) @@ -147,7 +135,7 @@ class Messager(slixmpp.ClientXMPP): self.logger.debug('[Messager] Connection were established successfully') return True except Exception as e: - self.logger.error('[Messager] Connection to server is failed! ' + e) + self.logger.error('[Messager] Connection to server is failed! Error Message: {}'.format(str(e))) return False def set_file_name_md5(self):