From 45403917de6c0a416d35b4019813449f4022262b Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Fri, 2 May 2014 08:53:19 +0200 Subject: [PATCH] Minor cleanup in discovery --- discover/discover.go | 50 ++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/discover/discover.go b/discover/discover.go index 4a2f90eaa..ac9aef311 100644 --- a/discover/discover.go +++ b/discover/discover.go @@ -6,7 +6,6 @@ import ( "fmt" "log" "net" - "strings" "sync" "time" @@ -76,6 +75,20 @@ func (d *Discoverer) ExtAnnounceOK() bool { return d.extAnnounceOK } +func (d *Discoverer) Lookup(node string) []string { + d.registryLock.Lock() + addr, ok := d.registry[node] + d.registryLock.Unlock() + + if ok { + return addr + } else if len(d.extServer) != 0 { + // We might want to cache this, but not permanently so it needs some intelligence + return d.externalLookup(node) + } + return nil +} + func (d *Discoverer) announcementPkt() []byte { var addrs []Address for _, astr := range d.listenAddrs { @@ -203,7 +216,7 @@ func (d *Discoverer) recvAnnouncements() { for _, a := range pkt.Addresses { var nodeAddr string if len(a.IP) > 0 { - nodeAddr = fmt.Sprintf("%s:%d", ipStr(a.IP), a.Port) + nodeAddr = fmt.Sprintf("%s:%d", net.IP(a.IP), a.Port) } else { ua := addr.(*net.UDPAddr) ua.Port = int(a.Port) @@ -285,39 +298,8 @@ func (d *Discoverer) externalLookup(node string) []string { var addrs []string for _, a := range pkt.Addresses { - var nodeAddr string - if len(a.IP) > 0 { - nodeAddr = fmt.Sprintf("%s:%d", ipStr(a.IP), a.Port) - } + nodeAddr := fmt.Sprintf("%s:%d", net.IP(a.IP), a.Port) addrs = append(addrs, nodeAddr) } return addrs } - -func (d *Discoverer) Lookup(node string) []string { - d.registryLock.Lock() - addr, ok := d.registry[node] - d.registryLock.Unlock() - - if ok { - return addr - } else if len(d.extServer) != 0 { - // We might want to cache this, but not permanently so it needs some intelligence - return d.externalLookup(node) - } - return nil -} - -func ipStr(ip []byte) string { - var f = "%d" - var s = "." - if len(ip) > 4 { - f = "%x" - s = ":" - } - var ss = make([]string, len(ip)) - for i := range ip { - ss[i] = fmt.Sprintf(f, ip[i]) - } - return strings.Join(ss, s) -}