All files
This commit is contained in:
152
start-servers.py
Normal file
152
start-servers.py
Normal file
@@ -0,0 +1,152 @@
|
||||
import subprocess
|
||||
import time
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
import select
|
||||
|
||||
servers_config = [
|
||||
{
|
||||
"region": "World",
|
||||
"nation": "World",
|
||||
"host": "localhost",
|
||||
"port": 9000,
|
||||
"neighbors": ["localhost:8001", "localhost:8004"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 9000},
|
||||
],
|
||||
},
|
||||
{
|
||||
"region": "FRA",
|
||||
"nation": "France",
|
||||
"host": "localhost",
|
||||
"port": 8001,
|
||||
"neighbors": ["localhost:9000", "localhost:8002", "localhost:8003", "localhost:8101"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 8001},
|
||||
{"host": "localhost", "port": 8101},
|
||||
],
|
||||
},
|
||||
{
|
||||
"region": "Paris",
|
||||
"nation": "France",
|
||||
"host": "localhost",
|
||||
"port": 8002,
|
||||
"neighbors": ["localhost:8001", "localhost:8101"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 8002},
|
||||
{"host": "localhost", "port": 8102},
|
||||
],
|
||||
},
|
||||
{
|
||||
"region": "Marseille",
|
||||
"nation": "France",
|
||||
"host": "localhost",
|
||||
"port": 8003,
|
||||
"neighbors": ["localhost:8001"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 8003},
|
||||
{"host": "localhost", "port": 8103},
|
||||
],
|
||||
},
|
||||
{
|
||||
"region": "USA",
|
||||
"nation": "USA",
|
||||
"host": "localhost",
|
||||
"port": 8004,
|
||||
"neighbors": ["localhost:9000", "localhost:8005", "localhost:8006"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 8004},
|
||||
{"host": "localhost", "port": 8104},
|
||||
],
|
||||
},
|
||||
{
|
||||
"region": "NYC",
|
||||
"nation": "USA",
|
||||
"host": "localhost",
|
||||
"port": 8005,
|
||||
"neighbors": ["localhost:8004"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 8005},
|
||||
{"host": "localhost", "port": 8105},
|
||||
],
|
||||
},
|
||||
{
|
||||
"region": "LA",
|
||||
"nation": "USA",
|
||||
"host": "localhost",
|
||||
"port": 8006,
|
||||
"neighbors": ["localhost:8004"],
|
||||
"servers": [
|
||||
{"host": "localhost", "port": 8006},
|
||||
{"host": "localhost", "port": 8106},
|
||||
],
|
||||
}
|
||||
]
|
||||
|
||||
config_filename = "servers_config.json"
|
||||
with open(config_filename, "w") as f:
|
||||
json.dump(servers_config, f, indent=2)
|
||||
|
||||
processes = []
|
||||
server_script = os.path.abspath("server.py")
|
||||
|
||||
for s in servers_config:
|
||||
region = s["region"]
|
||||
host = s["host"]
|
||||
|
||||
for srv in s["servers"]:
|
||||
port = srv["port"]
|
||||
print(f"[{region}] Starting server on port {port} ...")
|
||||
p = subprocess.Popen(
|
||||
[sys.executable, server_script, region, host, str(port), config_filename],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
)
|
||||
processes.append((region, port, p))
|
||||
time.sleep(0.3)
|
||||
|
||||
print("\nAll servers launched.")
|
||||
print("Use: python client.py <username> localhost <port>")
|
||||
print("Regions:", ", ".join([s["region"] for s in servers_config]))
|
||||
|
||||
try:
|
||||
while True:
|
||||
for region, port, p in list(processes):
|
||||
while True:
|
||||
ready, _, _ = select.select([p.stdout], [], [], 0)
|
||||
if p.stdout in ready:
|
||||
line = p.stdout.readline()
|
||||
if not line:
|
||||
break
|
||||
print(f"[{region}:{port}] {line.strip()}")
|
||||
else:
|
||||
break
|
||||
|
||||
while True:
|
||||
ready, _, _ = select.select([p.stderr], [], [], 0)
|
||||
if p.stderr in ready:
|
||||
line = p.stderr.readline()
|
||||
if not line:
|
||||
break
|
||||
print(f"[{region}:{port}][ERR] {line.strip()}")
|
||||
else:
|
||||
break
|
||||
|
||||
if p.poll() is not None:
|
||||
print(f"\n[{region}:{port}] Server exited.")
|
||||
processes.remove((region, port, p))
|
||||
|
||||
if not processes:
|
||||
print("All servers stopped.")
|
||||
break
|
||||
|
||||
time.sleep(0.1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("\nShutting down all servers...")
|
||||
for _, _, p in processes:
|
||||
p.terminate()
|
||||
print("Bye!")
|
||||
Reference in New Issue
Block a user