|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +ROOT = Path(__file__).resolve().parents[2] |
| 7 | +if str(ROOT) not in sys.path: |
| 8 | + sys.path.insert(0, str(ROOT)) |
| 9 | + |
| 10 | +import pytaps as taps # noqa: E402 |
| 11 | + |
| 12 | + |
| 13 | +logger = taps.setup_logger("TAPS Feature Client", "yellow") |
| 14 | + |
| 15 | + |
| 16 | +def build_remote_endpoint(args): |
| 17 | + remote = taps.RemoteEndpoint() |
| 18 | + if args.remote_address: |
| 19 | + remote.with_address(args.remote_address) |
| 20 | + else: |
| 21 | + remote.with_hostname(args.remote_host) |
| 22 | + remote.with_port(args.port) |
| 23 | + return remote |
| 24 | + |
| 25 | + |
| 26 | +def build_local_endpoint(args): |
| 27 | + if not args.local_address and not args.interface and args.local_port is None: |
| 28 | + return None |
| 29 | + local = taps.LocalEndpoint() |
| 30 | + if args.local_address: |
| 31 | + local.with_address(args.local_address) |
| 32 | + if args.interface: |
| 33 | + local.with_interface(args.interface) |
| 34 | + if args.local_port is not None: |
| 35 | + local.with_port(args.local_port) |
| 36 | + return local |
| 37 | + |
| 38 | + |
| 39 | +def build_transport_properties(args): |
| 40 | + properties = taps.TransportProperties() |
| 41 | + properties.ignore("congestionControl") |
| 42 | + properties.ignore("preserveOrder") |
| 43 | + properties.prefer("multistreaming") |
| 44 | + properties.prefer("zeroRttMsg") |
| 45 | + properties.set_property("connPriority", args.priority) |
| 46 | + if args.transport == "udp": |
| 47 | + properties.prohibit("reliability") |
| 48 | + properties.require("preserveMsgBoundaries") |
| 49 | + elif args.transport == "tcp": |
| 50 | + properties.require("reliability") |
| 51 | + properties.ignore("preserveMsgBoundaries") |
| 52 | + else: |
| 53 | + properties.ignore("reliability") |
| 54 | + return properties |
| 55 | + |
| 56 | + |
| 57 | +class FeatureClient: |
| 58 | + def __init__(self): |
| 59 | + self.connection = None |
| 60 | + self.received = [] |
| 61 | + |
| 62 | + async def handle_monitoring_update(self, update): |
| 63 | + health = update["snapshot"]["healthSummary"] |
| 64 | + logger.info( |
| 65 | + "monitor trigger=%s severity=%s guidance=%s", |
| 66 | + update["trigger"], |
| 67 | + health["severity"], |
| 68 | + update["snapshot"]["operationalGuidance"], |
| 69 | + ) |
| 70 | + |
| 71 | + async def handle_sent(self, context, connection): |
| 72 | + logger.info("sent message_id=%s props=%s", context.message_id, context.get_properties()) |
| 73 | + |
| 74 | + async def handle_send_error(self, context, reason, connection): |
| 75 | + logger.warning("send error message_id=%s reason=%s", context.message_id, reason) |
| 76 | + |
| 77 | + async def handle_expired(self, context, connection): |
| 78 | + logger.info("expired message_id=%s lifetime=%s", context.message_id, context.lifetime) |
| 79 | + |
| 80 | + async def handle_received(self, data, context, connection): |
| 81 | + logger.info( |
| 82 | + "received echo bytes=%s seq=%s props=%s", |
| 83 | + len(data), |
| 84 | + context.receive_sequence, |
| 85 | + context.get_properties(), |
| 86 | + ) |
| 87 | + self.received.append(data) |
| 88 | + |
| 89 | + async def handle_reestablishment_suggested(self, advice, candidates, connection): |
| 90 | + logger.info("reestablishment advice=%s candidate_count=%s", advice, len(candidates)) |
| 91 | + |
| 92 | + async def main(self, args): |
| 93 | + remote = build_remote_endpoint(args) |
| 94 | + local = build_local_endpoint(args) |
| 95 | + properties = build_transport_properties(args) |
| 96 | + preconnection = taps.Preconnection( |
| 97 | + local_endpoint=local, |
| 98 | + remote_endpoint=remote, |
| 99 | + transport_properties=properties, |
| 100 | + ) |
| 101 | + preconnection.subscribe_monitoring(self.handle_monitoring_update) |
| 102 | + preconnection.set_address_family_policy(args.prefer_family, preference_adjustment=2) |
| 103 | + if args.avoid_protocol: |
| 104 | + preconnection.set_protocol_policy( |
| 105 | + args.avoid_protocol, |
| 106 | + available=True, |
| 107 | + preference_adjustment=-4, |
| 108 | + racing_cooldown=10, |
| 109 | + ) |
| 110 | + if args.alternate_remote: |
| 111 | + preconnection.note_alternate_remote( |
| 112 | + args.remote_address or args.remote_host, |
| 113 | + args.alternate_remote, |
| 114 | + protocol="quic", |
| 115 | + ) |
| 116 | + |
| 117 | + first_context = taps.MessageContext( |
| 118 | + priority=10, |
| 119 | + safely_replayable=True, |
| 120 | + lifetime=args.lifetime, |
| 121 | + final=False, |
| 122 | + ) |
| 123 | + self.connection = await preconnection.initiate_with_send( |
| 124 | + args.payload, |
| 125 | + first_context, |
| 126 | + ) |
| 127 | + await self.connection.wait_ready(timeout=args.timeout) |
| 128 | + self.connection.on_sent(self.handle_sent) |
| 129 | + self.connection.on_send_error(self.handle_send_error) |
| 130 | + self.connection.on_expired(self.handle_expired) |
| 131 | + self.connection.on_received(self.handle_received) |
| 132 | + self.connection.on_reestablishment_suggested(self.handle_reestablishment_suggested) |
| 133 | + self.connection.subscribe_monitoring(self.handle_monitoring_update) |
| 134 | + |
| 135 | + logger.info( |
| 136 | + "ready protocol=%s read_only=%s", |
| 137 | + self.connection.protocol, |
| 138 | + self.connection.get_properties()["readOnly"], |
| 139 | + ) |
| 140 | + |
| 141 | + await self.connection.receive(min_incomplete_length=1, max_length=4096, timeout=args.timeout) |
| 142 | + |
| 143 | + batch = [] |
| 144 | + for idx in range(args.batch_size): |
| 145 | + context = self.connection.new_message_context( |
| 146 | + msgPriority=idx, |
| 147 | + safelyReplayable=self.connection.protocol == "udp", |
| 148 | + final=False, |
| 149 | + ) |
| 150 | + batch.append((f"{args.payload}-{idx}".encode(), context, True)) |
| 151 | + await self.connection.send_batch(batch) |
| 152 | + |
| 153 | + expired = self.connection.new_message_context( |
| 154 | + msgLifetime=0, |
| 155 | + safelyReplayable=self.connection.protocol == "udp", |
| 156 | + final=False, |
| 157 | + ) |
| 158 | + await self.connection.send(b"this message should expire", expired) |
| 159 | + |
| 160 | + if args.degrade_path: |
| 161 | + self.connection.note_soft_error( |
| 162 | + "demo path degradation", |
| 163 | + penalty=4, |
| 164 | + lifetime=120, |
| 165 | + ) |
| 166 | + |
| 167 | + await asyncio.sleep(args.settle_time) |
| 168 | + snapshot = self.connection.get_monitoring_snapshot() |
| 169 | + logger.info("monitoring snapshot=%s", snapshot) |
| 170 | + self.connection.close() |
| 171 | + await self.connection.wait_closed(timeout=args.timeout) |
| 172 | + |
| 173 | + |
| 174 | +def parse_args(): |
| 175 | + parser = argparse.ArgumentParser( |
| 176 | + description="PyTAPS feature demo client for racing, messages, and monitoring." |
| 177 | + ) |
| 178 | + parser.add_argument("--remote-host", default="localhost") |
| 179 | + parser.add_argument("--remote-address", default=None) |
| 180 | + parser.add_argument("--port", type=int, default=7777) |
| 181 | + parser.add_argument("--local-address", default=None) |
| 182 | + parser.add_argument("--local-port", type=int, default=None) |
| 183 | + parser.add_argument("--interface", default=None) |
| 184 | + parser.add_argument("--payload", default="hello taps") |
| 185 | + parser.add_argument("--batch-size", type=int, default=3) |
| 186 | + parser.add_argument("--priority", type=int, default=50) |
| 187 | + parser.add_argument("--lifetime", type=float, default=None) |
| 188 | + parser.add_argument("--timeout", type=float, default=5.0) |
| 189 | + parser.add_argument("--settle-time", type=float, default=0.25) |
| 190 | + parser.add_argument("--prefer-family", choices=["ipv4", "ipv6"], default="ipv6") |
| 191 | + parser.add_argument("--avoid-protocol", default=None) |
| 192 | + parser.add_argument("--alternate-remote", default=None) |
| 193 | + parser.add_argument("--degrade-path", action="store_true") |
| 194 | + parser.add_argument( |
| 195 | + "--transport", |
| 196 | + choices=["auto", "tcp", "udp"], |
| 197 | + default="auto", |
| 198 | + ) |
| 199 | + return parser.parse_args() |
| 200 | + |
| 201 | + |
| 202 | +if __name__ == "__main__": |
| 203 | + asyncio.run(FeatureClient().main(parse_args())) |
0 commit comments