2019-03-24 01:52:02 +08:00
|
|
|
from abc import ABC, abstractmethod
|
2020-01-27 00:10:33 +08:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
AsyncContextManager,
|
|
|
|
AsyncIterable,
|
|
|
|
KeysView,
|
|
|
|
List,
|
|
|
|
Tuple,
|
|
|
|
)
|
|
|
|
|
|
|
|
from async_service import ServiceAPI
|
2019-07-26 17:30:51 +08:00
|
|
|
|
2019-07-28 22:30:51 +08:00
|
|
|
from libp2p.peer.id import ID
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2019-07-28 22:30:51 +08:00
|
|
|
|
2019-07-26 17:30:51 +08:00
|
|
|
from .pb import rpc_pb2
|
2020-01-27 00:10:33 +08:00
|
|
|
from .typing import ValidatorFn
|
2019-07-29 12:42:13 +08:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2019-08-08 14:22:06 +08:00
|
|
|
from .pubsub import Pubsub # noqa: F401
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
|
2020-01-27 00:10:33 +08:00
|
|
|
class ISubscriptionAPI(
|
|
|
|
AsyncContextManager["ISubscriptionAPI"], AsyncIterable[rpc_pb2.Message]
|
|
|
|
):
|
|
|
|
@abstractmethod
|
2020-01-28 00:29:05 +08:00
|
|
|
async def unsubscribe(self) -> None:
|
2020-01-27 00:10:33 +08:00
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def get(self) -> rpc_pb2.Message:
|
|
|
|
...
|
2019-12-17 18:17:28 +08:00
|
|
|
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
class IPubsubRouter(ABC):
|
2019-03-24 01:52:02 +08:00
|
|
|
@abstractmethod
|
2019-08-11 16:47:54 +08:00
|
|
|
def get_protocols(self) -> List[TProtocol]:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
:return: the list of protocols supported by the router
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-08-01 06:00:12 +08:00
|
|
|
def attach(self, pubsub: "Pubsub") -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Attach is invoked by the PubSub constructor to attach the router to a
|
|
|
|
freshly initialized PubSub instance.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
:param pubsub: pubsub instance to attach to
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-08-11 16:47:54 +08:00
|
|
|
def add_peer(self, peer_id: ID, protocol_id: TProtocol) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Notifies the router that a new peer has been connected.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
:param peer_id: id of peer to add
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-07-26 17:30:51 +08:00
|
|
|
def remove_peer(self, peer_id: ID) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Notifies the router that a peer has been disconnected.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
:param peer_id: id of peer to remove
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-08-11 16:47:54 +08:00
|
|
|
async def handle_rpc(self, rpc: rpc_pb2.RPC, sender_peer_id: ID) -> None:
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Invoked to process control messages in the RPC envelope.
|
|
|
|
It is invoked after subscriptions and payload messages have been processed
|
2019-05-07 11:44:13 +08:00
|
|
|
TODO: Check if this interface is ok. It's not the exact same as the go code, but the go
|
|
|
|
code is really confusing with the msg origin, they specify `rpc.from` even when the rpc
|
|
|
|
shouldn't have a from
|
2019-03-24 01:52:02 +08:00
|
|
|
:param rpc: rpc message
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-07-27 11:27:47 +08:00
|
|
|
async def publish(self, msg_forwarder: ID, pubsub_msg: rpc_pb2.Message) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Invoked to forward a new message that has been validated.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-07-29 12:09:35 +08:00
|
|
|
:param msg_forwarder: peer_id of message sender
|
2019-07-25 14:08:16 +08:00
|
|
|
:param pubsub_msg: pubsub message to forward
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-07-27 11:27:47 +08:00
|
|
|
async def join(self, topic: str) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Join notifies the router that we want to receive and forward messages
|
|
|
|
in a topic. It is invoked after the subscription announcement.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
:param topic: topic to join
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-07-27 11:27:47 +08:00
|
|
|
async def leave(self, topic: str) -> None:
|
2019-10-25 02:10:45 +08:00
|
|
|
"""
|
|
|
|
Leave notifies the router that we are no longer interested in a topic.
|
|
|
|
It is invoked after the unsubscription announcement.
|
2019-10-24 14:41:10 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
:param topic: topic to leave
|
|
|
|
"""
|
2019-12-17 18:17:28 +08:00
|
|
|
|
|
|
|
|
2020-01-27 00:10:33 +08:00
|
|
|
class IPubsub(ServiceAPI):
|
|
|
|
@property
|
2019-12-17 18:17:28 +08:00
|
|
|
@abstractmethod
|
2020-01-27 00:10:33 +08:00
|
|
|
def my_id(self) -> ID:
|
2019-12-17 18:17:28 +08:00
|
|
|
...
|
|
|
|
|
2020-01-27 00:10:33 +08:00
|
|
|
@property
|
2019-12-17 18:17:28 +08:00
|
|
|
@abstractmethod
|
2020-01-27 00:10:33 +08:00
|
|
|
def protocols(self) -> Tuple[TProtocol, ...]:
|
|
|
|
...
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
def topic_ids(self) -> KeysView[str]:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def set_topic_validator(
|
|
|
|
self, topic: str, validator: ValidatorFn, is_async_validator: bool
|
|
|
|
) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def remove_topic_validator(self, topic: str) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def wait_until_ready(self) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def subscribe(self, topic_id: str) -> ISubscriptionAPI:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def unsubscribe(self, topic_id: str) -> None:
|
|
|
|
...
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def publish(self, topic_id: str, data: bytes) -> None:
|
2019-12-17 18:17:28 +08:00
|
|
|
...
|