response message create-send

This commit is contained in:
Volkan Şahin 2016-03-30 18:51:50 +03:00
parent a282695c10
commit dca5a2d720
5 changed files with 47 additions and 23 deletions

View file

@ -29,11 +29,12 @@ class ExecutionManager(object):
self.logger = scope.getLogger()
self.db_service = scope.getDbService()
self.event_manager.register_event(MessageType.EXECUTE_SCRIPT, self.execute_script)
self.event_manager.register_event(MessageType.REQUEST_FILE, self.request_file)
self.event_manager.register_event(MessageType.MOVE_FILE, self.move_file)
self.event_manager.register_event(MessageType.EXECUTE_TASK, self.execute_task)
self.event_manager.register_event(MessageType.EXECUTE_POLICY, self.execute_policy)
#TODO DEBUG
self.event_manager.register_event(str(MessageType.EXECUTE_SCRIPT), self.execute_script)
self.event_manager.register_event(str(MessageType.REQUEST_FILE), self.request_file)
self.event_manager.register_event(str(MessageType.MOVE_FILE), self.move_file)
self.event_manager.register_event(str(MessageType.EXECUTE_TASK), self.execute_task)
self.event_manager.register_event('EXECUTE_POLICY', self.execute_policy)
def execute_policy(self, arg):
self.logger.debug('[ExecutionManager] Updating policies...')

View file

@ -112,7 +112,6 @@ class Messager(slixmpp.ClientXMPP):
print('----->'+str(msg['body']))
self.event_manger.fireEvent(message_type, str(msg['body']))
def connect_to_server(self): # Connect to the XMPP server and start processing XMPP stanzas.
try:
self.logger.debug('[Messager] Connecting to server as thread')

View file

@ -4,16 +4,17 @@
import datetime
class Result(object):
class Response(object):
"""docstring for Plugin"""
def __init__(self, type, id, code=None, message=None, context=None):
def __init__(self, type, id, code=None, message=None, data=None, content_type=None):
self.type = type
self.id = id
self.code = code
self.message = message
self.context = context.data
self.content_type = context.content_type
self.data = data
self.content_type = content_type
self.timestamp = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
def get_type(self):
@ -41,21 +42,17 @@ class Result(object):
self.message = message
def get_data(self):
return self.context.data
return self.data
def set_data(self, data):
self.context.data = data
self.data = data
def get_content_type(self):
return self.context.content_type
return self.content_type
def set_content_type(self, content_type):
self.context.content_type = content_type
self.content_type = content_type
def get_timestamp(self):
return self.timestamp
class Context():
def __init__(self, data=None, content_type=None):
self.data = type
self.content_type = id

View file

@ -4,17 +4,26 @@
import threading
from base.Scope import Scope
from base.model.Response import Response
from base.model.MessageType import MessageType
from base.model.MessageCode import MessageCode
from base.messaging.Messaging import Messaging
class Context(object):
def __init__(self):
self.data = {}
def put(self,var_name,data):
def put(self, var_name, data):
self.data[var_name] = data
def get(self, var_name):
return self.data[var_name]
def empty_data(self):
self.data = {}
class Plugin(threading.Thread):
"""
This is a thread inherit class and have a queue.
@ -25,12 +34,18 @@ class Plugin(threading.Thread):
threading.Thread.__init__(self)
self.name = name
self.InQueue = InQueue
scope = Scope.getInstance()
self.logger = scope.getLogger()
self.response_queue = scope.getResponseQueue()
self.messaging = scope.getMessageManager()
self.messager =None
self.keep_run = True
self.context = Context()
def run(self):
while self.keep_run:
try:
item_obj = self.InQueue.get(block=True)
@ -38,26 +53,36 @@ class Plugin(threading.Thread):
print(obj_name)
if obj_name == "TASK":
command = Scope.getInstance().getPluginManager().findCommand(self.getName(), item_obj.command_cls_id)
command.handle_task(item_obj,self.context)
command.handle_task(item_obj, self.context)
# TODO create response message from context and item_obj. item_obj is task
#TODO Message Code keep
#item_obj.id ??
response = Response(type=MessageType.TASK_STATUS, id='id', code=MessageCode.TASK_PROCESSED, message='__message__', data=self.context.get('data'), content_type=self.context.get('content_type'))
#self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG
Scope.getInstance().getMessager().send_direct_message(self.messaging.response_msg(response)) #TODO REMOVE
# Empty context for next use
self.context.empty_data()
# TODO add result to response queue
elif obj_name == "PROFILE":
plugin = item_obj.plugin
plugin_name = plugin.name
profile_data = item_obj.profile_data
policy_module = Scope.getInstance().getPluginManager().findPolicyModule(plugin_name)
policy_module.handle_policy(profile_data,self.context)
# TODO create response message from context and item_obj. item_obj is profile
policy_module.handle_policy(profile_data, self.context)
#TODO Message Code keep
#item_obj.id ??
response = Response(type=MessageType.POLICY_STATUS, id='id', code=MessageCode.POLICY_PROCESSED, message='__message__', data=self.context.get('data'), content_type=self.context.get('content_type'))
#self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG
Scope.getInstance().getMessager().send_direct_message(self.messaging.response_msg(response))#TODO REMOVE
# Empty context for next use
self.context.empty_data()
elif obj_name == "KILL_SIGNAL":
self.keep_run = False
self.logger.debug('[Plugin] Killing queue ! Plugin Name : ' + str(self.name))

View file

@ -4,4 +4,6 @@
def handle_policy(profile_data,context):
context.put('data','dataa')
context.put('content_type','type')
print("This is policy file - plugin 1")