BW3-Core/bw_server.py

128 lines
4.1 KiB
Python
Raw Normal View History

2018-06-15 22:20:20 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: bw_server.py
@date: 09.12.2017
@author: Bastian Schroll
@description: BOSWatch server application
"""
from boswatch.utils import paths
2019-02-27 22:05:59 +01:00
2018-06-15 22:20:20 +02:00
if not paths.makeDirIfNotExist(paths.LOG_PATH):
print("cannot find/create log directory: %s", paths.LOG_PATH)
exit(1)
try:
import logging
import logging.config
print(paths.CONFIG_PATH + "logger_server.ini")
logging.config.fileConfig(paths.CONFIG_PATH + "logger_server.ini")
logging.debug("")
logging.debug("######################## NEW LOG ############################")
logging.debug("BOSWatch server has started ...")
except Exception as e: # pragma: no cover
print("cannot load logger")
print(e)
exit(1)
try:
logging.debug("Import python module")
2018-06-15 22:20:20 +02:00
import argparse
logging.debug("- argparse")
# following is temp for testing
import time
import sys
import threading
import queue
2018-06-15 22:20:20 +02:00
logging.debug("Import BOSWatch module")
from boswatch.configYaml import ConfigYAML
2018-06-15 22:20:20 +02:00
from boswatch.network.server import TCPServer
2019-03-01 12:16:06 +01:00
from boswatch.packet import Packet
2018-06-15 22:20:20 +02:00
from boswatch.utils import header
2018-09-23 17:49:53 +02:00
from boswatch.network.broadcast import BroadcastServer
2019-02-27 22:05:59 +01:00
except: # pragma: no cover
logging.exception("cannot import module")
2018-06-15 22:20:20 +02:00
exit(1)
try:
header.logoToLog()
header.infoToLog()
header.logoToScreen()
logging.debug("parse args")
# With -h or --help you get the Args help
parser = argparse.ArgumentParser(prog="bw_server.py",
description="""BOSWatch is a Python Script to receive and
decode german BOS information with rtl_fm and multimon-NG""",
epilog="""More options you can find in the extern client.ini
file in the folder /config""")
parser.add_argument("-c", "--config", help="Name to configuration File", required=True)
args = parser.parse_args()
bwConfig = ConfigYAML()
if not bwConfig.loadConfigFile(paths.CONFIG_PATH + args.config):
2019-02-27 22:05:59 +01:00
logging.error("cannot load config file")
exit(1)
2019-02-27 22:05:59 +01:00
except: # pragma: no cover
logging.exception("error occurred")
exit(1)
import router_test
2019-02-27 22:05:59 +01:00
# ############################# begin server system
try:
if bwConfig.get("server", "useBroadcast", default=False):
2019-02-27 22:05:59 +01:00
bcServer = BroadcastServer()
bcServer.start()
2018-06-15 22:20:20 +02:00
incomingQueue = queue.Queue()
bwServer = TCPServer(incomingQueue)
2018-06-15 22:20:20 +02:00
if bwServer.start():
while 1:
if incomingQueue.empty(): # pause only when no data
2019-02-27 22:05:59 +01:00
time.sleep(0.1) # reduce cpu load (wait 100ms)
else:
data = incomingQueue.get()
2018-06-15 22:20:20 +02:00
logging.info("get data from %s (waited in queue %0.3f sec.)", data[0], time.time() - data[2])
2019-02-27 22:05:59 +01:00
logging.debug("%s packet(s) still waiting in queue", incomingQueue.qsize())
2018-06-15 22:20:20 +02:00
bwPacket = Packet((data[1]))
bwPacket.set("clientIP", data[0])
bwPacket.addServerData()
2019-02-27 22:05:59 +01:00
# todo implement routing
incomingQueue.task_done()
2018-06-15 22:20:20 +02:00
except KeyboardInterrupt: # pragma: no cover
logging.warning("Keyboard interrupt")
except SystemExit: # pragma: no cover
logging.error("BOSWatch interrupted by an error")
except: # pragma: no cover
logging.exception("BOSWatch interrupted by an error")
finally: # pragma: no cover
# try-except-blocks are necessary because there is a change that the vars
# bwServer or bwPluginManager are not defined in case of an early error
try:
bwServer.stop()
2019-02-27 22:05:59 +01:00
if "bcServer" in locals():
bcServer.stop()
2018-06-15 22:20:20 +02:00
except: # pragma: no cover
pass
logging.debug("BOSWatch has ended ...")