-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
204 lines (182 loc) · 9.8 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
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
from tornado.web import StaticFileHandler
import tornado.httpserver
import yarp
from threading import Lock
import sqlite3 as sl
import json
import os
import signal, sys, threading
import secrets
from python_code.internal_handlers.generic_handlers.NavClickHandler import NavClickHandler
from python_code.internal_handlers.generic_handlers.ButtonsHandler import ButtonsHandler
from python_code.internal_handlers.generic_handlers.IndexHandler import IndexHandler
from python_code.internal_handlers.media_handlers.AudioInHandler import AudioInHandler
from python_code.internal_handlers.media_handlers.AudioOutHandler import AudioOutHandler
from python_code.internal_handlers.credential_handlers.LoginHandler import LoginHandler, ActiveUsersRegister
from python_code.internal_handlers.credential_handlers.RegisterHandler import RegisterHandler
from python_code.internal_handlers.credential_handlers.LogoutHandler import LogoutHandler
from python_code.internal_handlers.credential_handlers.AuthHandler import AuthHandler
from python_code.utils.cookieServer import CookieServer
## Execution example
# python3 server.py --camera_port 10009 --camera_host 192.168.92.109 --map_port 10014 --no_ssl
# python3 server.py --camera_port 10010 --camera_host 192.168.20.162 --map_port 10010 --no_ssl
# python3 server.py --simulate --no_ssl
# python3 server.py --simulate
ABSPATH = os.path.dirname(os.path.realpath(__file__))
ADMINKEY = "1234qwer"
SERVERPORT = 16001
NETWORK = None
RESFINDER = None
NAVCLICKPORT = None
HEADCLICKPORT = None
MAPCLICKPORT = None
MICPORT = None
NAVCLICKPORTNAME = "/webview/navClick:o"
HEADCLICKPORTNAME = "/webview/headClick:o"
MAPCLICKPORTNAME = "/webview/mapClick:o"
MICPORTNAME = "/webview/microphone:o"
WEBLOCK = Lock()
AUDIOBUFFLEN = 640
dirname = os.path.dirname(__file__)
dbPath = os.path.join(dirname, "static/sql_db/users.db")
certificates_folder = os.path.join(dirname, "resources/certificates")
certificates_name = "host"
loginDb = sl.connect(dbPath)
def createUsersTable(inputDb):
sql_create_projects_table = ''' CREATE TABLE IF NOT EXISTS users (
id integer primary key,
name text not null,
password text not null) '''
try:
c = inputDb.cursor()
c.execute(sql_create_projects_table)
except Exception as e:
print("This is an error")
print(e)
if __name__ == "__main__":
RESFINDER = yarp.ResourceFinder()
RESFINDER.configure(sys.argv)
createUsersTable(loginDb)
commonAUR = ActiveUsersRegister(True,True)
if RESFINDER.check("server_port"):
SERVERPORT = RESFINDER.find("server_port").asInt32()
if not RESFINDER.check("no_yarp"):
NETWORK = yarp.Network()
NETWORK.init()
AUDIOBUFFLEN = RESFINDER.find("audioBufLen").asInt32() if RESFINDER.check("audioBufLen") else AUDIOBUFFLEN
MICPORT = yarp.BufferedPortSound()
NAVCLICKPORT = yarp.Port()
MAPCLICKPORT = yarp.Port()
HEADCLICKPORT = yarp.Port()
MICPORTNAME = RESFINDER.find("mic_port").asString() if RESFINDER.check("mic_port") else MICPORTNAME
NAVCLICKPORTNAME = RESFINDER.find("nav_click_port").asString() if RESFINDER.check("nav_click_port") else NAVCLICKPORTNAME
MAPCLICKPORTNAME = RESFINDER.find("map_click_port").asString() if RESFINDER.check("map_click_port") else MAPCLICKPORTNAME
HEADCLICKPORTNAME = RESFINDER.find("head_click_port").asString() if RESFINDER.check("head_click_port") else HEADCLICKPORTNAME
MICPORT.open(MICPORTNAME)
NAVCLICKPORT.open(NAVCLICKPORTNAME)
MAPCLICKPORT.open(MAPCLICKPORTNAME)
HEADCLICKPORT.open(HEADCLICKPORTNAME)
if RESFINDER.check("no_ssl") or RESFINDER.check("traefik"):
certificates_folder = None
certificates_name = None
else:
certificates_folder = RESFINDER.find("certificates_path").asString() if RESFINDER.check("certificates_path") else certificates_folder
certificates_name = RESFINDER.find("certificates_name").asString() if RESFINDER.check("certificates_name") else certificates_name
if RESFINDER.check("simulate"):
handlersList = [(r'/', IndexHandler,{"inputNetwork": NETWORK,
"cameraPort": "",
"mapPort": "",
"audioPort": "",
"cameraHost": "",
"resFinder": None,
"absPath": ABSPATH,
"audioBufferLen": AUDIOBUFFLEN,
"mapHost": "",
"simulate":True,
"isSsl": (not RESFINDER.check("no_ssl")) or RESFINDER.check("traefik")}),
(r'/auth',AuthHandler),
(r'/wsa',AudioInHandler,{"soundPort": MICPORT}),
(r'/wsc',AudioOutHandler,{"network": NETWORK, "audioPort": ""}),
(r'/login', LoginHandler,{"absPath": ABSPATH,"aur": commonAUR,"my_db": loginDb}),
(r'/logout', LogoutHandler,{"absPath": ABSPATH,"aur": commonAUR,"my_db": loginDb}),
(r'/register', RegisterHandler,{"absPath": ABSPATH,"aur": commonAUR,"my_db": loginDb,"adminkey": ADMINKEY}),
(r"/static/(.*)", StaticFileHandler,{'path':'static'}),
(r"/ws", NavClickHandler, {"webLock": WEBLOCK,
"navPort": NAVCLICKPORT,
"headPort": HEADCLICKPORT,
"mapPort": MAPCLICKPORT}),
(r"/wsb", ButtonsHandler, {"webLock": WEBLOCK,
"navPort": NAVCLICKPORT})]
elif not RESFINDER.check("simulate") and NETWORK is not None:
MAPPORT = None
CAMERAPORT = None
AUDIOPORT = None
MAPHOST = None
CAMERAHOST = None
if RESFINDER.check("camera_name"):
tempConn = yarp.NetworkBase_queryName(RESFINDER.find("camera_name").toString())
CAMERAPORT = str(tempConn.getPort())
CAMERAHOST = tempConn.getHost()
if RESFINDER.check("map_name"):
tempConn = yarp.NetworkBase_queryName(RESFINDER.find("map_name").toString())
MAPPORT = str(tempConn.getPort())
MAPHOST = tempConn.getHost()
if RESFINDER.check("audio_name"):
AUDIOPORT = RESFINDER.find("audio_name").toString()
if RESFINDER.check("camera_port"):
CAMERAPORT = RESFINDER.find("camera_port").toString()
else:
if CAMERAPORT is None:
print("Error! Camera port not found")
sys.exit()
if RESFINDER.check("map_port"):
MAPPORT = RESFINDER.find("map_port").toString()
else:
if MAPPORT is None:
print("Error! Map port not found")
sys.exit()
if RESFINDER.check("camera_host"):
CAMERAHOST = RESFINDER.find("camera_host").asString()
else:
if CAMERAHOST is None:
print("Error! Camera host not found")
sys.exit()
if RESFINDER.check("map_host"):
MAPHOST = RESFINDER.find("map_host").asString()
handlersList = [(r'/', IndexHandler,{"inputNetwork": NETWORK,
"cameraPort": CAMERAPORT,
"mapPort": MAPPORT,
"audioPort": AUDIOPORT,
"cameraHost": CAMERAHOST,
"resFinder": RESFINDER,
"absPath": ABSPATH,
"audioBufferLen": AUDIOBUFFLEN,
"mapHost": MAPHOST,
"isSsl": (not RESFINDER.check("no_ssl")) or RESFINDER.check("traefik"),
"simulate": False}),
(r'/auth',AuthHandler),
(r'/wsa',AudioInHandler,{"soundPort": MICPORT}),
(r'/wsc',AudioOutHandler,{"network": NETWORK, "audioPort": AUDIOPORT}),
(r'/login', LoginHandler,{"absPath": ABSPATH, "aur": commonAUR,"my_db": loginDb}),
(r'/logout', LogoutHandler,{"absPath": ABSPATH,"aur": commonAUR,"my_db": loginDb}),
(r'/register', RegisterHandler,{"absPath": ABSPATH,"aur": commonAUR,"my_db": loginDb,"adminkey": ADMINKEY}),
(r"/static/(.*)", StaticFileHandler,{'path':'static'}),
(r"/ws", NavClickHandler, {"webLock": WEBLOCK,
"navPort": NAVCLICKPORT,
"headPort": HEADCLICKPORT,
"mapPort": MAPCLICKPORT}),
(r"/wsb", ButtonsHandler, {"webLock": WEBLOCK,
"navPort": NAVCLICKPORT})]
server = CookieServer(handlersList,SERVERPORT,certificates_folder,certificates_name,True,secrets.token_urlsafe())
def signal_handler(signal, frame):
print('exiting')
server.stop()
print('before exit')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGHUP, signal_handler)
print('Running on port %d' % SERVERPORT)
print('Press Ctrl+C to stop')
#signal.pause()
server.start()