mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-14 07:22:18 +03:00
Messaging Thread on construction
This commit is contained in:
parent
e082797042
commit
a736a12ec6
3 changed files with 74 additions and 36 deletions
|
@ -6,8 +6,11 @@ from base.config.ConfigManager import ConfigManager
|
||||||
from base.deamon.BaseDeamon import BaseDaemon
|
from base.deamon.BaseDeamon import BaseDaemon
|
||||||
from base.logger.AhenkLogger import Logger
|
from base.logger.AhenkLogger import Logger
|
||||||
from base.Scope import Scope
|
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 sys,logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class AhenkDeamon(BaseDaemon):
|
class AhenkDeamon(BaseDaemon):
|
||||||
|
@ -31,6 +34,18 @@ class AhenkDeamon(BaseDaemon):
|
||||||
globalscope.setLogger(logger)
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
pidfilePath='/var/run/ahenk.pid'
|
pidfilePath='/var/run/ahenk.pid'
|
||||||
|
@ -38,11 +53,12 @@ if __name__ == '__main__':
|
||||||
ahenkdaemon = AhenkDeamon(pidfilePath)
|
ahenkdaemon = AhenkDeamon(pidfilePath)
|
||||||
|
|
||||||
print (sys.argv)
|
print (sys.argv)
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
if sys.argv[1] == "start":
|
if sys.argv[1] == "start":
|
||||||
print ("starting")
|
print ("starting")
|
||||||
ahenkdaemon.start()
|
ahenkdaemon.run()
|
||||||
print (ahenkdaemon.get_pid())
|
#print (ahenkdaemon.get_pid())
|
||||||
elif sys.argv[1] == 'stop':
|
elif sys.argv[1] == 'stop':
|
||||||
ahenkdaemon.stop()
|
ahenkdaemon.stop()
|
||||||
elif sys.argv[1] == 'restart':
|
elif sys.argv[1] == 'restart':
|
||||||
|
|
|
@ -12,6 +12,7 @@ class Scope(object):
|
||||||
super(Scope, self).__init__()
|
super(Scope, self).__init__()
|
||||||
self.customMap = {}
|
self.customMap = {}
|
||||||
self.configurationManager=None
|
self.configurationManager=None
|
||||||
|
self.messageManager=None
|
||||||
self.logger=None
|
self.logger=None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -43,3 +44,9 @@ class Scope(object):
|
||||||
|
|
||||||
def setLogger(self,logger):
|
def setLogger(self,logger):
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
|
|
||||||
|
def getMessageManager(self):
|
||||||
|
return self.messageManager
|
||||||
|
|
||||||
|
def setMessageManager(self,messageManager):
|
||||||
|
self.messageManager = messageManager
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Author: Volkan Şahin <basaran.ismaill@gmail.com>
|
# Author: Volkan Şahin <bm.volkansahin@gmail.com>
|
||||||
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
||||||
import sys
|
import sys
|
||||||
sys.path.append('../..')
|
sys.path.append('../..')
|
||||||
import slixmpp
|
import slixmpp
|
||||||
|
import asyncio
|
||||||
|
import threading
|
||||||
|
from threading import Thread
|
||||||
|
from multiprocessing import Process
|
||||||
from slixmpp.exceptions import IqError, IqTimeout
|
from slixmpp.exceptions import IqError, IqTimeout
|
||||||
from ahenkd import AhenkDeamon
|
#from ahenkd import AhenkDeamon
|
||||||
|
|
||||||
"""
|
"""
|
||||||
--fetch parameters of connection from conf file
|
--fetch parameters of connection from conf file
|
||||||
|
@ -28,52 +32,62 @@ class Messaging(slixmpp.ClientXMPP):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
# global scope of ahenk
|
# global scope of ahenk
|
||||||
scope = AhenkDeamon.scope()
|
#scope = AhenkDeamon.scope()
|
||||||
|
|
||||||
# logger comes from ahenk deamon
|
# logger comes from ahenk deamon
|
||||||
self.logger = scope.getLogger()
|
#self.logger = scope.getLogger()
|
||||||
|
|
||||||
# configurationManager comes from ahenk deamon
|
# configurationManager comes from ahenk deamon
|
||||||
self.configurationManager = scope.getConfigurationManager()
|
#self.configurationManager = scope.getConfigurationManager()
|
||||||
|
|
||||||
#set parameters
|
#set parameters
|
||||||
slixmpp.ClientXMPP.__init__(self, self.configurationManager.get('CONNECTION', 'jid'), self.configurationManager.get('Connection_Param', 'password'))
|
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.nick = self.configurationManager.get('CONNECTION', 'nick')
|
||||||
self.receiver=self.configurationManager.get('CONNECTION','receiverJid')
|
self.receiver=self.configurationManager.get('CONNECTION','receiverJid')
|
||||||
self.sendfile=open(self.configurationManager.get('CONNECTION','sendFilePath'), 'rb')
|
self.sendfile=open(self.configurationManager.get('CONNECTION','sendFilePath'), 'rb')
|
||||||
self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam')
|
self.receivefile=self.configurationManager.get('CONNECTION', 'receiveFileParam')
|
||||||
self.logger.info('Parameters were established')
|
self.logger.info('Parameters were established')
|
||||||
|
"""
|
||||||
self.add_event_handler("session_start", self.start)
|
self.room=None
|
||||||
self.room=self.add_event_handler("groupchat_invite", self.invite_auto_accept)
|
self.register_extensions()
|
||||||
|
|
||||||
self.add_listeners()
|
self.add_listeners()
|
||||||
|
|
||||||
|
print("connecting")
|
||||||
|
self.connect()
|
||||||
|
print("connected")
|
||||||
#!!! you have to use modified slixmpp for file transfering
|
#!!! you have to use modified slixmpp for file transfering
|
||||||
#self.send_file()
|
#self.send_file()
|
||||||
|
|
||||||
def add_listeners(self):
|
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("groupchat_message", self.recv_muc_message)
|
||||||
self.add_event_handler("message", self.recv_direct_message)
|
self.add_event_handler("message", self.recv_direct_message)
|
||||||
|
|
||||||
|
#self.room=self.add_event_handler("groupchat_invite", self.invite_auto_accept)
|
||||||
|
|
||||||
#file_listeners
|
#file_listeners
|
||||||
#self.add_event_handler("socks5_connected", self.stream_opened)
|
#self.add_event_handler("socks5_connected", self.stream_opened)
|
||||||
#self.add_event_handler("socks5_data", self.stream_data)
|
#self.add_event_handler("socks5_data", self.stream_data)
|
||||||
#self.add_event_handler("socks5_closed", self.stream_closed)
|
#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):
|
def stream_opened(self, sid):
|
||||||
self.logger.info('Stream opened. %s', sid)
|
#self.logger.info('Stream opened. %s', sid)
|
||||||
return open(self.receivefile, 'wb')
|
return open(self.receivefile, 'wb')
|
||||||
|
|
||||||
def stream_data(self, data):
|
def stream_data(self, data):
|
||||||
self.logger.info('Stream data.')
|
#self.logger.info('Stream data.')
|
||||||
self.file.write(data)
|
self.file.write(data)
|
||||||
|
|
||||||
def stream_closed(self, exception):
|
def stream_closed(self, exception):
|
||||||
self.logger.info('Stream closed. %s', exception)
|
#self.logger.info('Stream closed. %s', exception)
|
||||||
self.file.close()
|
self.file.close()
|
||||||
#self.disconnect()
|
#self.disconnect()
|
||||||
|
|
||||||
|
@ -100,7 +114,7 @@ class Messaging(slixmpp.ClientXMPP):
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def start(self, event):
|
def session_start(self, event):
|
||||||
self.get_roster()
|
self.get_roster()
|
||||||
self.send_presence()
|
self.send_presence()
|
||||||
|
|
||||||
|
@ -122,39 +136,40 @@ class Messaging(slixmpp.ClientXMPP):
|
||||||
print("%s : %s" % (str(msg['mucnick']),str(msg['body'])))
|
print("%s : %s" % (str(msg['mucnick']),str(msg['body'])))
|
||||||
|
|
||||||
def send_direct_message(self,msg):
|
def send_direct_message(self,msg):
|
||||||
|
#need connection control
|
||||||
self.send_message(mto=self.receiver,mbody=msg,mtype='chat')
|
self.send_message(mto=self.receiver,mbody=msg,mtype='chat')
|
||||||
|
|
||||||
def recv_direct_message(self, msg):
|
def recv_direct_message(self, msg):
|
||||||
if msg['type'] in ('chat', 'normal'):
|
if msg['type'] in ('chat', 'normal'):
|
||||||
|
self.disconnect()
|
||||||
print ("%s : %s" % (msg['from'], msg['body']))
|
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:
|
try:
|
||||||
self.register_plugin('xep_0030') # Service Discovery
|
self.register_plugin('xep_0030') # Service Discovery
|
||||||
self.register_plugin('xep_0045') # Multi-User Chat
|
self.register_plugin('xep_0045') # Multi-User Chat
|
||||||
self.register_plugin('xep_0199') # XMPP Ping
|
self.register_plugin('xep_0199') # XMPP Ping
|
||||||
self.register_plugin('xep_0065') # SOCKS5 Bytestreams
|
self.register_plugin('xep_0065') # SOCKS5 Bytestreams
|
||||||
|
|
||||||
self.logger.info('Plugins were registered: xep_0030,xep_0045,xep_0199,xep_0065')
|
#self.logger.info('Extension were registered: xep_0030,xep_0045,xep_0199,xep_0065')
|
||||||
|
|
||||||
self.connect()
|
|
||||||
self.process()
|
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
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
|
return False
|
||||||
|
|
||||||
|
def create_conn_thread(self):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
print("deneme")
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
Loading…
Reference in a new issue