1
0
Fork 0

log exceptions as exceptions, more debugging, more cleanup

This commit is contained in:
khr 2020-03-29 01:25:30 +00:00
parent 3c7ee16f43
commit 15c7a0341b
1 changed files with 22 additions and 24 deletions

View File

@ -72,14 +72,14 @@ class socket_util(object):
string_message = json.dumps(message)
select.select([], [self.soc], [])
self.write_int(len(string_message))
self.write(string_message)
self.write(string_message.encode('utf-8'))
return 0
def write_int(self, integer):
integer_buf = struct.pack('>i', integer)
self.write(integer_buf)
def write(self, data):
def write(self, data: bytes):
#socket.bind(address)
data_len = len(data)
offset = 0
@ -122,8 +122,10 @@ class socket_util(object):
class MinecraftWrapper(socket_util):
def __init__(self, command, host, port):
super().__init__(host, port)
self.logger = LOG.getChild("MinecraftWrapper")
self.logger.debug(command)
self.command = command
LOG.info("Starting Wrapper Polling Thread")
self.logger.info("Starting Wrapper Polling Thread")
poll_process = threading.Thread(target=self.cli_poll)
poll_process.daemon = True
poll_process.start()
@ -131,16 +133,19 @@ class MinecraftWrapper(socket_util):
def socket_reset(self):
super().socket_reset()
self.logger.debug("Connecting to {}:{}".format(self.host, self.port))
self.soc.connect((self.host, self.port))
LOG.info("Socket Connected")
self.logger.info("Socket Connected")
def exe_mc(self):
self.proc = subprocess.Popen(self.command, shell=True, stdout=PIPE, stdin=PIPE, universal_newlines=True)
self.proc = subprocess.Popen(
" ".join(self.command), shell=True, stdout=PIPE, stdin=PIPE, universal_newlines=True
)
for stdout_line in iter(self.proc.stdout.readline, ""):
yield stdout_line
return_code = self.proc.wait()
if return_code:
raise subprocess.CalledProcessError(return_code, cmd)
raise subprocess.CalledProcessError(return_code, self.command)
def msg_process(self):
while(not self.exit):
@ -154,7 +159,7 @@ class MinecraftWrapper(socket_util):
self.msg_handle(rcv)
if status == 0: self.msglist.pop()
except Exception as e:
LOG.info(e)
self.logger.exception(e)
self.socket_reset()
@ -167,23 +172,22 @@ class MinecraftWrapper(socket_util):
def proc_monitor(self):
try:
if self.proc.poll() is not None:
if self.proc is not None and self.proc.poll() is not None:
self.exit = True
self.close_socket()
sys.exit(0)
except:
LOG.error("poll error")
except Exception as e:
self.logger.exception("poll error")
pass
def cli_poll(self):
prog = re.compile("^\[(.*)\] \[(.*)\]: <(.*)> (.*)")
prog = re.compile("\[.*\] \[(.*)\] \[(.*)\]: <(.*)> (.*)")
EXAMPLE = "[07:36:28] [Server thread/INFO] [minecraft/DedicatedServer]: <khr_> test"
for line in self.exe_mc():
LOG.info(line.rstrip('\n'))
# regex to get user and text: ^<(.*)> (.*)\n
self.logger.info(line.rstrip('\n'))
result = prog.search(line)
if result:
#LOG.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n'))
#msb.send({"user":result.group(3),"msg":result.group(4).rstrip('\n')})
self.logger.info("user: " + result.group(3) + " msg: " +result.group(4).rstrip('\n'))
self.msglist.insert(0, {"user":result.group(3),"msg":result.group(4).rstrip('\n')})
class MinecraftServerBridge(socket_util):
@ -244,7 +248,7 @@ class MinecraftServerBridge(socket_util):
user_id = new_user + ":" + global_config['server_name']
self.api.register("m.login.application_service",username = "mc_" + msg['user'])
except Exception as e:
LOG.info(e)
LOG.exception(e)
#for each room we're aware of, post server chat inside. Eventually 1 room should equal 1 server
for room in roomsync:
#generate a unique transaction id based on the current time
@ -355,7 +359,7 @@ def make_config(configfile, server=True):
def main():
logging.basicConfig()
logging.basicConfig(level=logging.DEBUG)
parser = argparse.ArgumentParser()
mode_group = parser.add_mutually_exclusive_group(required=True)
@ -395,14 +399,8 @@ def main():
appservice_token=global_config["as_token"],
)
LOG.info("All Threads Running")
cmd = ""
# Allow stdin commands to the minecraft server
while (not minecraft.exit):
if minecraft.proc != None and 'stop' not in cmd:
cmd = input()
minecraft.proc.stdin.write(cmd + '\n')
else:
time.sleep(1)
time.sleep(1)
LOG.info("Calling exit() in main thread...")
sys.exit()