Commit 9babd239c454e21c77c5f3b1ae00961fdeaec95a

Authored by haribo
1 parent 6bb819d8
Exists in master and in 1 other branch dev

agent & sender systems deleted

src/alert_manager/agent.py deleted
... ... @@ -1,59 +0,0 @@
1   -from common.agent import Agent
2   -from threading import Thread, Event
3   -
4   -class AlertManagerAgent(Agent):
5   -
6   - MSG_CHANGE_STRAT = "Change current strategy"
7   -
8   - def __init__(self):
9   - Agent.__init__(self, Agent.ALERT_MANAGER)
10   - self.actions_by_message[AlertManagerAgent.MSG_CHANGE_STRAT] = self.change_strategy
11   -
12   -
13   - def work(self):
14   - '''
15   - Overriding Agent's method
16   - Start routine of the new thread.
17   -
18   - Called once before starting the communications
19   -
20   - In the Alert Manager, this method creates the thread that listen to VOEvents
21   - '''
22   -
23   - self.voevent_listener = VOEventListener()
24   - self.voevent_listener.start()
25   -
26   -
27   - def change_strategy(self):
28   - '''
29   - Its role and arguments are to be determined
30   - '''
31   - #TODO: la méthode ...
32   - pass
33   -
34   - def shutdown(self):
35   - self.voevent_listener.shutdown_event.set()
36   - self.voevent_listener.join()
37   - Agent.shutdown(self)
38   -
39   -class VOEventListener(Thread):
40   - '''
41   - Thread created by AlertManagerAgent
42   - Listens to the VOEvent network and creates request with the good events.
43   - '''
44   -
45   - #TODO: définir les rôles de cette classe
46   -
47   - def __init__(self):
48   - self.shutdown_event = Event()
49   - self.shutdown_event.clear()
50   - Thread.__init__(self, name="VOEventListener")
51   -
52   - def run(self):
53   - '''
54   - Function called when the .start() is invoked
55   - Calls the main loop for the VOEvent waiting/analyze
56   - '''
57   -
58   - pass
59   -
60 0 \ No newline at end of file
src/alert_manager/apps.py
... ... @@ -3,8 +3,3 @@ from django.apps import AppConfig
3 3  
4 4 class AlertManagerConfig(AppConfig):
5 5 name = 'alert_manager'
6   -
7   - def ready(self):
8   - from alert_manager.agent import AlertManagerAgent
9   - self.agent = AlertManagerAgent()
10   - self.agent.start()
... ...
src/analyzer/agent.py deleted
... ... @@ -1,30 +0,0 @@
1   -from common.agent import Agent
2   -
3   -class AnalyzerAgent(Agent):
4   -
5   - MSG_ANALYZE = "Analyze next image"
6   -
7   - def __init__(self):
8   - Agent.__init__(self, Agent.ANALYZER)
9   - self.actions_by_message[AnalyzerAgent.MSG_ANALYZE] = self.analyze
10   -
11   -
12   - def work(self):
13   - '''
14   - Overriding Agent's method
15   - Start routine of the new thread.
16   -
17   - Called once before starting the communications
18   -
19   - In the Analyzer, this might do nothing
20   - '''
21   -
22   - pass
23   -
24   -
25   - def analyze(self):
26   - '''
27   - Its role and arguments are to be determined
28   - '''
29   - #TODO: la méthode ...
30   - pass
31 0 \ No newline at end of file
src/analyzer/apps.py
... ... @@ -2,8 +2,3 @@ from django.apps import AppConfig
2 2  
3 3 class AnalyzerConfig(AppConfig):
4 4 name = 'analyzer'
5   -
6   - def ready(self):
7   - from analyzer.agent import AnalyzerAgent
8   - self.agent = AnalyzerAgent()
9   - self.agent.start()
10 5 \ No newline at end of file
... ...
src/common/agent.py deleted
... ... @@ -1,122 +0,0 @@
1   -import os
2   -import configparser
3   -from threading import Thread
4   -import common
5   -import socket
6   -import sys
7   -
8   -import time
9   -
10   -BUFFER_SIZE = 256
11   -CONFIG_FILE = os.path.join(os.path.abspath(common.__path__[0]),"pyros_agent_config.ini")
12   -
13   -class Agent(Thread):
14   - '''
15   - MEANT TO BE OVERRIDEN
16   - Handler for the multi-threading modules and the inter-module communications.
17   - Uses the library 'threading' for the threads, and 'socket' for the communications
18   -
19   - How to use it :
20   - - Create a class inheriting from Agent, passing a following Agent name as parameter
21   - - Override 'work method' : this method will be called in a separate thread when you will do MyClass.start()
22   - - Override 'analyse_message' method : this method will be called when a message is received
23   - - Call MyClass.start() to start the agent
24   - '''
25   -
26   - ''' Agent names '''
27   - SCHEDULER = "Scheduler"
28   - ALERT_MANAGER = "Alert manager"
29   - MAJORDOME = "Majordome"
30   - ENV_MONITORING = "Environment monitoring"
31   - OBS_MANAGER = "Observation manager"
32   - ANALYZER = "Analyzer"
33   - DASHBOARD = "Dashboard"
34   -
35   -
36   - MSG_SHUTDOWN = "Shutdown"
37   -
38   - def __init__(self, agent_name):
39   - self.agent_name = agent_name
40   - self.init_from_config_file()
41   - self.buffer_size = BUFFER_SIZE
42   - self.actions_by_message = {}
43   - Thread.__init__(self, name=agent_name)
44   -
45   -
46   - def init_from_config_file(self):
47   - '''
48   - Initiate agent's ip and ports for communication
49   - '''
50   - config = configparser.ConfigParser()
51   - config.read(CONFIG_FILE)
52   - self.ip = config.get(self.agent_name, 'ip')
53   - self.receive_port = int(config.get(self.agent_name, 'receive_port'))
54   -
55   -
56   - def run(self):
57   - '''
58   - Run in the newly created thread.
59   -
60   - Called at the creation of the thread.
61   - Same for every agent
62   - '''
63   - print("Starting agent:\t", self.agent_name)
64   - return
65   - self.work()
66   - self.receive()
67   -
68   -
69   - def work(self):
70   - '''
71   - TO OVERRIDE
72   - Initialize the new thread before starting communication.
73   - The function might create a new thread if needed (majordome, observation manager, alert manager)
74   - '''
75   -
76   - pass
77   -
78   -
79   - def receive(self):
80   - '''
81   - Generic function to receive network information
82   - Call analyse_message when information is received
83   - '''
84   -
85   -
86   - self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
87   - self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
88   - self.server_socket.bind((self.ip, self.receive_port))
89   - self.server_socket.listen(12)
90   -
91   - while True:
92   - try:
93   - conn, addr = self.server_socket.accept()
94   - data = conn.recv(self.buffer_size).decode()
95   - conn.close()
96   - self.analyze_message(data)
97   - except (SystemExit, KeyboardInterrupt, socket.error):
98   - self.server_socket.close()
99   - print("Interrupting agent %s" % (self.agent_name))
100   - break
101   -
102   -
103   - def analyze_message(self, message):
104   - '''
105   - Checks if the received message is in the Agent's messages
106   - Calls the associated function if yes.
107   - '''
108   -
109   - if message == Agent.MSG_SHUTDOWN:
110   - self.shutdown()
111   - if message in self.actions_by_message.keys():
112   - self.actions_by_message[message]()
113   - else:
114   - raise ValueError("Unknown message '%s' in agent '%s'" % (message, self.agent_name))
115   -
116   -
117   - def shutdown(self):
118   - '''
119   - Function called when the thread needs to be shut down
120   - MUST be overriden if the thread has created other threads
121   - '''
122   - sys.exit()
123 0 \ No newline at end of file
src/common/sender.py deleted
... ... @@ -1,25 +0,0 @@
1   -import socket
2   -import configparser
3   -import common
4   -import os
5   -
6   -CONFIG_FILE = os.path.join(os.path.abspath(common.__path__[0]),"pyros_agent_config.ini")
7   -
8   -class Sender():
9   -
10   - @staticmethod
11   - def send_to(dest_agent_name, message): #cette méthode va probablement dégager dans une classe en static
12   - '''
13   - Send 'message' to 'dest_agent'
14   - '''
15   -
16   - config = configparser.ConfigParser()
17   - config.read(CONFIG_FILE)
18   -
19   - dest_ip = config.get(dest_agent_name, 'ip')
20   - dest_receive_port = int(config.get(dest_agent_name, 'receive_port'))
21   -
22   - client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23   - client_socket.connect((dest_ip, dest_receive_port))
24   - client_socket.send(bytes(message, 'UTF-8'))
25   - client_socket.close() # il faudra voir si les data passent bien avant que la socket se coupe. Peut-être utiliser shutdown
src/common/tests.py
1   -from django.test import TestCase
2   -import threading
3   -import configparser
4   -import os
5   -import common
6   -from common.agent import Agent
7   -from common.sender import Sender
8   -
9   -class AgentTests(TestCase):
10   -
11   - def setUp(self):
12   - self.config_file = os.path.join(os.path.abspath(common.__path__[0]),"pyros_agent_config.ini")
13   - self.config = configparser.ConfigParser()
14   - self.config.read(self.config_file)
15   -
16   - def test_all_agents_created(self):
17   - '''
18   - Tests if all the agents are alive, and send them stop message
19   - '''
20   - return
21   - threads = threading.enumerate()
22   - thread_names = [thread.name for thread in threads if thread.name != "MainThread"]
23   - self.assertEqual(len(thread_names), 7, "There should be 7 threads (7 agents)")
24   - for section in list(self.config)[1:]:
25   - self.assertIn(section, thread_names, "Missing thread : %s" %(section))
26   -
27   - for thread in [thread for thread in threads if thread.name != "MainThread"]:
28   - Sender.send_to(thread.name, Agent.MSG_SHUTDOWN)
29   - thread.join()
30   -
31   - self.assertEqual(threading.active_count(), 1, "Only the MainThread should still be alive")
32 1 \ No newline at end of file
  2 +""" No tests """
33 3 \ No newline at end of file
... ...
src/dashboard/agent.py deleted
... ... @@ -1,21 +0,0 @@
1   -from common.agent import Agent
2   -
3   -class DashboardAgent(Agent):
4   -
5   - #TODO : Définir comment on communique avec le dashboard, donc les messages ...
6   -
7   - def __init__(self):
8   - Agent.__init__(self, Agent.DASHBOARD)
9   -
10   -
11   - def work(self):
12   - '''
13   - Overriding Agent's method
14   - Start routine of the new thread.
15   -
16   - Called once before starting the communications
17   -
18   - In the Dashboard Manager, this method might do nothing
19   - '''
20   -
21   - pass
src/dashboard/apps.py
... ... @@ -2,8 +2,3 @@ from django.apps import AppConfig
2 2  
3 3 class DashboardConfig(AppConfig):
4 4 name = 'dashboard'
5   -
6   - def ready(self):
7   - from dashboard.agent import DashboardAgent
8   - self.agent = DashboardAgent()
9   - self.agent.start()
10 5 \ No newline at end of file
... ...
src/majordome/agent.py deleted
... ... @@ -1,81 +0,0 @@
1   -from common.agent import Agent
2   -from threading import Thread, Event
3   -
4   -class MajordomeAgent(Agent):
5   -
6   - MSG_OBS_FINISHED = "Observation finished"
7   - MSG_SCHEDULING_FINISHED = "Scheduling finished"
8   - MSG_NIGHT_COMING = "Night is coming"
9   -
10   - def __init__(self):
11   - Agent.__init__(self, Agent.MAJORDOME)
12   - self.actions_by_message[self.MSG_OBS_FINISHED] = self.observation_finished
13   - self.actions_by_message[self.MSG_SCHEDULING_FINISHED] = self.scheduling_finished
14   - self.actions_by_message[self.MSG_NIGHT_COMING] = self.night_coming
15   -
16   - def work(self):
17   - '''
18   - Overriding Agent's method
19   - Start routine of the new thread.
20   -
21   - Called once before starting the communications
22   - In the Majordome, this method creates the thread in charge of the execution
23   - '''
24   -
25   - self.majordome_executor = MajordomeExecutor()
26   - self.majordome_executor.start()
27   -
28   - def observation_finished(self):
29   - '''
30   - Called when the observation manager has finished its observation
31   -
32   - Unlock the Event for the secondary thread execution
33   - '''
34   - #TODO: débloquer l'Event quand il existera
35   - pass
36   -
37   -
38   - def scheduling_finished(self):
39   - '''
40   - Called when the scheduler has finished its execution (schedule is ready)
41   -
42   - Unlock the Event for the secondary thread execution
43   - '''
44   -
45   - #TODO: débloquer l'Event quand il existera
46   - pass
47   -
48   -
49   - def night_coming(self):
50   - '''
51   - Called when the night is close to start the system
52   -
53   - TBD
54   - '''
55   - #TODO: la méthode
56   - pass
57   -
58   - def shutdown(self):
59   - self.majordome_executor.shutdown_event.set()
60   - self.majordome_executor.join()
61   - Agent.shutdown(self)
62   -
63   -class MajordomeExecutor(Thread):
64   - '''
65   - Execution of the Majordome (main loop)
66   - '''
67   -
68   - def __init__(self):
69   - self.waiting_event = Event()
70   - self.waiting_event.clear()
71   - self.shutdown_event = Event()
72   - self.shutdown_event.clear()
73   - Thread.__init__(self, name="MajordomeExecutor")
74   -
75   - def run(self):
76   - '''
77   - Function called when the .start() is invoked
78   - Calls the main loop for the sequences waiting/execution
79   - '''
80   - pass
81   -
82 0 \ No newline at end of file
src/majordome/apps.py
... ... @@ -3,8 +3,3 @@ from django.apps import AppConfig
3 3  
4 4 class MajordomeConfig(AppConfig):
5 5 name = 'majordome'
6   -
7   - def ready(self):
8   - from majordome.agent import MajordomeAgent
9   - self.agent = MajordomeAgent()
10   - self.agent.start()
11 6 \ No newline at end of file
... ...
src/monitoring/agent.py deleted
... ... @@ -1,19 +0,0 @@
1   -from common.agent import Agent
2   -
3   -class MonitoringAgent(Agent):
4   -
5   - def __init__(self):
6   - Agent.__init__(self, Agent.ENV_MONITORING)
7   -
8   -
9   - def work(self):
10   - '''
11   - Overriding Agent's method
12   - Start routine of the new thread.
13   -
14   - Called once before starting the communications
15   - '''
16   -
17   - # vu que le monitoring risque de ne rien écouter, il faudra peut-être override run()
18   - pass
19   -
src/monitoring/apps.py
... ... @@ -3,8 +3,3 @@ from django.apps import AppConfig
3 3  
4 4 class MonitoringConfig(AppConfig):
5 5 name = 'monitoring'
6   -
7   - def ready(self):
8   - from monitoring.agent import MonitoringAgent
9   - self.agent = MonitoringAgent()
10   - self.agent.start()
11 6 \ No newline at end of file
... ...
src/observation_manager/agent.py deleted
... ... @@ -1,62 +0,0 @@
1   -from common.agent import Agent
2   -from threading import Thread, Event
3   -
4   -class ObservationManagerAgent(Agent):
5   -
6   - MSG_NEXT_SEQUENCE = "Execute next sequence"
7   -
8   - def __init__(self):
9   - Agent.__init__(self, Agent.OBS_MANAGER)
10   - self.actions_by_message[self.MSG_NEXT_SEQUENCE] = self.execute_next_sequence
11   -
12   - def work(self):
13   - '''
14   - Overriding Agent's method
15   - Start routine of the new thread.
16   -
17   - Called once before starting the communications
18   -
19   - In the Observation Manager, this method creates the thread that executes a sequence
20   - '''
21   -
22   - self.observation_executor = ObservationExecutor()
23   - self.observation_executor.start()
24   -
25   - def execute_next_sequence(self):
26   - '''
27   - Called when the majordome wants to execute the next PLANNED sequence
28   -
29   - Unlock the Event for the secondary thread execution
30   - '''
31   - #TODO : Débloquer l'Event du thread secondaire quand il existera
32   - pass
33   -
34   -
35   - def shutdown(self):
36   - self.observation_executor.shutdown_event.set()
37   - self.observation_executor.join()
38   - Agent.shutdown(self)
39   -
40   -class ObservationExecutor(Thread):
41   - '''
42   - Executes the sequences.
43   - This class is stoppable at any 'checkpoint', thanks to an Event
44   -
45   - Another Event allows it to block, waiting for the next sequence to be executed
46   - '''
47   -
48   - def __init__(self):
49   - self.waiting_event = Event()
50   - self.waiting_event.clear()
51   - self.stop_event = Event()
52   - self.stop_event.clear()
53   - self.shutdown_event = Event()
54   - self.shutdown_event.clear()
55   - Thread.__init__(self, name="ObservationExecutor")
56   -
57   - def run(self):
58   - '''
59   - Function called when the .start() is invoked
60   - Calls the main loop for the sequences waiting/execution
61   - '''
62   - pass
63 0 \ No newline at end of file
src/observation_manager/apps.py
... ... @@ -3,9 +3,3 @@ from django.apps import AppConfig
3 3  
4 4 class ObservationManagerConfig(AppConfig):
5 5 name = 'observation_manager'
6   -
7   - def ready(self):
8   - from observation_manager.agent import ObservationManagerAgent
9   - self.agent = ObservationManagerAgent()
10   - self.agent.start()
11   -
12 6 \ No newline at end of file
... ...
src/scheduler/agent.py deleted
... ... @@ -1,34 +0,0 @@
1   -from common.agent import Agent
2   -from scheduler.models import Scheduler
3   -
4   -class SchedulerAgent(Agent):
5   -
6   - MSG_FIRST_SCHEDULE = 'First schedule'
7   - MSG_RESCHEDULE = 'Re-schedule'
8   -
9   - def __init__(self):
10   - Agent.__init__(self, Agent.SCHEDULER)
11   - self.actions_by_message[SchedulerAgent.MSG_FIRST_SCHEDULE] = self.first_schedule
12   - self.actions_by_message[SchedulerAgent.MSG_RESCHEDULE] = self.re_schedule
13   -
14   -
15   - def work(self):
16   - '''
17   - Overriding Agent's method
18   - Start routine of the new thread.
19   -
20   - Called once before starting the communications
21   - '''
22   - #sensiblement, le scheduler n'a rien à initialiser
23   -
24   - pass
25   -
26   -
27   - def first_schedule(self):
28   - scheduler = Scheduler()
29   - scheduler.make_schedule()
30   -
31   -
32   - def re_schedule(self):
33   - scheduler = Scheduler()
34   - scheduler.re_schedule()
src/scheduler/apps.py
... ... @@ -2,8 +2,3 @@ from django.apps import AppConfig
2 2  
3 3 class SchedulerConfig(AppConfig):
4 4 name = 'scheduler'
5   -
6   - def ready(self):
7   - from scheduler.agent import SchedulerAgent
8   - self.agent = SchedulerAgent()
9   - self.agent.start()
10 5 \ No newline at end of file
... ...