Messaging Thread on construction

This commit is contained in:
Volkan Şahin 2016-03-01 12:41:55 +02:00
parent e082797042
commit a736a12ec6
3 changed files with 74 additions and 36 deletions

View file

@ -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':

View file

@ -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

View file

@ -1,12 +1,16 @@
#!/usr/bin/env python3
# -*- 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>
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")