Commit aa99bbb5 authored by Matt Joiner's avatar Matt Joiner
Browse files

Move things around a bit

parent 5f3f5f06
package dht
import (
"sync"
"github.com/anacrolix/dht/v2/krpc"
)
// Populates the node table.
func (s *Server) Bootstrap() (ts TraversalStats, err error) {
initialAddrs, err := s.traversalStartingNodes()
if err != nil {
return
}
var outstanding sync.WaitGroup
triedAddrs := newBloomFilterForTraversal()
var onAddr func(addr Addr)
onAddr = func(addr Addr) {
if triedAddrs.Test([]byte(addr.String())) {
return
}
ts.NumAddrsTried++
outstanding.Add(1)
triedAddrs.AddString(addr.String())
s.findNode(addr, s.id, func(m krpc.Msg, err error) {
defer outstanding.Done()
s.mu.Lock()
defer s.mu.Unlock()
if err != nil {
return
}
ts.NumResponses++
if r := m.R; r != nil {
r.ForAllNodes(func(ni krpc.NodeInfo) {
onAddr(NewAddr(ni.Addr.UDP()))
})
}
})
}
s.mu.Lock()
for _, addr := range initialAddrs {
onAddr(NewAddr(addr.Addr.UDP()))
}
s.mu.Unlock()
outstanding.Wait()
return
}
......@@ -817,55 +817,6 @@ func (s *Server) findNode(addr Addr, targetID int160, callback func(krpc.Msg, er
})
}
type TraversalStats struct {
NumAddrsTried int
NumResponses int
}
func (me TraversalStats) String() string {
return fmt.Sprintf("%#v", me)
}
// Populates the node table.
func (s *Server) Bootstrap() (ts TraversalStats, err error) {
initialAddrs, err := s.traversalStartingNodes()
if err != nil {
return
}
var outstanding sync.WaitGroup
triedAddrs := newBloomFilterForTraversal()
var onAddr func(addr Addr)
onAddr = func(addr Addr) {
if triedAddrs.Test([]byte(addr.String())) {
return
}
ts.NumAddrsTried++
outstanding.Add(1)
triedAddrs.AddString(addr.String())
s.findNode(addr, s.id, func(m krpc.Msg, err error) {
defer outstanding.Done()
s.mu.Lock()
defer s.mu.Unlock()
if err != nil {
return
}
ts.NumResponses++
if r := m.R; r != nil {
r.ForAllNodes(func(ni krpc.NodeInfo) {
onAddr(NewAddr(ni.Addr.UDP()))
})
}
})
}
s.mu.Lock()
for _, addr := range initialAddrs {
onAddr(NewAddr(addr.Addr.UDP()))
}
s.mu.Unlock()
outstanding.Wait()
return
}
// Returns how many nodes are in the node table.
func (s *Server) NumNodes() int {
s.mu.Lock()
......
package dht
import "fmt"
type TraversalStats struct {
NumAddrsTried int
NumResponses int
}
func (me TraversalStats) String() string {
return fmt.Sprintf("%#v", me)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment