diff --git a/agent.py b/agent.py index 9d136245d9b2b0865d27341b6add81131e096cdb..6164ff5d15f87a16b4b3249dfdad67cc467dd0d8 100644 --- a/agent.py +++ b/agent.py @@ -10,13 +10,13 @@ class Agent(Schedulable): def __init__(self, identifier: int) -> None: self.id = identifier - Schedulable.__init__(self, client_id="Agent"+str(self.id)) + Schedulable.__init__(self, client_id="Agent" + str(self.id)) self.subscribe("scheduler/agent/wakeup", self.wake_up) self.neighbors = [] self.next_neighbors = [] - self.subscribe("amas/agent/"+str(self.id)+"/neighbor", self.add_neighbor) + self.subscribe("amas/agent/" + str(self.id) + "/neighbor", self.add_neighbor) self.on_initialization() @@ -82,8 +82,9 @@ class Agent(Schedulable): self.publish("metric", str(self.send_metric())) self.publish("cycle_done", "") + if __name__ == '__main__': print("id agent: ", int(sys.argv[1])) a = Agent(int(sys.argv[1])) - a.run() \ No newline at end of file + a.run() diff --git a/amas.py b/amas.py index 632ec34e2c9852d0e2009ab54272500179fc2d33..2708131235cfaebf75382133e4c838b2f91697e8 100644 --- a/amas.py +++ b/amas.py @@ -2,6 +2,7 @@ Amas class """ from ast import literal_eval +from typing import List from tool.schedulable import Schedulable from tool.ssh_client import SSHClient, Cmd @@ -12,7 +13,7 @@ class Amas(Schedulable, SSHClient): Amas class """ - def __init__(self): + def __init__(self) -> None: Schedulable.__init__(self, client_id="Amas") SSHClient.__init__(self) @@ -29,10 +30,20 @@ class Amas(Schedulable, SSHClient): self.client.publish("amas/action_done", "") def on_initial_agents_creation(self): + """ + Convenient method to initially create the agents, is called at the end of initialization + """ pass - def add_agent(self, experience_name, args): - + def add_agent(self, experience_name: str, args: List = None) -> None: + """ + Function that need to be called to create a new agent + :param experience_name: name of the experience folder + :param args: if any argument is needed to initiate the new agent + :return: None + """ + if args is None: + args = [] command = "nohup python " command += "\"Desktop/mqtt_goyon/iotamak-core/" + experience_name + "/agent.py\" " command += str(self.next_id) + " " @@ -48,20 +59,28 @@ class Amas(Schedulable, SSHClient): self.client.publish("amas/agent/new", self.next_id) self.next_id += 1 - def push_agent(self): - + def push_agent(self) -> None: + """ + Method used to start new agent trough ssh + """ total_pi = len(self.clients) for client in range(total_pi): self.run_cmd(client, list(self.agents_cmd[ - client * len(self.agents_cmd) // total_pi:(client + 1) * len( - self.agents_cmd) // total_pi - ]) + client * len(self.agents_cmd) // total_pi:(client + 1) * len( + self.agents_cmd) // total_pi + ]) ) - def agent_log(self, client, userdata, message): + def agent_log(self, client, userdata, message) -> None: + """ + Called when the amas receive a log from any agent, print it in stdout + """ print("[Log] " + str(message.payload.decode("utf-8")) + " on topic " + message.topic) - def agent_metric(self, client, userdata, message): + def agent_metric(self, client, userdata, message) -> None: + """ + Called when the amas receive new metrics from any agent + """ # print("Received message ", literal_eval(message.payload.decode("utf-8")), " on topic '" + message.topic) result = literal_eval(message.payload.decode("utf-8")) agent_id = result.get("id") @@ -86,7 +105,9 @@ class Amas(Schedulable, SSHClient): pass def run(self) -> None: - + """ + Main function of the amas class + """ self.push_agent() while not self.exit_bool: diff --git a/environment.py b/environment.py index 1d5b90c65016387112fe278ea4dee4d5501498e6..9eacea23051fad5c19d6e9c68c3ffca631dfdac4 100644 --- a/environment.py +++ b/environment.py @@ -10,7 +10,7 @@ class Environment(Schedulable): Environment class """ - def __init__(self): + def __init__(self) -> None: Schedulable.__init__(self, client_id="Env") self.subscribe("scheduler/schedulable/wakeup", self.wake_up) @@ -38,7 +38,9 @@ class Environment(Schedulable): pass def run(self) -> None: - + """ + Main function of the env + """ while not self.exit_bool: self.wait() if self.exit_bool: diff --git a/scheduler.py b/scheduler.py index 5355e4ff81befb973da0d82267953c9d39e6fec0..93215956962afb0f7279b899ee77d54b15bf9952 100644 --- a/scheduler.py +++ b/scheduler.py @@ -1,3 +1,6 @@ +""" +Scheduler class file +""" import sys from time import sleep @@ -5,8 +8,11 @@ from tool.schedulable import Schedulable class Scheduler(Schedulable): + """ + Scheduler class, it's role is to make sure the amas, the env and all the agents stay in sync + """ - def __init__(self, execution_policy: int): + def __init__(self, execution_policy: int) -> None: Schedulable.__init__(self, client_id="Scheduler") self.sleep_between_cycle = 0 @@ -28,44 +34,69 @@ class Scheduler(Schedulable): print("Init done") - def pause(self, client, userdata, message): + def pause(self, client, userdata, message) -> None: + """ + Function called when the IHM pause/unpause the scheduler + """ self.paused = not self.paused self.ihm_token += 1 - def step(self, client, userdata, message): + def step(self, client, userdata, message) -> None: + """ + Function called by the IHM when the scheduler is in Step by Step mode + """ self.ihm_token += 1 - def update_schedulable(self, client, userdata, message): + def update_schedulable(self, client, userdata, message) -> None: + """ + Function called whenever the amas/env have finished an action and is waiting + """ self.schedulable_waiting += 1 print("__Schedulable is waiting") - def update_nbr_agent(self, client, userdata, message): + def update_nbr_agent(self, client, userdata, message) -> None: + """ + Called when a new agent is added to the system + param message: id of the new agent + """ self.nbr_agent += 1 self.subscribe("agent/" + str(message.payload.decode("utf-8")) + "/cycle_done", self.agent_done) - print("__Update agent : ",self.nbr_agent, str(message.payload.decode("utf-8"))) + print("__Update agent : ", self.nbr_agent, str(message.payload.decode("utf-8"))) - def agent_done(self, client, userdata, message): + def agent_done(self, client, userdata, message) -> None: + """ + Called whenever an agent have done an action and is waiting + """ self.agent_waiting += 1 print("__Agent done") - def wait_agent(self): + def wait_agent(self) -> None: + """ + Called when the scheduler is waiting for all agent do have finished their action + """ while self.agent_waiting < self.nbr_agent: sleep(self.wait_delay) self.agent_waiting = 0 - def wait_schedulable(self): + def wait_schedulable(self) -> None: + """ + Called when the scheduler is waiting for both amas and env to have finished their action + """ while self.schedulable_waiting < 2: sleep(self.wait_delay) self.schedulable_waiting = 0 - def wait_ihm(self): + def wait_ihm(self) -> None: + """ + Called when the scheduler is waiting for an action of the IHM + """ while self.ihm_token == 0: sleep(self.wait_delay) self.ihm_token -= 1 def first_part(self) -> None: """ - first part of a cycle + first part of a cycle : Amas/Env on_cycle_begin """ self.client.publish("scheduler/schedulable/wakeup", "") # Amas on cycle begin @@ -74,7 +105,7 @@ class Scheduler(Schedulable): def main_part(self) -> None: """ - main part of a cycle + main part of a cycle : Agent cycle """ self.client.publish("scheduler/agent/wakeup", "") # Agent doing phase 1 @@ -85,7 +116,7 @@ class Scheduler(Schedulable): def last_part(self) -> None: """ - last part of a cycle + last part of a cycle : Amas/Env on_cycle_end """ self.client.publish("scheduler/schedulable/wakeup", "") # Amas on cycle end @@ -94,7 +125,7 @@ class Scheduler(Schedulable): def run(self) -> None: """ - main part of amak core + Main function of the scheduler """ # wait that all schedulable have init