From bbf3e311b99cb9f32102c3cb3db4887a6ec91751 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Tue, 24 Nov 2020 23:57:15 +0100 Subject: [PATCH] Move socket configuration into command line options. --- plomrogue/config.py | 35 +++++++++++++++++++++++++++++++++++ rogue_chat.py | 15 +++++---------- rogue_chat_curses.py | 7 +++++-- rogue_chat_curses.sh | 2 +- 4 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 plomrogue/config.py diff --git a/plomrogue/config.py b/plomrogue/config.py new file mode 100644 index 0000000..fcb74dd --- /dev/null +++ b/plomrogue/config.py @@ -0,0 +1,35 @@ +from plomrogue.io_websocket import PlomWebSocketServer +from plomrogue.io_tcp import PlomTCPServer, PlomTCPServerSSL +from plomrogue.errors import ArgError + +def parse_command_line_arguments(): + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('savefile') + parser.add_argument('--server-type', nargs='+', required=True, + choices=['tcp', 'tcp_ssl', 'ws']) + parser.add_argument('--port', nargs='+', required=True, type=int) + parser.add_argument('--keyfile') + parser.add_argument('--certfile') + opts = parser.parse_args() + if len(opts.server_type) != len(opts.port): + raise ArgError('number of ports unequal to number of server types') + if 'tcp_ssl' in opts.server_type and not (opts.keyfile or opts.certfile): + raise ArgError('need key and cert file for tcp_ssl server') + config = { + 'savefile': opts.savefile, + 'servers': {}, + 'keyfile': opts.keyfile, + 'certfile': opts.certfile, + } + for i in range(len(opts.port)): + server_type = opts.server_type[i] + if server_type == 'ws': + config['servers'][opts.port[i]] = PlomWebSocketServer + elif server_type == 'tcp': + config['servers'][opts.port[i]] = PlomTCPServer + elif server_type == 'tcp_ssl': + config['servers'][opts.port[i]] = PlomTCPServerSSL + return config + +config = parse_command_line_arguments() diff --git a/rogue_chat.py b/rogue_chat.py index 1f40913..407dc29 100755 --- a/rogue_chat.py +++ b/rogue_chat.py @@ -1,7 +1,5 @@ #!/usr/bin/env python3 from plomrogue.game import Game -from plomrogue.io_websocket import PlomWebSocketServer -from plomrogue.io_tcp import PlomTCPServer from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_NICK, cmd_PING, cmd_THING, cmd_MAP, cmd_TURN, cmd_MAP_LINE, cmd_GET_ANNOTATION, cmd_ANNOTATE, cmd_PORTAL, cmd_GET_GAMESTATE, @@ -13,13 +11,9 @@ from plomrogue.commands import (cmd_ALL, cmd_LOGIN, cmd_NICK, cmd_PING, cmd_THIN from plomrogue.tasks import (Task_WAIT, Task_MOVE, Task_WRITE, Task_PICK_UP, Task_DROP, Task_FLATTEN_SURROUNDINGS) from plomrogue.things import Thing_Player, Thing_Item, Thing_Furniture -import sys -if len(sys.argv) != 2: - print('wrong number of arguments, expected one (save file)') - exit(1) -savefile = sys.argv[1] -game = Game(savefile) +from plomrogue.config import config +game = Game(config['savefile']) game.register_command(cmd_PING) game.register_command(cmd_ALL) game.register_command(cmd_LOGIN) @@ -56,5 +50,6 @@ game.register_thing_type(Thing_Item) game.register_thing_type(Thing_Furniture) game.read_savefile() game.io.start_loop() -game.io.start_server(8000, PlomWebSocketServer) -game.io.start_server(5000, PlomTCPServer) +for port in config['servers']: + game.io.start_server(port, config['servers'][port], + config['certfile'], config['keyfile']) diff --git a/rogue_chat_curses.py b/rogue_chat_curses.py index d22539b..2362d99 100755 --- a/rogue_chat_curses.py +++ b/rogue_chat_curses.py @@ -3,6 +3,7 @@ import curses import queue import threading import time +import sys from plomrogue.game import GameBase from plomrogue.parser import Parser from plomrogue.mapping import YX, MapGeometrySquare, MapGeometryHex @@ -900,5 +901,7 @@ class TUI: elif key in self.movement_keys: move_explorer(self.movement_keys[key]) -#TUI('localhost:5000') -TUI('wss://plomlompom.com/rogue_chat/') +if len(sys.argv) != 2: + raise ArgError('wrong number of arguments, need game host') +host = sys.argv[1] +TUI(host) diff --git a/rogue_chat_curses.sh b/rogue_chat_curses.sh index fa78c01..5fd7385 100755 --- a/rogue_chat_curses.sh +++ b/rogue_chat_curses.sh @@ -3,4 +3,4 @@ python3 -m venv .venv . .venv/bin/activate pip install -r requirements_client.txt -./rogue_chat_curses.py +./rogue_chat_curses.py 'wss://plomlompom.com/rogue_chat/' -- 2.30.2