From 0cbbe9bac31c7c4e9700639cb56fbe1bab383bfa Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Sun, 10 May 2015 23:58:23 +0100 Subject: [PATCH] make Peer.String() return the common case When we want a string representation of a Peer, we nearly always just want the full name. So make String() return that and introduce Info() to return expanded info. --- router/connection.go | 8 ++++---- router/handshake.go | 2 +- router/local_peer.go | 4 ++-- router/mac_cache.go | 2 +- router/peer.go | 10 +++++----- router/peers.go | 4 ++-- router/router.go | 8 ++++---- weaver/main.go | 2 +- 8 files changed, 20 insertions(+), 20 deletions(-) diff --git a/router/connection.go b/router/connection.go index ed0651a4..891572a5 100644 --- a/router/connection.go +++ b/router/connection.go @@ -82,17 +82,17 @@ func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak { return T func (conn *RemoteConnection) Shutdown(error) {} func (conn *RemoteConnection) Log(args ...interface{}) { - log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote.FullName())), args...)...) + log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...) } func (conn *RemoteConnection) String() string { from := "" if conn.local != nil { - from = conn.local.FullName() + from = conn.local.String() } to := "" if conn.remote != nil { - to = conn.remote.FullName() + to = conn.remote.String() } return fmt.Sprint("Connection ", from, "->", to) } @@ -231,7 +231,7 @@ func (conn *LocalConnection) ReceivedHeartbeat(remoteUDPAddr *net.UDPAddr, connU if oldRemoteUDPAddr == nil { return conn.sendFastHeartbeats() } else if oldRemoteUDPAddr.String() != remoteUDPAddr.String() { - log.Println("Peer", conn.remote.FullName(), "moved from", old, "to", remoteUDPAddr) + log.Println("Peer", conn.remote, "moved from", old, "to", remoteUDPAddr) } return nil }) diff --git a/router/handshake.go b/router/handshake.go index 84dc9341..4a003a19 100644 --- a/router/handshake.go +++ b/router/handshake.go @@ -76,7 +76,7 @@ func (conn *LocalConnection) handshake(enc *gob.Encoder, dec *gob.Decoder, accep } } if existingConn, found := conn.local.ConnectionTo(name); found && existingConn.Established() { - return fmt.Errorf("Already have connection to %s at %s", existingConn.Remote().FullName(), existingConn.RemoteTCPAddr()) + return fmt.Errorf("Already have connection to %s at %s", existingConn.Remote(), existingConn.RemoteTCPAddr()) } uid, err := strconv.ParseUint(uidStr, 10, 64) if err != nil { diff --git a/router/local_peer.go b/router/local_peer.go index 69d71481..13ff253a 100644 --- a/router/local_peer.go +++ b/router/local_peer.go @@ -38,7 +38,7 @@ func (peer *LocalPeer) Relay(srcPeer, dstPeer *Peer, df bool, frame []byte, dec if !found { // Not necessarily an error as there could be a race with the // dst disappearing whilst the frame is in flight - log.Println("Received packet for unknown destination:", dstPeer.FullName()) + log.Println("Received packet for unknown destination:", dstPeer) return nil } conn, found := peer.ConnectionTo(relayPeerName) @@ -162,7 +162,7 @@ func (peer *LocalPeer) handleAddConnection(conn Connection) error { log.Fatal("Attempt made to add connection to peer with unknown remote peer") } toName := conn.Remote().Name - dupErr := fmt.Errorf("Multiple connections to %s added to %s", conn.Remote().FullName(), peer.FullName()) + dupErr := fmt.Errorf("Multiple connections to %s added to %s", conn.Remote(), peer) // deliberately non symmetrical if dupConn, found := peer.connections[toName]; found { if dupConn == conn { diff --git a/router/mac_cache.go b/router/mac_cache.go index 4408fe09..d251d194 100644 --- a/router/mac_cache.go +++ b/router/mac_cache.go @@ -89,7 +89,7 @@ func (cache *MacCache) String() string { cache.RLock() defer cache.RUnlock() for key, entry := range cache.table { - fmt.Fprintf(&buf, "%v -> %s (%v)\n", intmac(key), entry.peer.FullName(), entry.lastSeen) + fmt.Fprintf(&buf, "%v -> %s (%v)\n", intmac(key), entry.peer, entry.lastSeen) } return buf.String() } diff --git a/router/peer.go b/router/peer.go index 1dd6f223..086e6e44 100644 --- a/router/peer.go +++ b/router/peer.go @@ -33,13 +33,13 @@ func NewPeer(name PeerName, nickName string, uid uint64, version uint64) *Peer { } func (peer *Peer) String() string { - peer.RLock() - defer peer.RUnlock() - return fmt.Sprint(peer.FullName(), " (v", peer.version, ") (UID ", peer.UID, ")") + return fmt.Sprint(peer.Name, "(", peer.NickName, ")") } -func (peer *Peer) FullName() string { - return fmt.Sprint(peer.Name, "(", peer.NickName, ")") +func (peer *Peer) Info() string { + peer.RLock() + defer peer.RUnlock() + return fmt.Sprint(peer.String(), " (v", peer.version, ") (UID ", peer.UID, ")") } func (peer *Peer) Version() uint64 { diff --git a/router/peers.go b/router/peers.go index c2767ed0..ff75c0c2 100644 --- a/router/peers.go +++ b/router/peers.go @@ -150,13 +150,13 @@ func (peers *Peers) GarbageCollect() []*Peer { func (peers *Peers) String() string { var buf bytes.Buffer peers.ForEach(func(peer *Peer) { - fmt.Fprint(&buf, peer, "\n") + fmt.Fprintln(&buf, peer.Info()) for conn := range peer.Connections() { established := "" if !conn.Established() { established = " (unestablished)" } - fmt.Fprintf(&buf, " -> %s [%v%s]\n", conn.Remote().FullName(), conn.RemoteTCPAddr(), established) + fmt.Fprintf(&buf, " -> %s [%v%s]\n", conn.Remote(), conn.RemoteTCPAddr(), established) } }) return buf.String() diff --git a/router/router.go b/router/router.go index 3d9b993e..0df6a813 100644 --- a/router/router.go +++ b/router/router.go @@ -56,11 +56,11 @@ type PacketSourceSink interface { func NewRouter(config RouterConfig, name PeerName, nickName string) *Router { router := &Router{RouterConfig: config, GossipChannels: make(map[uint32]*GossipChannel)} onMacExpiry := func(mac net.HardwareAddr, peer *Peer) { - log.Println("Expired MAC", mac, "at", peer.FullName()) + log.Println("Expired MAC", mac, "at", peer) } onPeerGC := func(peer *Peer) { router.Macs.Delete(peer) - log.Println("Removed unreachable peer", peer.FullName()) + log.Println("Removed unreachable peer", peer) } router.Ourself = NewLocalPeer(name, nickName, router) router.Macs = NewMacCache(macMaxAge, onMacExpiry) @@ -105,7 +105,7 @@ func (router *Router) UsingPassword() bool { func (router *Router) Status() string { var buf bytes.Buffer - fmt.Fprintln(&buf, "Our name is", router.Ourself.FullName()) + fmt.Fprintln(&buf, "Our name is", router.Ourself) fmt.Fprintln(&buf, "Sniffing traffic on", router.Iface) fmt.Fprintf(&buf, "MACs:\n%s", router.Macs) fmt.Fprintf(&buf, "Peers:\n%s", router.Peers) @@ -328,7 +328,7 @@ func (router *Router) handleUDPPacketFunc(relayConn *LocalConnection, dec *Ether dstMac := dec.eth.DstMAC if router.Macs.Enter(srcMac, srcPeer) { - log.Println("Discovered remote MAC", srcMac, "at", srcPeer.FullName()) + log.Println("Discovered remote MAC", srcMac, "at", srcPeer) } if po != nil { router.LogFrame("Injecting", frame, &dec.eth) diff --git a/weaver/main.go b/weaver/main.go index 12335c57..ecdbd89d 100644 --- a/weaver/main.go +++ b/weaver/main.go @@ -119,7 +119,7 @@ func main() { config.LogFrame = logFrameFunc(debug) router := weave.NewRouter(config, name, nickName) - log.Println("Our name is", router.Ourself.FullName()) + log.Println("Our name is", router.Ourself) router.Start() initiateConnections(router, peers) if httpAddr != "" {