예제 #1
0
    def startService(self):
        """Connect to labrad in a loop, reconnecting after connection loss."""
        self.running = True
        while self.running:
            self.report('Connecting...')
            try:
                dv = DataVaultMultiHead(self.host, self.port, self.password,
                                        self.hub, self.session_store)
                self.stop_func = yield self.start(dv)
                self.report('Connected')
                self.connected = True
            except Exception:
                self.report('Data Vault failed to start')
                traceback.print_exc()
            else:
                try:
                    yield dv.onShutdown()
                except Exception:
                    self.report('Disconnected with error')
                    traceback.print_exc()
                else:
                    self.report('Disconnected')
                self.hub.disconnect(dv)
                self.connected = False

            if self.running:
                self.report('Will reconnect in {} seconds...'.format(
                            self.reconnectDelay))
                yield util.wakeupCall(self.reconnectDelay)
예제 #2
0
    def startService(self):
        """Connect to labrad in a loop, reconnecting after connection loss."""
        self.running = True
        while self.running:
            self.report('Connecting...')
            try:
                dv = DataVaultMultiHead(self.host, self.port, self.password,
                                        self.hub, self.session_store)
                self.stop_func = yield self.start(dv)
                self.report('Connected')
                self.connected = True
            except Exception:
                self.report('Data Vault failed to start')
                traceback.print_exc()
            else:
                try:
                    yield dv.onShutdown()
                except Exception:
                    self.report('Disconnected with error')
                    traceback.print_exc()
                else:
                    self.report('Disconnected')
                self.hub.disconnect(dv)
                self.connected = False

            if self.running:
                self.report('Will reconnect in {} seconds...'.format(
                    self.reconnectDelay))
                yield util.wakeupCall(self.reconnectDelay)
예제 #3
0
 def startConnection(self):
     """Attempt to start the data vault and connect to LabRAD."""
     print 'Connecting to %s:%d...' % (self.host, self.port)
     self.server = DataVaultMultiHead(self.host, self.port, self.password, self.hub, self.session_store)
     self.server.onStartup().addErrback(self._error)
     self.server.onShutdown().addCallbacks(self._disconnected, self._error)
     try:
         self.server.configure_tls(self.host, "starttls")
     except AttributeError:
         print "pylabrad doesn't support TLS"
     self.cxn = TCPClient(self.host, self.port, self.server)
     self.addService(self.cxn)
예제 #4
0
class DataVaultConnector(MultiService):
    """Service that connects the Data Vault to a single LabRAD manager

    If the manager is stopped or we lose the network connection,
    this service attempts to reconnect so that we will come
    back online when the manager is back up.
    """
    reconnectDelay = 10

    def __init__(self, host, port, password, hub, session_store):
        MultiService.__init__(self)
        self.host = host
        self.port = port
        self.password = password
        self.hub = hub
        self.session_store = session_store
        self.die = False

    def startService(self):
        MultiService.startService(self)
        self.startConnection()

    def startConnection(self):
        """Attempt to start the data vault and connect to LabRAD."""
        print 'Connecting to %s:%d...' % (self.host, self.port)
        self.server = DataVaultMultiHead(self.host, self.port, self.password, self.hub, self.session_store)
        self.server.onStartup().addErrback(self._error)
        self.server.onShutdown().addCallbacks(self._disconnected, self._error)
        try:
            self.server.configure_tls(self.host, "starttls")
        except AttributeError:
            print "pylabrad doesn't support TLS"
        self.cxn = TCPClient(self.host, self.port, self.server)
        self.addService(self.cxn)

    def _disconnected(self, data):
        print 'Disconnected from %s:%d.' % (self.host, self.port)
        self.hub.disconnect(self.server)
        return self._reconnect()

    def _error(self, failure):
        print failure.getErrorMessage()
        self.hub.disconnect(self.server)
        return self._reconnect()

    def _reconnect(self):
        """Clean up from the last run and reconnect."""
        ## hack: manually clearing the dispatcher...
        #dispatcher.connections.clear()
        #dispatcher.senders.clear()
        #dispatcher._boundMethods.clear()
        ## end hack

        if hasattr(self, 'cxn'):
            self.removeService(self.cxn)
            del self.cxn
        if self.die:
            print "Connecting terminating permanently"
            self.stopService()
            self.disownServiceParent()
            return False
        else:
            reactor.callLater(self.reconnectDelay, self.startConnection)
            print 'Will try to reconnect to %s:%d in %d seconds...' % (self.host, self.port, self.reconnectDelay)