mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2025-12-06 07:12:04 +01:00
add intern list of all connected clients in server
This commit is contained in:
parent
b23cc40e4a
commit
c89e6ef28c
|
|
@ -21,7 +21,10 @@ import time
|
||||||
|
|
||||||
logging.debug("- %s loaded", __name__)
|
logging.debug("- %s loaded", __name__)
|
||||||
|
|
||||||
_clients = [] # module wide global list for received data sets
|
_dataPackets = [] # module wide global list for received data sets
|
||||||
|
_lockDataPackets = threading.Lock()
|
||||||
|
|
||||||
|
_clients = {}
|
||||||
_lockClients = threading.Lock()
|
_lockClients = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -32,7 +35,11 @@ class TCPHandler(socketserver.BaseRequestHandler):
|
||||||
"""!Handles the request from an single client in a own thread
|
"""!Handles the request from an single client in a own thread
|
||||||
|
|
||||||
Insert a request in the clients[] list and send a [ack]"""
|
Insert a request in the clients[] list and send a [ack]"""
|
||||||
data = 1
|
with _lockClients:
|
||||||
|
_clients[threading.current_thread().name] = {"address": self.client_address[0], "timestamp": time.time()}
|
||||||
|
|
||||||
|
logging.info("Client connected: %s", self.client_address[0])
|
||||||
|
data = 1 # to enter while loop
|
||||||
cur_thread = threading.current_thread().name
|
cur_thread = threading.current_thread().name
|
||||||
req_name = str(cur_thread) + " " + self.client_address[0]
|
req_name = str(cur_thread) + " " + self.client_address[0]
|
||||||
|
|
||||||
|
|
@ -44,10 +51,8 @@ class TCPHandler(socketserver.BaseRequestHandler):
|
||||||
|
|
||||||
# add a new entry at first position (index 0) with client IP
|
# add a new entry at first position (index 0) with client IP
|
||||||
# and the decoded data dict as an string in utf-8 and an timestamp
|
# and the decoded data dict as an string in utf-8 and an timestamp
|
||||||
# lock.acquire() # todo check if needed - only append not modify data
|
with _lockDataPackets:
|
||||||
with _lockClients:
|
_dataPackets.insert(0, (self.client_address[0], data, time.time())) # time() to calc time in queue
|
||||||
_clients.insert(0, (self.client_address[0], data, time.time())) # time() to calc time in queue
|
|
||||||
# lock.release() # todo check if needed
|
|
||||||
logging.debug("Add data to queue")
|
logging.debug("Add data to queue")
|
||||||
|
|
||||||
logging.debug(req_name + " send: [ack]")
|
logging.debug(req_name + " send: [ack]")
|
||||||
|
|
@ -58,6 +63,10 @@ class TCPHandler(socketserver.BaseRequestHandler):
|
||||||
logging.debug(req_name + " connection closed")
|
logging.debug(req_name + " connection closed")
|
||||||
except: # pragma: no cover
|
except: # pragma: no cover
|
||||||
logging.exception(req_name + " error while receiving")
|
logging.exception(req_name + " error while receiving")
|
||||||
|
finally:
|
||||||
|
with _lockClients:
|
||||||
|
del _clients[threading.current_thread().name]
|
||||||
|
logging.info("Client disconnected: %s", self.client_address[0])
|
||||||
|
|
||||||
|
|
||||||
class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
|
@ -115,16 +124,18 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clientsConnected():
|
def countClientsConnected():
|
||||||
"""!Number of currently connected Clients
|
"""!Number of currently connected Clients
|
||||||
|
|
||||||
@todo works not safe atm
|
|
||||||
@return Connected clients"""
|
@return Connected clients"""
|
||||||
if threading.active_count() > 2:
|
with _lockClients:
|
||||||
# must subtract the server() and the serve() Thread
|
return len(_clients)
|
||||||
return threading.active_count() - 2
|
|
||||||
else:
|
@staticmethod
|
||||||
return 0
|
def getClientsConnected():
|
||||||
|
# todo insert comment
|
||||||
|
# todo return full list or write a print/debug method?
|
||||||
|
return _clients
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def getData():
|
def getData():
|
||||||
|
|
@ -132,11 +143,9 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
must be polled by main program
|
must be polled by main program
|
||||||
|
|
||||||
@return Next data packet.py from intern queue"""
|
@return Next data packet.py from intern queue"""
|
||||||
if _clients:
|
if _dataPackets:
|
||||||
# lock.acquire() # todo check if needed - here is a modify of the list?
|
with _lockDataPackets:
|
||||||
with _lockClients:
|
message = _dataPackets.pop()
|
||||||
message = _clients.pop()
|
|
||||||
# lock.release() # todo check if needed
|
|
||||||
logging.debug("Get data from queue")
|
logging.debug("Get data from queue")
|
||||||
return message
|
return message
|
||||||
return None
|
return None
|
||||||
|
|
@ -146,13 +155,11 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
"""!Get packets waiting in queue
|
"""!Get packets waiting in queue
|
||||||
|
|
||||||
@return Packets in queue"""
|
@return Packets in queue"""
|
||||||
return len(_clients) # no lock needed - only reading
|
return len(_dataPackets) # no lock needed - only reading
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def flushData():
|
def flushData():
|
||||||
"""!To flush all existing data in queue"""
|
"""!To flush all existing data in queue"""
|
||||||
logging.debug("Flush data queue")
|
logging.debug("Flush data queue")
|
||||||
# lock.acquire() # todo check if needed - here is a modify?
|
with _lockDataPackets:
|
||||||
with _lockClients:
|
_dataPackets.clear()
|
||||||
_clients.clear()
|
|
||||||
# lock.release() # todo check if needed
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue