Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "go.vocdoni.io/dvote/ipfssync/subpub" "go.vocdoni.io/dvote/log" "go.vocdoni.io/dvote/util" ) func main() { log.Init("info", "stdout") messages := make(chan *subpub.Message) groupKey := []byte("test") port := 6543 privKey := util.RandomHex(32) sp := subpub.NewSubPub(privKey, groupKey, int32(port), false) sp.Start(context.Background(), messages) }
Output:
Index ¶
- Constants
- type Gossip
- type Message
- type SubPub
- func (ps *SubPub) AddPeer(peer string) error
- func (s *SubPub) Address() string
- func (ps *SubPub) Close() error
- func (ps *SubPub) Listen(receiver chan<- *Message)
- func (ps *SubPub) ReadMessage(r *bufio.Reader) (*Message, error)
- func (s *SubPub) SendBroadcast(msg Message) error
- func (ps *SubPub) SendMessage(w *bufio.Writer, msg []byte) error
- func (s *SubPub) SendUnicast(address string, msg Message) error
- func (ps *SubPub) Start(ctx context.Context, receiver chan *Message)
- func (ps *SubPub) String() string
- func (ps *SubPub) Unicast(address string, message []byte) error
Examples ¶
Constants ¶
const ( IPv4 = 4 IPv6 = 6 )
const GossipBufSize = 128
GossipBufSize is the number of incoming messages to buffer for each topic.
const UnicastBufSize = 128
UnicastBufSize is the number of unicast incoming messages to buffer.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Gossip ¶
type Gossip struct { // Messages is a channel of messages received from other peers in the topic Messages chan *Message // contains filtered or unexported fields }
Gossip represents a subscription to a single PubSub topic. Messages can be published to the topic with Gossip.Publish, and received messages are pushed to the Messages channel.
type SubPub ¶
type SubPub struct { Key ecdsa.PrivateKey GroupKey [32]byte Topic string NoBootStrap bool BootNodes []string PubKey string Private bool NodeID string Port int32 Host host.Host MaxDHTpeers int Gossip *Gossip // Gossip deals with broadcasts Streams sync.Map // this is a thread-safe map[libpeer.ID]bufioWithMutex UnicastMsgs chan *Message // UnicastMsgs passes unicasts around Messages chan *Message // both unicasts and broadcasts end up being passed to Messages DiscoveryPeriod time.Duration // These are used in testing OnPeerAdd func(id libpeer.ID) OnPeerRemove func(id libpeer.ID) // contains filtered or unexported fields }
SubPub is a simplified PubSub protocol using libp2p
func NewSubPub ¶
NewSubPub creates a new SubPub instance. The private key is used to identify the node (by derivating its pubKey) on the p2p network. The groupKey is a secret shared among the PubSub participants. Only those with the key will be able to join. If private enabled, a libp2p private network is created using the groupKey as shared secret (experimental). If private enabled the default bootnodes will not work.
func (*SubPub) SendBroadcast ¶
func (*SubPub) SendMessage ¶
SendMessage encrypts and writes a message on the readwriter buffer