seed rand, add dht command

This commit is contained in:
Alex Grintsvayg 2018-02-27 13:30:54 -05:00
parent ef5a03747d
commit 058f350ad0
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
2 changed files with 67 additions and 0 deletions

63
cmd/dht.go Normal file
View file

@ -0,0 +1,63 @@
package cmd
import (
"strconv"
"time"
"github.com/lbryio/lbry.go/dht"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
d := &cobra.Command{
Use: "dht <action>",
Args: cobra.ExactArgs(1),
Short: "Do DHT things",
Run: dhtCmd,
}
RootCmd.AddCommand(d)
ping := &cobra.Command{
Use: "ping <ip>",
Args: cobra.ExactArgs(1),
Short: "Ping a node on the DHT",
Run: dhtPingCmd,
}
d.AddCommand(ping)
}
func dhtCmd(cmd *cobra.Command, args []string) {
log.Errorln("chose a command")
}
func dhtPingCmd(cmd *cobra.Command, args []string) {
//ip := args[0]
port := 49449 // + (rand.Int() % 10)
config := dht.NewStandardConfig()
config.Address = "127.0.0.1:" + strconv.Itoa(port)
config.PrimeNodes = []string{
"127.0.0.1:10001",
}
d := dht.New(config)
log.Println("Starting...")
go d.Run()
time.Sleep(2 * time.Second)
for {
peers, err := d.FindNode("012b66fc7052d9a0c8cb563b8ede7662003ba65f425c2661b5c6919d445deeb31469be8b842d6faeea3f2b3ebcaec845")
if err != nil {
time.Sleep(time.Second * 1)
continue
}
log.Println("Found peers:", peers)
break
}
log.Println("done")
}

View file

@ -1,6 +1,9 @@
package main
import (
"math/rand"
"time"
"github.com/lbryio/lbry.go/cmd"
log "github.com/sirupsen/logrus"
@ -9,6 +12,7 @@ import (
var Version string
func main() {
rand.Seed(time.Now().UnixNano())
log.SetLevel(log.DebugLevel)
cmd.Execute()
}