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

Agent : Update how canvas update work

parent d9f8225f
Branches
No related tags found
No related merge requests found
No preview for this file type
......@@ -64,5 +64,6 @@ class Agent(Schedulable, BaseAgent):
self.on_cycle_end()
self.publish("metric", str(self.send_metric()))
self.publish("canvas", str(self.to_canvas()))
self.publish("cycle_done", "")
self.nbr_cycle += 1
......@@ -54,4 +54,5 @@ class AsyncAgent(AsyncControlable, BaseAgent):
self.on_act()
self.on_cycle_end()
self.publish("metric", str(self.send_metric()))
self.publish("canvas", str(self.to_canvas()))
......@@ -74,6 +74,17 @@ class BaseAgent:
"[AGENT] " + str(self.id) + " : " + message
)
def to_canvas(self) -> dict:
"""
To override if the canvas is used, automatically called each cycle
the dict must contains :
* 'x', 'y' keys -> int values
* 'id' of the agent (self.id) -> int value
* 'color' in hex -> str (e.g. '#FF0000')
* 'cycle' self.nbr_cycle -> int
"""
return {}
def send_metric(self) -> Dict:
"""
Should be override if the neighbor need to be aware of any other info, should be a dict
......
......@@ -92,19 +92,21 @@ class BaseAmas(SSHClient):
if len(self.agents_cmd[i_min]) > len(self.agents_cmd[elem]):
i_min = elem
self.agents_cmd[i_min].append(Cmd(command))
self.client.publish("amas/agent/new", str({"id": self.next_id, "ip": i_min}))
else:
have_found = False
for i_client in range(len(self.clients)):
if self.clients[i_client].hostname == client_ip:
self.agents_cmd[i_client].append(Cmd(command))
self.client.publish("amas/agent/new", str({"id": self.next_id, "ip": i_client}))
have_found = True
break
if not have_found:
self.agents_cmd[0].append(Cmd(command))
self.client.publish("amas/agent/new", str({"id": self.next_id, "ip": 0}))
self.subscribe("agent/" + str(self.next_id) + "/metric", self.agent_metric)
self.client.publish("amas/agent/new", self.next_id)
self.next_id += 1
def push_agent(self) -> None:
......
"""
Scheduler class file
"""
from ast import literal_eval
from threading import Semaphore
from time import sleep, time
......@@ -67,7 +68,10 @@ class Scheduler(Schedulable):
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)
self.subscribe(
"agent/" + literal_eval(message.payload.decode("utf-8")).get("id") + "/cycle_done",
self.agent_done)
# print("__Update agent : ", self.nbr_agent, str(message.payload.decode("utf-8")))
def agent_done(self, client, userdata, message) -> None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment