qri: github.com/qri-io/qri/p2p Index | Files | Directories

package p2p

import "github.com/qri-io/qri/p2p"

Package p2p implements qri peer-to-peer communication.

Index

Package Files

bootstrap.go connected.go dataset.go datasets.go discovery.go events.go log.go log_diff.go message.go network_notifee.go node.go p2p.go peers.go ping.go profile.go qri_peers.go resolve_ref.go search.go wrapped_stream.go

Constants

const (
    // MtDatasetLog gets log of a dataset
    MtDatasetLog = MsgType("dataset_log")
    // NumPeersToContact is the number of peers to contact with messages
    NumPeersToContact = 15
)
const (
    // QriProtocolID is the top level Protocol Identifier
    QriProtocolID = protocol.ID("/qri")
    // QriServiceTag tags the type & version of the qri service
    QriServiceTag = "qri/0.8.3-dev"
)
const MtConnected = MsgType("connected")

MtConnected announces a peer connecting to the network

const MtDatasetInfo = MsgType("dataset_info")

MtDatasetInfo gets info on a dataset

const MtDatasets = MsgType("list_datasets")

MtDatasets is a dataset list message

const MtEvents = MsgType("list_events")

MtEvents is a message to announce added / removed datasets to the network

const MtLogDiff = MsgType("log_diff")

MtLogDiff gets info on a dataset

const (
    // MtPing is a ping/pong message
    MtPing = MsgType("ping")
)
const MtProfile = MsgType("profile")

MtProfile is a peer info message

const MtResolveDatasetRef = MsgType("resolve_dataset_ref")

MtResolveDatasetRef resolves a dataset reference

const MtSearch = MsgType("search")

MtSearch is a search message

Variables

var ErrNotConnected = fmt.Errorf("no p2p connection")

ErrNotConnected is for a missing required network connection

var ErrQriProtocolNotSupported = fmt.Errorf("peer doesn't support the qri protocol")

ErrQriProtocolNotSupported is returned when a connection can't be upgraded

var MtQriPeers = MsgType("qri_peers")

MtQriPeers is a request to get a list of known qri peers

func MakeHandlers Uses

func MakeHandlers(n *QriNode) map[MsgType]HandlerFunc

MakeHandlers generates a map of MsgTypes to their corresponding handler functions

func NewMessageID Uses

func NewMessageID() string

NewMessageID generates a random message identifier TODO - replace with UUIDs

func NewTestableQriNode Uses

func NewTestableQriNode(r repo.Repo, p2pconf *config.P2P) (p2ptest.TestablePeerNode, error)

NewTestableQriNode creates a new node, as a TestablePeerNode, usable by testing utilities.

func ParseMultiaddrs Uses

func ParseMultiaddrs(addrs []string) (maddrs []ma.Multiaddr, err error)

ParseMultiaddrs turns a slice of strings into a slice of Multiaddrs

type DatasetLogRequest Uses

type DatasetLogRequest struct {
    Ref    repo.DatasetRef
    Limit  int
    Offset int
}

DatasetLogRequest encapsulates options for requesting dataset history

type DatasetLogResponse Uses

type DatasetLogResponse struct {
    History []repo.DatasetRef
    Err     error
}

DatasetLogResponse encapsulates option for responding to a dataset history request

type DatasetsListParams Uses

type DatasetsListParams struct {
    Term   string
    Limit  int
    Offset int
}

DatasetsListParams encapsulates options for requesting datasets

type EventsParams Uses

type EventsParams struct {
    Limit, Offset int
    Since         time.Time
}

EventsParams encapsulates options for requesting Event logs

type HandlerFunc Uses

type HandlerFunc func(ws *WrappedStream, msg Message) (hangup bool)

HandlerFunc is the signature of a function that can handle p2p messages

type Message Uses

type Message struct {
    Type     MsgType
    ID       string
    Created  time.Time
    Deadline time.Time
    // peer that originated this message
    Initiator peer.ID
    // Headers proxies the concept of HTTP headers, but with no
    // mandatory fields. It's intended to be small & simple on purpose
    // In the future we can upgrade this to map[string]interface{} while keeping
    // backward compatibility
    Headers map[string]string
    // Body carries the payload of a message, if any
    Body []byte
    // contains filtered or unexported fields
}

Message is a serializable/encodable object that we send & receive on a Stream.

func NewJSONBodyMessage Uses

func NewJSONBodyMessage(initiator peer.ID, t MsgType, body interface{}) (Message, error)

NewJSONBodyMessage is a convenience wrapper for json-encoding a message

func NewMessage Uses

func NewMessage(initiator peer.ID, t MsgType, body []byte) Message

NewMessage creates a message. provided initiator should always be the peerID of the local node

func (Message) Header Uses

func (m Message) Header(key string) (value string)

Header gets a header value for a given key

func (Message) Update Uses

func (m Message) Update(body []byte) Message

Update returns a new message with an updated body

func (Message) UpdateJSON Uses

func (m Message) UpdateJSON(body interface{}) (Message, error)

UpdateJSON updates a messages by JSON-encoding a body

func (Message) WithHeaders Uses

func (m Message) WithHeaders(keyval ...string) Message

WithHeaders adds a sequence of key,value,key,value as headers

type MsgType Uses

type MsgType string

MsgType indicates the type of message being sent

func (MsgType) String Uses

func (mt MsgType) String() string

String implements the Stringer interface for MsgType

type PeerConnectionParams Uses

type PeerConnectionParams struct {
    Peername  string
    ProfileID profile.ID
    PeerID    peer.ID
    Multiaddr ma.Multiaddr
}

PeerConnectionParams defines parameters for the ConnectToPeer command

type QriNode Uses

type QriNode struct {
    // ID is the node's identifier both locally & on the network
    // Identity has a relationship to privateKey (hash of PublicKey)
    ID  peer.ID

    // Online indicates weather this is node is connected to the p2p network
    Online bool

    // Discovery service, can be provided by an ipfs node
    Discovery discovery.Service

    // Repo is a repository of this node's qri data
    // note that repo's are built upon a cafs.Filestore, which
    // may contain a reference to a functioning IPFS node. In that case
    // QriNode should piggyback non-qri-specific p2p functionality on the
    // ipfs node provided by repo
    Repo repo.Repo

    // node keeps a set of IOStreams for "node local" io, often to the
    // command line, to give feedback to the user. These may be piped to
    // local http handlers/websockets/stdio, but these streams are meant for
    // local feedback as opposed to p2p connections
    LocalStreams ioes.IOStreams
    // contains filtered or unexported fields
}

QriNode encapsulates a qri peer-2-peer node

func NewQriNode Uses

func NewQriNode(r repo.Repo, p2pconf *config.P2P) (node *QriNode, err error)

NewQriNode creates a new node from a configuration. To get a fully connected node that's searching for peers call: n, _ := NewQriNode(r, cfg) n.GoOnline()

func (*QriNode) Addrs Uses

func (n *QriNode) Addrs() pstore.AddrBook

Addrs returns the AddrBook for the node.

func (*QriNode) AnnounceConnected Uses

func (n *QriNode) AnnounceConnected() error

AnnounceConnected kicks off a notice to other peers that a profile has connected

func (*QriNode) Bootstrap Uses

func (n *QriNode) Bootstrap(boostrapAddrs []string, boostrapPeers chan pstore.PeerInfo)

Bootstrap samples a subset of peers & requests their peers list This is a naive version of IPFS bootstrapping, which we'll add in once qri's settled on a shared-state implementation

func (*QriNode) BootstrapIPFS Uses

func (n *QriNode) BootstrapIPFS()

BootstrapIPFS connects this node to standard ipfs nodes for file exchange

func (*QriNode) ClosestConnectedQriPeers Uses

func (n *QriNode) ClosestConnectedQriPeers(profileID profile.ID, max int) (pid []peer.ID)

ClosestConnectedQriPeers checks if a peer is connected, and if so adds it to the top of a slice cap(max) of peers to try to connect to TODO - In the future we'll use a few tricks to improve on just iterating the list at a bare minimum we should grab a randomized set of peers

func (*QriNode) ConnectToPeer Uses

func (n *QriNode) ConnectToPeer(ctx context.Context, p PeerConnectionParams) (*profile.Profile, error)

ConnectToPeer takes a raw peer ID & tries to work out a route to that peer, explicitly connecting to them.

func (*QriNode) ConnectedPeers Uses

func (n *QriNode) ConnectedPeers() []string

ConnectedPeers lists all IPFS connected peers

func (*QriNode) ConnectedQriPeerIDs Uses

func (n *QriNode) ConnectedQriPeerIDs() []peer.ID

ConnectedQriPeerIDs returns a slice of peer.IDs this peer is currently connected to

func (*QriNode) ConnectedQriProfiles Uses

func (n *QriNode) ConnectedQriProfiles() map[profile.ID]*config.ProfilePod

ConnectedQriProfiles lists all connected peers that support the qri protocol

func (*QriNode) Context Uses

func (n *QriNode) Context() context.Context

Context returns this node's context

func (*QriNode) DisconnectFromPeer Uses

func (n *QriNode) DisconnectFromPeer(ctx context.Context, p PeerConnectionParams) error

DisconnectFromPeer explicitly closes a connection to a peer

func (*QriNode) DiscoverPeerstoreQriPeers Uses

func (n *QriNode) DiscoverPeerstoreQriPeers(store pstore.Peerstore)

DiscoverPeerstoreQriPeers handles the case where a store has seen peers that support the qri protocol, but we haven't added them to our own peers list

func (*QriNode) EncapsulatedAddresses Uses

func (n *QriNode) EncapsulatedAddresses() []ma.Multiaddr

EncapsulatedAddresses returns a slice of full multaddrs for this node

func (*QriNode) GetIPFSNamesys Uses

func (n *QriNode) GetIPFSNamesys() (namesys.NameSystem, error)

GetIPFSNamesys returns a namesystem from IPFS

func (*QriNode) GoOnline Uses

func (n *QriNode) GoOnline() (err error)

GoOnline puts QriNode on the distributed web, ensuring there's an active peer-2-peer host participating in a peer-2-peer network, and kicks off requests to connect to known bootstrap peers that support the QriProtocol

func (*QriNode) HandlePeerFound Uses

func (n *QriNode) HandlePeerFound(pinfo pstore.PeerInfo)

HandlePeerFound deals with the discovery of a peer that may or may not support the qri protocol

func (*QriNode) Host Uses

func (n *QriNode) Host() host.Host

Host returns the node's Host

func (*QriNode) IPFSCoreAPI Uses

func (n *QriNode) IPFSCoreAPI() (coreiface.CoreAPI, error)

IPFSCoreAPI returns a IPFS API interface instance

func (*QriNode) Keys Uses

func (n *QriNode) Keys() pstore.KeyBook

Keys returns the KeyBook for the node.

func (*QriNode) ListenAddresses Uses

func (n *QriNode) ListenAddresses() ([]string, error)

ListenAddresses gives the listening addresses of this node on the p2p network as a slice of strings

func (*QriNode) PeerInfo Uses

func (n *QriNode) PeerInfo(pid peer.ID) pstore.PeerInfo

PeerInfo returns peer peer ID & network multiaddrs from the Host Peerstore

func (*QriNode) Peers Uses

func (n *QriNode) Peers() []peer.ID

Peers returns a list of currently connected peer IDs

func (*QriNode) Ping Uses

func (n *QriNode) Ping(peerID peer.ID) (time.Duration, error)

Ping initiates a ping message from peer to a peer.ID

func (*QriNode) QriStreamHandler Uses

func (n *QriNode) QriStreamHandler(s net.Stream)

QriStreamHandler is the handler we register with the multistream muxer

func (*QriNode) ReceiveMessages Uses

func (n *QriNode) ReceiveMessages() chan Message

ReceiveMessages adds a listener for newly received messages

func (*QriNode) RequestDataset Uses

func (n *QriNode) RequestDataset(ref *repo.DatasetRef) (err error)

RequestDataset fetches info about a dataset from qri peers It's expected the local peer has attempted to canonicalize the reference before sending to the network ref is used as an outparam, populating with data on success

func (*QriNode) RequestDatasetLog Uses

func (n *QriNode) RequestDatasetLog(ref repo.DatasetRef, limit, offset int) ([]repo.DatasetRef, error)

RequestDatasetLog gets the log information of Peer's dataset

func (*QriNode) RequestDatasetsList Uses

func (n *QriNode) RequestDatasetsList(pid peer.ID, p DatasetsListParams) ([]repo.DatasetRef, error)

RequestDatasetsList gets a list of a peer's datasets

func (*QriNode) RequestEventsList Uses

func (n *QriNode) RequestEventsList(pid peer.ID, p EventsParams) ([]*repo.Event, error)

RequestEventsList fetches a log of events from a peer

func (*QriNode) RequestLogDiff Uses

func (n *QriNode) RequestLogDiff(ref *repo.DatasetRef) (ldr base.LogDiffResult, err error)

RequestLogDiff fetches info about a dataset from qri peers It's expected the local peer has attempted to canonicalize the reference before sending to the network

func (*QriNode) RequestNewPeers Uses

func (n *QriNode) RequestNewPeers(ctx context.Context, peers []QriPeer)

RequestNewPeers intersects a provided list of peer info with this node's existing connections and attempts to connect to any peers it doesn't have connections to

func (*QriNode) RequestProfile Uses

func (n *QriNode) RequestProfile(pid peer.ID) (*profile.Profile, error)

RequestProfile get's qri profile information on a peer ID

func (*QriNode) RequestQriPeers Uses

func (n *QriNode) RequestQriPeers(id peer.ID) ([]QriPeer, error)

RequestQriPeers asks a designated peer for a list of qri peers

func (*QriNode) ResolveDatasetRef Uses

func (n *QriNode) ResolveDatasetRef(ref *repo.DatasetRef) (err error)

ResolveDatasetRef completes a dataset reference

func (*QriNode) Search Uses

func (n *QriNode) Search(terms string, limit, offset int) (res []*repo.DatasetRef, err error)

Search broadcasts a search request to all connected peers, aggregating results

func (*QriNode) SendMessage Uses

func (n *QriNode) SendMessage(msg Message, replies chan Message, pids ...peer.ID) error

SendMessage opens a stream & sends a message from p to one ore more peerIDs

func (*QriNode) SimplePeerInfo Uses

func (n *QriNode) SimplePeerInfo() pstore.PeerInfo

SimplePeerInfo returns a PeerInfo with just the ID and Addresses.

func (*QriNode) StartDiscovery Uses

func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error

StartDiscovery initiates peer discovery, allocating a discovery services if one doesn't exist, then registering to be notified on peer discovery

func (*QriNode) UpgradeToQriConnection Uses

func (n *QriNode) UpgradeToQriConnection(pinfo pstore.PeerInfo) error

UpgradeToQriConnection attempts to open a Qri protocol connection to a peer it records whether the peer supports Qri in the host Peerstore, returns ErrQriProtocolNotSupported if the connection cannot be upgraded, and sets a priority in the host Connection Manager if the connection is upgraded

type QriPeer Uses

type QriPeer struct {
    ProfileID    string
    PeerID       string
    NetworkAddrs []string
}

QriPeer is a minimial struct that combines a profileID & network addresses

type WrappedStream Uses

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

WrappedStream wraps a libp2p stream. We encode/decode whenever we write/read from a stream, so we can just carry the encoders and bufios with us

func WrapStream Uses

func WrapStream(s net.Stream) *WrappedStream

WrapStream takes a stream and complements it with r/w bufios and decoder/encoder. In order to write raw data to the stream we can use wrap.w.Write(). To encode something into it we can wrap.enc.Encode(). Finally, we should wrap.w.Flush() to actually send the data. Handling incoming data works similarly with wrap.r.Read() for raw-reading and wrap.dec.Decode() to decode.

Directories

PathSynopsis
testPackage p2ptest defines utilities for qri peer-2-peer testing

Package p2p imports 39 packages (graph) and is imported by 8 packages. Updated 2019-07-22. Refresh now. Tools for package owners.