From a736a12ec610b495272e5a580aa24bd0504f40ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Tue, 1 Mar 2016 12:41:55 +0200 Subject: [PATCH] Messaging Thread on construction --- opt/ahenk/ahenkd.py | 22 +++++++- opt/ahenk/base/Scope.py | 7 +++ opt/ahenk/base/messaging/Messaging.py | 81 ++++++++++++++++----------- 3 files changed, 74 insertions(+), 36 deletions(-) diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index 3f4ab81..de8de8a 100644 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -6,8 +6,11 @@ from base.config.ConfigManager import ConfigManager from base.deamon.BaseDeamon import BaseDaemon from base.logger.AhenkLogger import Logger from base.Scope import Scope -#from base.messaging.Messaging import Messaging +from base.messaging.Messaging import Messaging +from multiprocessing import Process +from threading import Thread import sys,logging +import time class AhenkDeamon(BaseDaemon): @@ -31,6 +34,18 @@ class AhenkDeamon(BaseDaemon): globalscope.setLogger(logger) + xmpp = Messaging() + print("xmpp is created") + p = Process(target=xmpp.connect_to_server) + print("Process thread starting") + p.start() + print("Process tread started") + print("waiting 5sn ") + time.sleep(5) + print("sleep is over ") + xmpp.send_direct_message("asdasdas")# not working ->connection error + + if __name__ == '__main__': pidfilePath='/var/run/ahenk.pid' @@ -38,11 +53,12 @@ if __name__ == '__main__': ahenkdaemon = AhenkDeamon(pidfilePath) print (sys.argv) + if len(sys.argv) == 2: if sys.argv[1] == "start": print ("starting") - ahenkdaemon.start() - print (ahenkdaemon.get_pid()) + ahenkdaemon.run() + #print (ahenkdaemon.get_pid()) elif sys.argv[1] == 'stop': ahenkdaemon.stop() elif sys.argv[1] == 'restart': diff --git a/opt/ahenk/base/Scope.py b/opt/ahenk/base/Scope.py index a315800..7125c80 100644 --- a/opt/ahenk/base/Scope.py +++ b/opt/ahenk/base/Scope.py @@ -12,6 +12,7 @@ class Scope(object): super(Scope, self).__init__() self.customMap = {} self.configurationManager=None + self.messageManager=None self.logger=None @staticmethod @@ -43,3 +44,9 @@ class Scope(object): def setLogger(self,logger): self.logger = logger + + def getMessageManager(self): + return self.messageManager + + def setMessageManager(self,messageManager): + self.messageManager = messageManager diff --git a/opt/ahenk/base/messaging/Messaging.py b/opt/ahenk/base/messaging/Messaging.py index aa88b38..603b322 100644 --- a/opt/ahenk/base/messaging/Messaging.py +++ b/opt/ahenk/base/messaging/Messaging.py @@ -1,12 +1,16 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Author: Volkan Şahin +# Author: Volkan Şahin # Author: İsmail BAŞARAN import sys sys.path.append('../..') import slixmpp +import asyncio +import threading +from threading import Thread +from multiprocessing import Process from slixmpp.exceptions import IqError, IqTimeout -from ahenkd import AhenkDeamon +#from ahenkd import AhenkDeamon """ --fetch parameters of connection from conf file @@ -28,52 +32,62 @@ class Messaging(slixmpp.ClientXMPP): def __init__(self): # global scope of ahenk - scope = AhenkDeamon.scope() + #scope = AhenkDeamon.scope() # logger comes from ahenk deamon - self.logger = scope.getLogger() + #self.logger = scope.getLogger() # configurationManager comes from ahenk deamon - self.configurationManager = scope.getConfigurationManager() + #self.configurationManager = scope.getConfigurationManager() #set parameters slixmpp.ClientXMPP.__init__(self, self.configurationManager.get('CONNECTION', 'jid'), self.configurationManager.get('Connection_Param', 'password')) + + #slixmpp.ClientXMPP.__init__(self, "volkan@localhost", "volkan") + #self.receiver="caner@localhost" + """ self.nick = self.configurationManager.get('CONNECTION', 'nick') self.receiver=self.configurationManager.get('CONNECTION','receiverJid') self.sendfile=open(self.configurationManager.get('CONNECTION','sendFilePath'), 'rb') self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam') self.logger.info('Parameters were established') - - self.add_event_handler("session_start", self.start) - self.room=self.add_event_handler("groupchat_invite", self.invite_auto_accept) - + """ + self.room=None + self.register_extensions() self.add_listeners() + + print("connecting") + self.connect() + print("connected") #!!! you have to use modified slixmpp for file transfering #self.send_file() def add_listeners(self): + self.add_event_handler("session_start", self.session_start) self.add_event_handler("groupchat_message", self.recv_muc_message) self.add_event_handler("message", self.recv_direct_message) + #self.room=self.add_event_handler("groupchat_invite", self.invite_auto_accept) + #file_listeners #self.add_event_handler("socks5_connected", self.stream_opened) #self.add_event_handler("socks5_data", self.stream_data) #self.add_event_handler("socks5_closed", self.stream_closed) - self.logger.info('Listeners were added') + #self.logger.info('Listeners were added') def stream_opened(self, sid): - self.logger.info('Stream opened. %s', sid) + #self.logger.info('Stream opened. %s', sid) return open(self.receivefile, 'wb') def stream_data(self, data): - self.logger.info('Stream data.') + #self.logger.info('Stream data.') self.file.write(data) def stream_closed(self, exception): - self.logger.info('Stream closed. %s', exception) + #self.logger.info('Stream closed. %s', exception) self.file.close() #self.disconnect() @@ -100,7 +114,7 @@ class Messaging(slixmpp.ClientXMPP): - def start(self, event): + def session_start(self, event): self.get_roster() self.send_presence() @@ -122,39 +136,40 @@ class Messaging(slixmpp.ClientXMPP): print("%s : %s" % (str(msg['mucnick']),str(msg['body']))) def send_direct_message(self,msg): + #need connection control self.send_message(mto=self.receiver,mbody=msg,mtype='chat') def recv_direct_message(self, msg): if msg['type'] in ('chat', 'normal'): + self.disconnect() print ("%s : %s" % (msg['from'], msg['body'])) - def connectToServer(self): + def connect_to_server(self):# Connect to the XMPP server and start processing XMPP stanzas. + try: + + loop = asyncio.get_event_loop() + loop.run_until_complete(self.process()) + + #self.logger.info('Connection were established successfully') + return True + except Exception as e: + print('Connection to server is failed (%s)\n' % (e.strerror)) + #self.logger.error('Connection to server is failed! '+e) + return False + + def register_extensions(self): try: self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0045') # Multi-User Chat self.register_plugin('xep_0199') # XMPP Ping self.register_plugin('xep_0065') # SOCKS5 Bytestreams - self.logger.info('Plugins were registered: xep_0030,xep_0045,xep_0199,xep_0065') - - self.connect() - self.process() + #self.logger.info('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065') return True except Exception as e: - self.logger.error('Connection to server failed!') + #self.logger.error('Extension registration is failed!(%s)\n' % (e.errno, e.strerror)) return False + def create_conn_thread(self): -if __name__ == '__main__': - - xmpp = Messaging(None,None) - xmpp.register_plugin('xep_0030') # Service Discovery - xmpp.register_plugin('xep_0045') # Multi-User Chat - xmpp.register_plugin('xep_0199') # XMPP Ping - xmpp.register_plugin('xep_0065') # SOCKS5 Bytestreams - - self.logger.info('Plugins were registered: xep_0030,xep_0045,xep_0199,xep_0065') - - # Connect to the XMPP server and start processing XMPP stanzas. - xmpp.connect() - xmpp.process() + print("deneme")