-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
173 lines (137 loc) · 4.97 KB
/
Copy pathserver.py
File metadata and controls
173 lines (137 loc) · 4.97 KB
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
#!/usr/bin/env python3
import os
import sys
import json
import time
import socket
import signal
import logging
from pathlib import Path
from dataclasses import asdict
from dataclasses import dataclass
ROOT_DIR = Path(__file__).resolve().parent
project_root = ROOT_DIR / "entralinked"
sys.path.insert(0, str(project_root))
from configuration import Configuration
from model.dlc.dlc import DlcList
from model.user.user_manager import UserManager
from model.player.player_manager import PlayerManager
from network.dns.server import DnsServer
from network.http.http_server import HttpServer
from network.gamespy.server import GameSpyServer
from utility.db_manager import db
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
handlers=[
logging.StreamHandler(sys.stdout)
]
)
@dataclass
class Context:
configuration: Configuration
user_manager: UserManager
player_manager: PlayerManager
dlc_list: DlcList
class GameSync:
def __init__(self):
begin_time = int(time.time() * 1000)
self.initialized = False
self.configuration = self._load_config_file()
logger.info(f"Using configuration {self.configuration}")
self.context = Context(self.configuration, UserManager(), PlayerManager(), DlcList())
host_address = None
host_name = getattr(self.configuration, 'host_name', 'local')
if host_name in ("local", "localhost"):
host_address = self._get_local_host()
else:
try:
host_address = socket.gethostbyname(host_name)
except socket.gaierror as e:
host_address = self._get_local_host()
logger.error(f"Could not resolve host name - falling back to {host_address} ", exc_info=e)
if host_address is None:
logger.critical("ABORTING - hostAddress is null!")
sys.exit(1)
self.host_address = host_address
self.dns_server = DnsServer(self.host_address)
self.gamespy_server = GameSpyServer(self.context)
self.http_server = HttpServer(self.context)
signal.signal(signal.SIGINT, self._signal_handler)
signal.signal(signal.SIGTERM, self._signal_handler)
started = self.start_servers()
if started:
time_taken = int(time.time() * 1000) - begin_time
logger.info(f"Startup complete! Took a total of {time_taken} milliseconds")
logger.info(f"Configure your DS to use the following DNS server: {self.host_address}")
else:
self.stop_servers()
self.initialized = True
def _get_local_host(self) -> str:
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
except Exception:
return "127.0.0.1"
def _load_config_file(self) -> Configuration:
logger.info("Loading configuration ...")
configuration = None
try:
config_file = "config.json"
if not os.path.exists(config_file):
logger.info("No configuration file exists - default configuration will be used")
configuration = Configuration()
else:
with open(config_file, 'r', encoding="UTF-8") as f:
data = json.load(f)
configuration = Configuration(**data)
with open(config_file, 'w', encoding="UTF-8") as f:
json.dump(asdict(configuration), f, indent=4)
except Exception as e:
logger.error("Could not load configuration - default configuration will be used", exc_info=e)
configuration = Configuration()
return configuration
def start_servers(self) -> bool:
logger.info("Starting servers ...")
try:
self.dns_server.start()
self.http_server.start()
self.gamespy_server.start()
return True
except Exception as e:
logger.error(f"Failed to start servers: {e}")
return False
def stop_servers(self):
logger.info("Stopping servers ...")
if self.http_server:
try:
self.http_server.stop()
except Exception:
pass
if self.gamespy_server:
try:
self.gamespy_server.stop()
except Exception:
pass
if self.dns_server:
try:
self.dns_server.stop()
except Exception:
pass
db.close()
def run_forever(self):
if not self.initialized:
return
try:
while True:
time.sleep(1.0)
except KeyboardInterrupt:
pass
def _signal_handler(self, sig, frame):
self.stop_servers()
sys.exit(0)
if __name__ == "__main__":
app = GameSync()
app.run_forever()