split router classes and make own module

This commit is contained in:
Bastian Schroll 2019-03-04 14:48:25 +01:00
parent 6306a845c7
commit 8ae23b4d0f
5 changed files with 102 additions and 60 deletions

View file

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

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

@ -0,0 +1,30 @@
#!/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:
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

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

@ -0,0 +1,58 @@
#!/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
"""
# todo think about implement threading for routers and the plugin calls (THREAD SAFETY!!!)
import logging
import copy
logging.debug("- %s loaded", __name__)
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

View file

@ -9,71 +9,23 @@
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:
def __init__(self):
self._routerDict = {}
@ -97,7 +49,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 +67,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)

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()