|
|
@ -200,9 +200,44 @@ func (sw *Switch) OnStop() { |
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// Peers
|
|
|
|
|
|
|
|
// Peers returns the set of peers that are connected to the switch.
|
|
|
|
func (sw *Switch) Peers() IPeerSet { |
|
|
|
return sw.peers |
|
|
|
// Broadcast runs a go routine for each attempted send, which will block
|
|
|
|
// trying to send for defaultSendTimeoutSeconds. Returns a channel
|
|
|
|
// which receives broadcast result for each attempted send (success=false if times out).
|
|
|
|
// NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
|
|
|
|
// TODO: Something more intelligent.
|
|
|
|
|
|
|
|
type BroadcastResult struct { |
|
|
|
PeerKey string |
|
|
|
Success bool |
|
|
|
} |
|
|
|
|
|
|
|
func (sw *Switch) Broadcast(chID byte, msg interface{}) chan BroadcastResult { |
|
|
|
successChan := make(chan BroadcastResult, len(sw.peers.List())) |
|
|
|
sw.Logger.Debug("Broadcast", "channel", chID, "msg", msg) |
|
|
|
for _, peer := range sw.peers.List() { |
|
|
|
go func(peer Peer) { |
|
|
|
success := peer.Send(chID, msg) |
|
|
|
successChan <- BroadcastResult{peer.Key(), success} |
|
|
|
}(peer) |
|
|
|
} |
|
|
|
return successChan |
|
|
|
} |
|
|
|
|
|
|
|
func (sw *Switch) TryBroadcast(chID byte, msg interface{}) chan BroadcastResult { |
|
|
|
successChan := make(chan BroadcastResult, len(sw.peers.List())) |
|
|
|
sw.Logger.Debug("TryBroadcast", "channel", chID, "msg", msg) |
|
|
|
for _, peer := range sw.peers.List() { |
|
|
|
success := peer.TrySend(chID, msg) |
|
|
|
if success { |
|
|
|
successChan <- BroadcastResult{peer.Key(), success} |
|
|
|
} else { |
|
|
|
go func(peer Peer) { |
|
|
|
success := peer.Send(chID, msg) |
|
|
|
successChan <- BroadcastResult{peer.Key(), success} |
|
|
|
}(peer) |
|
|
|
} |
|
|
|
} |
|
|
|
return successChan |
|
|
|
} |
|
|
|
|
|
|
|
// NumPeers returns the count of outbound/inbound and outbound-dialing peers.
|
|
|
@ -219,21 +254,9 @@ func (sw *Switch) NumPeers() (outbound, inbound, dialing int) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// Broadcast runs a go routine for each attempted send, which will block
|
|
|
|
// trying to send for defaultSendTimeoutSeconds. Returns a channel
|
|
|
|
// which receives success values for each attempted send (false if times out).
|
|
|
|
// NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
|
|
|
|
// TODO: Something more intelligent.
|
|
|
|
func (sw *Switch) Broadcast(chID byte, msg interface{}) chan bool { |
|
|
|
successChan := make(chan bool, len(sw.peers.List())) |
|
|
|
sw.Logger.Debug("Broadcast", "channel", chID, "msg", msg) |
|
|
|
for _, peer := range sw.peers.List() { |
|
|
|
go func(peer Peer) { |
|
|
|
success := peer.Send(chID, msg) |
|
|
|
successChan <- success |
|
|
|
}(peer) |
|
|
|
} |
|
|
|
return successChan |
|
|
|
// Peers returns the set of peers that are connected to the switch.
|
|
|
|
func (sw *Switch) Peers() IPeerSet { |
|
|
|
return sw.peers |
|
|
|
} |
|
|
|
|
|
|
|
// StopPeerForError disconnects from a peer due to external error.
|
|
|
|