|
@ -1,11 +1,11 @@ |
|
|
package peer |
|
|
package peer |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
. "github.com/tendermint/tendermint/common" |
|
|
. "github.com/tendermint/tendermint/binary" |
|
|
. "github.com/tendermint/tendermint/binary" |
|
|
"github.com/tendermint/tendermint/merkle" |
|
|
"github.com/tendermint/tendermint/merkle" |
|
|
"atomic" |
|
|
|
|
|
|
|
|
"sync/atomic" |
|
|
"sync" |
|
|
"sync" |
|
|
"io" |
|
|
|
|
|
"errors" |
|
|
"errors" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -38,14 +38,14 @@ var ( |
|
|
CLIENT_DUPLICATE_PEER_ERROR = errors.New("Duplicate peer") |
|
|
CLIENT_DUPLICATE_PEER_ERROR = errors.New("Duplicate peer") |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func NewClient(newPeerCb func(*Connect) *Peer) *Client { |
|
|
|
|
|
|
|
|
func NewClient(newPeerCb func(*Connection) *Peer) *Client { |
|
|
self := newPeerCb(nil) |
|
|
self := newPeerCb(nil) |
|
|
if self == nil { |
|
|
if self == nil { |
|
|
Panicf("newPeerCb(nil) must return a prototypical peer for self") |
|
|
Panicf("newPeerCb(nil) must return a prototypical peer for self") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
inQueues := make(map[String]chan *InboundMsg) |
|
|
inQueues := make(map[String]chan *InboundMsg) |
|
|
for chName, channel := peer.channels { |
|
|
|
|
|
|
|
|
for chName, channel := range self.channels { |
|
|
inQueues[chName] = make(chan *InboundMsg) |
|
|
inQueues[chName] = make(chan *InboundMsg) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -103,33 +103,33 @@ func (c *Client) Broadcast(chName String, msg Msg) { |
|
|
func (c *Client) PopMessage(chName String) *InboundMsg { |
|
|
func (c *Client) PopMessage(chName String) *InboundMsg { |
|
|
if atomic.LoadUint32(&c.stopped) == 1 { return nil } |
|
|
if atomic.LoadUint32(&c.stopped) == 1 { return nil } |
|
|
|
|
|
|
|
|
channel := c.Channel(chName) |
|
|
|
|
|
|
|
|
channel := c.self.Channel(chName) |
|
|
q := c.inQueues[chName] |
|
|
q := c.inQueues[chName] |
|
|
if q == nil { Panicf("Expected inQueues[%f], found none", chName) } |
|
|
if q == nil { Panicf("Expected inQueues[%f], found none", chName) } |
|
|
|
|
|
|
|
|
for { |
|
|
for { |
|
|
select { |
|
|
select { |
|
|
case <-quit: |
|
|
|
|
|
|
|
|
case <-c.quit: |
|
|
return nil |
|
|
return nil |
|
|
case msg := <-q: |
|
|
|
|
|
|
|
|
case inMsg := <-q: |
|
|
// skip if known.
|
|
|
// skip if known.
|
|
|
if channel.Filter().Has(msg) { |
|
|
|
|
|
|
|
|
if channel.Filter().Has(inMsg.Msg) { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
return msg |
|
|
|
|
|
|
|
|
return inMsg |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Updates self's filter for a channel & broadcasts it.
|
|
|
// Updates self's filter for a channel & broadcasts it.
|
|
|
// TODO: maybe don't expose this
|
|
|
|
|
|
|
|
|
// TODO: rename, same name is confusing.
|
|
|
func (c *Client) UpdateFilter(chName String, filter Filter) { |
|
|
func (c *Client) UpdateFilter(chName String, filter Filter) { |
|
|
if atomic.LoadUint32(&c.stopped) == 1 { return } |
|
|
if atomic.LoadUint32(&c.stopped) == 1 { return } |
|
|
|
|
|
|
|
|
c.self.Channel(chName).UpdateFilter(filter) |
|
|
c.self.Channel(chName).UpdateFilter(filter) |
|
|
|
|
|
|
|
|
c.Broadcast("", &NewFilterMsg{ |
|
|
c.Broadcast("", &NewFilterMsg{ |
|
|
Channel: chName, |
|
|
|
|
|
|
|
|
ChName: chName, |
|
|
Filter: filter, |
|
|
Filter: filter, |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
@ -137,12 +137,13 @@ func (c *Client) UpdateFilter(chName String, filter Filter) { |
|
|
func (c *Client) StopPeer(peer *Peer) { |
|
|
func (c *Client) StopPeer(peer *Peer) { |
|
|
// lock
|
|
|
// lock
|
|
|
c.mtx.Lock() |
|
|
c.mtx.Lock() |
|
|
p, _ := c.peers.Remove(peer.RemoteAddress()) |
|
|
|
|
|
|
|
|
peerValue, _ := c.peers.Remove(peer.RemoteAddress()) |
|
|
c.mtx.Unlock() |
|
|
c.mtx.Unlock() |
|
|
// unlock
|
|
|
// unlock
|
|
|
|
|
|
|
|
|
if p != nil { |
|
|
|
|
|
p.Stop() |
|
|
|
|
|
|
|
|
peer_ := peerValue.(*Peer) |
|
|
|
|
|
if peer_ != nil { |
|
|
|
|
|
peer_.Stop() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|