Move calls to Notifee inside Swarm

This commit is contained in:
mhchia
2019-09-23 15:01:58 +08:00
parent 6f8394e4bd
commit 8d2415a404
3 changed files with 39 additions and 22 deletions

View File

@ -223,8 +223,7 @@ class Swarm(INetwork):
await listener.listen(maddr)
# Call notifiers since event occurred
for notifee in self.notifees:
await notifee.listen(self, maddr)
self.notify_listen(maddr)
return True
except IOError:
@ -234,13 +233,6 @@ class Swarm(INetwork):
# No maddr succeeded
return False
def register_notifee(self, notifee: INotifee) -> None:
"""
:param notifee: object implementing Notifee interface
:return: true if notifee registered successfully, false otherwise
"""
self.notifees.append(notifee)
def add_router(self, router: IPeerRouting) -> None:
self.router = router
@ -279,8 +271,7 @@ class Swarm(INetwork):
# Store muxed_conn with peer id
self.connections[muxed_conn.peer_id] = swarm_conn
# Call notifiers since event occurred
for notifee in self.notifees:
await notifee.connected(self, swarm_conn)
self.notify_connected(swarm_conn)
await swarm_conn.start()
return swarm_conn
@ -294,3 +285,32 @@ class Swarm(INetwork):
# TODO: Should be changed to remove the exact connection,
# if we have several connections per peer in the future.
del self.connections[peer_id]
# Notifee
# TODO: Remeber the spawn notifying tasks and clean them up when closing.
def register_notifee(self, notifee: INotifee) -> None:
"""
:param notifee: object implementing Notifee interface
:return: true if notifee registered successfully, false otherwise
"""
self.notifees.append(notifee)
def notify_opened_stream(self, stream: INetStream) -> None:
asyncio.gather(
*[notifee.opened_stream(self, stream) for notifee in self.notifees]
)
# TODO: `notify_closed_stream`
def notify_connected(self, conn: INetConn) -> None:
asyncio.gather(*[notifee.connected(self, conn) for notifee in self.notifees])
def notify_disconnected(self, conn: INetConn) -> None:
asyncio.gather(*[notifee.disconnected(self, conn) for notifee in self.notifees])
def notify_listen(self, multiaddr: Multiaddr) -> None:
asyncio.gather(*[notifee.listen(self, multiaddr) for notifee in self.notifees])
# TODO: `notify_listen_close`