def setUp(self): self.p1 = UDPProxyProtocol(agg_max_size=1446, agg_timeout=1, inject_rssi=True) self.p2 = UDPProxyProtocol(('127.0.0.1', 14553)) self.p1.peer = self.p2 self.p2.peer = self.p1 self.ep1 = reactor.listenUDP(14551, self.p1) self.ep2 = reactor.listenUDP(0, self.p2)
def init_mavlink(profile, wlans): cfg = getattr(settings, '%s_mavlink' % (profile, )) cmd_rx = ('%s -p %d -u %d -K %s' % \ (os.path.join(settings.path.bin_dir, 'wfb_rx'), cfg.stream_rx, cfg.port_rx, os.path.join(settings.path.conf_dir, cfg.keypair))).split() + wlans cmd_tx = ('%s -p %d -u %d -K %s -B %d -G %s -S %d -M %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_tx'), cfg.stream_tx, cfg.port_tx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.bandwidth, "short" if cfg.short_gi else "long", cfg.stbc, cfg.mcs_index)).split() + wlans if cfg.listen: connect = None listen = cfg.listen else: connect = ('127.0.0.1', cfg.connect) listen = 0 p_in = UDPProxyProtocol(connect, agg_max_size=settings.common.radio_mtu, agg_timeout=settings.common.mavlink_agg_timeout, inject_rssi=cfg.inject_rssi) p_tx_l = [ UDPProxyProtocol(('127.0.0.1', cfg.port_tx + i)) for i, _ in enumerate(wlans) ] p_rx = UDPProxyProtocol() p_rx.peer = p_in sockets = [ reactor.listenUDP(listen, p_in), reactor.listenUDP(cfg.port_rx, p_rx) ] sockets += [reactor.listenUDP(0, p_tx) for p_tx in p_tx_l] log.msg('Telem RX: %s' % (' '.join(cmd_rx), )) log.msg('Telem TX: %s' % (' '.join(cmd_tx), )) ant_f = AntennaFactory(p_in, p_tx_l) if cfg.stats_port: reactor.listenTCP(cfg.stats_port, ant_f) dl = [ RXProtocol(ant_f, cmd_rx, 'telem rx').start(), TXProtocol(cmd_tx, 'telem tx').start() ] def _cleanup(x): for s in sockets: s.stopListening() return x return defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup)\ .addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)
def init_mavlink(profile, wlans): cfg = getattr(settings, '%s_mavlink' % (profile,)) cmd_rx = ('%s -p %d -u %d -K %s -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_rx'), cfg.stream_rx, cfg.port_rx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.fec_k, cfg.fec_n)).split() + wlans cmd_tx = ('%s -p %d -u %d -K %s -B %d -G %s -S %d -L %d -M %d -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_tx'), cfg.stream_tx, cfg.port_tx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.bandwidth, "short" if cfg.short_gi else "long", cfg.stbc, cfg.ldpc, cfg.mcs_index, cfg.fec_k, cfg.fec_n)).split() + wlans if connect_re.match(cfg.peer): m = connect_re.match(cfg.peer) connect = m.group('addr'), int(m.group('port')) listen = None log.msg('Connect telem stream %d(RX), %d(TX) to %s:%d' % (cfg.stream_rx, cfg.stream_tx, connect[0], connect[1])) elif listen_re.match(cfg.peer): m = listen_re.match(cfg.peer) listen = m.group('addr'), int(m.group('port')) connect = None log.msg('Listen for telem stream %d(RX), %d(TX) on %s:%d' % (cfg.stream_rx, cfg.stream_tx, listen[0], listen[1])) else: raise Exception('Unsupport peer address: %s' % (cfg.peer,)) # The first argument is not None only if we initiate mavlink connection p_in = UDPProxyProtocol(connect, agg_max_size=settings.common.radio_mtu, agg_timeout=settings.common.mavlink_agg_timeout, inject_rssi=cfg.inject_rssi) p_tx_l = [UDPProxyProtocol(('127.0.0.1', cfg.port_tx + i)) for i, _ in enumerate(wlans)] p_rx = UDPProxyProtocol() p_rx.peer = p_in sockets = [ reactor.listenUDP(listen[1] if listen else 0, p_in), reactor.listenUDP(cfg.port_rx, p_rx) ] sockets += [ reactor.listenUDP(0, p_tx) for p_tx in p_tx_l ] log.msg('Telem RX: %s' % (' '.join(cmd_rx),)) log.msg('Telem TX: %s' % (' '.join(cmd_tx),)) ant_f = AntennaFactory(p_in, p_tx_l) if cfg.stats_port: reactor.listenTCP(cfg.stats_port, ant_f) dl = [RXProtocol(ant_f, cmd_rx, 'telem rx').start(), TXProtocol(cmd_tx, 'telem tx').start()] def _cleanup(x): for s in sockets: s.stopListening() return x return defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup)\ .addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)
def init_tunnel(profile, wlans): cfg = getattr(settings, '%s_tunnel' % (profile, )) cmd_rx = ('%s -p %d -u %d -K %s -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_rx'), cfg.stream_rx, cfg.port_rx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.fec_k, cfg.fec_n)).split() + wlans cmd_tx = ('%s -p %d -u %d -K %s -B %d -G %s -S %d -L %d -M %d -k %d -n %d' % \ (os.path.join(settings.path.bin_dir, 'wfb_tx'), cfg.stream_tx, cfg.port_tx, os.path.join(settings.path.conf_dir, cfg.keypair), cfg.bandwidth, "short" if cfg.short_gi else "long", cfg.stbc, cfg.ldpc, cfg.mcs_index, cfg.fec_k, cfg.fec_n)).split() + wlans p_in = TUNTAPProtocol() p_tx_l = [ UDPProxyProtocol(('127.0.0.1', cfg.port_tx + i)) for i, _ in enumerate(wlans) ] p_rx = UDPProxyProtocol() p_rx.peer = p_in tun_ep = TUNTAPTransport(reactor, p_in, cfg.ifname, cfg.ifaddr, mtu=settings.common.radio_mtu) sockets = [reactor.listenUDP(cfg.port_rx, p_rx)] sockets += [reactor.listenUDP(0, p_tx) for p_tx in p_tx_l] log.msg('Tunnel RX: %s' % (' '.join(cmd_rx), )) log.msg('Tunnel TX: %s' % (' '.join(cmd_tx), )) ant_f = AntennaFactory(p_in, p_tx_l) if cfg.stats_port: reactor.listenTCP(cfg.stats_port, ant_f) dl = [ RXProtocol(ant_f, cmd_rx, 'tunnel rx').start(), TXProtocol(cmd_tx, 'tunnel tx').start() ] def _cleanup(x): tun_ep.loseConnection() for s in sockets: s.stopListening() return x return defer.gatherResults(dl, consumeErrors=True).addBoth(_cleanup)\ .addErrback(lambda f: f.trap(defer.FirstError) and f.value.subFailure)
class UDPProxyTestCase(unittest.TestCase): def setUp(self): self.p1 = UDPProxyProtocol(agg_max_size=1446, agg_timeout=1, inject_rssi=True) self.p2 = UDPProxyProtocol(('127.0.0.1', 14553)) self.p1.peer = self.p2 self.p2.peer = self.p1 self.ep1 = reactor.listenUDP(14551, self.p1) self.ep2 = reactor.listenUDP(0, self.p2) def tearDown(self): self.ep1.stopListening() self.ep2.stopListening() self.p1._cleanup() self.p2._cleanup() @defer.inlineCallbacks def test_proxy(self): addr = ('127.0.0.1', 14551) p = SendPacket('test', addr, 10) ep3 = reactor.listenUDP(9999, p) ep4 = reactor.listenUDP(14553, Echo()) try: ts = time.time() _data, _addr = yield p.df self.assertGreater(time.time() - ts, 1.0) self.assertEqual(_addr, addr) self.assertEqual(_data, 'test' * 10) finally: ep4.stopListening() ep3.stopListening() @defer.inlineCallbacks def test_rssi_injection(self): addr = ('127.0.0.1', 14551) p = SendPacket('test', addr) ep3 = reactor.listenUDP(9999, p) yield df_sleep(0.1) try: self.p1.send_rssi(1, 2, 3, 4) ts = time.time() _data, _addr = yield p.df self.assertLess(time.time() - ts, 1.0) self.assertEqual(_addr, addr) self.assertEqual( _data, '\xfd\t\x00\x00\x00\x03\xf2m\x00\x00\x02\x00\x03\x00\x01\x01d\x00\x04\xa8\xad' ) finally: ep3.stopListening()