p2p

package
v0.0.0-...-dde17ac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 24, 2018 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package p2p handles peer-to-peer networking for the sharding package.

Notes: Gossip sub topics can be identified by their proto message types.

topic := proto.MessageName(myMsg)

Then we can assume that only these message types are broadcast in that gossip subscription.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	// Peer represents the sender of the message.
	Peer Peer
	// Data can be any type of message found in sharding/p2p/proto package.
	Data interface{}
}

Message represents a message received from an external peer.

type Sender

type Sender interface {
	Send(msg interface{}, peer Peer)
}

Sender represents a struct that is able to relay information via shardp2p. Server implements this interface.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a placeholder for a p2p service. To be designed.

func NewServer

func NewServer() (*Server, error)

NewServer creates a new p2p server instance.

func (*Server) Broadcast

func (s *Server) Broadcast(msg interface{})

Broadcast a message to the world.

func (*Server) Feed

func (s *Server) Feed(msg interface{}) *event.Feed

P2P feed is a one to many subscription feed of the argument type.

Messages received via p2p protocol are sent to subscribers by these event feeds. Message consumers should not use event feeds to reply to or broadcast messages. The p2p server will not relay them to peers. Rather, use the Send() or Broadcast() method on p2p.Server.

Event feeds from p2p will always be of type p2p.Message. The message contains information about the sender, aka the peer, and the message payload itself.

feed, err := ps.Feed(MyMessage{})
ch := make(chan p2p.Message, 100) // Choose a reasonable buffer size!
sub := feed.Subscribe(ch)

// Wait until my message comes from a peer.
msg := <- ch
fmt.Printf("Message received: %v", msg.Data)
Example

Feeds can be use to subscribe to any type of message.

s, err := NewServer()
if err != nil {
	panic(err)
}

// Let's wait for a puzzle from our peers then try to solve it.
type Puzzle struct {
	Challenge string
	Answer    string
}

feed := s.Feed(Puzzle{})

ch := make(chan Message, 5) // Small buffer size. I don't expect many puzzles.
sub := feed.Subscribe(ch)

// Always close these resources.
defer sub.Unsubscribe()
defer close(ch)

// Wait until we have a puzzle to solve.
msg := <-ch
puzzle, ok := msg.Data.(Puzzle)

if !ok {
	panic("Received a message that wasn't a puzzle!")
}

fmt.Printf("Received puzzle %s from peer %v\n", puzzle, msg.Peer)

if puzzle.Answer == "fourteen" {
	fmt.Println("I solved the puzzle!")
} else {
	fmt.Println("The answer isn't \"fourteen\"... giving up")
}
Output:

func (*Server) Send

func (s *Server) Send(msg interface{}, peer Peer)

Send a message to a specific peer.

func (*Server) Start

func (s *Server) Start()

Start the main routine for an p2p server.

func (*Server) Stop

func (s *Server) Stop() error

Stop the main p2p loop.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL