Skip to content

Instantly share code, notes, and snippets.

@nictuku
Created November 10, 2012 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nictuku/4052652 to your computer and use it in GitHub Desktop.
Save nictuku/4052652 to your computer and use it in GitHub Desktop.
supernode (incomplete)
// this is not complete and doesn't compile, but hopefully it's a useful guideline.
randInfoHash := false
if infoHash != "" {
if infoHash == "rand" {
randInfoHash = true
// No need to insist in getting peers for a specific infohash,
// since we're probing the address space.
numTargetPeers = 1
} else {
_, err := fmt.Sscanf(infoHash, "%x", &infoHash)
if err != nil {
l4g.Exitf("infoHash:", err.Error())
}
if len(infoHash) != 20 {
l4g.Exitf("len(infoHash): got %d, want %d", len(infoHash), 20)
}
l4g.Stderr("Initial infoHash: %x", infoHash)
}
}
dht, err := dht.NewDHTNode(port, numTargetPeers, true)
if err != nil {
l4g.Error("DHT node creation error", err)
return
}
go dht.DoDHT()
go drainresults(dht)
go func() {
l4g.Exit(http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil))
}()
queryTick := time.Tick(10 * time.Second)
for {
select {
case <-queryTick:
if randInfoHash {
infoHash = string(nodeId())
l4g.Info("querying for infoHash: %x", infoHash)
}
if infoHash != "" {
go dht.PeersRequest(infoHash, sendAnnouncements)
}
}
}
}
func nodeId() []byte {
b := make([]byte, 20)
if _, err := rand.Read(b); err != nil {
l4g.Exitf("nodeId rand: %v", err)
}
return b
}
// drainresults loops, constantly reading any new peer information sent by the
// DHT node and just ignoring them. We don't care about those :-P.
func drainresults(n *dht.DHTEngine) {
for {
<-n.PeersRequestResults
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment