Skip to content
Snippets Groups Projects
Commit bd8673bb authored by shinedday's avatar shinedday
Browse files

Add a proper exit

parent 5a1f0840
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,11 @@ WSL (pour pouvoir démarer l'amas)
# ?
Start procédure :
1. start broker
2. start scheduler
3. Start amas/env
```mermaid
flowchart TD
......
......@@ -23,6 +23,9 @@ class Agent(MqttClient):
self.next_neighbors = []
self.subscribe("amas/agent/"+str(self.id)+"/neighbor", self.add_neighbor)
self.exit_bool = False
self.subscribe("ihm/exit", self.exit_procedure)
self.on_initialization()
self.publish("cycle_done", "")
......@@ -31,6 +34,9 @@ class Agent(MqttClient):
def on_initialization(self):
pass
def exit_procedure(self, client, userdata, message):
self.exit_bool = True
def add_neighbor(self, client, userdata, message):
print(message.payload.decode("utf-8"))
result = literal_eval(message.payload.decode("utf-8"))
......@@ -76,9 +82,11 @@ class Agent(MqttClient):
return {"id": self.id}
def run(self):
while True:
while not self.exit_bool:
self.wait()
if self.exit_bool:
return
self.on_cycle_begin()
......
......@@ -31,16 +31,24 @@ class Amas(MqttClient):
self.next_id = 0
self.exit_bool = False
self.subscribe("ihm/exit", self.exit_procedure)
self.agents_metric = [None for _ in range(self.next_id)]
self.on_initialization()
self.on_initial_agents_creation()
self.agents_metric = [None for _ in range(self.next_id)]
self.client.publish("amas/action_done", "")
def on_initial_agents_creation(self):
pass
def exit_procedure(self, client, userdata, message):
self.exit_bool = True
def scheduler_wake_up(self, client, userdata, message):
self.wake_up_token = True
print("waked up")
......@@ -106,9 +114,12 @@ class Amas(MqttClient):
def run(self) -> None:
while True:
while not self.exit_bool:
self.wait()
if self.exit_bool:
return
self.on_cycle_begin()
self.client.publish("amas/action_done", "")
......
......@@ -20,6 +20,9 @@ class Environment(MqttClient):
self.wait_delay = 0.01
self.wake_up_token = False
self.exit_bool = False
self.subscribe("ihm/exit", self.exit_procedure)
self.on_initialization()
self.client.publish("env/action_done", "")
......@@ -35,6 +38,10 @@ class Environment(MqttClient):
self.wake_up_token = False
print("end wait")
def exit_procedure(self, client, userdata, message):
self.exit_bool = True
def on_initialization(self) -> None:
"""
This method will be executed at the end of __init__()
......@@ -54,8 +61,12 @@ class Environment(MqttClient):
pass
def run(self) -> None:
while True:
while not self.exit_bool:
self.wait()
if self.exit_bool:
return
self.on_cycle_begin()
self.client.publish("env/action_done", "")
......
ihm.py 0 → 100644
from mqtt_client import MqttClient
class Ihm(MqttClient):
def __init__(self):
MqttClient.__init__(self, "Ihm")
def run(self):
exit_bool = False
while not exit_bool:
cmd = input()
if cmd.lower() == "exit":
self.client.publish("ihm/exit")
exit_bool = True
import sys
from ast import literal_eval
from time import sleep
......
from ast import literal_eval
import sys
import pathlib
......
......@@ -21,8 +21,14 @@ class Scheduler(MqttClient):
self.agent_waiting = 0
self.schedulable_waiting = 0
self.exit_bool = False
self.subscribe("ihm/exit", self.exit_procedure)
print("Init done")
def exit_procedure(self, client, userdata, message):
self.exit_bool = True
def update_schedulable(self, client, userdata, message):
self.schedulable_waiting += 1
print("__Schedulable is waiting")
......@@ -87,7 +93,7 @@ class Scheduler(MqttClient):
print("Waiting agents :", self.nbr_agent)
self.wait_agent()
while True:
while not self.exit_bool:
print("Cycle : ", self.nbr_cycle)
print("-First part")
......@@ -101,6 +107,9 @@ class Scheduler(MqttClient):
input()
self.nbr_cycle += 1
self.client.publish("scheduler/schedulable/wakeup", "")
self.client.publish("scheduler/agent/wakeup", "")
if __name__ == '__main__':
s = Scheduler()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment