Tribler/py-ipv8

Rewrite docs to showcase `PeerObserver`

qstokkink opened this issue · 0 comments

Currently our docs, main code, and downstream projects are filled with periodic tasks that relate to managing peers. For example:

class MyCommunity(Community):
community_id = os.urandom(20)
def started(self):
async def print_peers():
print("I am:", self.my_peer, "\nI know:", [str(p) for p in self.get_peers()])
# We register a asyncio task with this overlay.
# This makes sure that the task ends when this overlay is unloaded.
# We call the 'print_peers' function every 5.0 seconds, starting now.
self.register_task("print_peers", print_peers, interval=5.0, delay=0)

However, since #1148, we now have PeerObserver interfaces. We can use this to make the introductory tutorial a bit less "scary":

class MyCommunity(Community, PeerObserver):
    community_id = os.urandom(20)

    def on_peer_added(self, peer: Peer) -> None:
        print("I am:", self.my_peer, "I found:", peer)

    def on_peer_removed(self, peer: Peer) -> None:
        pass

    def started(self):
        self.network.add_peer_observer(self)

We should encourage overlay programmers to use on_peer_added instead of the introduction request/response callbacks (those have some low-level "gotcha's" regarding being introduced to yourself etc.) or periodic tasks.