-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation_client.py
More file actions
232 lines (181 loc) · 6.52 KB
/
Copy pathsimulation_client.py
File metadata and controls
232 lines (181 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import json
import os
import sys
import docker
from docker.models.containers import Container
import time
import paho.mqtt.client as mqtt
import socket
from dotenv import load_dotenv
from simulation_terminal import SynchronousTerminal
load_dotenv() # take environment variables from .env.
class DockerOrchestrator:
def __init__(self, pub_ip):
try:
self.client = docker.from_env()
except:
print("Is docker running???")
sys.exit(2)
self.public_ip = pub_ip
self.running_peers: dict[bytes, dict[str, int | Container]] = {}
def run(self, peer_address, os_port, io_port):
wkdir = os.getcwd() + "/log"
print(f"Creating node: {peer_address.hex(sep=':')} os: {os_port} io: {io_port}")
transport = f"tcp://{self.public_ip}:{os_port}"
c_obj = self.client.containers.run(
"starfish",
auto_remove=True,
detach=True,
environment={
"ADDRESS": f"{peer_address.hex(sep=':')}",
"TRANSPORT": f"{transport}",
},
volumes={wkdir: {"bind": "/home/ubuntu/log", "mode": "rw"}},
# ports: container_in : host
ports={os_port: os_port, 2321: io_port},
)
self.running_peers[peer_address] = {
"os": os_port,
"io": io_port,
"container": c_obj,
}
def stop(self, peer_address):
print(f"Stopping node: {peer_address.hex(sep=':')}")
self.running_peers[peer_address]["container"].stop()
# self.running_peers[peer_address]["container"].remove()
del self.running_peers[peer_address]
def get_output(self, peer_address):
container = self.running_peers[peer_address]["container"]
return container.logs()
def get_filestorage_list(self, peer_address):
container = self.running_peers[peer_address]["container"]
code, output = container.exec_run("ls filestorage/")
return output.decode("utf-8").split("\n")
###################################################################
# USE MQTT Client.... (for worker processes)
# Register to the cloud that IP address exists
# starfish/ips/<IP> = val
# listen for commands
# starfish/ips/<IP>/command = COMMAND.
# spawn peer with address
# kill peer
#
# RPC Sequence:
# Check if command is blank or ACK-123123.
# Send command request... client then does the command and sets it to ACK-##
#
# starfish/ips/<IP>/peers/PEERID
# shows peer address, OS port, and IO port
MQTT_SERVER = os.getenv("MQTT_SERVER", "")
MQTT_PORT = int(os.getenv("MQTT_PORT", 1883))
MQTT_USER = os.getenv("MQTT_USER", "")
MQTT_PWD = os.getenv("MQTT_PWD", "")
HEARTBEAT = 10
def get_my_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
my_ip = s.getsockname()[0]
s.close()
return my_ip
def is_port_in_use(port: int) -> bool:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0
MY_IP = get_my_local_ip()
orchestra = DockerOrchestrator(MY_IP)
os_port_counter = 9280
io_port_counter = 2321
def on_connect(client: mqtt.Client, userdata, flags, reason_code, properties):
# print(f"Connected with result code {reason_code}")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
val = time.time()
ip = MY_IP
print(f"Publishing IP {ip}")
client.publish(f"starfish/ips/{ip}", val, 1) # don't retain this one.
initial = {"command": "ack", "return": 0}
client.publish(f"starfish/ips/{ip}/command", json.dumps(initial), 1, retain=True)
client.subscribe(
f"starfish/ips/{ip}/command"
) # the '#' means wildcard of any subtopics.
def on_message(client, userdata, msg):
# print(msg.topic + " " + str(msg.payload))
if msg.payload == b"":
return
if msg.topic == f"starfish/ips/{MY_IP}/command":
run_command(client, msg.payload)
def run_command(client: mqtt.Client, command):
s = command.decode("utf-8")
c = json.loads(s)
if c["command"] == "ack":
return
if c["command"] == "new":
# create new
create_node(client, c["peerID"])
if c["command"] == "kill":
# create new
kill_node(client, c["peerID"])
if c["command"] == "tel-connect":
host = c["host"]
port = c["port"]
peerID = c["peerID"]
transport = c["transport"]
try:
st = SynchronousTerminal(host, port)
st.connect_peer(bytes.fromhex(peerID), transport)
except Exception as e:
print("ERROR---- tel connect")
print(e)
if c["command"] == "tel-start":
host = c["host"]
port = c["port"]
pgrm = c["pgrm"]
usr = c["user"]
try:
st = SynchronousTerminal(host, port)
st.start_program(pgrm, usr)
except Exception as e:
print("ERROR---- tel start")
print(e)
ret = 0
if "return" in c:
ret = c["return"]
value = {"command": "ack", "return": ret}
client.publish(f"starfish/ips/{MY_IP}/command", json.dumps(value), 1, retain=True)
def create_node(client: mqtt.Client, peerID):
global os_port_counter
global io_port_counter
while is_port_in_use(os_port_counter):
os_port_counter += 1
while is_port_in_use(io_port_counter):
io_port_counter += 1
peer_address = bytes.fromhex(peerID)
orchestra.run(peer_address, os_port_counter, io_port_counter)
value = {"os": os_port_counter, "io": io_port_counter}
# retain means when a later peer subscribes, they will be updated.
client.publish(
f"starfish/ips/{MY_IP}/peers/{peerID}", json.dumps(value), 1, retain=True
)
os_port_counter += 1
io_port_counter += 1
def kill_node(client: mqtt.Client, peerID):
peer_address = bytes.fromhex(peerID)
try:
orchestra.stop(peer_address)
except:
pass
# retain means when a later peer subscribes, they will be updated.
client.publish(
f"starfish/ips/{MY_IP}/peers/{peerID}", "", 1, retain=True
) # clear topic
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.loop_start()
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.username_pw_set(MQTT_USER, MQTT_PWD)
mqttc.connect(MQTT_SERVER, MQTT_PORT, 30)
while True:
val = time.time()
mqttc.publish(f"starfish/ips/{MY_IP}", val, 1) # don't retain this one.
time.sleep(HEARTBEAT)
mqttc.loop_stop()