- Official website: https://libp2p.io/
- Code: HTTPS://GITHUB.COM/LIBP2P/GO-LIBP2P
Install go-libp2p
Go get-u-D github.com/libp2p/go-libp2p/...
CD $GOPATH/src/github.com/libp2p/go-libp2p
Make
Make Deps
The official website gives the installation instructions:
Go get in-u means download the latest version of the dependency,-D means only download does not install, of course you need to install Golang to use go get, it is recommended to install 1.10.x version
Golang installation package: HTTPS://STUDYGOLANG.COM/DL
You need to download the GX components when make, if you want to use make directly to download the best or "scientific Internet", if you download the GX directly on GitHub do not have to worry about "scientific internet problem"
Make Deps is to download all dependencies, here will use the GX to complete the download, the first time you will find that he is going to Ipfs.io to get the dependency package, but even "scientific internet" is still inaccessible, in fact, as long as the local first start a IPFs node on it, he will priority in the local IPF s node Get resources
IPFs Download and install: https://ipfs.io/docs/install/
Start command: IPFs daemon
Then go make deps after a long wait, you can complete the dependent installation
Many small partners are not in this step to give up the go-libp2p ah? In fact, I also quite dislike this GX, as Govendor with comfortable, unfortunately want to continue to explore go-libp2p you have to master the use of GX, and to get used to using GX
GX Tutorial: HTTPS://GITHUB.COM/WHYRUSLEEPING/GX
Installation is still relatively easy, be sure to look at the manual and then go to see the code and examples, it is difficult to find its beauty
LIBP2P Manual: Https://github.com/libp2p/specs
Example
Pingservice
Using LIBP2P to do the service is the purpose of our study, through the Pingservice to get started is a good choice, simply look at the Pingservice code, you will find the implementation of a service is very simple, host. The Host interface is the core
Pingservice:http://github.com/libp2p/go-libp2p/p2p/protocol/ping/ping.go
......
const ID = "/ipfs/ping/1.0.0"
type PingService struct {
Host host.Host
}
func NewPingService(h host.Host) *PingService {
ps := &PingService{h}
h.SetStreamHandler(ID, ps.PingHandler)
return ps
}
func (p *PingService) PingHandler(s inet.Stream) {
......
}
func (ps *PingService) Ping(ctx context.Context, p peer.ID) (<-chan time.Duration, error) {
s, err := ps.Host.NewStream(ctx, p, ID)
......
}
......
This code snippet demonstrates how to pass the host. The host interface constructs a service, and the host interface is described as follows:
Host is an object participating in a peer network, which implements protocols or provides services. It handles requests like a Server, and issues requests like a Client. It is the called Host because it is both Server and Client (and Peer could be confusing).
The code for the Host interface is posted at the end of the article, and this service uses the following two methods mainly
-
Setstreamhandler (PID protocol.id, Handler inet. Streamhandler)
The callback method is specified by Host.setstreamhandler for the "/ipfs/ping/1.0.0" protocol, where the Pinghandler method, the inet in the parameter, is specified. The stream interface extends the IO interface, where the input stream of S is read when the request arrives.
-
Newstream (CTX context. Context, P peer.id, PIDs ... protocol.id) (inet. Stream, error)
When the Ping method is called, first open a stream through the newstream, open the stream in the peer network is a target, this goal is peer.id, because this peer may register a lot of services, so also to indicate the service ID, is the parameter protocol.id, The rest of the work is to write packets to the output stream of S.
TODO: Call Pingservice
Feel that invoking a service is more complex than writing a service,
First of all, Newhost is a very complex operation, the parameters of the Network interface we use Swarm to fill, then Swarm what is it? It is here an implementation of the Network interface,
The host interface has two implementations, and we use Basichost to instantiate the host interface.
Basichost: (Github.com/libp2p/go-libp2p/p2p/host/basic/basic_host.go)
The second parameter net is the type of the Network interface. We can fill it with a swarm object to get an implementation of the Host interface.
// NewHost constructs a new * BasicHost and activates it by attaching its stream and connection handlers to the given [inet.Network] (http://inet.Network).
func NewHost (ctx context.Context, net inet.Network, opts * HostOpts) (* BasicHost, error) {
...
Go-libp2p-host interface
// Host is an object participating in a p2p network, which implements protocols or provides services. It handles requests like a Server, and issues requests like a Client. It is called Host because it is both Server and Client (and Peer may be confusing).
type Host interface {
// ID returns the (local) [peer.ID](http://peer.ID) associated with this Host
ID() peer.ID
// Peerstore returns the Host's repository of Peer Addresses and Keys.
Peerstore() pstore.Peerstore
// Returns the listen addresses of the Host
Addrs() []ma.Multiaddr
// Networks returns the Network interface of the Host
Network() inet.Network
// Mux returns the Mux multiplexing incoming streams to protocol handlers
Mux() *msmux.MultistreamMuxer
// Connect ensures there is a connection between this host and the peer with given [peer.ID](http://peer.ID). Connect will absorb the addresses in pi into its internal peerstore. If there is not an active connection, Connect will issue a h.Network.Dial, and block until a connection is open, or an error is returned.
// TODO: Relay + NAT.
Connect(ctx context.Context, pi pstore.PeerInfo) error
// SetStreamHandler sets the protocol handler on the Host's Mux.
// This is equivalent to:
// host.Mux().SetHandler(proto, handler)
// (Threadsafe)
SetStreamHandler(pid protocol.ID, handler inet.StreamHandler)
// SetStreamHandlerMatch sets the protocol handler on the Host's Mux
// using a matching function for protocol selection.
SetStreamHandlerMatch(protocol.ID, func(string) bool, inet.StreamHandler)
// RemoveStreamHandler removes a handler on the mux that was set by
// SetStreamHandler
RemoveStreamHandler(pid protocol.ID)
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given [protocol.ID](http://protocol.ID). If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header.
// (Threadsafe)
NewStream(ctx context.Context, p peer.ID, pids ...protocol.ID) (inet.Stream, error)
// Close shuts down the host, its Network, and services.
Close() error
// ConnManager returns this hosts connection manager
ConnManager() ifconnmgr.ConnManager
}
Network interface in Go-libp2p-net
// Network is the interface used to connect to the outside world. It dials and listens for connections. it uses a Swarm to pool connnections (see swarm pkg, and peerstream.Swarm). Connections are encrypted with a TLS-like protocol.
type Network interface {
Dialer
io.Closer
// SetStreamHandler sets the handler for new streams opened by the remote side. This operation is threadsafe.
SetStreamHandler(StreamHandler)
// SetConnHandler sets the handler for new connections opened by the remote side. This operation is threadsafe.
SetConnHandler(ConnHandler)
// NewStream returns a new stream to given peer p. If there is no connection to p, attempts to create one.
NewStream(context.Context, peer.ID) (Stream, error)
// Listen tells the network to start listening on given multiaddrs.
Listen(...ma.Multiaddr) error
// ListenAddresses returns a list of addresses at which this network listens.
ListenAddresses() []ma.Multiaddr
// InterfaceListenAddresses returns a list of addresses at which this network listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to use the known local interfaces.
InterfaceListenAddresses() ([]ma.Multiaddr, error)
// Process returns the network's Process
Process() goprocess.Process
}