add some error handling in router

This commit is contained in:
Bastian Schroll 2019-03-01 21:36:38 +01:00
parent 98d2ea344b
commit 7f55be4823
3 changed files with 41 additions and 22 deletions

View file

@ -14,6 +14,7 @@
@author: Bastian Schroll @author: Bastian Schroll
@description: Class for the BOSWatch packet router @description: Class for the BOSWatch packet router
""" """
# todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!)
import logging import logging
import copy import copy
import importlib import importlib
@ -25,7 +26,7 @@ class _Router:
def __init__(self, name): def __init__(self, name):
self.__name = name self.__name = name
self.__route = [] self.__route = []
logging.debug("add new router: %s", self.__name) logging.debug("[%s] new router", self.__name)
def addRoute(self, route): def addRoute(self, route):
logging.debug("[%s] add route: %s", self.__name, route) logging.debug("[%s] add route: %s", self.__name, route)
@ -64,7 +65,7 @@ class RouterManager:
def __del__(self): def __del__(self):
del self.__routerDict del self.__routerDict
def buildRouter(self, config): def buildRouter(self, config):
self.__routerDict = {} # all routers and loaded modules/plugins would be unloaded self.__routerDict = {} # all routers and loaded modules/plugins would be unloaded
logging.debug("build routers") logging.debug("build routers")
@ -73,32 +74,48 @@ class RouterManager:
# because a router can be a valid target and we need his reference # because a router can be a valid target and we need his reference
for router in config.get("router"): for router in config.get("router"):
self.__routerDict[router.get("name")] = _Router(router.get("name")) self.__routerDict[router.get("name")] = _Router(router.get("name"))
for router in config.get("router"): for router in config.get("router"):
for route in router.get("route"): for route in router.get("route"):
if route.get("type") == "plugin": routerName = router.get("name")
importedFile = importlib.import_module(route.get("type") + "." + route.get("name")) routeType = route.get("type")
loadedClass = importedFile.BoswatchPlugin(route.get("config")) routeName = route.get("name")
self.__routerDict[router.get("name")].addRoute(loadedClass._run)
if routeType is None or routeName is None:
elif route.get("type") == "module": logging.error("type or name error in config: %s", route)
importedFile = importlib.import_module(route.get("type") + "." + route.get("name")) continue
loadedClass = importedFile.BoswatchModule(route.get("config"))
self.__routerDict[router.get("name")].addRoute(loadedClass._run) try:
if routeType == "plugin":
elif route.get("type") == "router": importedFile = importlib.import_module(routeType + "." + routeName)
self.__routerDict[router.get("name")].addRoute(self.__routerDict[route.get("name")].runRouter) loadedClass = importedFile.BoswatchPlugin(route.get("config"))
self.__routerDict[routerName].addRoute(loadedClass._run)
elif routeType == "module":
importedFile = importlib.import_module(routeType + "." + routeName)
loadedClass = importedFile.BoswatchModule(route.get("config"))
self.__routerDict[routerName].addRoute(loadedClass._run)
elif routeType == "router":
self.__routerDict[routerName].addRoute(self.__routerDict[routeName].runRouter)
else:
logging.warning("unknown type: %s", routeType)
except ModuleNotFoundError:
logging.error("%s not found: %s", route.get("type"), route.get("name"))
logging.debug("finished building routers")
self._showRouterRoute()
def runRouter(self, routerList, bwPacket): def runRouter(self, routerList, bwPacket):
for router in routerList: for router in routerList:
if router in self.__routerDict: if router in self.__routerDict:
self.__routerDict[router].runRouter(bwPacket) self.__routerDict[router].runRouter(bwPacket)
def showRouterRoute(self): def _showRouterRoute(self):
for name, router in self.__routerDict.items(): for name, router in self.__routerDict.items():
logging.debug("Route for %s", name) logging.debug("Route for %s", name)
for route in router.route: for route in router.route:
logging.debug("- %s", route) logging.debug(" | %s", route)

View file

@ -38,11 +38,13 @@ try:
logging.debug("Import python module") logging.debug("Import python module")
import argparse import argparse
logging.debug("- argparse") logging.debug("- argparse")
import queue
logging.debug("- queue")
# following is temp for testing # following is temp for testing
import time import time
import sys import sys
import threading import threading
import queue
logging.debug("Import BOSWatch module") logging.debug("Import BOSWatch module")
from boswatch.configYaml import ConfigYAML from boswatch.configYaml import ConfigYAML

View file

@ -16,8 +16,8 @@ print()
routMan = RouterManager() routMan = RouterManager()
routMan.buildRouter(config) routMan.buildRouter(config)
print() print()
routMan.showRouterRoute()
routMan.runRouter(config.get("alarmRouter"), bwPack) routMan.runRouter(config.get("alarmRouter"), bwPack)