Textile's libp2p alphabet, part 2

Translation  of an  entry-level article on the Textile project blog   from December 12, 2019.





In the  previous article,  we started with the question: "How do you approach your first p2p application?" After some thought, we quickly concluded that the decision  not to rely on a centralized server  and to focus on making a peer- to- peer application  came with a lot of added complexity. The two main groups of "problems" are  application state  and  infrastructure protocol diversity . Fortunately, we've found that we don't have to reinvent the wheel by re-solving a bunch of infrastructure problems — instead, we can use a great p2p networking stack: the libp2p library.





In today's post we'll go a little further and introduce a toy application to get a feel for how you can actually develop something with libp2p and hopefully motivate you to create your own p2p application. Seriously, you'd be surprised how easy it is!





application

Let's make a reservation right away , our program will now be written in the Go language  , using the  go-libp2p library . If you are not yet familiar with this language, we strongly recommend that you familiarize yourself. It's really good for applications dealing with concurrency and networking (like handling lots of p2p connections). Most of the IPFS / libp2p libraries have their base implementations written in Go. The golang.org tour is a great introduction to Go  .





So, our program will be a simple ping-pong application with some additional tweaks to make it more interesting, unlike the usual unsophisticated examples. Here are some of the features of our app (don't worry, we'll cover those in more detail later):





  • By default, the application clings to a free TCP port.





  • If the quic flag is specified, it will also connect to a listening port QUIC, which will become the preferred host address for playing ping-pong.





  • The host will use the mDNS service to discover new hosts on the local network.





  • On each newly discovered node (say, node A), our application will run its own sayMyAddr protocol (we will implement it), which will learn for us the preferred address for playing ping-pong for this node.





  • , - «» -. , , Ping, A Pong. !





( p2p-) . , :





  • (TCP, QUIC ..) ?





  • (, mDNS) - , , ?





  • (Streams) ? - , ?





, , , libp2p -   . , !





!

. ,   !  , , . , :









git clone git@github.com:textileio/go-libp2p-primer-article.git
cd go-libp2p-primer-article
code . //   VSCode,    -    ;)
      
      



:  main.go, , libp2p. , . , -quic true, QUIC. ! , : RegisterSayPreferAddr RegisterPingPong. ,  mDNS.





 discovery.go, mDNS. , , mDNS , . -  discovery.Notifee, , mDNS , .  :





  1. - ; -. …





  2. SayPreferAddr, , (addr) -. , …





  3. , PingPong, , ( ).





,  pingpong.go  RegisterPingPong, main.go, :





  • Handler: , PingPong. Handler HTTP REST.  Stream, io.ReadWriteCloser, , - .





  • playPingPong: ;  Stream  PingPong.





, - , . , , . , , ,  saymyaddr.go, pingpong.go.





, , , , , libp2p.





-, : go run * .go , go run * .go -quic . , -quic:





, , , , mDNS . "" -. "" , (- 5- , mDNS) "" , , ,    -.





, PingPong , - (multiaddr), , , QUIC.    -quic , !





, -quic, - , PingPong , QUIC.    -, ,   . , ?





?

. p2p- . PingPong , , - ! , , . pingpong.go:









const (
    protoPingPong = "/pingpong/1.0.0"
)
...
func RegisterPingPong(h host.Host) {
    pp := &pingPong{host: h}
    //     _pingpong_ .
    //  ,   /  ,
    //        ,
    //    , 
    //      .
    //  ,     semver,
    // . : http://bit.ly/2YaJsJr
    h.SetStreamHandler(protoPingPong, pp.Handler)
}
      
      



.





, mDNS. , ?  Kademlia DHT   pubsub - , .





, , .  - , , libp2p .  libp2p!





Libp2p    , p2p-.    - libp2p, , . , .





: , libp2p Go-, go get, , .  Usage readme- go-libp2p.





, , , , p2p- , ! - , ! ,   Slack, p2p-,  Twitter,  Textile. ,   , , P2P!





Ignacio Hagopian





: (StarVer)








All Articles