Make peer state iterator funcs receivers.

Rather than having the iterator functions a separate entities that access
the state to iterate, just expose the iterators as receivers on the state
itself.  This is more consistent with the style used throughout the code
and the other receivers on the state such as Count, OutboundCount, etc.
This commit is contained in:
Dave Collins 2014-01-24 14:13:19 -06:00
parent fea4109eb2
commit d58af1c3cf

View file

@ -93,6 +93,26 @@ func (p *peerState) NeedMoreOutbound() bool {
p.Count() < cfg.MaxPeers p.Count() < cfg.MaxPeers
} }
// forAllOutboundPeers is a helper function that runs closure on all outbound
// peers known to peerState.
func (p *peerState) forAllOutboundPeers(closure func(p *peer)) {
for e := p.outboundPeers.Front(); e != nil; e = e.Next() {
closure(e.Value.(*peer))
}
for e := p.persistentPeers.Front(); e != nil; e = e.Next() {
closure(e.Value.(*peer))
}
}
// forAllPeers is a helper function that runs closure on all peers known to
// peerState.
func (p *peerState) forAllPeers(closure func(p *peer)) {
for e := p.peers.Front(); e != nil; e = e.Next() {
closure(e.Value.(*peer))
}
p.forAllOutboundPeers(closure)
}
// handleAddPeerMsg deals with adding new peers. It is invoked from the // handleAddPeerMsg deals with adding new peers. It is invoked from the
// peerHandler goroutine. // peerHandler goroutine.
func (s *server) handleAddPeerMsg(state *peerState, p *peer) bool { func (s *server) handleAddPeerMsg(state *peerState, p *peer) bool {
@ -203,30 +223,10 @@ func (s *server) handleBanPeerMsg(state *peerState, p *peer) {
} }
// forAllOutboundPeers is a helper function that runs closure on all outbound
// peers known to peerState.
func forAllOutboundPeers(state *peerState, closure func(p *peer)) {
for e := state.outboundPeers.Front(); e != nil; e = e.Next() {
closure(e.Value.(*peer))
}
for e := state.persistentPeers.Front(); e != nil; e = e.Next() {
closure(e.Value.(*peer))
}
}
// forAllPeers is a helper function that runs closure on all peers known to
// peerState.
func forAllPeers(state *peerState, closure func(p *peer)) {
for e := state.peers.Front(); e != nil; e = e.Next() {
closure(e.Value.(*peer))
}
forAllOutboundPeers(state, closure)
}
// handleRelayInvMsg deals with relaying inventory to peers that are not already // handleRelayInvMsg deals with relaying inventory to peers that are not already
// known to have it. It is invoked from the peerHandler goroutine. // known to have it. It is invoked from the peerHandler goroutine.
func (s *server) handleRelayInvMsg(state *peerState, iv *btcwire.InvVect) { func (s *server) handleRelayInvMsg(state *peerState, iv *btcwire.InvVect) {
forAllPeers(state, func(p *peer) { state.forAllPeers(func(p *peer) {
if !p.Connected() { if !p.Connected() {
return return
} }
@ -241,7 +241,7 @@ func (s *server) handleRelayInvMsg(state *peerState, iv *btcwire.InvVect) {
// handleBroadcastMsg deals with broadcasting messages to peers. It is invoked // handleBroadcastMsg deals with broadcasting messages to peers. It is invoked
// from the peerHandler goroutine. // from the peerHandler goroutine.
func (s *server) handleBroadcastMsg(state *peerState, bmsg *broadcastMsg) { func (s *server) handleBroadcastMsg(state *peerState, bmsg *broadcastMsg) {
forAllPeers(state, func(p *peer) { state.forAllPeers(func(p *peer) {
excluded := false excluded := false
for _, ep := range bmsg.excludePeers { for _, ep := range bmsg.excludePeers {
if p == ep { if p == ep {
@ -306,7 +306,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
switch msg := querymsg.(type) { switch msg := querymsg.(type) {
case getConnCountMsg: case getConnCountMsg:
nconnected := 0 nconnected := 0
forAllPeers(state, func(p *peer) { state.forAllPeers(func(p *peer) {
if p.Connected() { if p.Connected() {
nconnected++ nconnected++
} }
@ -315,7 +315,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
case getPeerInfoMsg: case getPeerInfoMsg:
infos := make([]*PeerInfo, 0, state.peers.Len()) infos := make([]*PeerInfo, 0, state.peers.Len())
forAllPeers(state, func(p *peer) { state.forAllPeers(func(p *peer) {
if !p.Connected() { if !p.Connected() {
return return
} }
@ -535,7 +535,7 @@ out:
// Shutdown the peer handler. // Shutdown the peer handler.
case <-s.quit: case <-s.quit:
// Shutdown peers. // Shutdown peers.
forAllPeers(state, func(p *peer) { state.forAllPeers(func(p *peer) {
p.Shutdown() p.Shutdown()
}) })
break out break out