drawing-machine/startBluetooth.py

50 lines
976 B
Python
Raw Normal View History

2018-03-21 11:06:44 -04:00
#!/usr/bin/env python3
"""
Starts the Bluetooth server for the drawing machine. Taken from the
multiserver example in the nOBEX git repo.
"""
import os
import sys
import signal
import traceback
from threading import Thread
from servers.opp import OPPServer
def thread_serve(serv_class, arg):
t = Thread(target=serve, args=(serv_class, arg), daemon=True)
t.start()
return t
def serve(serv_class, *args, **kwargs):
server = serv_class(*args, **kwargs)
socket = server.start_service()
while True:
try:
server.serve(socket)
except:
traceback.print_exc()
def signal_handler(signal, frame):
print() # newline to move ^C onto its own line on display
sys.exit(0)
def main():
opp_conf = "./received"
signal.signal(signal.SIGINT, signal_handler)
# obexd conflicts with our own OBEX servers
os.system("killall obexd")
t = thread_serve(OPPServer, opp_conf)
# wait for completion (never)
t.join()
return 0
if __name__ == "__main__":
sys.exit(main())