def onChannelOpen(self, channel): wrapper = sync.DispatchWrapper(channel) self.locals["channel"] = wrapper self.locals["disconnect"] = wrapper.close self.locals["cmd"] = sync.CommandControl(channel, interactive=True) self.locals["events"] = event.EventRecorder(channel) protocol.invokeAndWait(protocol.removeChannelOpenListener, self) wrapper.addChannelListener(self)
def __init__(self): locals = { # @ReservedAssignment "connect": tcf.connect, "peers": print_peers() } protocol.startEventQueue() protocol.startDiscovery() protocol.invokeAndWait(protocol.addChannelOpenListener, self) code.InteractiveConsole.__init__(self, locals)
def run(self): while service._alive: try: time.sleep(locator.DATA_RETENTION_PERIOD / 4 / 1000.) protocol.invokeAndWait(self._callable) except RuntimeError: # TCF event dispatch is shut down return except Exception as x: service._log("Unhandled exception in TCF discovery timer thread", x)
def test(): global _services protocol.startEventQueue() atexit.register(protocol.getEventQueue().shutdown) # testTimer() try: c = tcf.connect("TCP:127.0.0.1:1534") except Exception as e: protocol.log(e) sys.exit() assert c.state == channel.STATE_OPEN if __TRACE: protocol.invokeAndWait(c.addTraceListener, TraceListener()) _services = protocol.invokeAndWait(c.getRemoteServices) print "services=", _services if "RunControl" in _services: # RunControl must be first _services.remove("RunControl") _services.insert(0, "RunControl") for service in _services: testFct = globals().get("test" + service) if testFct: print "Testing service '%s'..." % service try: testFct(c) print "Completed test of service '%s'." % service except Exception as e: protocol.log("Exception testing %s" % service, e) else: print "No test for service '%s' found." % service try: testSyncCommands(c) testTasks(c) testEvents(c) testDataCache(c) except Exception as e: protocol.log(e) if c.state == channel.STATE_OPEN: time.sleep(5) protocol.invokeLater(c.close) time.sleep(2)
def test(): global _services protocol.startEventQueue() atexit.register(protocol.getEventQueue().shutdown) # testTimer() try: c = tcf.connect("TCP:127.0.0.1:1534") except Exception as e: protocol.log(e) sys.exit() assert c.state == channel.STATE_OPEN if __TRACE: protocol.invokeAndWait(c.addTraceListener, TraceListener()) _services = sorted(protocol.invokeAndWait(c.getRemoteServices)) print("services=" + str(_services)) if "RunControl" in _services: # RunControl must be first _services.remove("RunControl") _services.insert(0, "RunControl") for service in _services: testFct = globals().get("test" + service) if testFct: print("Testing service '%s'..." % service) try: testFct(c) print("Completed test of service '%s'." % service) except Exception as e: protocol.log("Exception testing %s" % service, e) else: print("No test for service '%s' found." % service) try: testSyncCommands(c) testTasks(c) testEvents(c) testDataCache(c) except Exception as e: protocol.log(e) if c.state == channel.STATE_OPEN: time.sleep(5) protocol.invokeLater(c.close) time.sleep(2)
def run(self): try: while service._alive: sock = service.socket try: data, addr = sock.recvfrom(MAX_PACKET_SIZE) p = InputPacket(data, InetAddress(None, addr[0]), addr[1]) protocol.invokeAndWait(self._callable, p) except RuntimeError: # TCF event dispatch is shutdown return except socket.error as x: if sock != service.socket: continue # frequent error on windows, unknown reason if x.errno == 10054: continue port = sock.getsockname()[1] service._log("Cannot read from datagram socket at port %d" % port, x) time.sleep(2) except Exception as x: service._log("Unhandled exception in socket reading thread", x)
def __getattr__(self, attr): val = getattr(self._channel, attr, None) if val: if self._interactive and type(val) in (types.FunctionType, types.MethodType): val = DispatchWrapper(val) return val services = protocol.invokeAndWait(self._channel.getRemoteServices) if attr == "services": return services if attr in services: return ServiceWrapper(self, attr) raise AttributeError("Unknown service: %s. Use one of %s" % (attr, services))
def invoke(self, service, command, *args, **kwargs): cmd = None if not protocol.isDispatchThread(): if not kwargs.get("async"): cmd = protocol.invokeAndWait(self._invoke, service, command, *args, **kwargs) if cmd and self._interactive: return cmd.getE() else: with self._lock: self._queue.append((service, command, args, kwargs)) if len(self._queue) == 1: protocol.invokeLater(self._processQueue) return return cmd
def __call__(self, *args, **kwargs): return protocol.invokeAndWait(self.inner, *args, **kwargs)
def __getattr__(self, attr): val = protocol.invokeAndWait(getattr, self.inner, attr) if type(val) in (types.FunctionType, types.MethodType): return DispatchWrapper(val) return val
def cancel(self): return protocol.invokeAndWait(self.token.cancel)