Documentation ¶
Overview ¶
Package p2p implements qri peer-to-peer communication.
Index ¶
- Constants
- Variables
- func MakeHandlers(n *QriNode) map[MsgType]HandlerFunc
- func NewMessageID() string
- func NewTestableQriNode(r repo.Repo, p2pconf *config.P2P) (p2ptest.TestablePeerNode, error)
- func ParseMultiaddrs(addrs []string) (maddrs []ma.Multiaddr, err error)
- type DatasetLogRequest
- type DatasetLogResponse
- type DatasetsListParams
- type EventsParams
- type HandlerFunc
- type Message
- type MsgType
- type PeerConnectionParams
- type QriNode
- func (n *QriNode) Addrs() pstore.AddrBook
- func (n *QriNode) AnnounceConnected() error
- func (n *QriNode) Bootstrap(boostrapAddrs []string, boostrapPeers chan pstore.PeerInfo)
- func (n *QriNode) BootstrapIPFS()
- func (n *QriNode) ClosestConnectedQriPeers(profileID profile.ID, max int) (pid []peer.ID)
- func (n *QriNode) ConnectToPeer(ctx context.Context, p PeerConnectionParams) (*profile.Profile, error)
- func (n *QriNode) ConnectedPeers() []string
- func (n *QriNode) ConnectedQriPeerIDs() []peer.ID
- func (n *QriNode) ConnectedQriProfiles() map[profile.ID]*config.ProfilePod
- func (n *QriNode) Context() context.Context
- func (n *QriNode) DisconnectFromPeer(ctx context.Context, p PeerConnectionParams) error
- func (n *QriNode) DiscoverPeerstoreQriPeers(store pstore.Peerstore)
- func (n *QriNode) EncapsulatedAddresses() []ma.Multiaddr
- func (n *QriNode) GetIPFSNamesys() (namesys.NameSystem, error)
- func (n *QriNode) GoOnline() (err error)
- func (n *QriNode) HandlePeerFound(pinfo pstore.PeerInfo)
- func (n *QriNode) Host() host.Host
- func (n *QriNode) IPFSCoreAPI() (coreiface.CoreAPI, error)
- func (n *QriNode) Keys() pstore.KeyBook
- func (n *QriNode) ListenAddresses() ([]string, error)
- func (n *QriNode) PeerInfo(pid peer.ID) pstore.PeerInfo
- func (n *QriNode) Peers() []peer.ID
- func (n *QriNode) Ping(peerID peer.ID) (time.Duration, error)
- func (n *QriNode) QriStreamHandler(s net.Stream)
- func (n *QriNode) ReceiveMessages() chan Message
- func (n *QriNode) RequestDataset(ref *repo.DatasetRef) (err error)
- func (n *QriNode) RequestDatasetLog(ref repo.DatasetRef, limit, offset int) ([]repo.DatasetRef, error)
- func (n *QriNode) RequestDatasetsList(pid peer.ID, p DatasetsListParams) ([]repo.DatasetRef, error)
- func (n *QriNode) RequestEventsList(pid peer.ID, p EventsParams) ([]*repo.Event, error)
- func (n *QriNode) RequestLogDiff(ref *repo.DatasetRef) (ldr base.LogDiffResult, err error)
- func (n *QriNode) RequestNewPeers(ctx context.Context, peers []QriPeer)
- func (n *QriNode) RequestProfile(pid peer.ID) (*profile.Profile, error)
- func (n *QriNode) RequestQriPeers(id peer.ID) ([]QriPeer, error)
- func (n *QriNode) ResolveDatasetRef(ref *repo.DatasetRef) (err error)
- func (n *QriNode) Search(terms string, limit, offset int) (res []*repo.DatasetRef, err error)
- func (n *QriNode) SendMessage(msg Message, replies chan Message, pids ...peer.ID) error
- func (n *QriNode) SimplePeerInfo() pstore.PeerInfo
- func (n *QriNode) StartDiscovery(bootstrapPeers chan pstore.PeerInfo) error
- func (n *QriNode) UpgradeToQriConnection(pinfo pstore.PeerInfo) error
- type QriPeer
- type WrappedStream
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.0" )
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
Functions ¶
func MakeHandlers ¶ added in v0.3.0
func MakeHandlers(n *QriNode) map[MsgType]HandlerFunc
MakeHandlers generates a map of MsgTypes to their corresponding handler functions
func NewMessageID ¶ added in v0.3.0
func NewMessageID() string
NewMessageID generates a random message identifier TODO - replace with UUIDs
func NewTestableQriNode ¶ added in v0.5.2
NewTestableQriNode creates a new node, as a TestablePeerNode, usable by testing utilities.
Types ¶
type DatasetLogRequest ¶ added in v0.5.5
type DatasetLogRequest struct { Ref repo.DatasetRef Limit int Offset int }
DatasetLogRequest encapsulates options for requesting dataset history
type DatasetLogResponse ¶ added in v0.5.5
type DatasetLogResponse struct { History []repo.DatasetRef Err error }
DatasetLogResponse encapsulates option for responding to a dataset history request
type DatasetsListParams ¶ added in v0.3.0
DatasetsListParams encapsulates options for requesting datasets
type EventsParams ¶ added in v0.3.0
EventsParams encapsulates options for requesting Event logs
type HandlerFunc ¶ added in v0.3.0
type HandlerFunc func(ws *WrappedStream, msg Message) (hangup bool)
HandlerFunc is the signature of a function that can handle p2p messages
type Message ¶
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 ¶ added in v0.3.0
NewJSONBodyMessage is a convenience wrapper for json-encoding a message
func NewMessage ¶ added in v0.3.0
NewMessage creates a message. provided initiator should always be the peerID of the local node
func (Message) UpdateJSON ¶ added in v0.3.0
UpdateJSON updates a messages by JSON-encoding a body
func (Message) WithHeaders ¶ added in v0.3.0
WithHeaders adds a sequence of key,value,key,value as headers
type PeerConnectionParams ¶ added in v0.3.2
type PeerConnectionParams struct { Peername string ProfileID profile.ID PeerID peer.ID Multiaddr ma.Multiaddr }
PeerConnectionParams defines parameters for the ConnectToPeer command
type QriNode ¶
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 ¶
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) AnnounceConnected ¶ added in v0.3.2
AnnounceConnected kicks off a notice to other peers that a profile has connected
func (*QriNode) Bootstrap ¶
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 ¶
func (n *QriNode) BootstrapIPFS()
BootstrapIPFS connects this node to standard ipfs nodes for file exchange
func (*QriNode) ClosestConnectedQriPeers ¶ added in v0.6.1
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 ¶
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 ¶
ConnectedPeers lists all IPFS connected peers
func (*QriNode) ConnectedQriPeerIDs ¶ added in v0.3.2
ConnectedQriPeerIDs returns a slice of peer.IDs this peer is currently connected to
func (*QriNode) ConnectedQriProfiles ¶ added in v0.3.0
func (n *QriNode) ConnectedQriProfiles() map[profile.ID]*config.ProfilePod
ConnectedQriProfiles lists all connected peers that support the qri protocol
func (*QriNode) DisconnectFromPeer ¶ added in v0.3.2
func (n *QriNode) DisconnectFromPeer(ctx context.Context, p PeerConnectionParams) error
DisconnectFromPeer explicitly closes a connection to a peer
func (*QriNode) DiscoverPeerstoreQriPeers ¶
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 ¶
EncapsulatedAddresses returns a slice of full multaddrs for this node
func (*QriNode) GetIPFSNamesys ¶ added in v0.8.0
func (n *QriNode) GetIPFSNamesys() (namesys.NameSystem, error)
GetIPFSNamesys returns a namesystem from IPFS
func (*QriNode) GoOnline ¶ added in v0.6.0
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 ¶
HandlePeerFound deals with the discovery of a peer that may or may not support the qri protocol
func (*QriNode) IPFSCoreAPI ¶ added in v0.8.0
IPFSCoreAPI returns a IPFS API interface instance
func (*QriNode) ListenAddresses ¶ added in v0.3.0
ListenAddresses gives the listening addresses of this node on the p2p network as a slice of strings
func (*QriNode) PeerInfo ¶ added in v0.3.2
PeerInfo returns peer peer ID & network multiaddrs from the Host Peerstore
func (*QriNode) QriStreamHandler ¶ added in v0.3.0
QriStreamHandler is the handler we register with the multistream muxer
func (*QriNode) ReceiveMessages ¶ added in v0.3.0
ReceiveMessages adds a listener for newly received messages
func (*QriNode) RequestDataset ¶ added in v0.3.0
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 ¶ added in v0.1.1
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 ¶
func (n *QriNode) RequestDatasetsList(pid peer.ID, p DatasetsListParams) ([]repo.DatasetRef, error)
RequestDatasetsList gets a list of a peer's datasets
func (*QriNode) RequestEventsList ¶ added in v0.3.0
RequestEventsList fetches a log of events from a peer
func (*QriNode) RequestLogDiff ¶ added in v0.6.0
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 ¶ added in v0.5.6
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 ¶ added in v0.3.0
RequestProfile get's qri profile information on a peer ID
func (*QriNode) RequestQriPeers ¶ added in v0.5.6
RequestQriPeers asks a designated peer for a list of qri peers
func (*QriNode) ResolveDatasetRef ¶ added in v0.5.2
func (n *QriNode) ResolveDatasetRef(ref *repo.DatasetRef) (err error)
ResolveDatasetRef completes a dataset reference
func (*QriNode) Search ¶
Search broadcasts a search request to all connected peers, aggregating results
func (*QriNode) SendMessage ¶
SendMessage opens a stream & sends a message from p to one ore more peerIDs
func (*QriNode) SimplePeerInfo ¶ added in v0.3.2
SimplePeerInfo returns a PeerInfo with just the ID and Addresses.
func (*QriNode) StartDiscovery ¶
StartDiscovery initiates peer discovery, allocating a discovery services if one doesn't exist, then registering to be notified on peer discovery
func (*QriNode) UpgradeToQriConnection ¶ added in v0.6.0
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 ¶ added in v0.5.6
QriPeer is a minimial struct that combines a profileID & network addresses
type WrappedStream ¶
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 ¶
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.