-
Notifications
You must be signed in to change notification settings - Fork 0
/
painlessmesh.py
350 lines (303 loc) · 11.2 KB
/
painlessmesh.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python3
"""
Program implementing a painlessMesh node that can run on any computer
Dependencies:
Python 3
To run just make this script executable and run it.
"""
import argparse
import json
import logging
import random
import socket
import threading
import time
import types
port = 5555
logging.basicConfig()
logger = logging.getLogger(__name__)
PACKAGE_TYPE_DROP = 3
PACKAGE_TYPE_TIME_SYNC = 4
PACKAGE_TYPE_NODE_SYNC_REQUEST = 5
PACKAGE_TYPE_NODE_SYNC_REPLY = 6
PACKAGE_TYPE_BROADCAST = 8 # application data for everyone
PACKAGE_TYPE_SINGLE = 9 # application data for a single node
TIME_SYNC_ERROR = -1
TIME_SYNC_REQUEST = 0
TIME_REQUEST = 1
TIME_REPLY = 2
def _read_json_object(connection):
first_run = True
data = bytes()
while True:
b = connection.recv(1)
if b == b'\x00':
break
data += b
return json.loads(data.decode("utf-8"))
adjust_time = 0
def node_time():
return int(time.time() * 1000000 + adjust_time)
class Node(object):
def __init__(self):
self.node_id = 0
self.connections = {}
self.receive = None
self.mutex = threading.Lock()
def _maintenance(self):
while True:
self.mutex.acquire()
try:
curr_t = time.time()
logger.debug("Checking for unused connections")
for from_id, conn in dict(self.connections).items():
self.send_node_sync(from_id)
if curr_t - conn.last_received > 60:
self.remove_connection(from_id)
finally:
self.mutex.release()
time.sleep(6)
def init_maintenance(self):
self.thread = threading.Thread(target=self._maintenance)
self.thread.setDaemon(True)
self.thread.start()
def init_connection(self, conn):
logger.debug("New connection")
from_id = 0
try:
while True:
json_ = _read_json_object(conn)
self.mutex.acquire()
try:
logger.debug("Received message: {}".format(json_))
if (from_id == 0 and
(json_["type"] == PACKAGE_TYPE_TIME_SYNC or
json_["type"] == PACKAGE_TYPE_NODE_SYNC_REQUEST or
json_["type"] == PACKAGE_TYPE_NODE_SYNC_REPLY)):
from_id = json_["from"]
mesh_connection = Connection()
mesh_connection.tcp = conn
mesh_connection.node_id = from_id
self.connections[from_id] = mesh_connection
if from_id in self.connections:
self.connections[from_id].last_received = time.time()
self.handle_message(from_id, json_)
finally:
self.mutex.release()
except socket.error as e:
logger.error("Failed to read from client: {}".format(e))
self.mutex.acquire()
try:
self.remove_connection(from_id)
finally:
self.mutex.release()
def init_server(self, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("0.0.0.0", port))
sock.listen()
self.init_maintenance()
while True:
c, addr = sock.accept()
logger.debug("Connected from {}".format(addr))
thread = threading.Thread(target=self.init_connection, args=(c, ))
thread.setDaemon(True)
thread.start()
sock.close()
def init_client(self, addr):
self.init_maintenance()
while True:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(addr)
# client initialize with node sync to get all connections right ahead
pack = {
"dest": 0,
"from": self.node_id,
"type": PACKAGE_TYPE_NODE_SYNC_REQUEST,
"subs": []
}
sock.sendall(json.dumps(pack, separators=(',', ':')).
encode("utf-8") + b'\0')
self.init_connection(sock)
except Exception as e:
logger.debug("Exception: {}".format(e))
logger.warning("Unable to connect to: {}. Retrying in 10 seconds".
format(addr))
time.sleep(10)
def on_receive(self, callback):
self.receive = types.MethodType(callback, self)
def send_single(self, dest, msg):
pack = {
"dest": dest,
"from": self.node_id,
"type": PACKAGE_TYPE_SINGLE,
"msg": msg
}
for k, v in dict(self.connections).items():
if v.contains_id(dest):
self.safe_write(k, pack)
return True
return False
def send_broadcast(self, msg):
pack = {
"dest": 0,
"from": self.node_id,
"type": PACKAGE_TYPE_BROADCAST,
"msg": msg
}
for k in list(self.connections):
logger.debug("sendBroadcast: send message to {}.".format(k))
self.safe_write(k, pack)
return True
def safe_write(self, connection_id, data):
pkge = json.dumps(data, separators=(',', ':')).encode("utf-8")
logger.debug("Send to {}, msg {}".format(connection_id, pkge))
try:
if connection_id in self.connections:
# append a zero character
self.connections[connection_id].tcp.sendall(bytes(pkge) + b'\0')
return True
except Exception as e:
self.remove_connection(connection_id)
logger.error("Failed to write to client: {}".format(e))
return False
def remove_connection(self, conn_id):
if conn_id in self.connections:
logger.debug("Removing connection {}: ".format(conn_id))
self.connections[conn_id].tcp.close()
del self.connections[conn_id]
# Inform the other nodes
for other_id in self.connections:
self.send_node_sync(other_id, True)
return True
return False
def handle_message(self, from_id, json_):
global adjust_time
if not from_id in self.connections:
logger.debug("Unknown nodeId")
return False
if (json_["type"] == PACKAGE_TYPE_NODE_SYNC_REQUEST or
json_["type"] == PACKAGE_TYPE_NODE_SYNC_REPLY):
self.connections[from_id].subs = [SubConnection(x) for x in
json_["subs"]]
if json_["type"] == PACKAGE_TYPE_NODE_SYNC_REQUEST:
self.send_node_sync(from_id, False)
elif json_["type"] == PACKAGE_TYPE_TIME_SYNC:
time_received = node_time()
tS = json_["msg"]
if tS["type"] != TIME_REPLY:
pack = {
"dest": from_id,
"from": self.node_id,
"type": PACKAGE_TYPE_TIME_SYNC,
}
if tS["type"] == TIME_SYNC_REQUEST:
tS["t0"] = node_time()
elif tS["type"] == TIME_REQUEST:
tS["t1"] = time_received
tS["t2"] = node_time()
tS["type"] += 1
pack["msg"] = tS
self.safe_write(from_id, pack)
else:
adjust_time += ((tS["t1"] - tS["t0"]) / 2 + (tS["t2"] -
time_received) / 2)
elif json_["type"] == PACKAGE_TYPE_BROADCAST:
# if broadcast, forward it and pass it to received callback
for k, v in dict(self.connections).items():
if not v.contains_id(from_id): # Don't send it back
self.safe_write(k, json_)
if callable(self.receive):
self.receive(json_["from"], json_["msg"])
elif json_["type"] == PACKAGE_TYPE_SINGLE:
dest = json_["dest"]
if callable(self.receive) and dest == self.node_id:
self.receive(json_["from"], json_["msg"])
else:
# Forward it
for k, v in dict(self.connections).items():
if v.contains_id(dest):
# forwarding
self.safe_write(k, json_)
break
return True
def send_node_sync(self, from_id, request=True):
if from_id in self.connections:
pack = {
"dest": from_id,
"from": self.node_id
}
pack["type"] = PACKAGE_TYPE_NODE_SYNC_REQUEST if request else (
PACKAGE_TYPE_NODE_SYNC_REPLY)
pack["subs"] = [v.json() for k, v in self.connections.items() if
k != from_id]
self.safe_write(from_id, pack)
else:
logger.debug("Connection not found during node sync")
class SubConnection(object):
def __init__(self, json_ = {"nodeId": 0, "subs": []}):
self.node_id = json_["nodeId"]
self.subs = ([SubConnection(x) for x in json_["subs"]] if "subs" in
json_ else [])
def contains_id(self, id):
if self.node_id == id:
return True
if not len(self.subs): # or not self.subs?
return False
for k, v in self.subs.items():
if v.contains_id(id):
return True
def json(self):
pack = {
"nodeId": self.node_id,
"subs": [x.json() for x in self.subs]
}
return pack
class Connection(SubConnection):
def __init__(self):
super().__init__()
self.tcp = None
self.last_received = 0
node = None
def main():
global node, port
node = Node()
node.node_id = random.randint(0, 0xFFFFFFFF)
# print("Node ID: {}".format(node.node_id))
def on_receive(self, from_, msg):
print(msg)
node.on_receive(on_receive)
def task():
while True:
node.mutex.acquire()
msg = {
"topic": "logNode",
"nodeId": node.node_id
}
node.send_broadcast(msg)
node.mutex.release()
time.sleep(120)
main_thread = threading.Thread(target=task)
main_thread.setDaemon(True)
main_thread.start()
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="Enable debug messages",
action="store_true")
parser.add_argument("-p", "--port", help="Optional port (default is 5555)",
type=int, default=5555)
parser.add_argument("-c", "--client",
help="Run in client mode. Need to provide ip of the node to connect to.",
type=str)
args = parser.parse_args()
if args.verbose:
logger.setLevel(logging.DEBUG)
# Rest is needed to run the server/client. Don't change this unless you
# know what you are doing
port = args.port
if args.client:
node.init_client((args.client, port))
else:
node.init_server(port)
if __name__ == "__main__":
main()