-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupernode.go
98 lines (86 loc) · 2.18 KB
/
supernode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"bufio"
l4g "code.google.com/p/log4go"
"crypto/sha1"
"fmt"
"github.com/nictuku/dht"
"io"
"net/http"
"os"
"time"
)
const (
httpPort = 8080
)
func main() {
var numTargetPeers int
var sendAnnouncements bool
var file *os.File
var num int
var shalist [64]string
var err error
var part []byte
l4g.AddFilter("stdout", l4g.DEBUG, l4g.NewConsoleLogWriter())
if file, err = os.Open("hops.log"); err != nil {
return
}
reader := bufio.NewReader(file)
num = 0
for {
if part, _, err = reader.ReadLine(); err != nil {
break
}
if err == io.EOF {
err = nil
}
hasher := sha1.New()
hasher.Write(part)
shalist[num] = string(hasher.Sum(nil))
num = num + 1
}
sendAnnouncements = true
numTargetPeers = 64
port := 42345
l4g.Error("used port %d", port)
dht, err := dht.NewDHTNode(port, numTargetPeers, false)
if err != nil {
l4g.Error("DHT node creation error", err)
return
}
go dht.DoDHT()
go drainresults(dht)
queryTick := time.Tick(20 * time.Second)
go http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil)
for {
select {
case <-queryTick:
// trying the manual method from dht_test in case I have the format
// wrong
fmt.Printf("TICK ************************************\n")
fmt.Printf("TICK ************************************\n")
fmt.Printf("TICK ************************************\n")
fmt.Printf("TICK ************************************\n")
fmt.Printf("TICK ************************************\n")
fmt.Printf("TICK ************************************\n")
for i := 0; i < num; i++ {
l4g.Info("querying for infoHash: %x", shalist[i])
dht.PeersRequest(shalist[i], sendAnnouncements)
time.Sleep(10 * time.Second)
}
}
}
}
// drainresults loops, constantly reading any new peer information sent by the
// DHT node and just ignoring them. We don't care about those :-P.
// drainresults loops, printing the address of nodes it has found.
func drainresults(n *dht.DHT) {
fmt.Println("=========================== DHT")
for r := range n.PeersRequestResults {
for ih, peers := range r {
for _, x := range peers {
l4g.Warn("Found peer for infohash %x at %v", ih,dht.DecodePeerAddress(x))
}
}
}
}