-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
60 lines (50 loc) · 2.29 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
import sys
import argparse
from pathlib import Path
from shutil import copyfile
import asyncio
from aiohttp import web
from aiohttp_index import IndexMiddleware
from server_lib import browser_socket
from server_lib.connection_manager import ConnectionManager
async def init(loop, app, web_port):
handler = app.make_handler()
srv = await loop.create_server(handler, '0.0.0.0', web_port)
return srv
def main():
parser = argparse.ArgumentParser(description='Treasure Hunt Agent visualizer')
parser.add_argument('-p','--port', help='Port of the treasure hunt (Raft) server',
type=int, required=True)
parser.add_argument('-i','--map-file', help='Path to treasure hunt map file',
type=str, required=True)
parser.add_argument('-w','--web-port', help='Port for accessing visualizer from browser',
type=int, default=9000, required=False)
parser.add_argument('-v','--visualizer-port', help='Port to run the visualizer server where agent can connect',
type=int, default=9001, required=False)
args = parser.parse_args()
map_file = Path(args.map_file)
if not map_file.is_file():
print("[!] Could not find map file at", map_file)
sys.exit(0)
copyfile(str(map_file), str(Path("ui", "dist", "treasure-map.txt")))
game_port, visualizer_port, web_port = args.port, args.visualizer_port, args.web_port
loop = asyncio.get_event_loop()
app = web.Application(middlewares=[IndexMiddleware()])
browser_socket.init(app)
# This MUST be done after initializing browser socket.
# Otherwise, the static path binding on '/' will
# mess with '/socketio' paths from browser.
app.router.add_static('/', './ui/dist')
loop.run_until_complete(init(loop, app, web_port))
connection_manager = ConnectionManager(loop, visualizer_port, browser_socket)
connection_manager.connect('localhost', game_port)
print("================================================================")
print("*** Open this url in browser:", "http://localhost:{}".format(web_port))
print("*** Connect agent program to this port:", visualizer_port)
print("================================================================")
try:
loop.run_forever()
except Exception as e:
pass
if __name__ == '__main__':
main()