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

Fix some waking up issue

parent 0fa0dd08
No related branches found
No related tags found
No related merge requests found
......@@ -12,7 +12,7 @@ class Agent(Schedulable):
Schedulable.__init__(self, client_id="Agent"+str(self.id))
self.subscribe("scheduler/agent/wakeup", self.scheduler_wake_up)
self.subscribe("scheduler/agent/wakeup", self.wake_up)
self.neighbors = []
self.next_neighbors = []
......@@ -41,10 +41,6 @@ class Agent(Schedulable):
def publish(self, topic: str, message) -> None:
self.client.publish("agent/" + str(self.id) + "/" + topic, message)
def scheduler_wake_up(self, client, userdata, message):
self.wake_up_token = True
print("waked up")
def on_cycle_begin(self):
self.log("on_cycle_begin")
......
......@@ -15,7 +15,7 @@ class Amas(Schedulable):
def __init__(self):
Schedulable.__init__(self, client_id="Amas")
self.subscribe("scheduler/schedulable/wakeup", self.scheduler_wake_up)
self.subscribe("scheduler/schedulable/wakeup", self.wake_up)
self.next_id = 0
......@@ -29,10 +29,6 @@ class Amas(Schedulable):
def on_initial_agents_creation(self):
pass
def scheduler_wake_up(self, client, userdata, message):
self.wake_up_token = True
print("waked up")
def add_agent(self):
try:
s = pxssh.pxssh()
......
......@@ -13,16 +13,12 @@ class Environment(Schedulable):
def __init__(self):
Schedulable.__init__(self, client_id="Env")
self.subscribe("scheduler/schedulable/wakeup", self.scheduler_wake_up)
self.subscribe("scheduler/schedulable/wakeup", self.wake_up)
self.on_initialization()
self.client.publish("env/action_done", "")
def scheduler_wake_up(self, client, userdata, message):
self.wake_up_token = True
print("waked up")
def on_initialization(self) -> None:
"""
This method will be executed at the end of __init__()
......
......@@ -21,9 +21,13 @@ class Schedulable(MqttClient):
self.wait_delay: float = 0.01
self.wake_up_token: int = 0
def wake_up(self, client, userdata, message):
self.wake_up_token += 1
print("Waked up")
def wait(self):
# print("Waiting")
while not self.wake_up_token > 1:
while self.wake_up_token == 0:
sleep(self.wait_delay)
self.wake_up_token -= 1
# print("End wait")
......
......@@ -22,8 +22,6 @@ class Scheduler(Schedulable):
def update_schedulable(self, client, userdata, message):
self.schedulable_waiting += 1
if self.schedulable_waiting == 2:
self.wake_up_token += 1
print("__Schedulable is waiting")
def update_nbr_agent(self, client, userdata, message):
......@@ -33,10 +31,18 @@ class Scheduler(Schedulable):
def agent_done(self, client, userdata, message):
self.agent_waiting += 1
if self.agent_waiting >= self.nbr_agent:
self.wake_up_token += 1
print("__Agent done")
def wait_agent(self):
while self.agent_waiting < self.nbr_agent:
sleep(self.wait_delay)
self.agent_waiting = 0
def wait_schedulable(self):
while self.schedulable_waiting < 2:
sleep(self.wait_delay)
self.schedulable_waiting = 0
def first_part(self) -> None:
"""
first part of a cycle
......@@ -44,7 +50,7 @@ class Scheduler(Schedulable):
self.client.publish("scheduler/schedulable/wakeup", "")
# Amas on cycle begin
# Environment on cycle begin
self.wait()
self.wait_schedulable()
def main_part(self) -> None:
"""
......@@ -52,10 +58,10 @@ class Scheduler(Schedulable):
"""
self.client.publish("scheduler/agent/wakeup", "")
# Agent doing phase 1
self.wait()
self.wait_agent()
self.client.publish("scheduler/agent/wakeup", "")
# agent doing phase 2
self.wait()
self.wait_agent()
def last_part(self) -> None:
"""
......@@ -64,7 +70,7 @@ class Scheduler(Schedulable):
self.client.publish("scheduler/schedulable/wakeup", "")
# Amas on cycle end
# Environment on cycle end
self.wait()
self.wait_schedulable()
def run(self) -> None:
"""
......@@ -73,10 +79,10 @@ class Scheduler(Schedulable):
# wait that all schedulable have init
print("Waiting schedulable")
self.wait()
self.wait_schedulable()
# Wait that all agent have init
print("Waiting agents :", self.nbr_agent)
self.wait()
self.wait_agent()
while not self.exit_bool:
print("Cycle : ", self.nbr_cycle)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment