verified working locally
This commit is contained in:
parent
ff23314d33
commit
3d03162169
|
@ -20,7 +20,7 @@ class ProtocolError(Exception):
|
||||||
T = TypeVar("T")
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
def _try_with_backoff(fn: Callable, error_callback: Callable) -> socket.socket:
|
def _try_with_backoff(fn: Callable[[], T], error_callback: Callable) -> T:
|
||||||
backoff = 1
|
backoff = 1
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
@ -109,7 +109,7 @@ class SocketWrapper:
|
||||||
self._write(payload)
|
self._write(payload)
|
||||||
LOG.debug("sent {} bytes".format(len(payload)))
|
LOG.debug("sent {} bytes".format(len(payload)))
|
||||||
|
|
||||||
def _read(self, size) -> Optional[bytes]:
|
def _read(self, size) -> bytes:
|
||||||
data = b""
|
data = b""
|
||||||
while len(data) != size:
|
while len(data) != size:
|
||||||
newdata = self.soc.recv(size - len(data))
|
newdata = self.soc.recv(size - len(data))
|
||||||
|
@ -176,7 +176,9 @@ class MessageQueue:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
return json.loads(self.inbox.get())
|
m = self.inbox.get()
|
||||||
|
LOG.debug(m)
|
||||||
|
return json.loads(m)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.closed = True
|
self.closed = True
|
||||||
|
|
|
@ -214,6 +214,7 @@ def receive_messages(
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
global global_msg_queue
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--matrix_server_name", required=True)
|
parser.add_argument("--matrix_server_name", required=True)
|
||||||
|
|
26
wrapper.py
26
wrapper.py
|
@ -33,23 +33,28 @@ class ProcessWrapper:
|
||||||
def wait(self):
|
def wait(self):
|
||||||
return self.proc.wait()
|
return self.proc.wait()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def closed(self) -> bool:
|
||||||
|
return self.proc.returncode is not None
|
||||||
|
|
||||||
|
|
||||||
def send_process_output(
|
def send_process_output(
|
||||||
process: ProcessWrapper, msg_queue: message_queue.MessageQueue
|
process: ProcessWrapper, msg_queue: message_queue.MessageQueue
|
||||||
):
|
):
|
||||||
# "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test"
|
# "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test"
|
||||||
prog = re.compile(r"\[.*\] \[(.*)\] \[(.*)\]: <(.*)> (.*)")
|
log = LOG.getChild("process_output")
|
||||||
|
prog = re.compile(r"(\[.*\] )?\[(.*)\] \[(.*)\]: <(.*)> (.*)")
|
||||||
for line in process:
|
for line in process:
|
||||||
LOG.info(line.rstrip("\n"))
|
log.info(line.rstrip("\n"))
|
||||||
result = prog.search(line)
|
result = prog.search(line)
|
||||||
if result:
|
if result:
|
||||||
LOG.info(
|
log.info(
|
||||||
"user: {} msg: {}".format(
|
"user: {} msg: {}".format(
|
||||||
result.group(3), result.group(4).rstrip("\n"),
|
result.group(4), result.group(5).rstrip("\n"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
msg_queue.add(
|
msg_queue.add(
|
||||||
{"user": result.group(3), "msg": result.group(4).rstrip("\n")},
|
{"user": result.group(4), "msg": result.group(5).rstrip("\n")},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,13 +62,18 @@ def relay_queue_input(
|
||||||
process: ProcessWrapper, msg_queue: message_queue.MessageQueue
|
process: ProcessWrapper, msg_queue: message_queue.MessageQueue
|
||||||
):
|
):
|
||||||
log = LOG.getChild("relay_input")
|
log = LOG.getChild("relay_input")
|
||||||
|
while not process.closed:
|
||||||
|
try:
|
||||||
for message in msg_queue:
|
for message in msg_queue:
|
||||||
log.debug(message)
|
log.debug(message)
|
||||||
if "command" in message:
|
if "command" in message:
|
||||||
log.debug("forwarding to process")
|
command = message["command"] + "\n"
|
||||||
process.send(message["command"] + "\n")
|
log.debug("forwarding to process: {!r}".format(command))
|
||||||
|
process.send(command)
|
||||||
else:
|
else:
|
||||||
LOG.debug(message)
|
log.debug(message)
|
||||||
|
except Exception as e:
|
||||||
|
log.exception(e)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
Loading…
Reference in New Issue