Skip to content

Commit

Permalink
Allow for setup_tunnels to be called before the endpoint has opened
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman committed Jan 23, 2024
1 parent e47309b commit 4c7aacd
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions ipv8_rust_tunnels/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,31 @@ def setup_tunnels(self, tunnel_community: TunnelCommunity, settings: TunnelSetti
"""
self.prefix = tunnel_community.get_prefix()
self.settings = settings
self.apply_settings()

self.rust_ep.set_prefix(self.prefix)
self.rust_ep.set_max_relay_early(settings.max_relay_early)
self.rust_ep.set_peer_flags(settings.peer_flags)
def apply_settings(self) -> None:
"""
Apply tunnels settings to the RustEndpoint. If `RustEndpoint.open` hasn't been called yet,
the settings will be applied automatically when it is called.
"""
if self.prefix and self.settings and self.is_open():
self.rust_ep.set_prefix(self.prefix)
self.rust_ep.set_max_relay_early(self.settings.max_relay_early)
self.rust_ep.set_peer_flags(self.settings.peer_flags)

def set_max_relay_early(self, max_relay_early: int) -> None:
"""
Set the maximum number of relay_early cells that are allowed to pass a relay.
"""
self.rust_ep.set_max_relay_early(max_relay_early)
if self.is_open():
self.rust_ep.set_max_relay_early(max_relay_early)

def set_peer_flags(self, max_relay_early: int) -> None:
"""
Set peer flags.
"""
self.rust_ep.set_peer_flags(max_relay_early)
if self.is_open():
self.rust_ep.set_peer_flags(max_relay_early)

def datagram_received(self, ip: str, port: int, datagram: bytes) -> None:
"""
Expand Down Expand Up @@ -124,6 +133,7 @@ async def open(self) -> bool: # noqa: A003
:return: True is the Endpoint was successfully opened, False otherwise.
"""
self.rust_ep.open(self.datagram_received, self.worker_threads)
self.apply_settings()
return self.rust_ep.is_open()

def close(self) -> None:
Expand Down

0 comments on commit 4c7aacd

Please sign in to comment.