Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fix for IPv8 endpoint adapter compatibility with mainline IPv8 #567

Merged
merged 1 commit into from
May 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 9 additions & 70 deletions ipv8_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,33 @@
from twisted.internet import reactor



class Endpoint(object):
"""
Interface for sending messages over the Internet.
"""

__metaclass__ = abc.ABCMeta

def __init__(self):
self._logger = logging.getLogger(self.__class__.__name__)
self._listeners = []

def add_listener(self, listener):
"""
Add an EndpointListener to our listeners.

:raises: IllegalEndpointListenerError if the provided listener is not an EndpointListener
"""
if not isinstance(listener, EndpointListener):
raise IllegalEndpointListenerError(listener)
self._listeners.append(listener)

def remove_listener(self, listener):
"""
Remove a listener from our listeners, if it is registered.
"""
self._listeners = [l for l in self._listeners if l != listener]

def _deliver_later(self, listener, packet):
"""
Ensure that the listener is still loaded when delivering the packet later.
"""
if reactor.running and self.is_open() and listener in self._listeners:
listener.on_packet(packet)

def notify_listeners(self, packet):
"""
Send data to all listeners.

:param data: the data to send to all listeners.
"""
for listener in self._listeners:
reactor.callFromThread(self._deliver_later, listener, packet)

@abc.abstractmethod
def assert_open(self):
pass

@abc.abstractmethod
def is_open(self):
pass

@abc.abstractmethod
def get_address(self):
pass

@abc.abstractmethod
def send(self, socket_address, packet):
pass

@abc.abstractmethod
def open(self):
pass

@abc.abstractmethod
def close(self, timeout=0.0):
pass


class EndpointListener(object):
"""
Handler for messages coming in through an Endpoint.
"""

__metaclass__ = abc.ABCMeta

def __init__(self, endpoint):
def __init__(self, endpoint, main_thread=True):
"""
Create a new listener.

"""
self._use_main_thread = main_thread

self.endpoint = endpoint

self._netifaces_failed = False
self.my_estimated_lan = (self._get_lan_address(True)[0], self.endpoint._port)
self.my_estimated_wan = self.my_estimated_lan

@property
def use_main_thread(self):
"""
Does the callback of this listener need to be executed on the main thread.
"""
return self._use_main_thread

@abc.abstractmethod
def on_packet(self, packet):
"""
Expand Down