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
@description: Class for the BOSWatch packet router
"""
# todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!)
import logging
import copy
import importlib
@ -25,7 +26,7 @@ class _Router:
def __init__(self, name):
self.__name = name
self.__route = []
logging.debug("add new router: %s", self.__name)
logging.debug("[%s] new router", self.__name)
def addRoute(self, route):
logging.debug("[%s] add route: %s", self.__name, route)
@ -77,28 +78,44 @@ class RouterManager:
for router in config.get("router"):
for route in router.get("route"):
if route.get("type") == "plugin":
importedFile = importlib.import_module(route.get("type") + "." + route.get("name"))
routerName = router.get("name")
routeType = route.get("type")
routeName = route.get("name")
if routeType is None or routeName is None:
logging.error("type or name error in config: %s", route)
continue
try:
if routeType == "plugin":
importedFile = importlib.import_module(routeType + "." + routeName)
loadedClass = importedFile.BoswatchPlugin(route.get("config"))
self.__routerDict[router.get("name")].addRoute(loadedClass._run)
self.__routerDict[routerName].addRoute(loadedClass._run)
elif route.get("type") == "module":
importedFile = importlib.import_module(route.get("type") + "." + route.get("name"))
elif routeType == "module":
importedFile = importlib.import_module(routeType + "." + routeName)
loadedClass = importedFile.BoswatchModule(route.get("config"))
self.__routerDict[router.get("name")].addRoute(loadedClass._run)
self.__routerDict[routerName].addRoute(loadedClass._run)
elif route.get("type") == "router":
self.__routerDict[router.get("name")].addRoute(self.__routerDict[route.get("name")].runRouter)
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):
for router in routerList:
if router in self.__routerDict:
self.__routerDict[router].runRouter(bwPacket)
def showRouterRoute(self):
def _showRouterRoute(self):
for name, router in self.__routerDict.items():
logging.debug("Route for %s", name)
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")
import argparse
logging.debug("- argparse")
import queue
logging.debug("- queue")
# following is temp for testing
import time
import sys
import threading
import queue
logging.debug("Import BOSWatch module")
from boswatch.configYaml import ConfigYAML

View file

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