mirror of
https://github.com/BOSWatch/BW3-Core.git
synced 2026-04-21 06:03:50 +00:00
insert lock-methods into server
This commit is contained in:
parent
d08e11e17e
commit
a955058edd
1 changed files with 12 additions and 1 deletions
|
|
@ -17,6 +17,7 @@
|
||||||
import logging
|
import logging
|
||||||
import socketserver
|
import socketserver
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
logging.debug("- %s loaded", __name__)
|
logging.debug("- %s loaded", __name__)
|
||||||
|
|
||||||
|
|
@ -30,6 +31,7 @@ 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]"""
|
||||||
|
clientsLock = threading.Lock() # todo check if needed
|
||||||
data = 1
|
data = 1
|
||||||
cur_thread = threading.current_thread()
|
cur_thread = threading.current_thread()
|
||||||
req_name = str(cur_thread) + " " + self.client_address[0]
|
req_name = str(cur_thread) + " " + self.client_address[0]
|
||||||
|
|
@ -42,7 +44,9 @@ 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 the decoded data dict as an string in utf-8
|
||||||
_clients.insert(0, (self.client_address[0], data))
|
clientsLock.acquire() # todo check if needed - only append not modify data
|
||||||
|
_clients.insert(0, (self.client_address[0], data, time.time())) # time() to calc time in queue
|
||||||
|
clientsLock.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]")
|
||||||
|
|
@ -81,6 +85,7 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
self.flushData()
|
self.flushData()
|
||||||
|
|
||||||
self._server_thread = threading.Thread(target=self._server.serve_forever)
|
self._server_thread = threading.Thread(target=self._server.serve_forever)
|
||||||
|
self._server_thread.name = "Thread-BWServer"
|
||||||
self._server_thread.daemon = True
|
self._server_thread.daemon = True
|
||||||
self._server_thread.start()
|
self._server_thread.start()
|
||||||
logging.debug("TCPServer started in Thread: " + self._server_thread.name)
|
logging.debug("TCPServer started in Thread: " + self._server_thread.name)
|
||||||
|
|
@ -127,7 +132,10 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
|
||||||
@return Next data packet.py from intern queue"""
|
@return Next data packet.py from intern queue"""
|
||||||
if _clients:
|
if _clients:
|
||||||
|
clientsLock = threading.Lock() # todo check if needed
|
||||||
|
clientsLock.acquire() # todo check if needed - only append not modify data
|
||||||
message = _clients.pop()
|
message = _clients.pop()
|
||||||
|
clientsLock.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
|
||||||
|
|
@ -136,4 +144,7 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
def flushData():
|
def flushData():
|
||||||
"""!To flush all existing data in queue"""
|
"""!To flush all existing data in queue"""
|
||||||
logging.debug("Flush client data queue")
|
logging.debug("Flush client data queue")
|
||||||
|
clientsLock = threading.Lock() # todo check if needed
|
||||||
|
clientsLock.acquire() # todo check if needed - here is a modify?
|
||||||
_clients.clear()
|
_clients.clear()
|
||||||
|
clientsLock.release() # todo check if needed
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue