Created
October 13, 2015 01:48
-
-
Save corpix/0e96a477404a903c3ba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/nictuku/dht" | |
"log" | |
"play/dht/utils" | |
) | |
func main() { | |
config := dht.NewConfig() | |
config.Port = 11223 | |
config.DHTRouters = "" | |
network, err := dht.New(config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
go network.Run() | |
go utils.DrainResults(network) | |
utils.Poll(network) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/nictuku/dht" | |
"log" | |
"play/dht/utils" | |
) | |
func main() { | |
config := dht.NewConfig() | |
config.DHTRouters = "127.0.0.1:11223" | |
network, err := dht.New(config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
go network.Run() | |
go utils.DrainResults(network) | |
utils.Poll(network) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
// play/dht/utils | |
import ( | |
"github.com/nictuku/dht" | |
"log" | |
"time" | |
) | |
func DrainResults(n *dht.DHT) { | |
count := 0 | |
softPeers := 10 | |
for result := range n.PeersRequestResults { | |
for infoHash, peers := range result { | |
for _, addr := range peers { | |
log.Printf( | |
"Found peer %s %d: %v\n", | |
infoHash, | |
count, | |
dht.DecodePeerAddress(addr), | |
) | |
count++ | |
if count >= softPeers { | |
log.Printf("Too much peers %s \n", count) | |
softPeers *= 2 | |
} | |
} | |
} | |
} | |
} | |
func Poll(n *dht.DHT) { | |
for { | |
n.PeersRequest("a27e7ec7c62b1f899e07298ca96741b2951a2404", true) | |
time.Sleep(time.Second * 5) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment