def run(self): """Override Thread.run() ; define behaviour for the connections manager thread For each SpecConnection object in the connections dictionary, try to make a connection. If the connection is already established, nothing occurs. Poll the asyncore dispatchers for processing socket events. """ self.__started = True while not self.stopEvent.isSet(): self.lock.acquire() try: connection_dispatcher_keys = self.connectionDispatchers.keys() for k in connection_dispatcher_keys: connection = self.connectionDispatchers.get(k) if connection is not None: connection.makeConnection() if self.stopEvent.isSet(): break finally: self.lock.release() if len(self.connectionDispatchers) > 0: asyncore.loop(0.01, False, None, 1) if self.doEventsDispatching: SpecEventsDispatcher.dispatch() else: time.sleep(0.01) asyncore.loop(0.01, False, None, 1)
def connectToSpec(self, specVersion, timeout=200): if self.connection is not None: SpecEventsDispatcher.disconnect(self.connection, 'connected', self.connected) SpecEventsDispatcher.disconnect(self.connection, 'disconnected', self.disconnected) self.connection = SpecConnectionsManager.SpecConnectionsManager().getConnection(specVersion) self.specVersion = specVersion SpecEventsDispatcher.connect(self.connection, 'connected', self.connected) cb = self.__callbacks.get("connected") if cb is not None: cb = cb() SpecEventsDispatcher.connect(self.connection, 'connected', cb) SpecEventsDispatcher.connect(self.connection, 'disconnected', self.disconnected) cb = self.__callbacks.get("disconnected") if cb is not None: cb = cb() SpecEventsDispatcher.connect(self.connection, 'disconnected', cb) self.connection.registerChannel("status/ready", self.statusChanged) cb = self.__callbacks.get("statusChanged") if cb is not None: cb = cb() SpecEventsDispatcher.connect(self.connection, 'statusChanged', cb) if self.connection.isSpecConnected(): self.connected() else: try: SpecWaitObject.waitConnection(self.connection, timeout) except SpecClientTimeoutError: pass SpecEventsDispatcher.dispatch()
def wait(self): start_wait = time.time() while self.reply_pending: elapsed = time.time() - start_wait if elapsed > self.timeout: raise SpecClientTimeoutError("timeout waiting for command execution") SpecEventsDispatcher.dispatch() time.sleep(0.02)
def poll(self, timeout=0.01): """Poll the asynchronous socket connections and dispatch incomming events""" fd = self.getFdDispatchersDict() if -1 not in fd: asyncore.loop(timeout, False, fd, 1) else: pass SpecEventsDispatcher.dispatch()
def specDisconnected(self): """Emit the 'disconnected' signal when the remote Spec version is disconnected.""" SpecEventsDispatcher.dispatch() old_state = self.state self.state = DISCONNECTED if old_state == CONNECTED: logging.getLogger('SpecClient').info('Disconnected from %s:%s', self.host, (self.scanport and self.scanname) or self.port) SpecEventsDispatcher.emit(self, 'disconnected', ())
def specDisconnected(self): """Emit the 'disconnected' signal when the remote Spec version is disconnected.""" SpecEventsDispatcher.dispatch() old_state = self.state self.state = DISCONNECTED if old_state == CONNECTED: log.info('Disconnected from %s:%s', self.host, (self.scanport and self.scanname) or self.port) SpecEventsDispatcher.emit(self, 'disconnected', ())
def waitFunc(timeout): """Waiting function Arguments: timeout -- waiting time in milliseconds """ try: P = getattr(SpecConnectionsManager.SpecConnectionsManager(), "poll") except AttributeError: time.sleep(timeout / 1000.0) SpecEventsDispatcher.dispatch() else: P(timeout / 1000.0)
def connectToSpec(self, specVersion, timeout=200): if self.connection is not None: SpecEventsDispatcher.disconnect(self.connection, 'connected', self._connected) SpecEventsDispatcher.disconnect(self.connection, 'disconnected', self._disconnected) self.connection = SpecConnectionsManager().getConnection(specVersion) self.specVersion = specVersion SpecEventsDispatcher.connect(self.connection, 'connected', self._connected) SpecEventsDispatcher.connect(self.connection, 'disconnected', self._disconnected) if self.connection.isSpecConnected(): self._connected() else: try: waitConnection(self.connection, timeout) except SpecClientTimeoutError: pass SpecEventsDispatcher.dispatch()
def poll(self, timeout=0.01): """Poll the asynchronous socket connections and dispatch incomming events""" connection_dispatcher_keys = self.connectionDispatchers.keys() for k in connection_dispatcher_keys: connection = self.connectionDispatchers.get(k) if connection is not None: connection.makeConnection() connection_dispatchers = {} for condis in self.connectionDispatchers.itervalues(): if condis.socket is not None: try: connection_dispatchers[condis.socket.fileno()]=condis except: # BAD FILE DESCRIPTOR? continue asyncore.loop(timeout, False, connection_dispatchers, 1) SpecEventsDispatcher.dispatch()
def waitConnection(self, timeout = None): """Wait for the connection to Spec being established Arguments: timeout -- optional timeout (defaults to None) Exceptions: timeout -- raise a timeout exception on timeout """ connection = self.connection() if connection is not None: t = 0 while self.isdisconnected: SpecEventsDispatcher.dispatch() t0 = time.time() waitFunc(10) t += (time.time() - t0)*1000 if timeout is not None and t >= timeout: raise SpecClientTimeoutError
def wait(self, waitValue = None, timeout = None): """Block until the object's internal value gets updated Arguments: waitValue -- particular value to wait (defaults to None, meaning any value) timeout -- optional timeout (defaults to None) Exceptions: timeout -- raise a timeout exception on timeout """ t = 0 while not self.isdisconnected: SpecEventsDispatcher.dispatch() if self.value is not None: if waitValue is None: return if waitValue == self.value: return else: self.value = None if self.value is None: t0 = time.time() waitFunc(10) # 10 ms. t += (time.time() - t0)*1000 if timeout is not None and t >= timeout: raise SpecClientTimeoutError try: P = getattr(SpecConnectionsManager.SpecConnectionsManager(), "poll") except AttributeError: pass else: P()
def poll(self, timeout=0.01): """Poll the asynchronous socket connections and dispatch incomming events""" asyncore.loop(timeout, False, self.getFdDispatchersDict(), 1) SpecEventsDispatcher.dispatch()
def update(self, timeout=0.01): self.dispatcher.makeConnection() if self.dispatcher.socket is not None: fds = {self.dispatcher.socket.fileno(): self.dispatcher} asyncore.loop(timeout, False, fds, 1) SpecEventsDispatcher.dispatch()
def update_events(self): SpecEventsDispatcher.dispatch()