-
Notifications
You must be signed in to change notification settings - Fork 1
/
superbird-server.py
98 lines (94 loc) · 3.65 KB
/
superbird-server.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
#!/usr/bin/env python3
import umsgpack
import time
import threading
import traceback
import common.sb_common as sb_c
import utils.handlers.bt_handler as bt_handler
import utils.wamp.wamp_handler as wamp_h
import utils.handlers.pubsub_handler as pubsub_handler
import utils.bt_utils as bt_utils
# Un-MessagePack messages and send them to their respective handlers
def processMsg(data: bytearray):
try:
send = False
with_event = False
msg = umsgpack.unpackb(data)
msg_opcode = sb_c.opCodes(msg[0])
match msg_opcode:
case sb_c.opCodes.HELLO: # More info in superbird_util.py
send, resp, with_event, event = wamp_h.hello_handler(msg)
case sb_c.opCodes.AUTHENTICATE:
send, resp, with_event, event = wamp_h.authenticate_handler()
case sb_c.opCodes.CALL: # More info in superbird_util.py
send, resp, with_event, event = wamp_h.function_handler(msg)
case sb_c.opCodes.SUBSCRIBE: # More info in superbird_util.py
send, resp, with_event, event = wamp_h.subscribe_handler(msg)
case sb_c.opCodes.UNSUBSCRIBE:
send, resp, with_event, event = wamp_h.subscribe_handler(msg, True)
case _:
print("Unhandled opcode:", msg_opcode, " Msg:", msg)
if send:
bt_handler.addToOutbox(resp)
if with_event:
pubsub_handler.update_status()
# if with_event: # Sometimes we also need to send an event # Might not be needed
# bt_handler.addToOutbox(event)
except Exception:
print("\n\n~~~~~ Exception Start ~~~~~")
traceback.print_exc()
print("~~~~~ Exception End ~~~~~\n")
pass
def resetVars():
wamp_h.last_subscription = 63
sb_c.superbird_session = {}
pubsub_handler.pub_id = 1
connected = False
registered = False
stopThreads = threading.Event()
while True:
if not connected:
# We advertise the RFCOMM service that Superbird is expecting and accept any connections to it
server_sock = bt_utils.open_socket()
time.sleep(1)
while not registered: # We just need to register the service once
bt_utils.register_sdp()
print("Waiting for connection on RFCOMM channel", bt_utils.port)
registered = True
client_sock, client_info = server_sock.accept()
print("Connected")
outbox_thread = threading.Thread(target=bt_handler.outboxThread, args=(client_sock, stopThreads,), daemon=True)
sub_handler_thread = threading.Thread(target=pubsub_handler.subHandlerThread, args=(stopThreads,), daemon=True)
# Start outbox thread
outbox_thread.start()
# Start subscription handler thread
sub_handler_thread.start()
connected = True
if connected:
try:
while True:
data = bt_handler.get_msg(client_sock)
processMsg(data)
if not data:
break
except KeyboardInterrupt:
pass
except Exception:
print("\n\n~~~~~ Exception Start ~~~~~")
traceback.print_exc()
print("~~~~~ Exception End ~~~~~\n")
pass
client_sock.close()
stopThreads.set()
time.sleep(2) #allow time for threads to stop
server_sock.close()
client_sock.close()
connected = False
resetVars()
print("\n\nDisconnected. Reopening socket in 5s. Press Ctrl + C to stop")
try:
time.sleep(5)
stopThreads.clear()
except KeyboardInterrupt:
print("Quitting...")
raise SystemExit(0)