-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub-client.py
239 lines (212 loc) · 8.21 KB
/
sub-client.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
import json
import paho.mqtt.client as mqtt
from paho.mqtt.packettypes import PacketTypes
from paho.mqtt.properties import Properties
from paho.mqtt.reasoncodes import ReasonCodes
import time
import datetime
import socket
import argparse
import random
import threading
import os
from typing import Any, List, Dict
from util import (
dump_data,
calc_stats,
get_time,
record_connect,
connect_to_broker,
transport,
parse_yaml,
)
def periodic_disconnect(client: mqtt.Client, userdata: Dict[str, Any]):
"""Periodically disconnects the client based on the specified disconnect_perc. Ends on KeyboardInterrupt."""
while not userdata["disconnect_event"].is_set():
time.sleep(userdata["disconnect_interval"])
n: float = random.uniform(0, 1)
if n <= userdata["disconnect_perc"]:
client.disconnect()
userdata["e2e_data"].append(
{
"seq_num": -1,
"last_seq_num": userdata["e2e_data"][-1]["seq_num"]
if len(userdata["e2e_data"]) > 0
else -1,
"disconnect_time": get_time(),
"reconnect_time": -1,
}
)
# wait for reconnect before starting next interval
time.sleep(userdata["disconnect_duration"])
def on_connect(
client: mqtt.Client,
userdata: Dict[str, Any],
flags: Dict[str, Any],
reason: ReasonCodes,
properties: Properties,
):
"""Callback for when client receives a CONNACK response from broker"""
record_connect(userdata)
print("Connected with reason code " + reason.getName())
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("test", qos=userdata["qos"])
# Create and start disconnect thread only if:
# We want disconnections to happen (ie. disconnect_perc > 0)
# Thread has not already been created and started
if userdata["disconnect_thread"] is None and userdata["disconnect_perc"] > 0:
userdata["disconnect_event"] = threading.Event()
userdata["disconnect_thread"] = threading.Thread(
target=periodic_disconnect, args=[client, userdata]
)
userdata["disconnect_thread"].start()
def on_message(client: mqtt.Client, userdata: Dict[str, Any], msg: mqtt.MQTTMessage):
"""Callback for when a PUBLISH message is received from the server"""
rcv_time: float = get_time()
print(f"{msg.topic} {msg.payload} {msg.mid}")
if msg.topic == "test":
content: str = msg.payload.decode()
seq_num, send_time = content.split(" ")
pkt_data: Dict[str, Any] = {
"seq_num": int(seq_num),
"send_time": float(send_time),
"rcv_time": rcv_time,
"time_diff": (rcv_time - float(send_time)),
"qos": msg.qos,
}
userdata["e2e_data"].append(pkt_data)
def on_log(client, userdata, level, buf):
"""Logs messages sent and received by client"""
print(f"[{level}] {buf}")
if __name__ == "__main__":
# Process arguments
parser = argparse.ArgumentParser(
prog="sub-client",
usage="Usage: python sub-client.py -f <input-file-path>",
)
parser.add_argument(
"-f",
"--file",
help="Path to file with input variables",
required=False,
default="",
)
args = parser.parse_args()
# Initialise userdata to be passed to client callbacks
e2e_data: List[Dict[str, Any]] = []
conn_data: List[Dict[str, Any]] = []
userdata: Dict[str, Any] = { # default values
"qos": 0,
"label": "normal",
"tls": False,
"total_packets": 50,
"e2e_data": e2e_data,
"conn_time": -1,
"conn_tries": 0,
"conn_data": conn_data,
"disconnect_perc": 0,
"disconnect_interval": 10,
"disconnect_duration": 10,
"disconnect_event": None, # Optional[threading.Event]
"disconnect_thread": None, # Optional[threading.Thread]
}
userdata = parse_yaml(args.file, userdata, "subscriber")
print(f"userdata: {userdata}")
try:
# Initialise client and callbacks
client = mqtt.Client(
client_id="test-sub",
userdata=userdata,
protocol=mqtt.MQTTv5,
transport=transport,
)
client.username_pw_set("test", "test")
if userdata["tls"]:
client.tls_set()
client.on_connect = on_connect
client.on_message = on_message
client.on_log = on_log
# Initial connect
properties = Properties(PacketTypes.CONNECT)
properties.SessionExpiryInterval = 30
connect_to_broker(client, userdata, properties)
start_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
# Loop forever with periodic disconnects and reconnects
while True:
client.loop_forever()
# client disconnects and loop stops --> initiate reconnect after disconnect_duration
time.sleep(userdata["disconnect_duration"])
connected = False
userdata["conn_time"] = get_time()
while not connected:
userdata["conn_tries"] += 1
try:
client.reconnect()
connected = True
userdata["e2e_data"][-1]["reconnect_time"] = get_time()
except socket.timeout:
pass
except KeyboardInterrupt:
cur_date = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
if conn_data:
conn_data_fname = dump_data("sub-conn", conn_data, cur_date, userdata)
conn_delay_stats = calc_stats(conn_data)
conn_tries_stats = calc_stats(conn_data, "tries")
if e2e_data:
# Write collected data to file
# Can delete if we don't need to collect all the generated data
# Just collecting for now in case we want to do further analysis later on
data_fname = dump_data("sub", e2e_data, cur_date, userdata)
# Process collected data
print("Calculating statistics...")
e2e_stats = calc_stats(e2e_data)
stats_folder = "summary/"
stats_fname = (
stats_folder
+ "_qos"
+ str(userdata["qos"])
+ "_"
+ userdata["label"]
+ ("_tls" if userdata["tls"] else "")
+ ".json"
)
if not os.path.isdir(stats_folder):
os.mkdir(stats_folder)
with open(stats_fname, "r+") as stats_f:
cur_data = json.load(stats_f)
summary_data = {
"start_time": start_time,
"label": userdata["label"],
"e2e_data_file": data_fname,
"tls": userdata["tls"],
"qos": userdata["qos"],
"pkt_sent": userdata["total_packets"],
"pkt_recv": e2e_stats["count"],
"pkt_loss": (userdata["total_packets"] - e2e_stats["count"])
/ userdata["total_packets"],
"e2e_delay": e2e_stats,
}
if conn_data:
summary_data["conn_delay"] = conn_delay_stats
summary_data["conn_tries"] = conn_tries_stats
summary_data["conn_data_file"] = conn_data_fname
stats_f.seek(0)
json.dump({**cur_data, "subscriber": summary_data}, stats_f)
os.rename(
stats_fname,
stats_folder
+ cur_date
+ "_qos"
+ str(userdata["qos"])
+ "_"
+ userdata["label"]
+ ("_tls" if userdata["tls"] else "")
+ ".json",
)
# Stop disconnect thread, blocks until disconnect thread has been stopped
if userdata["disconnect_thread"] is not None:
print("Cancelling timer...")
userdata["disconnect_event"].set()
userdata["disconnect_thread"].join()
print("Subscriber closed successfully")