def wtest_interactive(self): b = socket.socket(socket.AF_INET, socket.SOCK_STREAM) b.bind(("localhost",0)) b.listen(1) c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) c.connect(b.getsockname()) s,a = b.accept() server = type("Server", (twink.OpenflowChannel, twink.LoggingChannel), {})() client = type("Client", (twink.OpenflowChannel, twink.LoggingChannel), {})() server.attach(s) client.attach(c) version, oftype, l, xid = twink.parse_ofp_header(server.recv()) assert oftype==0 version, oftype, l, xid = twink.parse_ofp_header(client.recv()) assert oftype==0 client.send(twink.ofp_header_only(2, version=client.version)) version, oftype, l, xid = twink.parse_ofp_header(server.recv()) assert oftype==2 server.send(twink.ofp_header_only(3, version=server.version)) version, oftype, l, xid = twink.parse_ofp_header(client.recv()) assert oftype==3 server.close() client.close() c.close() s.close() b.close()
def __call__(self, message, channel): hdr = twink.parse_ofp_header(message) if hdr[1] == 0: channel.send(twink.ofp_header_only(2, version=channel.version)) channel.send(twink.ofp_header_only(20, version=channel.version)) elif hdr[1] == 21: channel.close()
def test_pair1(self): a,b = twink.sched.socket.socketpair() x = type("ControllerChannelTestCaseX", (twink.OpenflowServerChannel,twink.LoggingChannel), {})(socket=a) x.handle = BarrieredReply() y = twink.ControllerChannel(socket=b) y.handle = lambda msg, ch: None x.start() y.start() xth = twink.sched.spawn(x.loop) result = dict(flag1=0, flag2=0) def cb1(message, ch): result["flag1"] += 1 flag2 = 0 def cb2(message, ch): result["flag2"] += 1 y.recv() y.send(twink.ofp_header_only(2, version=y.version), callback=cb1) y.send(twink.ofp_header_only(2, version=y.version), callback=cb2) y.send(twink.ofp_header_only(20, version=y.version)) with twink.ReadWrapper(y, timeout_pause): y.loop() assert result["flag1"] == 1 assert result["flag2"] == 1 x.close() y.close() xth.join(0.5)
def __call__(self, message, channel): p = twink.parse_ofp_header(message) if p[1] == 20: for msg in self.msgs: q = twink.parse_ofp_header(msg) if q[1] == 2: channel.send(twink.ofp_header_only(3, version=channel.version, xid=q[3])) channel.send(twink.ofp_header_only(21, version=channel.version, xid=p[3])) self.msgs = [] else: self.msgs.append(message)
def wtest_echo(self): global signal_stop # blocking server serv = twink.StreamServer(("localhost",0)) serv.channel_cls = type("Server",(twink.ControllerChannel, twink.AutoEchoChannel, twink.LoggingChannel),{ "accept_versions":[4,], "handle":staticmethod(dummy_handle)}) serv_thread = threading.Thread(target=serv.serve_forever) serv_thread.start() signal_stop = serv.shutdown try: s = create_connection(serv.server_address[:2]) ch = type("Client", (twink.OpenflowChannel, twink.LoggingChannel), {"accept_versions":[4,]})() ch.attach(s) msg = ch.recv() assert msg version, oftype, l, xid = twink.parse_ofp_header(msg) assert version==4 ch.send(twink.ofp_header_only(2, version=4)) msg = ch.recv() assert msg version, oftype, l, xid = twink.parse_ofp_header(msg) assert oftype == 3 except: logging.error("client error", exc_info=True) finally: ch.close() serv.shutdown() serv_thread.join()
def test_pair1(self): a,b = twink.sched.socket.socketpair() x = twink.AutoEchoChannel() x.handle = TypesCapture() y = twink.OpenflowChannel() x.attach(a) y.attach(b) y.recv() y.send(twink.ofp_header_only(2, version=y.version)) y.send(twink.ofp_header_only(5, version=y.version)) with twink.ReadWrapper(x, timeout_pause): x.loop() assert 0 in x.handle.types assert 5 in x.handle.types assert twink.parse_ofp_header(y.recv())[1] == 3 x.close() y.close()
def wtest_interactive(self): b = socket.socket(socket.AF_INET, socket.SOCK_STREAM) b.bind(("localhost", 0)) b.listen(1) c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) c.connect(b.getsockname()) s, a = b.accept() server = type("Server", (twink.OpenflowChannel, twink.LoggingChannel), {})() client = type("Client", (twink.OpenflowChannel, twink.LoggingChannel), {})() server.attach(s) client.attach(c) version, oftype, l, xid = twink.parse_ofp_header(server.recv()) assert oftype == 0 version, oftype, l, xid = twink.parse_ofp_header(client.recv()) assert oftype == 0 client.send(twink.ofp_header_only(2, version=client.version)) version, oftype, l, xid = twink.parse_ofp_header(server.recv()) assert oftype == 2 server.send(twink.ofp_header_only(3, version=server.version)) version, oftype, l, xid = twink.parse_ofp_header(client.recv()) assert oftype == 3 server.close() client.close() c.close() s.close() b.close()
def test_server(self): s = twink.sched.socket.socket(twink.sched.socket.AF_INET, twink.sched.socket.SOCK_STREAM) s.bind(("127.0.0.1", 0)) serv = type("S", (twink.StreamServer,), dict( channel_cls=type("Sc", (twink.AutoEchoChannel, twink.LoggingChannel), dict(handle=staticmethod(lambda a,b:None)))))(s) serv.start() c = twink.sched.socket.socket(twink.sched.socket.AF_INET, twink.sched.socket.SOCK_STREAM) c.connect(s.getsockname()) ch = type("Cc", (twink.OpenflowChannel, twink.LoggingChannel), {})() ch.attach(c) x = ch.recv() assert ch.version ch.send(twink.ofp_header_only(2, version=ch.version)) assert twink.parse_ofp_header(ch.recv())[1] == 3 ch.close() serv.stop()
def test_pair1(self): a,b = twink.sched.socket.socketpair() x = twink.OpenflowChannel(socket=a) y = type("OpenflowServerChannelTestCaseY", (twink.OpenflowServerChannel,), {})(socket=b) y.handle = auto_echo x.start() y.start() yth = twink.sched.spawn(y.loop) assert len(x.recv()) > 0 assert x.version == 4 x.send(twink.ofp_header_only(2, version=x.version)) p = twink.parse_ofp_header(x.recv()) assert p[1] == 3 x.close() y.close() yth.join(0.5)
def wtest_echo(self): global signal_stop # blocking server serv = twink.StreamServer(("localhost", 0)) serv.channel_cls = type("Server", (twink.ControllerChannel, twink.AutoEchoChannel, twink.LoggingChannel), { "accept_versions": [ 4, ], "handle": staticmethod(dummy_handle) }) serv_thread = threading.Thread(target=serv.serve_forever) serv_thread.start() signal_stop = serv.shutdown try: s = create_connection(serv.server_address[:2]) ch = type("Client", (twink.OpenflowChannel, twink.LoggingChannel), {"accept_versions": [ 4, ]})() ch.attach(s) msg = ch.recv() assert msg version, oftype, l, xid = twink.parse_ofp_header(msg) assert version == 4 ch.send(twink.ofp_header_only(2, version=4)) msg = ch.recv() assert msg version, oftype, l, xid = twink.parse_ofp_header(msg) assert oftype == 3 except: logging.error("client error", exc_info=True) finally: ch.close() serv.shutdown() serv_thread.join()
def auto_echo(msg, ch): p = twink.parse_ofp_header(msg) if p[1] == 2: ch.send(twink.ofp_header_only(3, version=ch.version, xid=p[3]))