go_libp2p_node

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2020 License: MIT Imports: 16 Imported by: 0

README

go-libp2p-node

Module that abstracts libp2p/go-libp2p suite.

The code has been extracted from another project to be used cross components.

Install

go get github.com/amirylm/go-libp2p-node

Usage

See example in ./node_test.go

Default options (discovery on local network):

import (
    libp2p_node "github.com/amirylm/go-libp2p-node"
)

func main() {
    n, err := libp2p_node.NewLibP2pNode(context.Background(), nil)
    defer n.Close()
    
    cn, err := n.Channel("echo")
    go func() {
        in, _, _ := cn.Listen()
        for {
            msg := <- in
            cn.Publish(msg.Data, msg.Type)
        }
    }()
}

Dev

Tests

Run ./scripts/test.sh under the root directory

Documentation

Index

Constants

View Source
const DefaultDiscoveryInterval = time.Minute * 10

DiscoveryInterval is how often we re-publish our mDNS records.

View Source
const DefaultDiscoveryServiceTag = "pubsub"

DiscoveryServiceTag is used in our mDNS advertisements to discover other chat peers.

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Messages chan ChannelMessage
	// contains filtered or unexported fields
}

func NewChannel

func NewChannel(ctx context.Context, self peer.ID, ps *pubsub.PubSub, name string) (*Channel, error)

func (*Channel) ListPeers

func (c *Channel) ListPeers() []peer.ID

ListPeers returns a list of peers we are connected to in the given topic.

func (*Channel) Listen

func (c *Channel) Listen() (chan ChannelMessage, chan error, chan bool)

Listen will subscribe to the current pubsub channel and publish the messages on out chan

func (*Channel) Publish

func (c *Channel) Publish(data []byte, mtype string) error

Publish sends a message on the pubsub topic.

type ChannelMessage

type ChannelMessage struct {
	Data []byte
	Type string
}

ChannelMessage represents a message to send

type DiscoveryOptions

type DiscoveryOptions struct {
	OnPeerFound OnPeerFound
	ServiceTag  string
	Interval    time.Duration
	Services    []discovery.Service
}

type LibP2pNodeOptions

type LibP2pNodeOptions struct {
	Addr      []string
	Discovery DiscoveryOptions
	Mux       MuxOptions
	Sec       SecurityOptions
	Raw       []libp2p.Option
}

func DefaultLibP2pNodeOptions

func DefaultLibP2pNodeOptions() *LibP2pNodeOptions

func NewLibP2pNodeOptions

func NewLibP2pNodeOptions(disc DiscoveryOptions) *LibP2pNodeOptions

type MuxOptions

type MuxOptions struct {
	Name      string
	Transport mux.Multiplexer
}

type Node

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

func NewLibP2pNode

func NewLibP2pNode(ctx context.Context, opts *LibP2pNodeOptions) (*Node, error)

func (*Node) Channel

func (n *Node) Channel(name string) (cn *Channel, err error)

func (*Node) Close

func (n *Node) Close() error

func (*Node) ID

func (n *Node) ID() string

type OnPeerFound

type OnPeerFound = func(pi peer.AddrInfo) bool

type SecurityOptions

type SecurityOptions struct {
	Name      string
	Transport interface{}
}

Jump to

Keyboard shortcuts

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