Changeset 56:1d491f271eaf in xplra for src


Ignore:
Timestamp:
03/07/13 16:48:41 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented a more proper handling of a broken connection in the Python client

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/client/python/xplra.py

    r53 r56  
    133133        """Construct the object."""
    134134        self._stream = None
     135        self._multiBuffers = []
    135136
    136137    def connect(self):
     
    147148            self._stream = s.makefile()
    148149
     150        for multiBuffer in self._multiBuffers:
     151            multiBuffer._reregister()
     152
    149153    @property
    150154    def isConnected(self):
     
    154158    def createMultiGetter(self):
    155159        """Create a new multi-dataref getter for this X-Plane object."""
    156         return MultiGetter(self)
     160        multiGetter = MultiGetter(self)
     161        self._multiBuffers.append(multiGetter)
     162        return multiGetter
    157163
    158164    def createMultiSetter(self):
    159165        """Create a new multi-dataref setter for this X-Plane object."""
    160         return MultiSetter(self)
     166        multiSetter = MultiSetter(self)
     167        self._multiBuffers.append(multiSetter)
     168        return multiSetter
     169
     170    def destroyMultiBuffer(self, multiBuffer):
     171        """Destroy the given multi-dataref buffer.
     172
     173        It actually removes it from the list of multiple buffers and
     174        unregisters it safely.
     175
     176        Returns the result of the safe unregistration if the buffer was found,
     177        None otherwise.
     178        """
     179        if multiBuffer in self._multiBuffers:
     180            self._multiBuffers.remove(multiBuffer)
     181            return multiBuffer.unregisterSafely()
     182        else:
     183            return None
    161184
    162185    def getVersions(self):
     
    283306        """Disconnect from X-Plane."""
    284307        if self._stream is not None:
    285             self._stream.close()
    286             self._stream = None
     308            try:
     309                self._stream.close()
     310            finally:
     311                self._stream = None
    287312
    288313    def _checkResult(self, resultCode = None, parameter = None, multi = False):
     
    484509        return self._values
    485510
     511    @property
     512    def wasRegistered(self):
     513        """Determine if this buffer was registered."""
     514        return self._wasRegistered
     515
    486516    def addInt(self, name):
    487517        """Add an integer to the buffer with the given name
     
    544574            self._xplane._flush()
    545575            self._xplane._checkResult()
     576            self._registeredID = None
     577
     578    def unregisterSafely(self):
     579        """Unregister the buffer from X-Plane, ignoring exceptions.
     580
     581        Returns True if the unregistration succeeded, False otherwise.
     582        """
     583        try:
     584            self.unregister()
     585            return True
     586        except:
     587            return False
     588        finally:
    546589            self._registeredID = None
    547590
     
    576619            s += chr(c)
    577620        return s
     621
     622    def _reregister(self):
     623        """Re-register the buffer in X-Plane, if it has been registered earlier
     624
     625        If it has not been registered, nothing is done. Otherwise the buffer
     626        gets registered, and the old ID is forgotten. This function is meant to
     627        be used by the XPlane object when it creates a new connection. If the
     628        registration fails, the original ID is restored, so the _reregister()
     629        could be called again
     630        """
     631        if self._registeredID is not None:
     632            origRegisteredID = self._registeredID
     633            try:
     634                self._registeredID = None
     635                self.register()
     636            except:
     637                self._registeredID = origRegisteredID
     638                raise
    578639
    579640    def _add(self, name, type, length = None, offset = None):
Note: See TracChangeset for help on using the changeset viewer.