|
@ -14,7 +14,7 @@ import ( |
|
|
type Peer struct { |
|
|
type Peer struct { |
|
|
outgoing bool |
|
|
outgoing bool |
|
|
conn *Connection |
|
|
conn *Connection |
|
|
channels map[String]*Channel |
|
|
|
|
|
|
|
|
channels map[string]*Channel |
|
|
|
|
|
|
|
|
mtx sync.Mutex |
|
|
mtx sync.Mutex |
|
|
quit chan struct{} |
|
|
quit chan struct{} |
|
@ -29,7 +29,9 @@ func NewPeer(conn *Connection) *Peer { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Peer) Start(peerInQueues map[String]chan *InboundMsg ) { |
|
|
|
|
|
|
|
|
func (p *Peer) Start(peerInQueues map[string]chan *InboundMsg ) { |
|
|
|
|
|
log.Debugf("Starting %v", p) |
|
|
|
|
|
p.conn.Start() |
|
|
for chName, _ := range p.channels { |
|
|
for chName, _ := range p.channels { |
|
|
go p.inHandler(chName, peerInQueues[chName]) |
|
|
go p.inHandler(chName, peerInQueues[chName]) |
|
|
go p.outHandler(chName) |
|
|
go p.outHandler(chName) |
|
@ -40,6 +42,7 @@ func (p *Peer) Stop() { |
|
|
// lock
|
|
|
// lock
|
|
|
p.mtx.Lock() |
|
|
p.mtx.Lock() |
|
|
if atomic.CompareAndSwapUint32(&p.stopped, 0, 1) { |
|
|
if atomic.CompareAndSwapUint32(&p.stopped, 0, 1) { |
|
|
|
|
|
log.Debugf("Stopping %v", p) |
|
|
close(p.quit) |
|
|
close(p.quit) |
|
|
p.conn.Stop() |
|
|
p.conn.Stop() |
|
|
} |
|
|
} |
|
@ -55,13 +58,13 @@ func (p *Peer) RemoteAddress() *NetAddress { |
|
|
return p.conn.RemoteAddress() |
|
|
return p.conn.RemoteAddress() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Peer) Channel(chName String) *Channel { |
|
|
|
|
|
|
|
|
func (p *Peer) Channel(chName string) *Channel { |
|
|
return p.channels[chName] |
|
|
return p.channels[chName] |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Queue the msg for output.
|
|
|
// Queue the msg for output.
|
|
|
// If the queue is full, just return false.
|
|
|
// If the queue is full, just return false.
|
|
|
func (p *Peer) TryQueueOut(chName String, msg Msg) bool { |
|
|
|
|
|
|
|
|
func (p *Peer) TryQueueOut(chName string, msg Msg) bool { |
|
|
channel := p.Channel(chName) |
|
|
channel := p.Channel(chName) |
|
|
outQueue := channel.OutQueue() |
|
|
outQueue := channel.OutQueue() |
|
|
|
|
|
|
|
@ -85,7 +88,8 @@ func (p *Peer) String() string { |
|
|
return fmt.Sprintf("Peer{%v-%v,o:%v}", p.LocalAddress(), p.RemoteAddress(), p.outgoing) |
|
|
return fmt.Sprintf("Peer{%v-%v,o:%v}", p.LocalAddress(), p.RemoteAddress(), p.outgoing) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Peer) inHandler(chName String, inboundMsgQueue chan<- *InboundMsg) { |
|
|
|
|
|
|
|
|
func (p *Peer) inHandler(chName string, inboundMsgQueue chan<- *InboundMsg) { |
|
|
|
|
|
log.Tracef("Peer %v inHandler [%v]", p, chName) |
|
|
channel := p.channels[chName] |
|
|
channel := p.channels[chName] |
|
|
inQueue := channel.InQueue() |
|
|
inQueue := channel.InQueue() |
|
|
|
|
|
|
|
@ -111,11 +115,13 @@ func (p *Peer) inHandler(chName String, inboundMsgQueue chan<- *InboundMsg) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.Tracef("Peer %v inHandler [%v] closed", p, chName) |
|
|
// cleanup
|
|
|
// cleanup
|
|
|
// (none)
|
|
|
// (none)
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *Peer) outHandler(chName String) { |
|
|
|
|
|
|
|
|
func (p *Peer) outHandler(chName string) { |
|
|
|
|
|
log.Tracef("Peer %v outHandler [%v]", p, chName) |
|
|
outQueue := p.channels[chName].outQueue |
|
|
outQueue := p.channels[chName].outQueue |
|
|
FOR_LOOP: |
|
|
FOR_LOOP: |
|
|
for { |
|
|
for { |
|
@ -123,12 +129,14 @@ func (p *Peer) outHandler(chName String) { |
|
|
case <-p.quit: |
|
|
case <-p.quit: |
|
|
break FOR_LOOP |
|
|
break FOR_LOOP |
|
|
case msg := <-outQueue: |
|
|
case msg := <-outQueue: |
|
|
|
|
|
log.Tracef("Sending msg to peer outQueue") |
|
|
// blocks until the connection is Stop'd,
|
|
|
// blocks until the connection is Stop'd,
|
|
|
// which happens when this peer is Stop'd.
|
|
|
// which happens when this peer is Stop'd.
|
|
|
p.conn.QueueOut(msg.Bytes) |
|
|
p.conn.QueueOut(msg.Bytes) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
log.Tracef("Peer %v outHandler [%v] closed", p, chName) |
|
|
// cleanup
|
|
|
// cleanup
|
|
|
// (none)
|
|
|
// (none)
|
|
|
} |
|
|
} |
|
@ -137,7 +145,7 @@ func (p *Peer) outHandler(chName String) { |
|
|
/* Channel */ |
|
|
/* Channel */ |
|
|
|
|
|
|
|
|
type Channel struct { |
|
|
type Channel struct { |
|
|
name String |
|
|
|
|
|
|
|
|
name string |
|
|
inQueue chan Msg |
|
|
inQueue chan Msg |
|
|
outQueue chan Msg |
|
|
outQueue chan Msg |
|
|
//stats Stats
|
|
|
//stats Stats
|
|
@ -145,13 +153,13 @@ type Channel struct { |
|
|
|
|
|
|
|
|
func NewChannel(name string, bufferSize int) *Channel { |
|
|
func NewChannel(name string, bufferSize int) *Channel { |
|
|
return &Channel{ |
|
|
return &Channel{ |
|
|
name: String(name), |
|
|
|
|
|
|
|
|
name: name, |
|
|
inQueue: make(chan Msg, bufferSize), |
|
|
inQueue: make(chan Msg, bufferSize), |
|
|
outQueue: make(chan Msg, bufferSize), |
|
|
outQueue: make(chan Msg, bufferSize), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (c *Channel) Name() String { |
|
|
|
|
|
|
|
|
func (c *Channel) Name() string { |
|
|
return c.name |
|
|
return c.name |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|