forked from xiaojiong/DhtCrawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dht.go
51 lines (41 loc) · 971 Bytes
/
Dht.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
package DhtCrawler
import (
"fmt"
"io"
"log"
)
type DhtNode struct {
node *KNode
table *KTable
network *Network
log *log.Logger
master chan string
krpc *KRPC
outChan chan string
}
func NewDhtNode(id *Id, logger io.Writer, outHashIdChan chan string, master chan string) *DhtNode {
node := new(KNode)
node.Id = *id
dht := new(DhtNode)
dht.outChan = outHashIdChan
dht.log = log.New(logger, "", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
dht.node = node
dht.table = new(KTable)
dht.network = NewNetwork(dht)
dht.krpc = NewKRPC(dht)
dht.master = master
return dht
}
func (dht *DhtNode) Run() {
//当前DHT节点运转进程
go func() { dht.network.Listening() }()
//自动结交更多DHT节点进程进程
go func() { dht.NodeFinder() }()
dht.log.Println(fmt.Sprintf("DhtCrawler %s is runing...", dht.network.Conn.LocalAddr().String()))
for {
select {
case msg := <-dht.master:
dht.log.Println(msg)
}
}
}