BW3-Core/bw_server.py

127 lines
4.2 KiB
Python
Raw Normal View History

2018-06-15 22:20:20 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
r"""!
2018-06-15 22:20:20 +02:00
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: bw_server.py
@date: 09.12.2017
@author: Bastian Schroll
@description: BOSWatch server application
"""
2019-03-03 19:14:00 +01:00
# pylint: disable=wrong-import-position
# pylint: disable=wrong-import-order
import sys
major_version = sys.version_info.major
assert major_version >= 3, "please use python3 to run BosWatch 3"
2018-06-15 22:20:20 +02:00
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)
import logging.config
logging.config.fileConfig(paths.CONFIG_PATH + "logger_server.ini")
logging.debug("")
logging.debug("######################## NEW LOG ############################")
logging.debug("BOSWatch server has started ...")
logging.debug("Import python modules")
import argparse
logging.debug("- argparse")
import queue
logging.debug("- queue")
import time
logging.debug("- time")
logging.debug("Import BOSWatch modules")
from boswatch.configYaml import ConfigYAML
from boswatch.network.server import TCPServer
from boswatch.packet import Packet
from boswatch.utils import header
from boswatch.network.broadcast import BroadcastServer
from boswatch.router.routerManager import RouterManager
2019-03-11 07:47:51 +01:00
from boswatch.utils import misc
header.logoToLog()
header.infoToLog()
# 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-10-21 20:56:51 +02:00
logging.fatal("cannot load config file")
2019-02-27 22:05:59 +01:00
exit(1)
# ############################# begin server system
2019-10-24 19:01:35 +02:00
bwRoutMan = None
bwServer = None
bcServer = None
2019-02-27 22:05:59 +01:00
2019-10-24 19:01:35 +02:00
try:
bwRoutMan = RouterManager()
2019-10-25 15:56:20 +02:00
if not bwRoutMan.buildRouters(bwConfig):
2019-10-21 20:56:51 +02:00
logging.fatal("Error while building routers")
exit(1)
2019-10-08 20:40:53 +02:00
bcServer = BroadcastServer()
if bwConfig.get("server", "useBroadcast", default=False):
2019-02-27 22:05:59 +01:00
bcServer.start()
2018-06-15 22:20:20 +02:00
incomingQueue = queue.Queue()
bwServer = TCPServer(incomingQueue)
2020-06-07 22:56:29 +02:00
if bwServer.start(port=bwConfig.get('server', 'port', default=8080)):
2018-06-15 22:20:20 +02:00
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)
2019-03-05 12:32:56 +01:00
# in worst case a packet have to wait 100ms until it will be processed
2019-02-27 22:05:59 +01:00
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])
2019-03-11 07:47:51 +01:00
misc.addServerDataToPacket(bwPacket, bwConfig)
2018-06-15 22:20:20 +02:00
2019-10-25 23:26:12 +02:00
logging.debug("[ --- ALARM --- ]")
2019-10-25 13:27:48 +02:00
bwRoutMan.runRouters(bwConfig.get("alarmRouter"), bwPacket)
2019-10-25 23:26:12 +02:00
logging.debug("[ --- END ALARM --- ]")
2019-02-27 22:05:59 +01:00
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")
2019-09-21 17:35:26 +02:00
finally:
2019-09-18 21:29:41 +02:00
logging.debug("Starting shutdown routine")
2019-10-24 19:01:35 +02:00
if bwRoutMan:
2019-10-25 15:36:10 +02:00
bwRoutMan.cleanup()
2019-10-24 19:01:35 +02:00
if bwServer:
bwServer.stop()
if bcServer:
bcServer.stop()
2019-09-21 17:35:26 +02:00
logging.debug("BOSWatch server has stopped ...")