tendermint/p2p/switch.go

204 lines
4.5 KiB
Go
Raw Normal View History

2014-07-07 20:03:50 -07:00
package p2p
import (
"errors"
"sync/atomic"
"time"
. "github.com/tendermint/tendermint/common"
)
/*
All communication amongst peers are multiplexed by "channels".
(Not the same as Go "channels")
To send a message, encapsulate it into a "Packet" and send it to each peer.
2014-07-10 22:14:23 -07:00
You can find all connected and active peers by iterating over ".Peers().List()".
".Broadcast()" is provided for convenience, but by iterating over
the peers manually the caller can decide which subset receives a message.
Inbound messages are received by calling ".Receive()".
*/
type Switch struct {
channels []ChannelDescriptor
pktRecvQueues map[string]chan *InboundPacket
2014-07-08 00:02:04 -07:00
peers *PeerSet
dialing *CMap
quit chan struct{}
2014-07-10 02:19:50 -07:00
started uint32
stopped uint32
}
var (
ErrSwitchStopped = errors.New("Switch already stopped")
ErrSwitchDuplicatePeer = errors.New("Duplicate peer")
)
const (
peerDialTimeoutSeconds = 30
)
func NewSwitch(channels []ChannelDescriptor) *Switch {
// make pktRecvQueues...
pktRecvQueues := make(map[string]chan *InboundPacket)
for _, chDesc := range channels {
pktRecvQueues[chDesc.Name] = make(chan *InboundPacket)
}
s := &Switch{
channels: channels,
pktRecvQueues: pktRecvQueues,
2014-07-08 00:02:04 -07:00
peers: NewPeerSet(),
dialing: NewCMap(),
quit: make(chan struct{}),
stopped: 0,
}
return s
}
2014-07-09 18:33:44 -07:00
func (s *Switch) Start() {
2014-07-10 02:19:50 -07:00
if atomic.CompareAndSwapUint32(&s.started, 0, 1) {
2014-07-14 16:15:13 -07:00
log.Info("Starting switch")
2014-07-10 02:19:50 -07:00
}
2014-07-09 18:33:44 -07:00
}
func (s *Switch) Stop() {
if atomic.CompareAndSwapUint32(&s.stopped, 0, 1) {
2014-07-14 16:15:13 -07:00
log.Info("Stopping switch")
close(s.quit)
// stop each peer.
2014-07-08 00:02:04 -07:00
for _, peer := range s.peers.List() {
peer.stop()
}
// empty tree.
2014-07-08 00:02:04 -07:00
s.peers = NewPeerSet()
}
}
2014-07-10 22:14:23 -07:00
func (s *Switch) AddPeerWithConnection(conn *Connection, outbound bool) (*Peer, error) {
if atomic.LoadUint32(&s.stopped) == 1 {
return nil, ErrSwitchStopped
}
2014-07-14 16:15:13 -07:00
log.Info("Adding peer with connection: %v, outbound: %v", conn, outbound)
// Create channels for peer
channels := map[string]*Channel{}
for _, chDesc := range s.channels {
channels[chDesc.Name] = newChannel(chDesc)
}
peer := newPeer(conn, channels)
2014-07-10 22:14:23 -07:00
peer.outbound = outbound
err := s.addPeer(peer)
if err != nil {
return nil, err
}
go peer.start(s.pktRecvQueues, s.StopPeerForError)
return peer, nil
}
func (s *Switch) DialPeerWithAddress(addr *NetAddress) (*Peer, error) {
if atomic.LoadUint32(&s.stopped) == 1 {
return nil, ErrSwitchStopped
}
2014-07-14 16:15:13 -07:00
log.Info("Dialing peer @ %v", addr)
s.dialing.Set(addr.String(), addr)
conn, err := addr.DialTimeout(peerDialTimeoutSeconds * time.Second)
s.dialing.Delete(addr.String())
if err != nil {
return nil, err
}
peer, err := s.AddPeerWithConnection(conn, true)
if err != nil {
return nil, err
}
return peer, nil
}
func (s *Switch) Broadcast(pkt Packet) (numSuccess, numFailure int) {
if atomic.LoadUint32(&s.stopped) == 1 {
return
}
2014-07-14 16:15:13 -07:00
log.Debug("Broadcast on [%v] len: %v", pkt.Channel, len(pkt.Bytes))
2014-07-08 00:02:04 -07:00
for _, peer := range s.peers.List() {
success := peer.TrySend(pkt)
2014-07-14 16:15:13 -07:00
log.Debug("Broadcast for peer %v success: %v", peer, success)
if success {
numSuccess += 1
} else {
numFailure += 1
}
}
return
}
/*
Receive blocks on a channel until a message is found.
*/
func (s *Switch) Receive(chName string) *InboundPacket {
if atomic.LoadUint32(&s.stopped) == 1 {
return nil
}
2014-07-14 16:15:13 -07:00
log.Debug("Receive on [%v]", chName)
q := s.pktRecvQueues[chName]
if q == nil {
Panicf("Expected pktRecvQueues[%f], found none", chName)
}
select {
case <-s.quit:
return nil
case inPacket := <-q:
return inPacket
}
}
2014-07-10 22:14:23 -07:00
func (s *Switch) NumOutboundPeers() (count int) {
peers := s.peers.List()
for _, peer := range peers {
if peer.outbound {
count++
}
}
return
}
func (s *Switch) Peers() IPeerSet {
2014-07-10 22:14:23 -07:00
return s.peers
}
// Disconnect from a peer due to external error.
// TODO: make record depending on reason.
func (s *Switch) StopPeerForError(peer *Peer, reason interface{}) {
2014-07-14 16:15:13 -07:00
log.Info("%v errored: %v", peer, reason)
s.StopPeer(peer, false)
}
// Disconnect from a peer.
// If graceful is true, last message sent is a disconnect message.
// TODO: handle graceful disconnects.
func (s *Switch) StopPeer(peer *Peer, graceful bool) {
2014-07-08 00:02:04 -07:00
s.peers.Remove(peer)
peer.stop()
}
func (s *Switch) addPeer(peer *Peer) error {
if s.stopped == 1 {
return ErrSwitchStopped
}
2014-07-08 00:02:04 -07:00
if s.peers.Add(peer) {
2014-07-14 16:15:13 -07:00
log.Debug("Adding: %v", peer)
return nil
} else {
2014-07-08 00:02:04 -07:00
// ignore duplicate peer
2014-07-14 16:15:13 -07:00
log.Info("Ignoring duplicate: %v", peer)
return ErrSwitchDuplicatePeer
}
}