-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
296 lines (227 loc) · 8.44 KB
/
demo.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
"""
Demo code with an echo socket
"""
import logging
import multiprocessing
import time
import noobWS
from noobWS.constants import Keyword
logging.basicConfig(format="%(asctime)s|%(levelname)s: %(message)s", level=logging.INFO)
PORTS = {
"multi1": {"cmd": "tcp://127.0.0.1:50000", "pub": "tcp://127.0.0.1:50001"},
"multi2": {"cmd": "tcp://127.0.0.1:50002", "pub": "tcp://127.0.0.1:50003"},
"single1": {"cmd": "tcp://127.0.0.1:50004", "pub": "tcp://127.0.0.1:50005"},
"single2": {"cmd": "tcp://127.0.0.1:50006", "pub": "tcp://127.0.0.1:50007"},
}
URIS = [
"wss://echo.websocket.org/",
"wss://echo.websocket.org/",
# "wss://demos.kaazing.com/echo", # not working for some reason
]
def start_single(idx: int, name: bytes = b""):
"""Start demo Single subscription NoobServer"""
key = "single{:d}".format(idx + 1)
ports = PORTS[key]
ss = noobWS.NoobServer(URIS[idx], ports["cmd"], ports["pub"], name=name)
# changed to show source socket
ss.out_formatter = lambda msg, uri_name: [
uri_name,
"{}: {}".format(key, msg).encode(),
]
ss.run()
def start_multi(idx: int, name: bytes = b""):
"""Start demo Multi subscription NoobServer"""
key = "multi{:d}".format(idx + 1)
ports = PORTS[key]
ms = noobWS.NoobServer(
dict(zip([b"echo1", b"echo2"], URIS)), ports["cmd"], ports["pub"], name=name
)
ms.run()
def demo_sc_ss():
"""Single Client x Single Socket"""
logging.info("DEMO: Single Client x Single Socket echo server.")
logging.info("Please ensure you can connect to %s", URIS[0])
sock_name = b"single-socket"
p = multiprocessing.Process(
target=start_single, args=(0,), kwargs={"name": sock_name}, daemon=True
)
p.start()
sleep_s = 3
logging.info("Wait ~%.1f seconds for the socket to initialize", sleep_s)
time.sleep(sleep_s)
ports = PORTS["single1"]
sc = noobWS.SingleClient(ports["cmd"], ports["pub"])
sc.start()
logging.info("SENDING>>> 'hello'")
sc.send("hello", uri_name=sock_name)
msg = sc.recv()
assert msg == [sock_name, b"single1: hello"], f"received {msg}"
logging.info(">>>RECEIVED [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> KILL_SIGNAL")
sc.shutdown_server()
msg = sc.recv()
assert msg == [
Keyword.command.value,
f"single1: {Keyword.shutdown.value}".encode(),
], f"received {msg}"
logging.info(">>>RECEIVED [%s]: %s", *msg)
time.sleep(1)
p.join()
sc.stop()
def demo_sc_ms():
"""Single Client x Multi Socket"""
logging.info("DEMO: Single Client x Multi Socket echo server.")
logging.info("Please ensure you can connect to %s", URIS)
p = multiprocessing.Process(
target=start_multi, args=(0,), kwargs={"name": b"multi-socket"}, daemon=True
)
p.start()
sleep_s = 3
logging.info("Wait ~%.1f seconds for the socket to initialize", sleep_s)
time.sleep(sleep_s)
ports = PORTS["multi1"]
sc = noobWS.SingleClient(ports["cmd"], ports["pub"])
sc.start()
logging.info("SENDING>>> 'hello' to echo1")
sc.send("hello, echo1", uri_name="echo1")
msg = sc.recv()
assert msg == [b"echo1", b"hello, echo1"], f"received {msg}"
logging.info(">>>RECEIVED [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> 'hello' to echo2")
sc.send("hello, echo2", uri_name="echo2")
msg = sc.recv()
assert msg == [b"echo2", b"hello, echo2"], f"received {msg}"
logging.info(">>>RECEIVED [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> KILL_SIGNAL")
sc.shutdown_server()
msg = sc.recv()
assert msg == [Keyword.command.value, Keyword.shutdown.value], f"received {msg}"
logging.info(">>>RECEIVED [%s]: %s", *msg)
time.sleep(1)
p.join()
sc.stop()
def demo_mc_ss():
logging.info("DEMO: Multi Client x Single Socket echo server.")
logging.info("Please ensure you can connect to %s", URIS)
procs = []
for idx in range(2):
# internal name assigned to identify itself in published messages
socket_name = "single{:d}".format(idx + 1).encode()
p = multiprocessing.Process(
target=start_single, args=(idx,), kwargs={"name": socket_name}, daemon=True
)
p.start()
procs.append(p)
sleep_s = 3
logging.info("Wait ~%.1f seconds for the socket to initialize", sleep_s)
time.sleep(sleep_s)
# corresponding NoobServers will be named based on kwargs (i.e. socket1, socket2)
mc = noobWS.MultiClient(socket1=PORTS["single1"], socket2=PORTS["single2"])
mc.start()
logging.info("SENDING>>> 'hello' to single1")
mc.send("socket1", "hello", uri_name="single1")
msg = mc.recv(None)
assert msg == [
"socket1",
b"single1",
b"single1: hello",
], f"received {msg}"
logging.info(">>>RECEIVED %s, [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> 'hello' to single2")
# since `socket2` is only has one external socket, it will default to the only socket
mc.send("socket2", "hello")
msg = mc.recv(None)
assert msg == [
"socket2",
b"single2",
b"single2: hello",
], f"received {msg}"
logging.info(">>>RECEIVED %s, [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> KILL_SIGNAL to all sockets")
for idx, socket_name in enumerate(mc.socket_dict):
ns_name = "socket{:d}".format(idx + 1)
mc.shutdown_server(ns_name)
msg = mc.recv(None)
assert msg == [
ns_name,
Keyword.command.value,
f"single{idx + 1:d}: {Keyword.shutdown.value}".encode(),
], f"received {msg}"
logging.info(">>>RECEIVED from %s, [%s]: %s", *msg)
logging.info("Wait a few seconds before joining")
time.sleep(5)
logging.info("joining")
for p in procs:
p.join()
logging.info("stopping client")
mc.stop()
def demo_mc_ms():
logging.info("DEMO: Multi Client x Multi Socket echo server.")
logging.info("Please ensure you can connect to %s", URIS)
procs = []
for idx in range(2):
# internal name assigned to identify itself in published messages
socket_name = "mc{:d}".format(idx + 1).encode()
p = multiprocessing.Process(
target=start_multi, args=(idx,), kwargs={"name": socket_name}, daemon=True
)
p.start()
procs.append(p)
sleep_s = 3
logging.info("Wait ~%.1f seconds for the socket to initialize", sleep_s)
time.sleep(sleep_s)
# corresponding NoobServers will be named based on kwargs (i.e. socket1, socket2)
mc = noobWS.MultiClient(socket1=PORTS["multi1"], socket2=PORTS["multi2"])
mc.start()
logging.info("SENDING>>> 'hello' to multi1.echo1")
mc.send("socket1", "hello to m1e1", uri_name="echo1")
msg = mc.recv(None)
assert msg == ["socket1", b"echo1", b"hello to m1e1"], f"received {msg}"
logging.info(">>>RECEIVED %s, [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> 'hello' to multi1.echo2")
mc.send("socket1", "hello to m1e2", uri_name="echo2")
msg = mc.recv(None)
assert msg == ["socket1", b"echo2", b"hello to m1e2"], f"received {msg}"
logging.info(">>>RECEIVED %s, [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> 'hello' to multi2.echo1")
mc.send("socket2", "hello to m2e1", uri_name="echo1")
msg = mc.recv(None)
assert msg == ["socket2", b"echo1", b"hello to m2e1"], f"received {msg}"
logging.info(">>>RECEIVED %s, [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> 'hello' to multi2.echo2")
mc.send("socket2", "hello to m2e2", uri_name="echo2")
msg = mc.recv(None)
assert msg == ["socket2", b"echo2", b"hello to m2e2"], f"received {msg}"
logging.info(">>>RECEIVED %s, [%s]: %s", *msg)
time.sleep(1)
logging.info("SENDING>>> KILL_SIGNAL to all sockets")
for idx, socket_name in enumerate(mc.socket_dict):
ns_name = "socket{:d}".format(idx + 1)
mc.shutdown_server(ns_name)
msg = mc.recv(None)
assert msg == [
ns_name,
Keyword.command.value,
Keyword.shutdown.value,
], f"received {msg}"
logging.info(">>>RECEIVED from %s, [%s]: %s", *msg)
logging.info("Wait a few seconds before joining")
time.sleep(5)
logging.info("joining")
for p in procs:
p.join()
logging.info("stopping client")
mc.stop()
if __name__ == "__main__":
demo_sc_ss()
demo_sc_ms()
demo_mc_ss()
demo_mc_ms()