mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
- Fix INotifee interface compliance in WebSocket demo - Fix handler function signatures to be async (THandler compatibility) - Fix is_closed method usage with proper type checking - Fix pytest.raises multiple exception type issue - Fix line length violations (E501) across multiple files - Add debugging logging to Noise security module for troubleshooting - Update WebSocket transport examples and tests - Improve transport registry error handling
50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
from .tcp.tcp import TCP
|
|
from .websocket.transport import WebsocketTransport
|
|
from .transport_registry import (
|
|
TransportRegistry,
|
|
create_transport_for_multiaddr,
|
|
get_transport_registry,
|
|
register_transport,
|
|
get_supported_transport_protocols,
|
|
)
|
|
from .upgrader import TransportUpgrader
|
|
from libp2p.abc import ITransport
|
|
|
|
def create_transport(protocol: str, upgrader: TransportUpgrader | None = None) -> ITransport:
|
|
"""
|
|
Convenience function to create a transport instance.
|
|
|
|
:param protocol: The transport protocol ("tcp", "ws", or custom)
|
|
:param upgrader: Optional transport upgrader (required for WebSocket)
|
|
:return: Transport instance
|
|
"""
|
|
# First check if it's a built-in protocol
|
|
if protocol == "ws":
|
|
if upgrader is None:
|
|
raise ValueError(f"WebSocket transport requires an upgrader")
|
|
return WebsocketTransport(upgrader)
|
|
elif protocol == "tcp":
|
|
return TCP()
|
|
else:
|
|
# Check if it's a custom registered transport
|
|
registry = get_transport_registry()
|
|
transport_class = registry.get_transport(protocol)
|
|
if transport_class:
|
|
transport = registry.create_transport(protocol, upgrader)
|
|
if transport is None:
|
|
raise ValueError(f"Failed to create transport for protocol: {protocol}")
|
|
return transport
|
|
else:
|
|
raise ValueError(f"Unsupported transport protocol: {protocol}")
|
|
|
|
__all__ = [
|
|
"TCP",
|
|
"WebsocketTransport",
|
|
"TransportRegistry",
|
|
"create_transport_for_multiaddr",
|
|
"create_transport",
|
|
"get_transport_registry",
|
|
"register_transport",
|
|
"get_supported_transport_protocols",
|
|
]
|