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

BroadcastServer class. More...

Public Member Functions

 __init__ (self, servePort=8080, listenPort=5000)
 Create an BroadcastServer instance.
 
 __del__ (self)
 
 start (self)
 Start the broadcast server in a new thread.
 
 stop (self)
 Stop the broadcast server.
 
 isRunning (self)
 Property of broadcast server running state.
 

Protected Member Functions

 _listen (self)
 Broadcast server worker thread.
 

Protected Attributes

 _socket
 
 _serverThread
 
 _serverShutdown
 
 _servePort
 

Detailed Description

BroadcastServer class.

Constructor & Destructor Documentation

◆ __init__()

boswatch.network.broadcast.BroadcastServer.__init__ (   self,
  servePort = 8080,
  listenPort = 5000 
)

Create an BroadcastServer instance.

Parameters
servePortport to serve as connection info (8080)
listenPortport to listen for broadcast packets (5000)
88 def __init__(self, servePort=8080, listenPort=5000):
89 r"""!Create an BroadcastServer instance
90
91 @param servePort: port to serve as connection info (8080)
92 @param listenPort: port to listen for broadcast packets (5000)"""
93 self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
94 self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
95 self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
96 self._socket.settimeout(2)
97 self._socket.bind(('', listenPort))
98 self._serverThread = None
99 self._serverShutdown = False
100 self._servePort = servePort
101

◆ __del__()

boswatch.network.broadcast.BroadcastServer.__del__ (   self)
102 def __del__(self): # pragma: no cover
103 if self.isRunning:
104 self.stop()
105 while self.isRunning:
106 pass
107

Member Function Documentation

◆ start()

boswatch.network.broadcast.BroadcastServer.start (   self)

Start the broadcast server in a new thread.

Returns
True or False
108 def start(self):
109 r"""!Start the broadcast server in a new thread
110
111 @return True or False"""
112 if not self.isRunning:
113 logging.debug("start udp broadcast server")
114 self._serverThread = threading.Thread(target=self._listen)
115 self._serverThread.name = "BroadServ"
116 self._serverThread.daemon = True
117 self._serverShutdown = False
118 self._serverThread.start()
119 return True
120 logging.warning("udp broadcast server always started")
121 return True
122

◆ stop()

boswatch.network.broadcast.BroadcastServer.stop (   self)

Stop the broadcast server.

Due to the timeout of the socket, stopping the thread can be delayed by two seconds. But function returns immediately.

Returns
True or False
123 def stop(self):
124 r"""!Stop the broadcast server
125
126 Due to the timeout of the socket,
127 stopping the thread can be delayed by two seconds.
128 But function returns immediately.
129
130 @return True or False"""
131
132 if self.isRunning:
133 logging.debug("stop udp broadcast server")
134 self._serverShutdown = True
135 return True
136 else:
137 logging.warning("udp broadcast server always stopped")
138 return True
139

◆ _listen()

boswatch.network.broadcast.BroadcastServer._listen (   self)
protected

Broadcast server worker thread.

This function listen for magic packets on broadcast address and send the connection info to the clients.

  • listen for the magic packet <BW-Request>
  • send connection info in an <BW-Result> macig packet
140 def _listen(self):
141 r"""!Broadcast server worker thread
142
143 This function listen for magic packets on broadcast
144 address and send the connection info to the clients.
145
146 - listen for the magic packet <BW-Request>
147 - send connection info in an <BW-Result> macig packet"""
148 logging.debug("start listening for magic")
149 while not self._serverShutdown:
150 try:
151 payload, address = self._socket.recvfrom(1024)
152 payload = str(payload, "UTF-8")
153 if payload == "<BW3-Request>":
154 logging.debug("received magic <BW3-Request> from: %s", address[0])
155 logging.info("send connection info in magic <BW3-Result> to: %s", address[0])
156 self._socket.sendto("<BW3-Result>;".encode() + str(self._servePort).encode(), address)
157 except socket.timeout:
158 continue # timeout is accepted (not block at recvfrom())
159 self._serverThread = None
160 logging.debug("udp broadcast server stopped")
161

◆ isRunning()

boswatch.network.broadcast.BroadcastServer.isRunning (   self)

Property of broadcast server running state.

163 def isRunning(self):
164 r"""!Property of broadcast server running state"""
165 if self._serverThread:
166 return True
167 return False

Field Documentation

◆ _socket

boswatch.network.broadcast.BroadcastServer._socket
protected

◆ _serverThread

boswatch.network.broadcast.BroadcastServer._serverThread
protected

◆ _serverShutdown

boswatch.network.broadcast.BroadcastServer._serverShutdown
protected

◆ _servePort

boswatch.network.broadcast.BroadcastServer._servePort
protected