From 9babd239c454e21c77c5f3b1ae00961fdeaec95a Mon Sep 17 00:00:00 2001 From: haribo Date: Fri, 29 Apr 2016 10:08:34 +0200 Subject: [PATCH] agent & sender systems deleted --- src/alert_manager/agent.py | 59 ----------------------------------------------------------- src/alert_manager/apps.py | 5 ----- src/analyzer/agent.py | 30 ------------------------------ src/analyzer/apps.py | 5 ----- src/common/agent.py | 122 -------------------------------------------------------------------------------------------------------------------------- src/common/sender.py | 25 ------------------------- src/common/tests.py | 32 +------------------------------- src/dashboard/agent.py | 21 --------------------- src/dashboard/apps.py | 5 ----- src/majordome/agent.py | 81 --------------------------------------------------------------------------------- src/majordome/apps.py | 5 ----- src/monitoring/agent.py | 19 ------------------- src/monitoring/apps.py | 5 ----- src/observation_manager/agent.py | 62 -------------------------------------------------------------- src/observation_manager/apps.py | 6 ------ src/scheduler/agent.py | 34 ---------------------------------- src/scheduler/apps.py | 5 ----- 17 files changed, 1 insertion(+), 520 deletions(-) delete mode 100644 src/alert_manager/agent.py delete mode 100644 src/analyzer/agent.py delete mode 100644 src/common/agent.py delete mode 100644 src/common/sender.py delete mode 100644 src/dashboard/agent.py delete mode 100644 src/majordome/agent.py delete mode 100644 src/monitoring/agent.py delete mode 100644 src/observation_manager/agent.py delete mode 100644 src/scheduler/agent.py diff --git a/src/alert_manager/agent.py b/src/alert_manager/agent.py deleted file mode 100644 index 1c3a724..0000000 --- a/src/alert_manager/agent.py +++ /dev/null @@ -1,59 +0,0 @@ -from common.agent import Agent -from threading import Thread, Event - -class AlertManagerAgent(Agent): - - MSG_CHANGE_STRAT = "Change current strategy" - - def __init__(self): - Agent.__init__(self, Agent.ALERT_MANAGER) - self.actions_by_message[AlertManagerAgent.MSG_CHANGE_STRAT] = self.change_strategy - - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - - In the Alert Manager, this method creates the thread that listen to VOEvents - ''' - - self.voevent_listener = VOEventListener() - self.voevent_listener.start() - - - def change_strategy(self): - ''' - Its role and arguments are to be determined - ''' - #TODO: la méthode ... - pass - - def shutdown(self): - self.voevent_listener.shutdown_event.set() - self.voevent_listener.join() - Agent.shutdown(self) - -class VOEventListener(Thread): - ''' - Thread created by AlertManagerAgent - Listens to the VOEvent network and creates request with the good events. - ''' - - #TODO: définir les rôles de cette classe - - def __init__(self): - self.shutdown_event = Event() - self.shutdown_event.clear() - Thread.__init__(self, name="VOEventListener") - - def run(self): - ''' - Function called when the .start() is invoked - Calls the main loop for the VOEvent waiting/analyze - ''' - - pass - \ No newline at end of file diff --git a/src/alert_manager/apps.py b/src/alert_manager/apps.py index 211b235..cf3753d 100644 --- a/src/alert_manager/apps.py +++ b/src/alert_manager/apps.py @@ -3,8 +3,3 @@ from django.apps import AppConfig class AlertManagerConfig(AppConfig): name = 'alert_manager' - - def ready(self): - from alert_manager.agent import AlertManagerAgent - self.agent = AlertManagerAgent() - self.agent.start() diff --git a/src/analyzer/agent.py b/src/analyzer/agent.py deleted file mode 100644 index 5675cd2..0000000 --- a/src/analyzer/agent.py +++ /dev/null @@ -1,30 +0,0 @@ -from common.agent import Agent - -class AnalyzerAgent(Agent): - - MSG_ANALYZE = "Analyze next image" - - def __init__(self): - Agent.__init__(self, Agent.ANALYZER) - self.actions_by_message[AnalyzerAgent.MSG_ANALYZE] = self.analyze - - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - - In the Analyzer, this might do nothing - ''' - - pass - - - def analyze(self): - ''' - Its role and arguments are to be determined - ''' - #TODO: la méthode ... - pass \ No newline at end of file diff --git a/src/analyzer/apps.py b/src/analyzer/apps.py index 8db2255..504419d 100644 --- a/src/analyzer/apps.py +++ b/src/analyzer/apps.py @@ -2,8 +2,3 @@ from django.apps import AppConfig class AnalyzerConfig(AppConfig): name = 'analyzer' - - def ready(self): - from analyzer.agent import AnalyzerAgent - self.agent = AnalyzerAgent() - self.agent.start() \ No newline at end of file diff --git a/src/common/agent.py b/src/common/agent.py deleted file mode 100644 index 1c006b5..0000000 --- a/src/common/agent.py +++ /dev/null @@ -1,122 +0,0 @@ -import os -import configparser -from threading import Thread -import common -import socket -import sys - -import time - -BUFFER_SIZE = 256 -CONFIG_FILE = os.path.join(os.path.abspath(common.__path__[0]),"pyros_agent_config.ini") - -class Agent(Thread): - ''' - MEANT TO BE OVERRIDEN - Handler for the multi-threading modules and the inter-module communications. - Uses the library 'threading' for the threads, and 'socket' for the communications - - How to use it : - - Create a class inheriting from Agent, passing a following Agent name as parameter - - Override 'work method' : this method will be called in a separate thread when you will do MyClass.start() - - Override 'analyse_message' method : this method will be called when a message is received - - Call MyClass.start() to start the agent - ''' - - ''' Agent names ''' - SCHEDULER = "Scheduler" - ALERT_MANAGER = "Alert manager" - MAJORDOME = "Majordome" - ENV_MONITORING = "Environment monitoring" - OBS_MANAGER = "Observation manager" - ANALYZER = "Analyzer" - DASHBOARD = "Dashboard" - - - MSG_SHUTDOWN = "Shutdown" - - def __init__(self, agent_name): - self.agent_name = agent_name - self.init_from_config_file() - self.buffer_size = BUFFER_SIZE - self.actions_by_message = {} - Thread.__init__(self, name=agent_name) - - - def init_from_config_file(self): - ''' - Initiate agent's ip and ports for communication - ''' - config = configparser.ConfigParser() - config.read(CONFIG_FILE) - self.ip = config.get(self.agent_name, 'ip') - self.receive_port = int(config.get(self.agent_name, 'receive_port')) - - - def run(self): - ''' - Run in the newly created thread. - - Called at the creation of the thread. - Same for every agent - ''' - print("Starting agent:\t", self.agent_name) - return - self.work() - self.receive() - - - def work(self): - ''' - TO OVERRIDE - Initialize the new thread before starting communication. - The function might create a new thread if needed (majordome, observation manager, alert manager) - ''' - - pass - - - def receive(self): - ''' - Generic function to receive network information - Call analyse_message when information is received - ''' - - - self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.server_socket.bind((self.ip, self.receive_port)) - self.server_socket.listen(12) - - while True: - try: - conn, addr = self.server_socket.accept() - data = conn.recv(self.buffer_size).decode() - conn.close() - self.analyze_message(data) - except (SystemExit, KeyboardInterrupt, socket.error): - self.server_socket.close() - print("Interrupting agent %s" % (self.agent_name)) - break - - - def analyze_message(self, message): - ''' - Checks if the received message is in the Agent's messages - Calls the associated function if yes. - ''' - - if message == Agent.MSG_SHUTDOWN: - self.shutdown() - if message in self.actions_by_message.keys(): - self.actions_by_message[message]() - else: - raise ValueError("Unknown message '%s' in agent '%s'" % (message, self.agent_name)) - - - def shutdown(self): - ''' - Function called when the thread needs to be shut down - MUST be overriden if the thread has created other threads - ''' - sys.exit() \ No newline at end of file diff --git a/src/common/sender.py b/src/common/sender.py deleted file mode 100644 index c34a713..0000000 --- a/src/common/sender.py +++ /dev/null @@ -1,25 +0,0 @@ -import socket -import configparser -import common -import os - -CONFIG_FILE = os.path.join(os.path.abspath(common.__path__[0]),"pyros_agent_config.ini") - -class Sender(): - - @staticmethod - def send_to(dest_agent_name, message): #cette méthode va probablement dégager dans une classe en static - ''' - Send 'message' to 'dest_agent' - ''' - - config = configparser.ConfigParser() - config.read(CONFIG_FILE) - - dest_ip = config.get(dest_agent_name, 'ip') - dest_receive_port = int(config.get(dest_agent_name, 'receive_port')) - - client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - client_socket.connect((dest_ip, dest_receive_port)) - client_socket.send(bytes(message, 'UTF-8')) - client_socket.close() # il faudra voir si les data passent bien avant que la socket se coupe. Peut-être utiliser shutdown diff --git a/src/common/tests.py b/src/common/tests.py index db42d5c..1acd823 100644 --- a/src/common/tests.py +++ b/src/common/tests.py @@ -1,31 +1 @@ -from django.test import TestCase -import threading -import configparser -import os -import common -from common.agent import Agent -from common.sender import Sender - -class AgentTests(TestCase): - - def setUp(self): - self.config_file = os.path.join(os.path.abspath(common.__path__[0]),"pyros_agent_config.ini") - self.config = configparser.ConfigParser() - self.config.read(self.config_file) - - def test_all_agents_created(self): - ''' - Tests if all the agents are alive, and send them stop message - ''' - return - threads = threading.enumerate() - thread_names = [thread.name for thread in threads if thread.name != "MainThread"] - self.assertEqual(len(thread_names), 7, "There should be 7 threads (7 agents)") - for section in list(self.config)[1:]: - self.assertIn(section, thread_names, "Missing thread : %s" %(section)) - - for thread in [thread for thread in threads if thread.name != "MainThread"]: - Sender.send_to(thread.name, Agent.MSG_SHUTDOWN) - thread.join() - - self.assertEqual(threading.active_count(), 1, "Only the MainThread should still be alive") \ No newline at end of file +""" No tests """ \ No newline at end of file diff --git a/src/dashboard/agent.py b/src/dashboard/agent.py deleted file mode 100644 index 53b2812..0000000 --- a/src/dashboard/agent.py +++ /dev/null @@ -1,21 +0,0 @@ -from common.agent import Agent - -class DashboardAgent(Agent): - - #TODO : Définir comment on communique avec le dashboard, donc les messages ... - - def __init__(self): - Agent.__init__(self, Agent.DASHBOARD) - - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - - In the Dashboard Manager, this method might do nothing - ''' - - pass diff --git a/src/dashboard/apps.py b/src/dashboard/apps.py index 97ac77d..39a10a9 100644 --- a/src/dashboard/apps.py +++ b/src/dashboard/apps.py @@ -2,8 +2,3 @@ from django.apps import AppConfig class DashboardConfig(AppConfig): name = 'dashboard' - - def ready(self): - from dashboard.agent import DashboardAgent - self.agent = DashboardAgent() - self.agent.start() \ No newline at end of file diff --git a/src/majordome/agent.py b/src/majordome/agent.py deleted file mode 100644 index df2a49f..0000000 --- a/src/majordome/agent.py +++ /dev/null @@ -1,81 +0,0 @@ -from common.agent import Agent -from threading import Thread, Event - -class MajordomeAgent(Agent): - - MSG_OBS_FINISHED = "Observation finished" - MSG_SCHEDULING_FINISHED = "Scheduling finished" - MSG_NIGHT_COMING = "Night is coming" - - def __init__(self): - Agent.__init__(self, Agent.MAJORDOME) - self.actions_by_message[self.MSG_OBS_FINISHED] = self.observation_finished - self.actions_by_message[self.MSG_SCHEDULING_FINISHED] = self.scheduling_finished - self.actions_by_message[self.MSG_NIGHT_COMING] = self.night_coming - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - In the Majordome, this method creates the thread in charge of the execution - ''' - - self.majordome_executor = MajordomeExecutor() - self.majordome_executor.start() - - def observation_finished(self): - ''' - Called when the observation manager has finished its observation - - Unlock the Event for the secondary thread execution - ''' - #TODO: débloquer l'Event quand il existera - pass - - - def scheduling_finished(self): - ''' - Called when the scheduler has finished its execution (schedule is ready) - - Unlock the Event for the secondary thread execution - ''' - - #TODO: débloquer l'Event quand il existera - pass - - - def night_coming(self): - ''' - Called when the night is close to start the system - - TBD - ''' - #TODO: la méthode - pass - - def shutdown(self): - self.majordome_executor.shutdown_event.set() - self.majordome_executor.join() - Agent.shutdown(self) - -class MajordomeExecutor(Thread): - ''' - Execution of the Majordome (main loop) - ''' - - def __init__(self): - self.waiting_event = Event() - self.waiting_event.clear() - self.shutdown_event = Event() - self.shutdown_event.clear() - Thread.__init__(self, name="MajordomeExecutor") - - def run(self): - ''' - Function called when the .start() is invoked - Calls the main loop for the sequences waiting/execution - ''' - pass - \ No newline at end of file diff --git a/src/majordome/apps.py b/src/majordome/apps.py index 445f019..f772cf5 100644 --- a/src/majordome/apps.py +++ b/src/majordome/apps.py @@ -3,8 +3,3 @@ from django.apps import AppConfig class MajordomeConfig(AppConfig): name = 'majordome' - - def ready(self): - from majordome.agent import MajordomeAgent - self.agent = MajordomeAgent() - self.agent.start() \ No newline at end of file diff --git a/src/monitoring/agent.py b/src/monitoring/agent.py deleted file mode 100644 index 05ad916..0000000 --- a/src/monitoring/agent.py +++ /dev/null @@ -1,19 +0,0 @@ -from common.agent import Agent - -class MonitoringAgent(Agent): - - def __init__(self): - Agent.__init__(self, Agent.ENV_MONITORING) - - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - ''' - - # vu que le monitoring risque de ne rien écouter, il faudra peut-être override run() - pass - diff --git a/src/monitoring/apps.py b/src/monitoring/apps.py index ec868f2..f27ffed 100644 --- a/src/monitoring/apps.py +++ b/src/monitoring/apps.py @@ -3,8 +3,3 @@ from django.apps import AppConfig class MonitoringConfig(AppConfig): name = 'monitoring' - - def ready(self): - from monitoring.agent import MonitoringAgent - self.agent = MonitoringAgent() - self.agent.start() \ No newline at end of file diff --git a/src/observation_manager/agent.py b/src/observation_manager/agent.py deleted file mode 100644 index 5e06af2..0000000 --- a/src/observation_manager/agent.py +++ /dev/null @@ -1,62 +0,0 @@ -from common.agent import Agent -from threading import Thread, Event - -class ObservationManagerAgent(Agent): - - MSG_NEXT_SEQUENCE = "Execute next sequence" - - def __init__(self): - Agent.__init__(self, Agent.OBS_MANAGER) - self.actions_by_message[self.MSG_NEXT_SEQUENCE] = self.execute_next_sequence - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - - In the Observation Manager, this method creates the thread that executes a sequence - ''' - - self.observation_executor = ObservationExecutor() - self.observation_executor.start() - - def execute_next_sequence(self): - ''' - Called when the majordome wants to execute the next PLANNED sequence - - Unlock the Event for the secondary thread execution - ''' - #TODO : Débloquer l'Event du thread secondaire quand il existera - pass - - - def shutdown(self): - self.observation_executor.shutdown_event.set() - self.observation_executor.join() - Agent.shutdown(self) - -class ObservationExecutor(Thread): - ''' - Executes the sequences. - This class is stoppable at any 'checkpoint', thanks to an Event - - Another Event allows it to block, waiting for the next sequence to be executed - ''' - - def __init__(self): - self.waiting_event = Event() - self.waiting_event.clear() - self.stop_event = Event() - self.stop_event.clear() - self.shutdown_event = Event() - self.shutdown_event.clear() - Thread.__init__(self, name="ObservationExecutor") - - def run(self): - ''' - Function called when the .start() is invoked - Calls the main loop for the sequences waiting/execution - ''' - pass \ No newline at end of file diff --git a/src/observation_manager/apps.py b/src/observation_manager/apps.py index 42d7977..ab694c5 100644 --- a/src/observation_manager/apps.py +++ b/src/observation_manager/apps.py @@ -3,9 +3,3 @@ from django.apps import AppConfig class ObservationManagerConfig(AppConfig): name = 'observation_manager' - - def ready(self): - from observation_manager.agent import ObservationManagerAgent - self.agent = ObservationManagerAgent() - self.agent.start() - \ No newline at end of file diff --git a/src/scheduler/agent.py b/src/scheduler/agent.py deleted file mode 100644 index 2ba86ae..0000000 --- a/src/scheduler/agent.py +++ /dev/null @@ -1,34 +0,0 @@ -from common.agent import Agent -from scheduler.models import Scheduler - -class SchedulerAgent(Agent): - - MSG_FIRST_SCHEDULE = 'First schedule' - MSG_RESCHEDULE = 'Re-schedule' - - def __init__(self): - Agent.__init__(self, Agent.SCHEDULER) - self.actions_by_message[SchedulerAgent.MSG_FIRST_SCHEDULE] = self.first_schedule - self.actions_by_message[SchedulerAgent.MSG_RESCHEDULE] = self.re_schedule - - - def work(self): - ''' - Overriding Agent's method - Start routine of the new thread. - - Called once before starting the communications - ''' - #sensiblement, le scheduler n'a rien à initialiser - - pass - - - def first_schedule(self): - scheduler = Scheduler() - scheduler.make_schedule() - - - def re_schedule(self): - scheduler = Scheduler() - scheduler.re_schedule() diff --git a/src/scheduler/apps.py b/src/scheduler/apps.py index 2b211a6..fa264d9 100644 --- a/src/scheduler/apps.py +++ b/src/scheduler/apps.py @@ -2,8 +2,3 @@ from django.apps import AppConfig class SchedulerConfig(AppConfig): name = 'scheduler' - - def ready(self): - from scheduler.agent import SchedulerAgent - self.agent = SchedulerAgent() - self.agent.start() \ No newline at end of file -- libgit2 0.21.2