magnetico/cmd/magneticod/dht/managers.go

65 lines
1.2 KiB
Go
Raw Normal View History

2017-08-08 13:58:51 +02:00
package dht
import (
"net"
"time"
"go.uber.org/zap"
"github.com/boramalper/magnetico/cmd/magneticod/dht/mainline"
)
2017-08-08 13:58:51 +02:00
type TrawlingManager struct {
// private
output chan Result
2017-08-08 13:58:51 +02:00
services []*mainline.TrawlingService
}
type Result interface {
InfoHash() [20]byte
PeerAddr() *net.TCPAddr
}
func NewTrawlingManager(mlAddrs []string, interval time.Duration) *TrawlingManager {
2017-08-08 13:58:51 +02:00
manager := new(TrawlingManager)
manager.output = make(chan Result, 20)
2017-08-08 13:58:51 +02:00
if mlAddrs == nil {
mlAddrs = []string{"0.0.0.0:0"}
}
for _, addr := range mlAddrs {
manager.services = append(manager.services, mainline.NewTrawlingService(
addr,
2000,
interval,
mainline.TrawlingServiceEventHandlers{
OnResult: manager.onTrawlingResult,
},
))
2017-08-08 13:58:51 +02:00
}
for _, service := range manager.services {
service.Start()
}
return manager
}
func (m *TrawlingManager) onTrawlingResult(res mainline.TrawlingResult) {
select {
case m.output <- res:
default:
zap.L().Warn("DHT manager output ch is full, result dropped!")
}
2017-08-08 13:58:51 +02:00
}
func (m *TrawlingManager) Output() <-chan Result {
2017-08-08 13:58:51 +02:00
return m.output
}
func (m *TrawlingManager) Terminate() {
for _, service := range m.services {
service.Terminate()
}
}