aboutsummaryrefslogtreecommitdiffstats
path: root/peer.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-02-02 23:15:39 +0800
committerobscuren <geffobscura@gmail.com>2014-02-02 23:15:39 +0800
commitcb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f (patch)
treeb03e749aee49256f576dba6ba36ce6ecf7d692bb /peer.go
parentb70fe3a9bea652c795fc75cf79ed21fc04ea0937 (diff)
downloadgo-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.tar
go-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.tar.gz
go-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.tar.bz2
go-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.tar.lz
go-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.tar.xz
go-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.tar.zst
go-tangerine-cb8a7d979d1a4b2a32317ee0a77815ec2ea38b9f.zip
upnp test
Diffstat (limited to 'peer.go')
-rw-r--r--peer.go35
1 files changed, 25 insertions, 10 deletions
diff --git a/peer.go b/peer.go
index 5d22b545c..a715e205d 100644
--- a/peer.go
+++ b/peer.go
@@ -24,6 +24,8 @@ const (
CapDiscoveryTy = 0x01
CapTxTy = 0x02
CapChainTy = 0x04
+
+ CapDefault = CapChainTy | CapTxTy | CapDiscoveryTy
)
var capsToString = map[Caps]string{
@@ -95,7 +97,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
}
}
-func NewOutboundPeer(addr string, ethereum *Ethereum) *Peer {
+func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
p := &Peer{
outputQueue: make(chan *ethwire.Msg, outputBufferSize),
quit: make(chan bool),
@@ -103,6 +105,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum) *Peer {
inbound: false,
connected: 0,
disconnect: 0,
+ caps: caps,
}
// Set up the connection in another goroutine so we don't block the main thread
@@ -165,7 +168,8 @@ func (p *Peer) writeMessage(msg *ethwire.Msg) {
// Outbound message handler. Outbound messages are handled here
func (p *Peer) HandleOutbound() {
// The ping timer. Makes sure that every 2 minutes a ping is send to the peer
- tickleTimer := time.NewTicker(2 * time.Minute)
+ pingTimer := time.NewTicker(2 * time.Minute)
+ serviceTimer := time.NewTicker(5 * time.Second)
out:
for {
select {
@@ -175,11 +179,20 @@ out:
p.lastSend = time.Now()
- case <-tickleTimer.C:
+ // Ping timer sends a ping to the peer each 2 minutes
+ case <-pingTimer.C:
p.writeMessage(ethwire.NewMessage(ethwire.MsgPingTy, ""))
- // Break out of the for loop if a quit message is posted
+ // Service timer takes care of peer broadcasting, transaction
+ // posting or block posting
+ case <-serviceTimer.C:
+ if p.caps&CapDiscoveryTy > 0 {
+ msg := p.peersMessage()
+ p.ethereum.BroadcastMsg(msg)
+ }
+
case <-p.quit:
+ // Break out of the for loop if a quit message is posted
break out
}
}
@@ -387,7 +400,7 @@ func (p *Peer) Stop() {
func (p *Peer) pushHandshake() error {
msg := ethwire.NewMessage(ethwire.MsgHandshakeTy, []interface{}{
- uint32(0), uint32(0), "/Ethereum(G) v0.0.1/", CapChainTy | CapTxTy | CapDiscoveryTy, p.port,
+ uint32(0), uint32(0), "/Ethereum(G) v0.0.1/", p.caps, p.port,
})
p.QueueMessage(msg)
@@ -395,18 +408,20 @@ func (p *Peer) pushHandshake() error {
return nil
}
-// Pushes the list of outbound peers to the client when requested
-func (p *Peer) pushPeers() {
+func (p *Peer) peersMessage() *ethwire.Msg {
outPeers := make([]interface{}, len(p.ethereum.InOutPeers()))
// Serialise each peer
for i, peer := range p.ethereum.InOutPeers() {
outPeers[i] = peer.RlpData()
}
- // Send message to the peer with the known list of connected clients
- msg := ethwire.NewMessage(ethwire.MsgPeersTy, outPeers)
+ // Return the message to the peer with the known list of connected clients
+ return ethwire.NewMessage(ethwire.MsgPeersTy, outPeers)
+}
- p.QueueMessage(msg)
+// Pushes the list of outbound peers to the client when requested
+func (p *Peer) pushPeers() {
+ p.QueueMessage(p.peersMessage())
}
func (p *Peer) handleHandshake(msg *ethwire.Msg) {