def test_close(Poller, ipv6): m = Manager() + Poller() server = Server() + UDPServer(0) server.register(m) m.start() try: assert pytest.wait_for(server, "ready") wait_host(server) host, port = server.host, server.port server.fire(close()) assert pytest.wait_for(server, "disconnected") server.unregister() def test(obj, attr): return attr not in obj.components assert pytest.wait_for(m, server, value=test) server = Server() + UDPServer((host, port)) server.register(m) assert pytest.wait_for(server, "ready", timeout=30.0) finally: m.stop()
def bind(request, manager, watcher): server = UDPServer(0).register(manager) assert watcher.wait("ready") host, port = server.host, server.port server.fire(close()) assert watcher.wait("closed") server.unregister() assert watcher.wait("unregistered") return host, port
def bind(manager, watcher): server = UDPServer(0).register(manager) assert watcher.wait('ready', channel='server') host, port = server.host, server.port server.fire(close()) assert watcher.wait('closed', channel='server') server.unregister() assert watcher.wait('unregistered', channel='server') return host, port
def test_basic(Poller, ipv6): m = Manager() + Poller() if ipv6: udp_server = UDP6Server(("::1", 0)) udp_client = UDP6Client(("::1", 0), channel="client") else: udp_server = UDPServer(0) udp_client = UDPClient(0, channel="client") server = Server() + udp_server client = Client() + udp_client server.register(m) client.register(m) m.start() try: assert pytest.wait_for(server, "ready") assert pytest.wait_for(client, "ready") wait_host(server) client.fire(write((server.host, server.port), b"foo")) assert pytest.wait_for(server, "data", b"foo") client.fire(close()) assert pytest.wait_for(client, "closed") server.fire(close()) assert pytest.wait_for(server, "closed") finally: m.stop()
def __init__(self, nodes=[], transport=None, **kwargs): super(Bridge, self).__init__(**kwargs) self.nodes = set(nodes) if transport: self.transport = transport else: self.transport = DefaultTransport(**kwargs) if isinstance(self.transport, DefaultTransport): self.write = self.udp elif isinstance(self.transport, Server): self.write = self.server elif isinstance(self.transport, Client): self.write = self.client else: raise TypeError("Unsupported transport type") if isinstance(self.transport, Client): self.server = False else: self.server = True self += self.transport if hasattr(self.transport, "address"): if self.transport.address in ["", "0.0.0.0"]: address = gethostbyname(gethostname()) self.ourself = (address, self.transport.port) else: self.ourself = self.transport.bind else: self.ourself = (gethostbyname(gethostname()), 0)
def init(self, bind=None, verbose=False): self.bind = bind or ("0.0.0.0", 53) if verbose: Debugger().register(self) self.transport = UDPServer(self.bind).register(self) self.protocol = DNS().register(self) self.dummy = Dummy().register(self)
def __init__(self, host="0.0.0.0", port=1338, channel=channel): super(Node, self).__init__(channel=channel) self.host = host self.port = port # Peers we keep track of self.peers = set() UDPServer((self.host, self.port), channel=self.channel).register(self)
def bind(request, manager, watcher): server = UDPServer(0).register(manager) assert watcher.wait('ready') host, port = server.host, server.port server.fire(close()) assert watcher.wait('closed') server.unregister() assert watcher.wait('unregistered') return host, port
def init(self, args, db, hosts, logger): self.args = args self.db = db self.hosts = hosts self.logger = logger self.bind = args.bind self.forward = args.forward self.peers = {} self.requests = {} self.cache = LRUCache(maxsize=args.cachesize) if args.daemon: Daemon(args.pidfile).register(self) if args.debug: Debugger(events=args.verbose, logger=logger).register(self) self.transport = UDPServer(self.bind).register(self) self.protocol = DNS().register(self)
def init(self, bind=None, verbose=False): self.bind = bind or (serverIP, PORT) self.transport = UDPServer(self.bind).register(self) self.protocol = DNS().register(self) self.dummy = Dummy().register(self)
class Bridge(Component): channel = "bridge" IgnoreEvents = [Read, Write, Error, Close] IgnoreChannels = [] def __init__(self, nodes=[], transport=None, **kwargs): super(Bridge, self).__init__(**kwargs) self.nodes = set(nodes) if transport: self.transport = transport else: self.transport = DefaultTransport(**kwargs) if isinstance(self.transport, DefaultTransport): self.write = self.udp elif isinstance(self.transport, Server): self.write = self.server elif isinstance(self.transport, Client): self.write = self.client else: raise TypeError("Unsupported transport type") if isinstance(self.transport, Client): self.server = False else: self.server = True self += self.transport if hasattr(self.transport, "address"): if self.transport.address in ["", "0.0.0.0"]: address = gethostbyname(gethostname()) self.ourself = (address, self.transport.port) else: self.ourself = self.transport.bind else: self.ourself = (gethostbyname(gethostname()), 0) def registered(self, c, m): if c == self: self.push(Helo(*self.ourself), "helo") @handler(filter=True, priority=100) def event(self, event, *args, **kwargs): channel = event.channel if True in [event.name == x.__name__ for x in self.IgnoreEvents]: return elif channel in self.IgnoreChannels: return elif getattr(event, "ignore", False): return else: event.ignore = True event.source = self.ourself try: s = dumps(event, -1) + "\x00\x00" except: return self.write(channel, event, s) def udp(self, channel, e, s): if self.nodes: for node in self.nodes: self.transport.write(node, s) else: target, channel = channel if type(target) is tuple: if type(target[0]) is int: node = ("<broadcast>", target[0]) elif type(target[0]) is str and ":" in target[0]: address, port = target[0].split(":", 1) port = int(port) node = (address, port) elif type(target[0]) is str: node = (target[0], self.transport.port) elif type(target[0]) is int: node = (self.transport.address, target[0]) else: raise TypeError("Invalid bridge target!") else: node = ("<broadcast>", self.transport.port) self.transport.write(node, s) def server(self, channel, e, s): self.transport.broadcast(s) def client(self, channel, e, s): self.transport.write(s) @handler("helo", filter=True) def helo(self, event, address, port): source = event.source if (address, port) == self.ourself or source in self.nodes: return True if not (address, port) in self.nodes: self.nodes.add((address, port)) self.push(Helo(*self.ourself)) def read(self, *args): if len(args) == 1: data = args[0] else: data = args[1] data = data.split("\x00\x00") for d in data: if d: self.push_event(loads(d)) def push_event(self, event): (target, channel) = event.channel source = event.source if type(target) is tuple: if len(target) == 2: target = target[1] else: target = "*" self.send(event, channel, target)
def init(self, bind=("0.0.0.0", 6666)): self.bind = bind self.transport = UDPServer(self.bind).register(self) self.protocol = DNS().register(self) self.handler = ProcessQuery().register(self)
def init(self): self.clients = {} self.tcp = TCPServer(("0.0.0.0", 7001), channel="tcp").register(self) self.udp = UDPServer(("0.0.0.0", 7000), channel="udp").register(self)