Documentation ¶
Index ¶
- Variables
- type Message
- func (message *Message) Data() []byte
- func (message *Message) From() []byte
- func (message *Message) IsInterfaceNil() bool
- func (message *Message) Key() []byte
- func (message *Message) Peer() p2p.PeerID
- func (message *Message) SeqNo() []byte
- func (message *Message) Signature() []byte
- func (message *Message) TopicIDs() []string
- type Messenger
- func (messenger *Messenger) Addresses() []string
- func (messenger *Messenger) Bootstrap() error
- func (messenger *Messenger) Broadcast(topic string, buff []byte)
- func (messenger *Messenger) BroadcastOnChannel(channel string, topic string, buff []byte)
- func (messenger *Messenger) BroadcastOnChannelBlocking(channel string, topic string, buff []byte)
- func (messenger *Messenger) Close() error
- func (messenger *Messenger) ConnectToPeer(address string) error
- func (messenger *Messenger) ConnectedAddresses() []string
- func (messenger *Messenger) ConnectedPeers() []p2p.PeerID
- func (messenger *Messenger) ConnectedPeersOnTopic(topic string) []p2p.PeerID
- func (messenger *Messenger) CreateTopic(name string, createChannelForTopic bool) error
- func (messenger *Messenger) HasTopic(name string) bool
- func (messenger *Messenger) HasTopicValidator(name string) bool
- func (messenger *Messenger) ID() p2p.PeerID
- func (messenger *Messenger) IsConnected(peerID p2p.PeerID) bool
- func (messenger *Messenger) IsConnectedToNetwork() bool
- func (messenger *Messenger) IsInterfaceNil() bool
- func (messenger *Messenger) OutgoingChannelLoadBalancer() p2p.ChannelLoadBalancer
- func (messenger *Messenger) PeerAddress(pid p2p.PeerID) string
- func (messenger *Messenger) Peers() []p2p.PeerID
- func (messenger *Messenger) ReceiveMessage(topic string, message p2p.MessageP2P) error
- func (messenger *Messenger) RegisterMessageProcessor(topic string, handler p2p.MessageProcessor) error
- func (messenger *Messenger) SendToConnectedPeer(topic string, buff []byte, peerID p2p.PeerID) error
- func (messenger *Messenger) TrimConnections()
- func (messenger *Messenger) UnregisterMessageProcessor(topic string) error
- type Network
- func (network *Network) GetMessageCount() int
- func (network *Network) IsPeerConnected(peerID p2p.PeerID) bool
- func (network *Network) ListAddresses() []string
- func (network *Network) ListAddressesExceptOne(peerIDToExclude p2p.PeerID) []string
- func (network *Network) LogMessage(message p2p.MessageP2P)
- func (network *Network) PeerIDs() []p2p.PeerID
- func (network *Network) PeerIDsExceptOne(peerIDToExclude p2p.PeerID) []p2p.PeerID
- func (network *Network) Peers() map[p2p.PeerID]*Messenger
- func (network *Network) PeersExceptOne(peerIDToExclude p2p.PeerID) map[p2p.PeerID]*Messenger
- func (network *Network) RegisterPeer(messenger *Messenger)
- func (network *Network) UnregisterPeer(peerID p2p.PeerID)
Constants ¶
This section is empty.
Variables ¶
var ErrCannotSendToSelf = errors.New("cannot send message to itself")
ErrCannotSendToSelf signals that a peer tried to send a message to itself
var ErrNilNetwork = errors.New("nil network")
ErrNilNetwork signals that a nil was given where a memp2p.Network instance was expected
var ErrNotConnectedToNetwork = errors.New("not connected to network")
ErrNotConnectedToNetwork signals that a peer tried to perform a network-related operation, but is not connected to any network
var ErrReceivingPeerNotConnected = errors.New("receiving peer not connected to network")
ErrReceivingPeerNotConnected signals that the receiving peer of a sending operation is not connected to the network
Functions ¶
This section is empty.
Types ¶
type Message ¶ added in v1.0.12
type Message struct {
// contains filtered or unexported fields
}
Message represents a message to be sent through the in-memory network simulated by the Network struct.
func NewMessage ¶ added in v1.0.12
NewMessage constructs a new Message instance from arguments
func (*Message) IsInterfaceNil ¶ added in v1.0.16
IsInterfaceNil returns true if there is no value under the interface
func (*Message) Key ¶ added in v1.0.12
Key returns the message public key (if it can not be recovered from From field)
type Messenger ¶
type Messenger struct { Network *Network P2PID p2p.PeerID Address string Topics map[string]p2p.MessageProcessor TopicsMutex sync.RWMutex }
Messenger is an implementation of the p2p.Messenger interface that uses no real networking code, but instead connects to a network simulated in memory (the Network struct). The Messenger is intended for use in automated tests instead of the real libp2p, in order to speed up their execution and reduce resource usage.
All message-sending functions imitate the synchronous/asynchronous behavior of the Messenger struct originally implemented for libp2p. Note that the Network ensures that all messengers are connected to all other messengers, thus when a Messenger is connected to an in-memory network, it reports being connected to all the nodes. Consequently, broadcasting a message will be received by all the messengers in the network.
func NewMessenger ¶
NewMessenger constructs a new Messenger that is connected to the Network instance provided as argument.
func (*Messenger) Addresses ¶
Addresses returns a list of all the physical addresses that this Messenger is bound to and listening to, depending on the available network interfaces of the machine. Being an in-memory simulation, the only possible address to return is an artificial one, built by the constructor NewMessenger().
func (*Messenger) Bootstrap ¶
Bootstrap does nothing, as it is not applicable to the in-memory messenger.
func (*Messenger) Broadcast ¶
Broadcast asynchronously sends the message to all peers in the network. It calls parametricBroadcast() with async=true, which means that peers will have their ReceiveMessage() function independently called as go-routines.
func (*Messenger) BroadcastOnChannel ¶
BroadcastOnChannel sends the message to all peers in the network. It calls parametricBroadcast() with async=false, which means that peers will have their ReceiveMessage() function called synchronously. The call to parametricBroadcast() is done as a go-routine, which means this function is, in fact, non-blocking, but it is identical with BroadcastOnChannelBlocking() in all other regards.
func (*Messenger) BroadcastOnChannelBlocking ¶
BroadcastOnChannelBlocking sends the message to all peers in the network. It calls parametricBroadcast() with async=false, which means that peers will have their ReceiveMessage() function called synchronously. The call to parametricBroadcast() is done synchronously as well. This function should be called as a go-routine.
func (*Messenger) ConnectToPeer ¶
ConnectToPeer usually does nothing, because peers connected to the in-memory network are already all connected to each other. This function will return an error if the Messenger is not connected to the network, though.
func (*Messenger) ConnectedAddresses ¶
ConnectedAddresses returns a slice of peer addresses to which this Messenger is connected. If this Messenger is connected to the network, then the addresses of all the other peers in the network are returned.
func (*Messenger) ConnectedPeers ¶
ConnectedPeers returns a slice of IDs belonging to the peers to which this Messenger is connected. If the Messenger is connected to the in₋memory network, then the function returns a slice containing the IDs of all the other peers connected to the network. Returns false if the Messenger is not connected.
func (*Messenger) ConnectedPeersOnTopic ¶
ConnectedPeersOnTopic returns a slice of IDs belonging to the peers in the network that have declared their interest in the given topic and are listening to messages on that topic.
func (*Messenger) CreateTopic ¶
CreateTopic adds the topic provided as argument to the list of topics of interest for this Messenger. It also registers a nil message validator to handle the messages received on this topic.
func (*Messenger) HasTopic ¶
HasTopic returns true if this Messenger has declared interest in the given topic; returns false otherwise.
func (*Messenger) HasTopicValidator ¶
HasTopicValidator returns true if this Messenger has declared interest in the given topic and has registered a non-nil validator on that topic. Returns false otherwise.
func (*Messenger) IsConnected ¶
IsConnected returns true if this Messenger is connected to the peer with the specified ID. It always returns true if the Messenger is connected to the network and false otherwise, regardless of the provided peer ID.
func (*Messenger) IsConnectedToNetwork ¶
IsConnectedToNetwork returns true if this messenger is connected to the in-memory network, false otherwise.
func (*Messenger) IsInterfaceNil ¶
IsInterfaceNil returns true if there is no value under the interface
func (*Messenger) OutgoingChannelLoadBalancer ¶
func (messenger *Messenger) OutgoingChannelLoadBalancer() p2p.ChannelLoadBalancer
OutgoingChannelLoadBalancer does nothing, as it is not applicable to the in-memory network.
func (*Messenger) PeerAddress ¶
PeerAddress creates the address string from a given peer ID.
func (*Messenger) Peers ¶
Peers returns a slice containing the P2P IDs of all the other peers that it has knowledge of. Since this is an in-memory network structured as a fully connected graph, this function returns the list of the P2P IDs of all the peers in the network (assuming this Messenger is connected).
func (*Messenger) ReceiveMessage ¶ added in v1.0.12
func (messenger *Messenger) ReceiveMessage(topic string, message p2p.MessageP2P) error
ReceiveMessage handles the received message by passing it to the message processor of the corresponding topic, given that this Messenger has previously registered a message processor for that topic. The Network will log the message only if the Network.LogMessages flag is set and only if the Messenger has the requested topic and MessageProcessor.
func (*Messenger) RegisterMessageProcessor ¶
func (messenger *Messenger) RegisterMessageProcessor(topic string, handler p2p.MessageProcessor) error
RegisterMessageProcessor sets the provided message processor to be the processor of received messages for the given topic.
func (*Messenger) SendToConnectedPeer ¶
SendToConnectedPeer sends a message directly to the peer specified by the ID.
func (*Messenger) TrimConnections ¶
func (messenger *Messenger) TrimConnections()
TrimConnections does nothing, as it is not applicable to the in-memory messenger.
func (*Messenger) UnregisterMessageProcessor ¶
UnregisterMessageProcessor unsets the message processor for the given topic (sets it to nil).
type Network ¶
type Network struct { LogMessages bool Messages []p2p.MessageP2P // contains filtered or unexported fields }
Network provides in-memory connectivity for the Messenger struct. It simulates a network where each peer is connected to all the other peers. The peers are connected to the network if they are in the internal `peers` map; otherwise, they are disconnected.
func NewNetwork ¶
NewNetwork constructs a new Network instance with an empty internal map of peers.
func (*Network) GetMessageCount ¶ added in v1.0.12
GetMessageCount returns the number of messages logged internally by the Network.
func (*Network) IsPeerConnected ¶
IsPeerConnected returns true if the peer represented by the provided ID is found in the inner `peers` map of the Network instance, which determines whether it is connected to the network or not.
func (*Network) ListAddresses ¶ added in v1.0.12
ListAddresses provides the addresses of the known peers.
func (*Network) ListAddressesExceptOne ¶
ListAddressesExceptOne provides the addresses of the known peers, except a specified one.
func (*Network) LogMessage ¶ added in v1.0.12
func (network *Network) LogMessage(message p2p.MessageP2P)
LogMessage adds a message to its internal log of messages.
func (*Network) PeerIDsExceptOne ¶
PeerIDsExceptOne provides a copy of its internal slice of peerIDs, excluding a specific peer.
func (*Network) PeersExceptOne ¶
PeersExceptOne provides a copy of its internal map of peers, excluding a specific peer.
func (*Network) RegisterPeer ¶
RegisterPeer adds a messenger to the Peers map and its PeerID to the peerIDs slice.
func (*Network) UnregisterPeer ¶
UnregisterPeer removes a messenger from the Peers map and its PeerID from the peerIDs slice.