Decouple local from global announcing (fixes #132)

This commit is contained in:
Jakob Borg 2014-04-16 16:49:01 +02:00
parent f72ee7a69e
commit 31bfd8c039
2 changed files with 21 additions and 29 deletions

View File

@ -468,22 +468,20 @@ next:
}
func discovery() *discover.Discoverer {
if !cfg.Options.LocalAnnEnabled {
disc, err := discover.NewDiscoverer(myID, cfg.Options.ListenAddress)
if err != nil {
warnf("No discovery possible (%v)", err)
return nil
}
infoln("Sending local discovery announcements")
if !cfg.Options.GlobalAnnEnabled {
cfg.Options.GlobalAnnServer = ""
} else {
infoln("Sending external discovery announcements")
if cfg.Options.LocalAnnEnabled {
infoln("Sending local discovery announcements")
disc.StartLocal()
}
disc, err := discover.NewDiscoverer(myID, cfg.Options.ListenAddress, cfg.Options.GlobalAnnServer)
if err != nil {
warnf("No discovery possible (%v)", err)
if cfg.Options.GlobalAnnEnabled {
infoln("Sending external discovery announcements")
disc.StartGlobal(cfg.Options.GlobalAnnServer)
}
return disc

View File

@ -40,7 +40,7 @@ var (
// When we hit this many errors in succession, we stop.
const maxErrors = 30
func NewDiscoverer(id string, addresses []string, extServer string) (*Discoverer, error) {
func NewDiscoverer(id string, addresses []string) (*Discoverer, error) {
disc := &Discoverer{
MyID: id,
ListenAddresses: addresses,
@ -48,32 +48,26 @@ func NewDiscoverer(id string, addresses []string, extServer string) (*Discoverer
ExtBroadcastIntv: 1800 * time.Second,
beacon: mc.NewBeacon("239.21.0.25", 21025),
registry: make(map[string][]string),
extServer: extServer,
}
// Receive announcements sent to the local multicast group.
go disc.recvAnnouncements()
// If we got a list of addresses that we listen on, announce those
// locally.
if len(disc.ListenAddresses) > 0 {
disc.localBroadcastTick = time.Tick(disc.BroadcastIntv)
disc.forcedBroadcastTick = make(chan time.Time)
go disc.sendLocalAnnouncements()
// If we have an external server address, also announce to that
// server.
if len(disc.extServer) > 0 {
go disc.sendExternalAnnouncements()
}
}
return disc, nil
}
func (d *Discoverer) StartLocal() {
d.localBroadcastTick = time.Tick(d.BroadcastIntv)
d.forcedBroadcastTick = make(chan time.Time)
go d.sendLocalAnnouncements()
}
func (d *Discoverer) StartGlobal(server string) {
d.extServer = server
go d.sendExternalAnnouncements()
}
func (d *Discoverer) announcementPkt() []byte {
var addrs []Address
for _, astr := range d.ListenAddresses {