-
Notifications
You must be signed in to change notification settings - Fork 0
/
aotp-mqtt-sender.py
66 lines (57 loc) · 1.72 KB
/
aotp-mqtt-sender.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
import paho.mqtt.client as mqtt
from time import sleep
from random import randint
import json
import sys
import common
AP = lambda x, P: common.apply_P(x, P)
def make_message_m():
H = {}
n_t = {}
p = common.rand_string(2)
P = []
for i in xrange(0, randint(3,6)):
P.append(randint(0,10))
for t in common.topics:
H_t = []
s = common.rand_string(2)
n = randint(4, 8)
n_t[t] = n
H_t.append(common.my_hash(s))
for x in xrange(1, n):
H_t.append(common.my_hash(str(H_t[x-1])) * AP(x, P))
H[t] = H_t
M = {}
for t in H:
M[t] = H[t][-1]
return (p, M, P, H, n_t)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_message = on_message
url = sys.argv[1] if len(sys.argv) == 2 else common.server_url
client.connect(url, 1883, 60)
print common.microseconds(), "starting bootstrap"
p, M, P, H, n_t = make_message_m()
m = {'p': p, 'M': M, 'P': P, 'n_t': n_t}
client.publish("hsos/aotp/bootstrap", json.dumps(m))
print common.microseconds(), "bootstrap message sent"
sleep(3)
for i in xrange(1, 200):
x = randint(0, len(common.topics)-1)
t = common.topics[x]
n = n_t[t]
if n == 1:
print t, "has no more hashes"
p, M, P, H, n_t = make_message_m()
m = {'p': p, 'M': M, 'P': P, 'n_t': n_t}
client.publish("hsos/aotp/bootstrap", json.dumps(m))
else:
topic = p + "/" + str(H[t][n-2])
print i, t, ">", topic
client.publish(topic, common.microseconds())
n_t[t] -= 1
sleep(5)
client.publish("hsos/aotp/finish", "")
client.disconnect()