Swarm: change notify_xxx back to async func

This commit is contained in:
mhchia
2020-02-04 22:56:13 +08:00
parent 13930ae718
commit 12cb0d9ac4
2 changed files with 29 additions and 23 deletions

View File

@ -274,7 +274,7 @@ class Swarm(Service, INetworkService):
await listener.listen(maddr, self.listener_nursery)
# Call notifiers since event occurred
self.notify_listen(maddr)
await self.notify_listen(maddr)
return True
except IOError:
@ -310,7 +310,7 @@ class Swarm(Service, INetworkService):
# Store muxed_conn with peer id
self.connections[muxed_conn.peer_id] = swarm_conn
# Call notifiers since event occurred
self.notify_connected(swarm_conn)
await self.notify_connected(swarm_conn)
return swarm_conn
def remove_conn(self, swarm_conn: SwarmConn) -> None:
@ -330,22 +330,28 @@ class Swarm(Service, INetworkService):
"""
self.notifees.append(notifee)
def notify_opened_stream(self, stream: INetStream) -> None:
for notifee in self.notifees:
self.manager.run_task(notifee.opened_stream, self, stream)
async def notify_opened_stream(self, stream: INetStream) -> None:
async with trio.open_nursery() as nursery:
for notifee in self.notifees:
nursery.start_soon(notifee.opened_stream, self, stream)
# TODO: `notify_closed_stream`
async def notify_connected(self, conn: INetConn) -> None:
async with trio.open_nursery() as nursery:
for notifee in self.notifees:
nursery.start_soon(notifee.connected, self, conn)
def notify_connected(self, conn: INetConn) -> None:
for notifee in self.notifees:
self.manager.run_task(notifee.connected, self, conn)
async def notify_disconnected(self, conn: INetConn) -> None:
async with trio.open_nursery() as nursery:
for notifee in self.notifees:
nursery.start_soon(notifee.disconnected, self, conn)
def notify_disconnected(self, conn: INetConn) -> None:
for notifee in self.notifees:
self.manager.run_task(notifee.disconnected, self, conn)
async def notify_listen(self, multiaddr: Multiaddr) -> None:
async with trio.open_nursery() as nursery:
for notifee in self.notifees:
nursery.start_soon(notifee.listen, self, multiaddr)
def notify_listen(self, multiaddr: Multiaddr) -> None:
for notifee in self.notifees:
self.manager.run_task(notifee.listen, self, multiaddr)
async def notify_closed_stream(self, stream: INetStream) -> None:
raise NotImplementedError
# TODO: `notify_listen_close`
async def notify_listen_close(self, multiaddr: Multiaddr) -> None:
raise NotImplementedError