-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient-connect.py
146 lines (132 loc) · 4.75 KB
/
client-connect.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
import paho.mqtt.client as mqtt
import time
import dns.resolver as dns
import statistics
#broker="comp3310.ddns.net"
ip=""
slowmessagesq0 = []
slowmessagesq1 = []
slowmessagesq2 = []
fastmessagesq0 = []
fastmessagesq1 = []
fastmessagesq2 = []
dupes = {
"counter/slow/q0" : 0,
"counter/slow/q1" : 0,
"counter/slow/q2" : 0,
"counter/fast/q0" : 0,
"counter/fast/q1" : 0,
"counter/fast/q2" : 0,
}
times = {
"counter/slow/q0" : [],
"counter/slow/q1" : [],
"counter/slow/q2" : [],
"counter/fast/q0" : [],
"counter/fast/q1" : [],
"counter/fast/q2" : [],
}
ooo = []
for i in dns.query("comp3310.ddns.net"):
ip = str(i).split('\n', 1)[0]
def on_log(client, userdata, level, buf):
print("log" + buf)
def on_connect(client, userdata, flags, rc):
if rc==0:
print("connected OK")
else:
print("bad connection returned code=", rc)
def on_message(client, userdata, msg):
topic=msg.topic
#print(time.time())
print(topic)
dupes[topic] = dupes[topic] + int(msg.dup)
times[topic].append((round(time.time()*1000, 2)))
print("msg.dup: ", msg.dup)
global slowmessagesq0, slowmessagesq1, slowmessagesq2, fastmessagesq0, fastmessagesq1, fastmessagesq2
#print("topic" + topic)
m_decode=str(msg.payload.decode("utf-8"))
if topic == "counter/slow/q0":
slowmessagesq0.append(m_decode);
elif topic == "counter/slow/q1":
slowmessagesq1.append(m_decode);
elif topic == "counter/slow/q2":
slowmessagesq2.append(m_decode);
elif topic == "counter/fast/q0":
fastmessagesq0.append(m_decode);
elif topic == "counter/fast/q1":
fastmessagesq1.append(m_decode);
elif topic == "counter/fast/q2":
fastmessagesq2.append(m_decode);
#print("message received", m_decode)
def messagerate(topic, messagelist, time):
print(topic, "has a message rate of", round(len(messagelist)/time, 2), "messages per second.")
amountmsg = len(messagelist)
current = int(messagelist[0])
loss = 0
i = 1
lost = []
differences = []
while i < amountmsg:
if messagelist[i]==0:
current=messagelist[i]
i += 1
continue
if int(messagelist[i]) < current:
ooo.append(int(messagelist[i]))
elif int(messagelist[i])-1 == current:
current=int(messagelist[i])
else:
for x in range(current, int(messagelist[i])):
lost.append(x)
loss += 1
current=int(messagelist[i])
i += 1
if topic.__contains__("fast"):
j = 0
while j < len(times[topic])-1:
differences.append(times[topic][j+1] - times[topic][j])
j += 1
print("The loss rate for this topic is", round(loss/(amountmsg+loss), 2), "%.", "There are a total of", len(lost), "lost/misplaced messages.")
print("The amount of duplicates for topic", topic, "are", dupes[topic], ".")
print("The Out-Of-Order rate for this topic is", round(len(ooo)/amountmsg), ".")
print("The mean inter-message gap for this topic is", statistics.mean(differences), "milliseconds.")
print("The gap variation is", statistics.stdev(differences), "milliseconds.")
print('\n')
broker = ip
client = mqtt.Client("3310-u6051965")
client.on_connect=on_connect
# client.on_log=on_log
client.username_pw_set("students", "33106331")
print("connecting to broker ", broker)
client.connect(broker)
client.loop_start()
client.on_message = on_message
# client.subscribe("counter/slow/q0", qos=0)
# time.sleep(15)
# client.unsubscribe("counter/slow/q0")
# client.subscribe("counter/slow/q1", qos=1)
# time.sleep(15)
# client.unsubscribe("counter/slow/q1")
# client.subscribe("counter/slow/q2", qos=2)
# time.sleep(15)
# client.unsubscribe("counter/slow/q2")
# client.subscribe("counter/fast/q0", qos=0)
# time.sleep(15)
# client.unsubscribe("counter/fast/q0")
# client.subscribe("counter/fast/q1", qos=1)
# time.sleep(15)
# client.unsubscribe("counter/fast/q1")
client.subscribe("counter/fast/q2", qos=2)
time.sleep(5)
client.unsubscribe("counter/fast/q2")
client.loop_stop()
client.disconnect()
print("done" + str(ip))
# messagerate("counter/slow/q0", slowmessagesq0, 15)
# messagerate("counter/slow/q1", slowmessagesq1, 15)
# messagerate("counter/slow/q2", slowmessagesq2, 15)
# messagerate("counter/fast/q0", fastmessagesq0, 15)
# messagerate("counter/fast/q1", fastmessagesq1, 15)
messagerate("counter/fast/q2", fastmessagesq2, 5)
print(times)