Pass server config to clients

This commit is contained in:
Emily 2020-03-29 19:04:20 +02:00
parent fee2005eef
commit ee08f6a3b1
8 changed files with 30 additions and 11 deletions

3
.gitignore vendored
View File

@ -96,3 +96,6 @@ ENV/
# Logs
logs/
# vscode
.vscode/

View File

@ -5,12 +5,15 @@ from . import base
from . import query
from . import player
from ..struct.server import Server
STATE_UNKNOWN = (0, base.BaseClient)
STATE_QUERY = (1, query.QueryClient)
STATE_PLAYER = (2, player.PlayerClient)
class Client:
def __init__(self, socket: socket.socket, ip: str, port: int):
def __init__(self, config: Server, socket: socket.socket, ip: str, port: int):
self.config = config
self.socket = socket
self.ip = ip
self.port = port
@ -19,7 +22,7 @@ class Client:
def set_state(self, state: tuple):
self.state = state
self.client = self.state[1](self.socket, self.ip, self.port)
self.client = self.state[1](self.config, self.socket, self.ip, self.port)
async def on_packet(self, packet: bytes):
if self.state == STATE_UNKNOWN:

View File

@ -1,11 +1,14 @@
import socket
import struct
from ..struct.server import Server
import logging
logger = logging.getLogger(__name__)
class BaseClient:
def __init__(self, socket: socket.socket, ip: str, port: int):
def __init__(self, config: Server, socket: socket.socket, ip: str, port: int):
self.config = config
self.socket = socket
self.ip = ip
self.port = port

View File

@ -1,13 +1,15 @@
import socket
from . import base
from ..struct.server import Server
import logging
logger = logging.getLogger(__name__)
class PlayerClient(base.BaseClient):
def __init__(self, socket: socket.socket, ip: str, port: int):
super().__init__(socket, ip, port)
def __init__(self, config: Server, socket: socket.socket, ip: str, port: int):
super().__init__(config, socket, ip, port)
logger.debug("Client resolved to PlayerClient")
async def on_packet(self, packet: bytes):
logger.debug("on_packet(%s)" % packet)

View File

@ -1,13 +1,19 @@
import socket
from . import base
from ..struct.server import Server
from ..shared import glob
import logging
logger = logging.getLogger(__name__)
class QueryClient(base.BaseClient):
def __init__(self, socket: socket.socket, ip: str, port: int):
super().__init__(socket, ip, port)
def __init__(self, config: Server, socket: socket.socket, ip: str, port: int):
super().__init__(config, socket, ip, port)
logger.debug("Client resolved to QueryClient")
async def on_packet(self, packet: bytes):
logger.debug("on_packet(%s)" % packet)
logger.debug("on_packet(%s)" % packet)
async def query_i():
pass

View File

@ -18,4 +18,3 @@ class Environment(Thread):
def run(self):
self.event_loop.run_until_complete(self.server.main())
print("Ended?")

View File

@ -34,7 +34,7 @@ class Server:
if addr not in self.clients:
ip, port = addr
self.clients[addr] = Client(self.socket, ip, port)
self.clients[addr] = Client(self.config, self.socket, ip, port)
await self.clients[addr].on_packet(data)
await asyncio.sleep(0)

View File

@ -10,8 +10,11 @@ if not os.path.isfile("config.json"):
with open("config.json", "r") as f:
config = json.load(f)
# aliases
conf = config["sampy"]
conf_log = conf["logging"]
# Setup logger
conf_log = config["sampy"]["logging"] # alias
## fix for logging level
default_logging_fallback = False
if type(conf_log["level"]) is not int: