mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-11 23:51:07 +00:00
feat: implement WebSocket transport with transport registry system - Add transport_registry.py for centralized transport management - Integrate WebSocket transport with new registry - Add comprehensive test suite for transport registry - Include WebSocket examples and demos - Update transport initialization and swarm integration
This commit is contained in:
@ -71,6 +71,10 @@ from libp2p.transport.tcp.tcp import (
|
||||
from libp2p.transport.upgrader import (
|
||||
TransportUpgrader,
|
||||
)
|
||||
from libp2p.transport.transport_registry import (
|
||||
create_transport_for_multiaddr,
|
||||
get_supported_transport_protocols,
|
||||
)
|
||||
from libp2p.utils.logging import (
|
||||
setup_logging,
|
||||
)
|
||||
@ -185,16 +189,67 @@ def new_swarm(
|
||||
|
||||
id_opt = generate_peer_id_from(key_pair)
|
||||
|
||||
|
||||
|
||||
# Generate X25519 keypair for Noise
|
||||
noise_key_pair = create_new_x25519_key_pair()
|
||||
|
||||
# Default security transports (using Noise as primary)
|
||||
secure_transports_by_protocol: Mapping[TProtocol, ISecureTransport] = sec_opt or {
|
||||
NOISE_PROTOCOL_ID: NoiseTransport(
|
||||
key_pair, noise_privkey=noise_key_pair.private_key
|
||||
),
|
||||
TProtocol(secio.ID): secio.Transport(key_pair),
|
||||
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(
|
||||
key_pair, peerstore=peerstore_opt
|
||||
),
|
||||
}
|
||||
|
||||
# Use given muxer preference if provided, otherwise use global default
|
||||
if muxer_preference is not None:
|
||||
temp_pref = muxer_preference.upper()
|
||||
if temp_pref not in [MUXER_YAMUX, MUXER_MPLEX]:
|
||||
raise ValueError(
|
||||
f"Unknown muxer: {muxer_preference}. Use 'YAMUX' or 'MPLEX'."
|
||||
)
|
||||
active_preference = temp_pref
|
||||
else:
|
||||
active_preference = DEFAULT_MUXER
|
||||
|
||||
# Use provided muxer options if given, otherwise create based on preference
|
||||
if muxer_opt is not None:
|
||||
muxer_transports_by_protocol = muxer_opt
|
||||
else:
|
||||
if active_preference == MUXER_MPLEX:
|
||||
muxer_transports_by_protocol = create_mplex_muxer_option()
|
||||
else: # YAMUX is default
|
||||
muxer_transports_by_protocol = create_yamux_muxer_option()
|
||||
|
||||
upgrader = TransportUpgrader(
|
||||
secure_transports_by_protocol=secure_transports_by_protocol,
|
||||
muxer_transports_by_protocol=muxer_transports_by_protocol,
|
||||
)
|
||||
|
||||
# Create transport based on listen_addrs or default to TCP
|
||||
if listen_addrs is None:
|
||||
transport = TCP()
|
||||
else:
|
||||
# Use the first address to determine transport type
|
||||
addr = listen_addrs[0]
|
||||
if addr.__contains__("tcp"):
|
||||
transport = TCP()
|
||||
elif addr.__contains__("quic"):
|
||||
raise ValueError("QUIC not yet supported")
|
||||
else:
|
||||
raise ValueError(f"Unknown transport in listen_addrs: {listen_addrs}")
|
||||
transport = create_transport_for_multiaddr(addr, upgrader)
|
||||
|
||||
if transport is None:
|
||||
# Fallback to TCP if no specific transport found
|
||||
if addr.__contains__("tcp"):
|
||||
transport = TCP()
|
||||
elif addr.__contains__("quic"):
|
||||
raise ValueError("QUIC not yet supported")
|
||||
else:
|
||||
supported_protocols = get_supported_transport_protocols()
|
||||
raise ValueError(
|
||||
f"Unknown transport in listen_addrs: {listen_addrs}. "
|
||||
f"Supported protocols: {supported_protocols}"
|
||||
)
|
||||
|
||||
# Generate X25519 keypair for Noise
|
||||
noise_key_pair = create_new_x25519_key_pair()
|
||||
|
||||
Reference in New Issue
Block a user