2021-08-20 06:43:59 +02:00
|
|
|
from typing import Optional, Any
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2021-08-20 20:10:07 +02:00
|
|
|
from bemani.backend.base import Base, Status
|
|
|
|
from bemani.common import Model
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.protocol import Node
|
2021-08-20 06:43:59 +02:00
|
|
|
from bemani.data import Config, Data
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UnrecognizedPCBIDException(Exception):
|
|
|
|
def __init__(self, pcbid: str, model: str, ip: str) -> None:
|
|
|
|
self.pcbid = pcbid
|
|
|
|
self.model = model
|
|
|
|
self.ip = ip
|
|
|
|
|
|
|
|
|
|
|
|
class Dispatch:
|
|
|
|
"""
|
|
|
|
Dispatch object responsible for taking a decoded tree of Node objects
|
|
|
|
from a game, looking up config, dispatching it to the correct game
|
|
|
|
class and then returning a response.
|
|
|
|
"""
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
def __init__(self, config: Config, data: Data, verbose: bool) -> None:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Initialize the Dispatch object.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
config - A dictionary of configuration used for various settigs.
|
|
|
|
data - A Data singleton for DB access.
|
|
|
|
verbose - Whether we get chatty to stdout or not.
|
|
|
|
"""
|
|
|
|
self.__verbose = verbose
|
|
|
|
self.__data = data
|
|
|
|
self.__config = config
|
|
|
|
|
|
|
|
def log(self, msg: str, *args: Any, **kwargs: Any) -> None:
|
|
|
|
"""
|
|
|
|
Given a message, format it and print it.
|
|
|
|
|
|
|
|
Note that this only prints to stdout if we were initialized with
|
|
|
|
verbose = True.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
msg - A formatstring that should be formatted with any
|
|
|
|
optional arguments or keyword arguments.
|
|
|
|
"""
|
|
|
|
if self.__verbose:
|
|
|
|
print(msg.format(*args, **kwargs))
|
|
|
|
|
|
|
|
def handle(self, tree: Node) -> Optional[Node]:
|
|
|
|
"""
|
|
|
|
Given a packet from a game, handle it and return a response.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
tree - A Node representing the root of a tree. Expected to
|
|
|
|
come from an external game.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A Node representing the root of a response tree, or None if
|
|
|
|
we had a problem parsing or generating a response.
|
|
|
|
"""
|
|
|
|
self.log("Received request:\n{}", tree)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if tree.name != "call":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Invalid request
|
|
|
|
self.log("Invalid root node {}", tree.name)
|
|
|
|
return None
|
|
|
|
|
|
|
|
if len(tree.children) != 1:
|
|
|
|
# Invalid request
|
|
|
|
self.log("Invalid number of children for root node")
|
|
|
|
return None
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
modelstring = tree.attribute("model")
|
2019-12-08 22:43:49 +01:00
|
|
|
model = Model.from_modelstring(modelstring)
|
2022-10-15 20:56:30 +02:00
|
|
|
pcbid = tree.attribute("srcid")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# If we are enforcing, bail out if we don't recognize thie ID
|
|
|
|
pcb = self.__data.local.machine.get_machine(pcbid)
|
2021-08-20 06:43:59 +02:00
|
|
|
if self.__config.server.enforce_pcbid and pcb is None:
|
2019-12-08 22:43:49 +01:00
|
|
|
self.log("Unrecognized PCBID {}", pcbid)
|
2022-10-15 20:56:30 +02:00
|
|
|
raise UnrecognizedPCBIDException(
|
|
|
|
pcbid, modelstring, self.__config.client.address
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# If we don't have a Machine, but we aren't enforcing, we must create it
|
|
|
|
if pcb is None:
|
|
|
|
pcb = self.__data.local.machine.create_machine(pcbid)
|
|
|
|
|
|
|
|
request = tree.children[0]
|
|
|
|
|
2021-08-20 06:43:59 +02:00
|
|
|
config = self.__config.clone()
|
2022-10-15 20:56:30 +02:00
|
|
|
config["machine"] = {
|
|
|
|
"pcbid": pcbid,
|
|
|
|
"arcade": pcb.arcade,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# If the machine we looked up is in an arcade, override the global
|
|
|
|
# paseli settings with the arcade paseli settings.
|
|
|
|
if pcb.arcade is not None:
|
|
|
|
arcade = self.__data.local.machine.get_arcade(pcb.arcade)
|
|
|
|
if arcade is not None:
|
2022-10-15 20:56:30 +02:00
|
|
|
config["paseli"]["enabled"] = arcade.data.get_bool("paseli_enabled")
|
|
|
|
config["paseli"]["infinite"] = arcade.data.get_bool("paseli_infinite")
|
|
|
|
if arcade.data.get_bool("mask_services_url"):
|
2019-12-08 22:43:49 +01:00
|
|
|
# Mask the address, no matter what the server settings are
|
2022-10-15 20:56:30 +02:00
|
|
|
config["server"]["uri"] = None
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
game = Base.create(self.__data, config, model)
|
2022-10-15 20:56:30 +02:00
|
|
|
method = request.attribute("method")
|
2019-12-08 22:43:49 +01:00
|
|
|
response = None
|
|
|
|
|
|
|
|
# If we are enforcing, make sure the PCBID isn't specified to be
|
|
|
|
# game-specific
|
2021-08-20 06:43:59 +02:00
|
|
|
if config.server.enforce_pcbid and pcb.game is not None:
|
2019-12-08 22:43:49 +01:00
|
|
|
if pcb.game != game.game:
|
2022-10-15 20:56:30 +02:00
|
|
|
self.log(
|
|
|
|
"PCBID {} assigned to game {}, but connected from game {}",
|
|
|
|
pcbid,
|
|
|
|
pcb.game,
|
|
|
|
game.game,
|
|
|
|
)
|
|
|
|
raise UnrecognizedPCBIDException(
|
|
|
|
pcbid, modelstring, config.client.address
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
if pcb.version is not None:
|
|
|
|
if pcb.version > 0 and pcb.version != game.version:
|
|
|
|
self.log(
|
|
|
|
"PCBID {} assigned to game {} version {}, but connected from game {} version {}",
|
|
|
|
pcbid,
|
|
|
|
pcb.game,
|
|
|
|
pcb.version,
|
|
|
|
game.game,
|
|
|
|
game.version,
|
|
|
|
)
|
2022-10-15 20:56:30 +02:00
|
|
|
raise UnrecognizedPCBIDException(
|
|
|
|
pcbid, modelstring, config.client.address
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
if pcb.version < 0 and (-pcb.version) < game.version:
|
|
|
|
self.log(
|
|
|
|
"PCBID {} assigned to game {} maximum version {}, but connected from game {} version {}",
|
|
|
|
pcbid,
|
|
|
|
pcb.game,
|
|
|
|
-pcb.version,
|
|
|
|
game.game,
|
|
|
|
game.version,
|
|
|
|
)
|
2022-10-15 20:56:30 +02:00
|
|
|
raise UnrecognizedPCBIDException(
|
|
|
|
pcbid, modelstring, config.client.address
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# First, try to handle with specific service/method function
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
handler = getattr(game, f"handle_{request.name}_{method}_request")
|
2019-12-08 22:43:49 +01:00
|
|
|
except AttributeError:
|
|
|
|
handler = None
|
|
|
|
if handler is not None:
|
|
|
|
response = handler(request)
|
|
|
|
|
|
|
|
if response is None:
|
|
|
|
# Now, try to pass it off to a generic service handler
|
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
handler = getattr(game, f"handle_{request.name}_requests")
|
2019-12-08 22:43:49 +01:00
|
|
|
except AttributeError:
|
|
|
|
handler = None
|
|
|
|
if handler is not None:
|
|
|
|
response = handler(request)
|
|
|
|
|
|
|
|
if response is None:
|
|
|
|
# Unrecognized handler
|
2020-01-07 22:29:07 +01:00
|
|
|
self.log(f"Unrecognized service {request.name} method {method}")
|
2019-12-08 22:43:49 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Make sure we have a status value if one wasn't provided
|
2022-10-15 20:56:30 +02:00
|
|
|
if "status" not in response.attributes:
|
|
|
|
response.set_attribute("status", str(Status.SUCCESS))
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
root = Node.void("response")
|
2019-12-08 22:43:49 +01:00
|
|
|
root.add_child(response)
|
2022-10-15 20:56:30 +02:00
|
|
|
root.set_attribute("dstid", pcbid)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
self.log("Sending response:\n{}", root)
|
|
|
|
|
|
|
|
return root
|