Merge branch 'split_router' into develop

This commit is contained in:
Bastian Schroll 2019-03-05 12:53:47 +01:00
commit 35c6de4c54
5 changed files with 137 additions and 60 deletions

View file

@ -0,0 +1,2 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

38
boswatch/router/route.py Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: route.py
@date: 04.03.2019
@author: Bastian Schroll
@description: Class for a single BOSWatch packet router route point
"""
class Route:
"""!Class for single routing points"""
def __init__(self, name, callback):
"""!Create a instance of an route point
@param name: name of the route point
@param callback: instance of the callback function
"""
self._name = name
self._callback = callback
@property
def name(self):
"""!Property to get the route point name"""
return self._name
@property
def callback(self):
"""!Porperty to get the callback function instance"""
return self._callback

72
boswatch/router/router.py Normal file
View file

@ -0,0 +1,72 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""!
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: router.py
@date: 01.03.2019
@author: Bastian Schroll
@description: Class for the BOSWatch packet router
"""
import logging
import copy
logging.debug("- %s loaded", __name__)
class Router:
"""!Class for the Router"""
def __init__(self, name):
"""!Create a new router
@param name: name of the router"""
self._name = name
self._routeList = []
logging.debug("[%s] new router", self._name)
def addRoute(self, route):
"""!Adds a route point to the router
@param route: instance of the Route class
"""
logging.debug("[%s] add route: %s", self._name, route.name)
self._routeList.append(route)
def runRouter(self, bwPacket):
"""!Run the router
@param bwPacket: instance of Packet class
@return a instance of Packet class
"""
logging.debug("[%s] started", self._name)
for routeObject in self._routeList:
logging.debug("[%s] -> run route: %s", self._name, routeObject)
bwPacket_tmp = routeObject.callback(copy.deepcopy(bwPacket)) # copy bwPacket to prevent edit the original
if bwPacket_tmp is None: # returning None doesnt change the bwPacket
continue
if bwPacket_tmp is False: # returning False stops the router immediately
logging.debug("[%s] stopped", self._name)
break
bwPacket = bwPacket_tmp
logging.debug("[%s] <- bwPacket returned: %s", self._name, bwPacket)
logging.debug("[%s] ended", self._name)
return bwPacket
@property
def name(self):
"""!Property to get the name of the router"""
return self._name
@property
def routeList(self):
"""!Property to get a list of all route points of this router"""
return self._routeList

View file

@ -9,81 +9,41 @@
German BOS Information Script
by Bastian Schroll
@file: router.py
@date: 01.03.2019
@file: routerManager.py
@date: 04.03.2019
@author: Bastian Schroll
@description: Class for the BOSWatch packet router
@description: Class for the BOSWatch packet router manager class
"""
# todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!)
import logging
import copy
import importlib
from boswatch.configYaml import ConfigYAML
from boswatch.router.router import Router
from boswatch.router.route import Route
logging.debug("- %s loaded", __name__)
class _Route:
def __init__(self, name, callback):
self._name = name
self._callback = callback
@property
def name(self):
return self._name
@property
def callback(self):
return self._callback
class _Router:
def __init__(self, name):
self._name = name
self._routeList = []
logging.debug("[%s] new router", self._name)
def addRoute(self, route):
logging.debug("[%s] add route: %s", self._name, route.name)
self._routeList.append(route)
def runRouter(self, bwPacket):
logging.debug("[%s] started", self._name)
for routeObject in self._routeList:
logging.debug("[%s] -> run route: %s", self._name, routeObject)
bwPacket_tmp = routeObject.callback(copy.deepcopy(bwPacket)) # copy bwPacket to prevent edit the original
if bwPacket_tmp is None: # returning None doesnt change the bwPacket
continue
if bwPacket_tmp is False: # returning False stops the router immediately
logging.debug("[%s] stopped", self._name)
break
bwPacket = bwPacket_tmp
logging.debug("[%s] <- bwPacket returned: %s", self._name, bwPacket)
logging.debug("[%s] ended", self._name)
return bwPacket
@property
def name(self):
return self._name
@property
def routeList(self):
return self._routeList
class RouterManager:
"""!Class to manage all routers"""
def __init__(self):
"""!Create new router"""
self._routerDict = {}
def __del__(self):
"""!Destroy the internal routerDict
All routers and route point instances will be destroyed too
Also destroys all instances from modules or plugins"""
# destroy all routers (also destroys all instances of modules/plugins)
del self._routerDict
# if there is an error, router list would be empty (see tmp variable)
def buildRouter(self, config):
"""!Initialize Routers from given config file
@param config: instance of ConfigYaml class
@return True or False"""
self._routerDict = {} # all routers and instances of modules/plugins would be destroyed
routerDict_tmp = {}
logging.debug("build routers")
@ -97,7 +57,7 @@ class RouterManager:
if router.get("name") in self._routerDict:
logging.error("duplicated router name: %s", router.get("name"))
return False
routerDict_tmp[router.get("name")] = _Router(router.get("name"))
routerDict_tmp[router.get("name")] = Router(router.get("name"))
for router in config.get("router"):
routerName = router.get("name")
@ -115,15 +75,15 @@ class RouterManager:
if routeType == "plugin":
importedFile = importlib.import_module(routeType + "." + routeName)
loadedClass = importedFile.BoswatchPlugin(routeConfig)
routerDict_tmp[routerName].addRoute(_Route(routeName, loadedClass._run))
routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run))
elif routeType == "module":
importedFile = importlib.import_module(routeType + "." + routeName)
loadedClass = importedFile.BoswatchModule(routeConfig)
routerDict_tmp[routerName].addRoute(_Route(routeName, loadedClass._run))
routerDict_tmp[routerName].addRoute(Route(routeName, loadedClass._run))
elif routeType == "router":
routerDict_tmp[routerName].addRoute(_Route(routeName, routerDict_tmp[routeName].runRouter))
routerDict_tmp[routerName].addRoute(Route(routeName, routerDict_tmp[routeName].runRouter))
else:
logging.error("unknown type '%s' in %s", routeType, route)
@ -139,6 +99,10 @@ class RouterManager:
return True
def runRouter(self, routerRunList, bwPacket):
"""!Run given Routers
@param routerRunList: string or list of router names in string form
@param bwPacket: instance of Packet class"""
if type(routerRunList) is str: # convert single string name to list
routerRunList = [routerRunList]
@ -147,6 +111,7 @@ class RouterManager:
self._routerDict[routerName].runRouter(bwPacket)
def _showRouterRoute(self):
"""!Show the routes of all routers"""
for name, routerObject in self._routerDict.items():
logging.debug("Route for %s", name)
counter = 0

View file

@ -43,7 +43,7 @@ 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 import RouterManager
from boswatch.router.routerManager import RouterManager
header.logoToLog()