magnetico/cmd/magneticod/dht/managers.go
Bora Alper c07daa3eca magneticod: metadata leech refactored heavily, now much more readable <3
+ persistence: we now make sure that rows are always closed using `defer`
2018-07-24 15:41:13 +03:00

48 lines
989 B
Go

package dht
import "github.com/boramalper/magnetico/cmd/magneticod/dht/mainline"
type TrawlingManager struct {
// private
output chan mainline.TrawlingResult
services []*mainline.TrawlingService
}
func NewTrawlingManager(mlAddrs []string) *TrawlingManager {
manager := new(TrawlingManager)
manager.output = make(chan mainline.TrawlingResult)
if mlAddrs == nil {
mlAddrs = []string{"0.0.0.0:0"}
}
for _, addr := range mlAddrs {
manager.services = append(manager.services, mainline.NewTrawlingService(
addr,
2000,
mainline.TrawlingServiceEventHandlers{
OnResult: manager.onResult,
},
))
}
for _, service := range manager.services {
service.Start()
}
return manager
}
func (m *TrawlingManager) onResult(res mainline.TrawlingResult) {
m.output <- res
}
func (m *TrawlingManager) Output() <-chan mainline.TrawlingResult {
return m.output
}
func (m *TrawlingManager) Terminate() {
for _, service := range m.services {
service.Terminate()
}
}