Skip to content

Commit

Permalink
Merge pull request #5 from wortelus/locking
Browse files Browse the repository at this point in the history
locking for updatingRREFdict parameter
  • Loading branch information
leleopard authored Oct 2, 2022
2 parents 254b6d4 + 5dbba6e commit 9c74637
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions pyxpudpserver/XPlaneUDPServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class XPlaneUDPServer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = True
self.updatingRREFdict = False
self.updatingRREFdict = threading.Lock()
self.statusMsg = "Not connected"
self.haveWeEverBeenConnectedToXP = False

Expand Down Expand Up @@ -204,7 +204,7 @@ def disableRedirectUDPtoXP(self):
Disables the redirection of traffic received by the class to XPlane
"""

self.RDRCT_TRAFFIC = False
try:
if self.DataRCVsock is not False:
Expand Down Expand Up @@ -279,30 +279,28 @@ def requestXPDref(self, dataref):
"""

self.updatingRREFdict = True

if self.XPAddress is not None: # check if we have XPlane's IP, if so continue, else log an error
if dataref in self.datarefsDict: # if the dataref has already been requested, then return, no need to do anything
logger.debug("dataref has already been requested")
return
else:
logger.debug("Requesting dataref %s", dataref)
self.datarefsDict[dataref] = 0.0 # initialise a new key for this dataref
index = len(self.datarefsDict)-1 # give it an index
self.datarefsIndices[index] = ['',None]
self.datarefsIndices[index][0] = dataref
with self.updatingRREFdict:
if self.XPAddress is not None: # check if we have XPlane's IP, if so continue, else log an error
if dataref in self.datarefsDict: # if the dataref has already been requested, then return, no need to do anything
logger.debug("dataref has already been requested")
return
else:
logger.debug("Requesting dataref %s", dataref)
self.datarefsDict[dataref] = 0.0 # initialise a new key for this dataref
index = len(self.datarefsDict)-1 # give it an index
self.datarefsIndices[index] = ['',None]
self.datarefsIndices[index][0] = dataref

RREF_Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#RREF_Sock.setblocking(0)
self.datarefsIndices[index][1] = RREF_Sock
RREF_Sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#RREF_Sock.setblocking(0)
self.datarefsIndices[index][1] = RREF_Sock

logger.debug("datarefs Dict: %s", self.datarefsDict)
logger.debug("datarefs Indices: %s", self.datarefsIndices)
logger.debug("datarefs Dict: %s", self.datarefsDict)
logger.debug("datarefs Indices: %s", self.datarefsIndices)

self.RREF_sockets.append(RREF_Sock)
self.RREF_sockets.append(RREF_Sock)

self.__sendRREFmessage(index)
self.updatingRREFdict = False
self.__sendRREFmessage(index)


def __sendRREFmessage(self, index):
Expand Down Expand Up @@ -340,14 +338,15 @@ def __resubscribeRREFs(self):
Private do not call - attempt to re subscribe all the RREFs previously subscribed (if XPlane was restarted for example)
"""
if len(self.datarefsIndices) > 0 and self.updatingRREFdict == False:
logger.info("Attempt to re subscribe datarefs with XPlane")
if PYTHON_VERSION == 2:
for index, RREF in self.datarefsIndices.iteritems():
self.__sendRREFmessage(index)
else:
for index, RREF in self.datarefsIndices.items():
self.__sendRREFmessage(index)
if len(self.datarefsIndices) > 0:
with self.updatingRREFdict:
logger.info("Attempt to re subscribe datarefs with XPlane")
if PYTHON_VERSION == 2:
for index, RREF in self.datarefsIndices.iteritems():
self.__sendRREFmessage(index)
else:
for index, RREF in self.datarefsIndices.items():
self.__sendRREFmessage(index)

def sendXPCmd(self, command, sendContinuous = False):
"""
Expand Down Expand Up @@ -428,11 +427,11 @@ def sendXPDref(self, dataref, index = 0, value = 0.0):
val = None
try:
val = float(value)
except:
except:
val = None

if (self.XPAddress is not None) and (val is not None):

dataref += '['+str(index)+']'

bytesval = pack('<f',val)
Expand Down Expand Up @@ -476,7 +475,7 @@ def run(self):
"""

lasttimeXPbeaconreceived = time.time()

while self.running:
current_time = time.time()
#print(self.statusMsg)
Expand Down Expand Up @@ -535,7 +534,7 @@ def run(self):
# Process incoming RREF dataref data
##---------------------------------------------------
try:
if self.updatingRREFdict == False: # to avoid having issues with dictionary iteration
with self.updatingRREFdict: # to avoid having issues with dictionary iteration

for index, RREF in self.datarefsIndices.items():
dataref = RREF[0]
Expand All @@ -545,7 +544,7 @@ def run(self):
index = unpack('<i', rrefdata[5:9])[0]
value = unpack('<f', rrefdata[9:13])[0]

self.datarefsDict[dataref] = value
self.datarefsDict[dataref] = value

except socket.error as msg: pass

Expand Down

0 comments on commit 9c74637

Please sign in to comment.