protocol

package module
v0.0.0-...-a7bdf43 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2018 License: MIT Imports: 9 Imported by: 0

README

go-libp2p-protocol

Building Protocols on top of LibP2P

Example

// Create a new libp2p host
host, err := libp2p.New(context.Background(), libp2p.Defaults)
if(err != nil) {
	panic(err)
}

proto := protocol.Protocol {
  Name: "name_your_protocol",
  Version: 1,
  Run: func(stream net.Stream, init bool) {
    // use init to differentiate between dialler and listener peer. 
    // init == true (peer who called AddPeer)

    fmt.Println("Stream opened to ", stream.Conn().RemotePeer())
  },
}

// This function will return you an extended host.
// It contains the host (xHost.Host) and a slice of protocols (xHost.Protocol)
xHost := protocol.SetProtocol(host, []protocol.Protocol{proto})

// Now you can add a peer.
// It will add the peer to the peerstore and open a stream.
// Protocol.Run function will will be invoked for both the peers.
xHost.AddPeerWithAddr("/ip4/127.0.0.1/tcp/51912/ipfs/QmbHvB9A2vLMno9LWkWnwnRHa5Fcjnogsq8nzgiooKfJTM")
// or
xHost.AddPeer(pi peerstore.PeerInfo)

And there you go.

Look into the examples for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrStringToPeerInfo

func AddrStringToPeerInfo(addr string) (peerstore.PeerInfo, error)

Types

type ExtendedHost

type ExtendedHost struct {
	Host     host.Host
	Protocol []Protocol
}

func SetProtocol

func SetProtocol(host host.Host, proto []Protocol) *ExtendedHost

func (*ExtendedHost) AddPeer

func (eh *ExtendedHost) AddPeer(info peerstore.PeerInfo) error

func (*ExtendedHost) AddPeerWithAddr

func (eh *ExtendedHost) AddPeerWithAddr(addr string) error

func (*ExtendedHost) GetCompleteAddr

func (eh *ExtendedHost) GetCompleteAddr() string

type Protocol

type Protocol struct {
	Name    string
	Version uint
	Run     StreamHandler
}

type StreamHandler

type StreamHandler func(stream net.Stream, initiator bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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