BOSWatch 3
Python Script to receive and decode German BOS Information with rtl_fm and multimon-NG
 
Loading...
Searching...
No Matches
boswatch.router.router.Router Class Reference

Class for the Router. More...

Public Member Functions

 __init__ (self, name)
 Create a new router.
 
 addRoute (self, route)
 Adds a route point to the router.
 
 runRouter (self, bwPacket)
 Run the router.
 

Data Fields

 name
 
 routeList
 

Protected Member Functions

 _process_route_recursive (self, bwPacket, start_index)
 Recursively process the route to support list branching (e.g.
 
 _getStatistics (self)
 Returns statistical information's from last router run.
 

Protected Attributes

 _cumTime
 
 _routerTime
 
 _runCount
 

Detailed Description

Class for the Router.

Constructor & Destructor Documentation

◆ __init__()

boswatch.router.router.Router.__init__ (   self,
  name 
)

Create a new router.

Parameters
namename of the router
26 def __init__(self, name):
27 r"""!Create a new router
28
29 @param name: name of the router"""
30 self.name = name
31 self.routeList = []
32
33 # for time counting
34 self._cumTime = 0
35 self._routerTime = 0
36
37 # for statistics
38 self._runCount = 0
39
40 logging.debug("[%s] add new router", self.name)
41

Member Function Documentation

◆ addRoute()

boswatch.router.router.Router.addRoute (   self,
  route 
)

Adds a route point to the router.

Parameters
routeinstance of the Route class
42 def addRoute(self, route):
43 r"""!Adds a route point to the router
44
45 @param route: instance of the Route class
46 """
47 logging.debug("[%s] add route: %s", self.name, route.name)
48 self.routeList.append(route)
49

◆ runRouter()

boswatch.router.router.Router.runRouter (   self,
  bwPacket 
)

Run the router.

Parameters
bwPacketinstance of Packet class
Returns
an instance of Packet class, a list of packets, or False
50 def runRouter(self, bwPacket):
51 r"""!Run the router
52
53 @param bwPacket: instance of Packet class
54 @return an instance of Packet class, a list of packets, or False
55 """
56 self._runCount += 1
57 tmpTime = time.time()
58
59 logging.debug("[%s] started", self.name)
60
61 # Start processing the route list recursively from index 0
62 final_result = self._process_route_recursive(bwPacket, 0)
63
64 logging.debug("[%s] finished", self.name)
65
66 self._routerTime = time.time() - tmpTime
67 self._cumTime += self._routerTime
68
69 return final_result
70

◆ _process_route_recursive()

boswatch.router.router.Router._process_route_recursive (   self,
  bwPacket,
  start_index 
)
protected

Recursively process the route to support list branching (e.g.

for multicast)

Parameters
bwPacketcurrent packet to process
start_indexindex of the routeList to start from
Returns
processed bwPacket, list of packets, or False
71 def _process_route_recursive(self, bwPacket, start_index):
72 r"""!Recursively process the route to support list branching (e.g. for multicast)
73
74 @param bwPacket: current packet to process
75 @param start_index: index of the routeList to start from
76 @return processed bwPacket, list of packets, or False
77 """
78 # Catch incoming lists from the manager (cascaded routers)
79 if isinstance(bwPacket, list):
80 logging.debug("[%s] Received a list of %d packets from manager. Branching immediately.", self.name, len(bwPacket))
81 results = []
82 for single_packet in bwPacket:
83 res = self._process_route_recursive(single_packet, start_index)
84 if res is not False and res is not None:
85 if isinstance(res, list):
86 results.extend(res)
87 else:
88 results.append(res)
89 return results if results else False
90
91 # Normal path for a single packet starts here
92 current_packet = bwPacket
93
94 for i in range(start_index, len(self.routeList)):
95 routeObject = self.routeList[i]
96 logging.debug("[%s] -> run route: %s", self.name, routeObject.name)
97
98 # State Isolation: pass a deep copy to prevent plugins from corrupting the RAM state of subsequent list items
99 bwPacket_tmp = routeObject.callback(copy.deepcopy(current_packet))
100
101 if bwPacket_tmp is None:
102 # returning None doesnt change the current_packet
103 continue
104
105 if bwPacket_tmp is False:
106 if routeObject.isRouter:
107 # A nested router filtered/stopped internally for this packet.
108 # That's just an empty branch - the parent router continues normally.
109 logging.debug("[%s] sub-router '%s' returned False - continuing with next route point", self.name, routeObject.name)
110 continue
111
112 # returning False stops the route immediately for this specific packet branch
113 logging.debug("[%s] stopped at route %s", self.name, routeObject.name)
114 return False
115
116 if isinstance(bwPacket_tmp, list):
117 # Branching logic: A module returned a list of packets
118 logging.debug("[%s] route %s returned a list. Branching for %d packets.", self.name, routeObject.name, len(bwPacket_tmp))
119 results = []
120
121 for single_packet in bwPacket_tmp:
122 # Recursively process the rest of the route (starting at next index) for each packet
123 res = self._process_route_recursive(single_packet, i + 1)
124
125 # Aggregate results
126 if res is not False and res is not None:
127 if isinstance(res, list):
128 results.extend(res)
129 else:
130 results.append(res)
131
132 # The recursive calls already finished the rest of the route for all branches.
133 # We return the aggregated results immediately to break out of this current loop level.
134 return results if results else False
135
136 # Normal single packet path: update the packet for the next iteration
137 current_packet = bwPacket_tmp
138
139 return current_packet
140

◆ _getStatistics()

boswatch.router.router.Router._getStatistics (   self)
protected

Returns statistical information's from last router run.

Returns
Statistics as pyton dict
141 def _getStatistics(self):
142 r"""!Returns statistical information's from last router run
143
144 @return Statistics as pyton dict"""
145 stats = {"type": "router",
146 "runCount": self._runCount,
147 "cumTime": self._cumTime,
148 "moduleTime": self._routerTime}
149 return stats

Field Documentation

◆ name

boswatch.router.router.Router.name

◆ routeList

boswatch.router.router.Router.routeList

◆ _cumTime

boswatch.router.router.Router._cumTime
protected

◆ _routerTime

boswatch.router.router.Router._routerTime
protected

◆ _runCount

boswatch.router.router.Router._runCount
protected