-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
101 lines (87 loc) · 2.67 KB
/
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
99
100
101
"""
Usage:
python2.7 server.py [options]
Options:
-h, --help show this screen
--debug run in debug mode (display network commands)
--interactive run in interactive mode, allowing voice commands to be entered via stdin
--no-vr run without voice recognition (automatic if not available)
"""
import os
import sys
import socket
import threading
import logging
# leap python binding
this_dir = os.path.dirname(os.path.realpath(__name__))
sys.path.insert(0, os.path.join(this_dir, 'libs', 'leap'))
import Leap
import communication as com
from commands import interpret_command
from controllers import (set_current_controller, disable_current_controller,
ObjectController)
# try to get VR
try:
from voice import VoiceRecognition
vr_available = True
except ImportError:
vr_available = False
def run_server():
print 'Server started: Ctrl-C to kill'
try:
while True:
pipe, _ = sock.accept()
pipe.settimeout(0.05)
com.clients.append(pipe)
except KeyboardInterrupt:
print 'interrupted'
def cleanup_server():
disable_current_controller()
sock.close()
for pipe in com.clients:
pipe.close()
if __name__ == '__main__':
if '-h' in sys.argv or '--help' in sys.argv:
print __doc__
sys.exit(0)
# debugging
if '--debug' in sys.argv:
com.debug = True
# TODO set log level to logging.DEBUG
# setup server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 1337))
sock.listen(0)
if vr_available and '--no-vr' not in sys.argv:
vr = VoiceRecognition()
vr.start()
# default mode
set_current_controller(ObjectController)
if '-i' in sys.argv or '--interactive' in sys.argv:
t = threading.Thread(target=run_server)
t.daemon = True
try:
t.start()
try:
exit_cmd = 'exit'
print 'Kill server and exit with "%s"' % exit_cmd
while True:
cmd = raw_input('(speak) ').strip().lower()
if not cmd:
pass
elif cmd == exit_cmd:
break
if not interpret_command(cmd):
print 'bad unrecognized command "%s"' % cmd
except EOFError:
print 'EOF'
except KeyboardInterrupt:
pass
except Exception as e:
logging.exception(e)
finally:
cleanup_server()
else:
run_server()
cleanup_server()