-
Notifications
You must be signed in to change notification settings - Fork 0
/
companion_games.py
90 lines (74 loc) · 3.18 KB
/
companion_games.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
"""
CompanionGames
Copyright (C) 2024 thiccaxe
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import asyncio
import logging
import os
from companion_manager import CompanionManager
from companion_server import CompanionServer
from config import Config, ConfigurationLoadError
from logger import setup_logging
from mdns_registration import CompanionGamesZeroconf
from server_data import ServerData
from websocket_server import WebsocketServer
class CompanionGames:
def __init__(self, config, secrets, loop: asyncio.AbstractEventLoop):
self._config = config
self._secrets = secrets
self._loop = loop
self._mdns = CompanionGamesZeroconf(self._config)
self._data = ServerData()
self._manager = CompanionManager(self._config, self._secrets, self._loop, self._data)
self._websocket_server = WebsocketServer(self._config, self._secrets, self._loop, self._data, self._manager)
self._companion_server = CompanionServer(self._config, self._secrets, self._loop, self._data, self._manager)
async def handler(self, websocket):
while True:
message = await websocket.recv()
logging.debug(message)
async def begin(self):
logging.info("Starting Companion Games Server")
async with self._mdns:
logging.info("Starting Companion Games Websocket Server")
async with self._websocket_server:
logging.info("Starting Companion Games Companion Server")
async with self._companion_server:
try:
logging.info("Ctrl^C to quit")
while True:
await asyncio.sleep(1)
except asyncio.CancelledError:
logging.info("Cancelled ...")
except KeyboardInterrupt:
logging.info("Cancelled ...")
finally:
logging.info("Stopping Server ...")
async def main():
logging.info("Starting companion games ...")
config = Config(
os.environ.get("COMPANION_GAMES_CONFIG", "./config.toml"),
os.environ.get("COMPANION_GAMES_SECRETS", "./secrets.toml"),
)
loop = asyncio.get_event_loop()
try:
await config.initialize()
companion_games = CompanionGames(config.config, config.secrets, loop)
await companion_games.begin()
except ConfigurationLoadError:
logging.error("Could not load configuration. Exiting")
return
finally:
await config.close()
if __name__ == "__main__":
setup_logging()
asyncio.run(main())