2019-03-24 01:52:02 +08:00
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
|
|
class IPubsubRouter(ABC):
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_protocols(self):
|
|
|
|
"""
|
|
|
|
:return: the list of protocols supported by the router
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def attach(self, pubsub):
|
|
|
|
"""
|
|
|
|
Attach is invoked by the PubSub constructor to attach the router to a
|
|
|
|
freshly initialized PubSub instance.
|
|
|
|
:param pubsub: pubsub instance to attach to
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def add_peer(self, peer_id, protocol_id):
|
|
|
|
"""
|
|
|
|
Notifies the router that a new peer has been connected
|
|
|
|
:param peer_id: id of peer to add
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def remove_peer(self, peer_id):
|
|
|
|
"""
|
|
|
|
Notifies the router that a peer has been disconnected
|
|
|
|
:param peer_id: id of peer to remove
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def handle_rpc(self, rpc):
|
|
|
|
"""
|
|
|
|
Invoked to process control messages in the RPC envelope.
|
|
|
|
It is invoked after subscriptions and payload messages have been processed
|
|
|
|
:param rpc: rpc message
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
2019-04-02 04:55:44 +08:00
|
|
|
def publish(self, sender_peer_id, rpc_message):
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
Invoked to forward a new message that has been validated
|
|
|
|
:param sender_peer_id: peer_id of message sender
|
2019-04-02 04:55:44 +08:00
|
|
|
:param rpc_message: message to forward
|
2019-03-24 01:52:02 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def join(self, topic):
|
|
|
|
"""
|
|
|
|
Join notifies the router that we want to receive and
|
|
|
|
forward messages in a topic. It is invoked after the
|
|
|
|
subscription announcement
|
|
|
|
:param topic: topic to join
|
|
|
|
"""
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def leave(self, topic):
|
|
|
|
"""
|
|
|
|
Leave notifies the router that we are no longer interested in a topic.
|
|
|
|
It is invoked after the unsubscription announcement.
|
|
|
|
:param topic: topic to leave
|
|
|
|
"""
|