BW3-Core/bw_server.py

193 lines
6.4 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
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 modules")
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 modules")
2019-02-27 08:39:54 +01:00
from boswatch import configYaml
2018-06-15 22:20:20 +02:00
from boswatch.network.server import TCPServer
from boswatch.packet.packet import Packet
from boswatch.plugin.pluginManager import PluginManager
from boswatch.descriptor.descriptor import Descriptor
from boswatch.filter.doubeFilter import DoubleFilter
from boswatch.utils import header
2018-09-23 17:49:53 +02:00
from boswatch.network.broadcast import BroadcastClient
from boswatch.network.broadcast import BroadcastServer
2018-06-15 22:20:20 +02:00
except Exception as e: # pragma: no cover
logging.exception("cannot import modules")
print("cannot import modules")
print(e)
exit(1)
2018-09-23 17:49:53 +02:00
# Test for the broadcast connection info function
server = BroadcastServer()
client = BroadcastClient()
server.start()
2018-09-23 21:27:40 +02:00
print(server.isRunning)
2018-09-23 17:49:53 +02:00
client.getConnInfo()
print(client.serverIP, client.serverPort)
server.stop()
2018-09-23 21:27:40 +02:00
print(server.isRunning)
time.sleep(2)
print(server.isRunning)
2018-09-23 17:49:53 +02:00
2018-09-25 19:06:56 +02:00
# test for the timer class
2018-09-21 15:27:53 +02:00
from boswatch.utils.timer import RepeatedTimer
from boswatch.network.netCheck import NetCheck
net = NetCheck()
2018-09-23 18:44:58 +02:00
test = RepeatedTimer(3, net.checkConn)
2018-09-21 15:27:53 +02:00
test.start()
2018-09-23 18:44:58 +02:00
time.sleep(10)
print(net.connectionState)
test.stop()
2018-09-21 15:27:53 +02:00
2018-06-15 22:20:20 +02:00
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()
2019-02-27 08:39:54 +01:00
bwConfig = configYaml.loadConfigFile(paths.CONFIG_PATH + args.config, "serverConfig")
if bwConfig is None:
2018-06-15 22:20:20 +02:00
logging.exception("cannot load config file")
print("cannot load config file")
exit(1) # without config cannot run
bwPluginManager = PluginManager()
bwPluginManager.searchPluginDir()
bwPluginManager.importAllPlugins()
bwPluginManager.loadAllPlugins()
bwDoubleFilter = DoubleFilter()
serverPaused = False # flag to pause alarming of server
serverStop = False # flag to stop the whole server application
# server flags test code
# ----------------------
# import threading
# def eins():
# global serverPaused
# serverPaused = True
# def zwei():
# global serverPaused
# serverPaused = False
def drei():
2018-06-18 07:05:36 +02:00
global serverStop
serverStop = True
2018-06-15 22:20:20 +02:00
#
# t1 = threading.Timer(1, eins)
# t2 = threading.Timer(5, zwei)
2018-09-21 15:27:53 +02:00
# t3 = threading.Timer(600, drei)
2018-06-15 22:20:20 +02:00
# t1.start()
# t2.start()
# t3.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 serverPaused:
logging.warning("Server pause flag received ...")
logging.debug("But all received packages will be cached!")
packetsOld = 0
while serverPaused is True:
time.sleep(0.2) # reduce cpu load (run all 200ms)
packetsNew = incomingQueue.qsize()
2018-06-15 22:20:20 +02:00
if packetsNew is not packetsOld:
logging.debug("%s packet(s) waiting in queue", packetsNew)
packetsOld = packetsNew
logging.warning("Server resumed ...")
if serverStop:
logging.warning("Server stop flag received ...")
break
if incomingQueue.empty(): # pause only when no data
2018-06-15 22:20:20 +02:00
time.sleep(0.1) # reduce cpu load (run all 100ms)
data = incomingQueue.get()
2018-06-15 22:20:20 +02:00
if data is not None:
logging.info("get data from %s (waited in queue %0.3f sec.)", data[0], time.time() - data[2])
logging.debug("%s packet(s) waiting in queue", incomingQueue.qsize())
2018-06-15 22:20:20 +02:00
bwPacket = Packet((data[1]))
if not bwDoubleFilter.filter(bwPacket):
continue
bwPacket.set("clientIP", data[0])
bwPacket.addServerData()
bwPluginManager.runAllPlugins(bwPacket)
# print(bwPacket.get("clientVersion")["major"])
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()
except: # pragma: no cover
pass
try:
bwPluginManager.unloadAllPlugins()
except: # pragma: no cover
pass
logging.debug("BOSWatch has ended ...")