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

Modify : how client are initialize in order to better match future ihm interface

parent ace9c625
Branches
Tags
No related merge requests found
...@@ -40,6 +40,9 @@ WSL (pour pouvoir démarer l'amas) ...@@ -40,6 +40,9 @@ WSL (pour pouvoir démarer l'amas)
# Random note # Random note
First procedure :
1. etre sur que le ssh est trust (faire un premier ssh hors du programe)
Start procédure : Start procédure :
1. start broker 1. start broker
......
""" """
Agent class file Agent class file
""" """
import sys
from ast import literal_eval from ast import literal_eval
from typing import Dict from typing import Dict
...@@ -13,10 +12,10 @@ class Agent(Schedulable): ...@@ -13,10 +12,10 @@ class Agent(Schedulable):
base class for agent base class for agent
""" """
def __init__(self, identifier: int) -> None: def __init__(self, identifier: int, broker_ip: str) -> None:
self.id = identifier self.id = identifier
Schedulable.__init__(self, client_id="Agent" + str(self.id)) Schedulable.__init__(self, broker_ip, "Agent" + str(self.id))
self.subscribe("scheduler/agent/wakeup", self.wake_up) self.subscribe("scheduler/agent/wakeup", self.wake_up)
...@@ -122,8 +121,3 @@ class Agent(Schedulable): ...@@ -122,8 +121,3 @@ class Agent(Schedulable):
self.publish("metric", str(self.send_metric())) self.publish("metric", str(self.send_metric()))
self.publish("cycle_done", "") self.publish("cycle_done", "")
if __name__ == '__main__':
a = Agent(int(sys.argv[1]))
a.run()
...@@ -4,6 +4,7 @@ Amas class ...@@ -4,6 +4,7 @@ Amas class
from ast import literal_eval from ast import literal_eval
from typing import List from typing import List
from tool.remote_client import RemoteClient
from tool.schedulable import Schedulable from tool.schedulable import Schedulable
from tool.ssh_client import SSHClient, Cmd from tool.ssh_client import SSHClient, Cmd
...@@ -13,10 +14,16 @@ class Amas(Schedulable, SSHClient): ...@@ -13,10 +14,16 @@ class Amas(Schedulable, SSHClient):
Amas class Amas class
""" """
def __init__(self) -> None: def __init__(self, broker_ip: str, clients: str) -> None:
Schedulable.__init__(self, client_id="Amas") Schedulable.__init__(self, broker_ip, "Amas")
SSHClient.__init__(self)
print(clients)
true_client = [RemoteClient(i.get("hostname"), i.get("user"), i.get("password")) for i in literal_eval(clients)]
print(true_client)
SSHClient.__init__(self, true_client)
self.broker_ip = broker_ip
self.subscribe("scheduler/schedulable/wakeup", self.wake_up) self.subscribe("scheduler/schedulable/wakeup", self.wake_up)
self.next_id = 0 self.next_id = 0
...@@ -46,7 +53,8 @@ class Amas(Schedulable, SSHClient): ...@@ -46,7 +53,8 @@ class Amas(Schedulable, SSHClient):
args = [] args = []
command = "nohup python " command = "nohup python "
command += "\"Desktop/mqtt_goyon/iotamak-core/" + experience_name + "/agent.py\" " command += "\"Desktop/mqtt_goyon/iotamak-core/" + experience_name + "/agent.py\" "
command += str(self.next_id) + " " command += str(self.next_id) + " \""
command += str(self.broker_ip) + "\" "
for arg in args: for arg in args:
command += str(arg) + " " command += str(arg) + " "
command += "&" command += "&"
...@@ -70,6 +78,7 @@ class Amas(Schedulable, SSHClient): ...@@ -70,6 +78,7 @@ class Amas(Schedulable, SSHClient):
self.agents_cmd) // total_pi self.agents_cmd) // total_pi
]) ])
) )
print("Amas, push agent done")
def agent_log(self, client, userdata, message) -> None: def agent_log(self, client, userdata, message) -> None:
""" """
......
...@@ -10,8 +10,8 @@ class Environment(Schedulable): ...@@ -10,8 +10,8 @@ class Environment(Schedulable):
Environment class Environment class
""" """
def __init__(self) -> None: def __init__(self, broker_ip: str) -> None:
Schedulable.__init__(self, client_id="Env") Schedulable.__init__(self, broker_ip, "Env")
self.subscribe("scheduler/schedulable/wakeup", self.wake_up) self.subscribe("scheduler/schedulable/wakeup", self.wake_up)
...@@ -56,8 +56,3 @@ class Environment(Schedulable): ...@@ -56,8 +56,3 @@ class Environment(Schedulable):
self.client.publish("env/action_done", "") self.client.publish("env/action_done", "")
self.nbr_cycle += 1 self.nbr_cycle += 1
if __name__ == '__main__':
s = Environment()
s.run()
...@@ -6,17 +6,17 @@ from os import path ...@@ -6,17 +6,17 @@ from os import path
from subprocess import Popen from subprocess import Popen
from time import sleep from time import sleep
from tool.confi_reader import read_ssh from tool.confi_reader import read_ssh, read_broker
from tool.mqtt_client import MqttClient from tool.mqtt_client import MqttClient
from tool.ssh_client import SSHClient, Cmd from tool.ssh_client import SSHClient, Cmd
from tool.update import VersionManager
class Ihm(MqttClient, SSHClient): class Ihm(MqttClient, SSHClient):
def __init__(self): def __init__(self):
MqttClient.__init__(self, "Ihm") self.broker_ip = read_broker(str(pathlib.Path(__file__).parent.resolve()) + "/tool/config.json")
SSHClient.__init__(self) MqttClient.__init__(self, self.broker_ip, "Ihm")
SSHClient.__init__(self, read_ssh(str(pathlib.Path(__file__).parent.resolve()) + "/tool/config.json"))
def run(self): def run(self):
...@@ -30,7 +30,7 @@ class Ihm(MqttClient, SSHClient): ...@@ -30,7 +30,7 @@ class Ihm(MqttClient, SSHClient):
# Envoie un signal a tout le monde pour exit # Envoie un signal a tout le monde pour exit
if cmd.lower() == "exit": if cmd.lower() == "exit":
self.client.publish("ihm/exit") self.client.publish("ping/exit")
exit_bool = True exit_bool = True
# cherche tout les potentiel agents sur les raspberry et les tuent # cherche tout les potentiel agents sur les raspberry et les tuent
...@@ -77,8 +77,7 @@ class Ihm(MqttClient, SSHClient): ...@@ -77,8 +77,7 @@ class Ihm(MqttClient, SSHClient):
print("Hostname :", self.clients[i_client].hostname, " User :", self.clients[i_client].user) print("Hostname :", self.clients[i_client].hostname, " User :", self.clients[i_client].user)
self.run_cmd(i_client, commands) self.run_cmd(i_client, commands)
updater = VersionManager() self.update()
updater.update()
# charge une experience et verifie le format # charge une experience et verifie le format
if cmd.lower() == "load": if cmd.lower() == "load":
...@@ -107,21 +106,22 @@ class Ihm(MqttClient, SSHClient): ...@@ -107,21 +106,22 @@ class Ihm(MqttClient, SSHClient):
cmd = input(">") cmd = input(">")
# start subprocess scheduler # start subprocess scheduler
p1 = Popen([sys.executable, 'scheduler.py', cmd]) p1 = Popen([sys.executable, 'scheduler.py', cmd, self.broker_ip])
sleep(1) sleep(1)
# start subprocess amas # start subprocess amas
p2 = Popen([sys.executable, experiment_folder + '/amas.py']) send_client = [ c.to_send() for c in self.clients]
p2 = Popen([sys.executable, experiment_folder + '/amas.py', self.broker_ip, str(send_client)])
# start subprocess env # start subprocess env
p3 = Popen([sys.executable, experiment_folder + '/env.py']) p3 = Popen([sys.executable, experiment_folder + '/env.py', self.broker_ip])
if cmd.lower() == "pause": if cmd.lower() == "pause":
self.client.publish("ihm/pause") self.client.publish("ping/pause")
if cmd.lower() == "unpause": if cmd.lower() == "unpause":
self.client.publish("ihm/unpause") self.client.publish("ping/unpause")
if cmd.lower() in ["s", "step"]: if cmd.lower() in ["s", "step"]:
self.client.publish("ihm/step") self.client.publish("ping/step")
if cmd.lower() == "ping": if cmd.lower() == "ping":
...@@ -134,9 +134,9 @@ class Ihm(MqttClient, SSHClient): ...@@ -134,9 +134,9 @@ class Ihm(MqttClient, SSHClient):
print("Hostname :", client.hostname, " Responded : ", result) print("Hostname :", client.hostname, " Responded : ", result)
if cmd.lower() == "mode": if cmd.lower() == "mode":
self.client.publish("ihm/mode") self.client.publish("ping/mode")
self.client.publish("ihm/step") self.client.publish("ping/step")
sleep(2) sleep(2)
......
...@@ -13,9 +13,9 @@ from random import randrange ...@@ -13,9 +13,9 @@ from random import randrange
class PhiAgent(Agent): class PhiAgent(Agent):
def __init__(self, identifier: int, total_agent): def __init__(self, identifier: int, broker_ip: str, total_agent):
self.total_agent = total_agent self.total_agent = total_agent
super().__init__(identifier) Agent.__init__(self, identifier, broker_ip)
def on_initialization(self): def on_initialization(self):
# 0: thinking, 1: hungry, 2: eat # 0: thinking, 1: hungry, 2: eat
...@@ -96,5 +96,5 @@ class PhiAgent(Agent): ...@@ -96,5 +96,5 @@ class PhiAgent(Agent):
if __name__ == '__main__': if __name__ == '__main__':
a = PhiAgent(int(sys.argv[1]), int(sys.argv[2])) a = PhiAgent(int(sys.argv[1]), str(sys.argv[2]), int(sys.argv[3]))
a.run() a.run()
...@@ -8,9 +8,9 @@ from amas import Amas ...@@ -8,9 +8,9 @@ from amas import Amas
class PhiAmas(Amas): class PhiAmas(Amas):
def __init__(self, nbr_agent): def __init__(self, broker_ip: str, clients, nbr_agent):
self.agent_to_create = nbr_agent self.agent_to_create = nbr_agent
super().__init__() super().__init__(broker_ip, clients)
def on_initial_agents_creation(self): def on_initial_agents_creation(self):
for _ in range(self.agent_to_create): for _ in range(self.agent_to_create):
...@@ -18,5 +18,5 @@ class PhiAmas(Amas): ...@@ -18,5 +18,5 @@ class PhiAmas(Amas):
if __name__ == '__main__': if __name__ == '__main__':
s = PhiAmas(3) s = PhiAmas(str(sys.argv[1]), sys.argv[2], 5)
s.run() s.run()
...@@ -10,9 +10,9 @@ from philosophers.fork import Fork ...@@ -10,9 +10,9 @@ from philosophers.fork import Fork
class PhiEnv(Environment): class PhiEnv(Environment):
def __init__(self, nbr_phil): def __init__(self, broker_ip, nbr_phil):
self.nbr_phil = nbr_phil self.nbr_phil = nbr_phil
super().__init__() super().__init__(broker_ip)
def on_initialization(self): def on_initialization(self):
self.forks = [] self.forks = []
...@@ -65,5 +65,5 @@ class PhiEnv(Environment): ...@@ -65,5 +65,5 @@ class PhiEnv(Environment):
if __name__ == '__main__': if __name__ == '__main__':
s = PhiEnv(3) s = PhiEnv(str(sys.argv[1]), 5)
s.run() s.run()
\ No newline at end of file
...@@ -12,9 +12,9 @@ class Scheduler(Schedulable): ...@@ -12,9 +12,9 @@ class Scheduler(Schedulable):
Scheduler class, it's role is to make sure the amas, the env and all the agents stay in sync 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) -> None: def __init__(self, execution_policy: int, broker_ip: str) -> None:
Schedulable.__init__(self, client_id="Scheduler") Schedulable.__init__(self, broker_ip, "Scheduler")
self.sleep_between_cycle = 0 self.sleep_between_cycle = 0
# 0: pas a pas, 1: auto # 0: pas a pas, 1: auto
...@@ -26,10 +26,10 @@ class Scheduler(Schedulable): ...@@ -26,10 +26,10 @@ class Scheduler(Schedulable):
self.subscribe("amas/agent/new", self.update_nbr_agent) self.subscribe("amas/agent/new", self.update_nbr_agent)
self.subscribe("amas/action_done", self.update_schedulable) self.subscribe("amas/action_done", self.update_schedulable)
self.subscribe("env/action_done", self.update_schedulable) self.subscribe("env/action_done", self.update_schedulable)
self.subscribe("ihm/step", self.step) self.subscribe("ping/step", self.step)
self.subscribe("ihm/pause", self.pause) self.subscribe("ping/pause", self.pause)
self.subscribe("ihm/unpause", self.unpause) self.subscribe("ping/unpause", self.unpause)
self.subscribe("ihm/mode", self.mode) self.subscribe("ping/mode", self.mode)
self.agent_waiting = 0 self.agent_waiting = 0
self.schedulable_waiting = 0 self.schedulable_waiting = 0
...@@ -172,5 +172,5 @@ class Scheduler(Schedulable): ...@@ -172,5 +172,5 @@ class Scheduler(Schedulable):
if __name__ == '__main__': if __name__ == '__main__':
s = Scheduler(int(sys.argv[1])) s = Scheduler(int(sys.argv[1]), str(sys.argv[2]))
s.run() s.run()
{ {
"broker" : "192.168.24.209", "broker" : "192.168.153.209",
"clients_ssh" : [ "clients_ssh" : [
{ {
"hostname" : "192.168.24.18", "hostname" : "192.168.153.18",
"user" : "pi", "user" : "pi",
"password" : "raspberry" "password" : "raspberry"
}, },
{ {
"hostname" : "192.168.24.61", "hostname" : "192.168.153.227",
"user" : "pi", "user" : "pi",
"password" : "raspberry" "password" : "raspberry"
}, },
{ {
"hostname" : "192.168.24.227", "hostname" : "192.168.153.61",
"user" : "pi", "user" : "pi",
"password" : "raspberry" "password" : "raspberry"
}, },
{ {
"hostname" : "192.168.24.75", "hostname" : "192.168.153.75",
"user" : "pi", "user" : "pi",
"password" : "raspberry" "password" : "raspberry"
} }
......
import pathlib
import sys
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from paho.mqtt.client import Client from paho.mqtt.client import Client
from tool.confi_reader import read_broker
class MqttClient: class MqttClient:
def __init__(self, client_id: str = None): def __init__(self, broker_ip, client_id):
self.client = Client(client_id=client_id) self.client = Client(client_id=client_id)
self.client.username_pw_set(username="goyon", password="mosquitto") self.client.username_pw_set(username="goyon", password="mosquitto")
self.client.connect(read_broker(str(pathlib.Path(__file__).parent.resolve()) + "/config.json")) self.client.connect(host=broker_ip)
self.client.loop_start() self.client.loop_start()
def subscribe(self, topic, fun): def subscribe(self, topic, fun):
......
...@@ -4,3 +4,6 @@ class RemoteClient: ...@@ -4,3 +4,6 @@ class RemoteClient:
self.hostname = hostname self.hostname = hostname
self.user = user self.user = user
self.password = password self.password = password
def to_send(self) -> dict:
return {"hostname": self.hostname, "user": self.user, "password": self.password}
...@@ -15,11 +15,11 @@ class Schedulable(MqttClient): ...@@ -15,11 +15,11 @@ class Schedulable(MqttClient):
Base class for Agent/Amas/Env/scheduler Base class for Agent/Amas/Env/scheduler
""" """
def __init__(self, client_id: str): def __init__(self, broker_ip: str, client_id: str):
MqttClient.__init__(self, client_id) MqttClient.__init__(self, broker_ip, client_id)
self.exit_bool: bool = False self.exit_bool: bool = False
self.subscribe("ihm/exit", self.exit_procedure) self.subscribe("ping/exit", self.exit_procedure)
self.nbr_cycle: int = 0 self.nbr_cycle: int = 0
self.wait_delay: float = 0.01 self.wait_delay: float = 0.01
......
import os
import pathlib import pathlib
import sys import sys
from typing import List
import paramiko
from pexpect import pxssh from pexpect import pxssh
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent)) sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.confi_reader import read_ssh from tool.remote_client import RemoteClient
class Cmd: class Cmd:
...@@ -17,13 +21,14 @@ class Cmd: ...@@ -17,13 +21,14 @@ class Cmd:
class SSHClient: class SSHClient:
def __init__(self): def __init__(self, clients: List[RemoteClient]):
self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve()) + "/config.json") self.clients = clients
def run_cmd(self, client: int, cmd: list): def run_cmd(self, client: int, cmd: list):
try: try:
s = pxssh.pxssh() s = pxssh.pxssh()
dest = self.clients[client] dest = self.clients[client]
print(dest.hostname, dest.user, dest.password)
s.login(dest.hostname, dest.user, dest.password) s.login(dest.hostname, dest.user, dest.password)
for command in cmd: for command in cmd:
...@@ -36,3 +41,40 @@ class SSHClient: ...@@ -36,3 +41,40 @@ class SSHClient:
except pxssh.ExceptionPxssh as e: except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.") print("pxssh failed on login.")
print(e) print(e)
def update(self):
for client in self.clients:
transport = paramiko.Transport((client.hostname, 22))
transport.connect(username=client.user, password=client.password)
sftp = MySFTPClient.from_transport(transport)
sftp.mkdir("./Desktop/mqtt_goyon/iotamak-core", ignore_existing=True)
sftp.put_dir("/mnt/d/work/stage m1/iotamak-core", "./Desktop/mqtt_goyon/iotamak-core")
sftp.close()
class MySFTPClient(paramiko.SFTPClient):
def put_dir(self, source, target):
''' Uploads the contents of the source directory to the target path. The
target directory needs to exists. All subdirectories in source are
created under target.
'''
for item in os.listdir(source):
if os.path.isfile(os.path.join(source, item)):
self.put(os.path.join(source, item), '%s/%s' % (target, item))
print(os.path.join(source, item))
else:
if any([i in item for i in [".git", "__pycache__", ".idea", ".vscode"]]):
pass
else:
self.mkdir('%s/%s' % (target, item), ignore_existing=True)
self.put_dir(os.path.join(source, item), '%s/%s' % (target, item))
print(os.path.join(source, item))
def mkdir(self, path, mode=511, ignore_existing=False):
''' Augments mkdir by adding an option to not fail if the folder exists '''
try:
super(MySFTPClient, self).mkdir(path, mode)
except IOError:
if ignore_existing:
pass
else:
raise
import os
import pathlib
import sys
import paramiko
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent))
from tool.confi_reader import read_ssh
class VersionManager:
def __init__(self):
self.clients = read_ssh(str(pathlib.Path(__file__).parent.resolve()) + "/config.json")
def update(self):
for client in self.clients:
transport = paramiko.Transport((client.hostname, 22))
transport.connect(username=client.user, password=client.password)
sftp = MySFTPClient.from_transport(transport)
sftp.mkdir("./Desktop/mqtt_goyon/iotamak-core", ignore_existing=True)
sftp.put_dir("/mnt/d/work/stage m1/iotamak-core", "./Desktop/mqtt_goyon/iotamak-core")
sftp.close()
class MySFTPClient(paramiko.SFTPClient):
def put_dir(self, source, target):
''' Uploads the contents of the source directory to the target path. The
target directory needs to exists. All subdirectories in source are
created under target.
'''
for item in os.listdir(source):
if os.path.isfile(os.path.join(source, item)):
self.put(os.path.join(source, item), '%s/%s' % (target, item))
print(os.path.join(source, item))
else:
if any([i in item for i in [".git", "__pycache__", ".idea", ".vscode"]]):
pass
else:
self.mkdir('%s/%s' % (target, item), ignore_existing=True)
self.put_dir(os.path.join(source, item), '%s/%s' % (target, item))
print(os.path.join(source, item))
def mkdir(self, path, mode=511, ignore_existing=False):
''' Augments mkdir by adding an option to not fail if the folder exists '''
try:
super(MySFTPClient, self).mkdir(path, mode)
except IOError:
if ignore_existing:
pass
else:
raise
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment