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.
This commit is contained in:
Matthias Radestock committed 2015-05-10 23:58:23 +01:00
1 parent db198652a5
commit 0cbbe9bac3
8 files changed
+20 -20

No files matched your search

+4 -4
View File
@@ -82,17 +82,17 @@ func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak { return T
func (conn *RemoteConnection) Shutdown(error) {} func (conn *RemoteConnection) Shutdown(error) {}
func (conn *RemoteConnection) Log(args ...interface{}) { 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 { func (conn *RemoteConnection) String() string {
from := "<nil>" from := "<nil>"
if conn.local != nil { if conn.local != nil {
from = conn.local.FullName() from = conn.local.String()
} }
to := "<nil>" to := "<nil>"
if conn.remote != nil { if conn.remote != nil {
to = conn.remote.FullName() to = conn.remote.String()
} }
return fmt.Sprint("Connection ", from, "->", to) return fmt.Sprint("Connection ", from, "->", to)
} }
@@ -231,7 +231,7 @@ func (conn *LocalConnection) ReceivedHeartbeat(remoteUDPAddr *net.UDPAddr, connU
if oldRemoteUDPAddr == nil { if oldRemoteUDPAddr == nil {
return conn.sendFastHeartbeats() return conn.sendFastHeartbeats()
} else if oldRemoteUDPAddr.String() != remoteUDPAddr.String() { } 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 return nil
}) })
+1 -1
View File
@@ -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() { 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) uid, err := strconv.ParseUint(uidStr, 10, 64)
if err != nil { if err != nil {
+2 -2
View File
@@ -38,7 +38,7 @@ func (peer *LocalPeer) Relay(srcPeer, dstPeer *Peer, df bool, frame []byte, dec
if !found { if !found {
// Not necessarily an error as there could be a race with the // Not necessarily an error as there could be a race with the
// dst disappearing whilst the frame is in flight // 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 return nil
} }
conn, found := peer.ConnectionTo(relayPeerName) 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") log.Fatal("Attempt made to add connection to peer with unknown remote peer")
} }
toName := conn.Remote().Name 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 // deliberately non symmetrical
if dupConn, found := peer.connections[toName]; found { if dupConn, found := peer.connections[toName]; found {
if dupConn == conn { if dupConn == conn {
+1 -1
View File
@@ -89,7 +89,7 @@ func (cache *MacCache) String() string {
cache.RLock() cache.RLock()
defer cache.RUnlock() defer cache.RUnlock()
for key, entry := range cache.table { 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() return buf.String()
} }
+5 -5
View File
@@ -33,13 +33,13 @@ func NewPeer(name PeerName, nickName string, uid uint64, version uint64) *Peer {
} }
func (peer *Peer) String() string { func (peer *Peer) String() string {
peer.RLock() return fmt.Sprint(peer.Name, "(", peer.NickName, ")")
defer peer.RUnlock()
return fmt.Sprint(peer.FullName(), " (v", peer.version, ") (UID ", peer.UID, ")")
} }
func (peer *Peer) FullName() string { func (peer *Peer) Info() string {
return fmt.Sprint(peer.Name, "(", peer.NickName, ")") peer.RLock()
defer peer.RUnlock()
return fmt.Sprint(peer.String(), " (v", peer.version, ") (UID ", peer.UID, ")")
} }
func (peer *Peer) Version() uint64 { func (peer *Peer) Version() uint64 {
+2 -2
View File
@@ -150,13 +150,13 @@ func (peers *Peers) GarbageCollect() []*Peer {
func (peers *Peers) String() string { func (peers *Peers) String() string {
var buf bytes.Buffer var buf bytes.Buffer
peers.ForEach(func(peer *Peer) { peers.ForEach(func(peer *Peer) {
fmt.Fprint(&buf, peer, "\n") fmt.Fprintln(&buf, peer.Info())
for conn := range peer.Connections() { for conn := range peer.Connections() {
established := "" established := ""
if !conn.Established() { if !conn.Established() {
established = " (unestablished)" 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() return buf.String()
+4 -4
View File
@@ -56,11 +56,11 @@ type PacketSourceSink interface {
func NewRouter(config RouterConfig, name PeerName, nickName string) *Router { func NewRouter(config RouterConfig, name PeerName, nickName string) *Router {
router := &Router{RouterConfig: config, GossipChannels: make(map[uint32]*GossipChannel)} router := &Router{RouterConfig: config, GossipChannels: make(map[uint32]*GossipChannel)}
onMacExpiry := func(mac net.HardwareAddr, peer *Peer) { 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) { onPeerGC := func(peer *Peer) {
router.Macs.Delete(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.Ourself = NewLocalPeer(name, nickName, router)
router.Macs = NewMacCache(macMaxAge, onMacExpiry) router.Macs = NewMacCache(macMaxAge, onMacExpiry)
@@ -105,7 +105,7 @@ func (router *Router) UsingPassword() bool {
func (router *Router) Status() string { func (router *Router) Status() string {
var buf bytes.Buffer 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.Fprintln(&buf, "Sniffing traffic on", router.Iface)
fmt.Fprintf(&buf, "MACs:\n%s", router.Macs) fmt.Fprintf(&buf, "MACs:\n%s", router.Macs)
fmt.Fprintf(&buf, "Peers:\n%s", router.Peers) fmt.Fprintf(&buf, "Peers:\n%s", router.Peers)
@@ -328,7 +328,7 @@ func (router *Router) handleUDPPacketFunc(relayConn *LocalConnection, dec *Ether
dstMac := dec.eth.DstMAC dstMac := dec.eth.DstMAC
if router.Macs.Enter(srcMac, srcPeer) { 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 { if po != nil {
router.LogFrame("Injecting", frame, &dec.eth) router.LogFrame("Injecting", frame, &dec.eth)
+1 -1
View File
@@ -119,7 +119,7 @@ func main() {
config.LogFrame = logFrameFunc(debug) config.LogFrame = logFrameFunc(debug)
router := weave.NewRouter(config, name, nickName) router := weave.NewRouter(config, name, nickName)
log.Println("Our name is", router.Ourself.FullName()) log.Println("Our name is", router.Ourself)
router.Start() router.Start()
initiateConnections(router, peers) initiateConnections(router, peers)
if httpAddr != "" { if httpAddr != "" {