From 6435335c3315a09959c0b38a3b4a1f5383df4b1c Mon Sep 17 00:00:00 2001 From: David Florness Date: Tue, 2 Jun 2020 22:44:57 -0600 Subject: [PATCH] No need for routing discovery when we have the ID of the node --- main.go | 75 ++++++++++++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 46 deletions(-) diff --git a/main.go b/main.go index 5b5c6ad..a1f81a8 100644 --- a/main.go +++ b/main.go @@ -13,8 +13,8 @@ import ( "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/protocol" - discovery "github.com/libp2p/go-libp2p-discovery" dht "github.com/libp2p/go-libp2p-kad-dht" + routing "github.com/libp2p/go-libp2p-routing" "github.com/rivo/tview" "github.com/whyrusleeping/go-logging" ) @@ -22,7 +22,6 @@ import ( var ( Logger = log.Logger("tallyard") ProtocolID = protocol.ID("/tallyard/0.0.0") - rendezvousString = "ohea7" electionOptions []string masterID peer.ID @@ -100,23 +99,23 @@ func bootstrap() { ctx = context.Background() h, err = libp2p.New(ctx, - // libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) { - // var err error - // kdht, err = dht.New(ctx, h) - // return kdht, err - // }), + libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) { + var err error + kdht, err = dht.New(ctx, h) + if err != nil { + return kdht, err + } + Logger.Info("boostrapping the DHT") + if err = kdht.Bootstrap(ctx); err != nil { + panic(err) + } + return kdht, err + }), ) if err != nil { panic(err) } - - kdht, err = dht.New(ctx, h) - Logger.Info("boostrapping the DHT") - if err = kdht.Bootstrap(ctx); err != nil { - panic(err) - } - Logger.Info("host:", h.ID()) Logger.Info(h.Addrs()) @@ -129,52 +128,36 @@ func bootstrap() { if err := h.Connect(ctx, *peerInfo); err != nil { Logger.Warning(err) } else { - Logger.Info("connection established with bootstrap node: ", *peerInfo) + Logger.Info("connection established with bootstrap node:", *peerInfo) } }() } wg.Wait() - Logger.Info("announcing ourselves...") - routingDiscovery := discovery.NewRoutingDiscovery(kdht) - discovery.Advertise(ctx, routingDiscovery, rendezvousString) - Logger.Info("successfully announced!") - if masterID == "" { // we are the master h.SetStreamHandler(ProtocolID, masterStreamHandler) - } else { // we are a slave - Logger.Info("searching for peers...") - peerChan, err := routingDiscovery.FindPeers(ctx, rendezvousString) + } else { // we are a slave + Logger.Info("attempting to open stream with master peer...") + stream, err := h.NewStream(ctx, masterID, ProtocolID) if err != nil { panic(err) } - for p := range peerChan { - Logger.Info("found peer:", p) - if p.ID != masterID { - continue - } - Logger.Info("attempting to open stream with peer...") - stream, err := h.NewStream(ctx, masterID, ProtocolID) - if err != nil { + Logger.Info("opened stream with master peer") + reader := bufio.NewReader(stream) + for { + str, err := reader.ReadString('\n') + if err == io.EOF { + break + } else if err != nil { panic(err) } - Logger.Info("opened stream with master peer") - reader := bufio.NewReader(stream) - for { - str, err := reader.ReadString('\n') - if err == io.EOF { - break - } else if err != nil { - panic(err) - } - if str[len(str)-1] == '\n' { - str = str[:len(str)-1] - } - fmt.Println(str) + if str[len(str)-1] == '\n' { + str = str[:len(str)-1] } - stream.Close() - Logger.Info("stream with master peer closed") + fmt.Println(str) } + stream.Close() + Logger.Info("stream with master peer closed") } } -- 2.38.4