refactor(ish): generalise an abstraction

LocalPeer.NextBroadcastHops -> ConnectionsTo
thus separating the computation of who we want to connect to from
obtaining the associated Connections
This commit is contained in:
Matthias Radestock committed 2015-04-04 20:36:48 +01:00
1 parent bcd2a0184c
commit 91ed22a953
2 files changed
+19 -19

No files matched your search

+7 -7
View File
@@ -230,13 +230,13 @@ func (c *GossipChannel) relayGossipUnicast(dstPeerName PeerName, msg []byte) err
} }
func (c *GossipChannel) relayGossipBroadcast(srcName PeerName, msg []byte) error { func (c *GossipChannel) relayGossipBroadcast(srcName PeerName, msg []byte) error {
if srcPeer, found := c.ourself.Router.Peers.Fetch(srcName); !found { nextHops := c.ourself.Router.Routes.Broadcast(srcName)
c.log("unable to relay broadcast from unknown peer", srcName) if len(nextHops) == 0 {
} else { return nil
protocolMsg := ProtocolMsg{ProtocolGossipBroadcast, msg} }
for _, conn := range c.ourself.NextBroadcastHops(srcPeer) { protocolMsg := ProtocolMsg{ProtocolGossipBroadcast, msg}
conn.SendProtocolMsg(protocolMsg) for _, conn := range c.ourself.ConnectionsTo(nextHops) {
} conn.(ProtocolSender).SendProtocolMsg(protocolMsg)
} }
return nil return nil
} }
+12 -12
View File
@@ -55,8 +55,12 @@ func (peer *LocalPeer) Relay(srcPeer, dstPeer *Peer, df bool, frame []byte, dec
} }
func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, df bool, frame []byte, dec *EthernetDecoder) { func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, df bool, frame []byte, dec *EthernetDecoder) {
for _, conn := range peer.NextBroadcastHops(srcPeer) { nextHops := peer.Router.Routes.Broadcast(srcPeer.Name)
err := conn.Forward(df, &ForwardedFrame{ if len(nextHops) == 0 {
return
}
for _, conn := range peer.ConnectionsTo(nextHops) {
err := conn.(*LocalConnection).Forward(df, &ForwardedFrame{
srcPeer: srcPeer, srcPeer: srcPeer,
dstPeer: conn.Remote(), dstPeer: conn.Remote(),
frame: frame}, frame: frame},
@@ -71,22 +75,18 @@ func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, df bool, frame []byte, dec
} }
} }
func (peer *LocalPeer) NextBroadcastHops(srcPeer *Peer) []*LocalConnection { func (peer *LocalPeer) ConnectionsTo(names []PeerName) []Connection {
nextHops := peer.Router.Routes.Broadcast(srcPeer.Name) conns := make([]Connection, 0, len(names))
if len(nextHops) == 0 {
return nil
}
nextConns := make([]*LocalConnection, 0, len(nextHops))
peer.RLock() peer.RLock()
defer peer.RUnlock() defer peer.RUnlock()
for _, hopName := range nextHops { for _, name := range names {
conn, found := peer.connections[hopName] conn, found := peer.connections[name]
// Again, !found could just be due to a race. // Again, !found could just be due to a race.
if found { if found {
nextConns = append(nextConns, conn.(*LocalConnection)) conns = append(conns, conn)
} }
} }
return nextConns return conns
} }
func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) error { func (peer *LocalPeer) CreateConnection(peerAddr string, acceptNewPeer bool) error {