2014-01-09 16:15:51 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"log"
|
2014-01-10 14:18:56 -06:00
|
|
|
"github.com/ethereum/ethwire-go"
|
2014-01-09 16:15:51 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Peer struct {
|
2014-01-10 03:58:46 -06:00
|
|
|
// Server interface
|
2014-01-09 16:15:51 -06:00
|
|
|
server *Server
|
2014-01-10 03:58:46 -06:00
|
|
|
// Net connection
|
2014-01-09 16:15:51 -06:00
|
|
|
conn net.Conn
|
2014-01-10 03:58:46 -06:00
|
|
|
// Output queue which is used to communicate and handle messages
|
2014-01-10 14:18:56 -06:00
|
|
|
outputQueue chan ethwire.InOutMsg
|
2014-01-10 03:58:46 -06:00
|
|
|
// Quit channel
|
2014-01-09 16:15:51 -06:00
|
|
|
quit chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPeer(conn net.Conn, server *Server) *Peer {
|
|
|
|
return &Peer{
|
2014-01-10 14:18:56 -06:00
|
|
|
outputQueue: make(chan ethwire.InOutMsg, 1), // Buffered chan of 1 is enough
|
2014-01-09 16:15:51 -06:00
|
|
|
quit: make(chan bool),
|
|
|
|
|
|
|
|
server: server,
|
|
|
|
conn: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs any RLP encoded data to the peer
|
2014-01-09 17:39:16 -06:00
|
|
|
func (p *Peer) QueueMessage(msgType string, data []byte) {
|
2014-01-10 14:18:56 -06:00
|
|
|
p.outputQueue <- ethwire.InOutMsg{MsgType: msgType, Data: data}
|
2014-01-09 16:15:51 -06:00
|
|
|
}
|
|
|
|
|
2014-01-10 03:58:46 -06:00
|
|
|
// Outbound message handler. Outbound messages are handled here
|
2014-01-09 16:15:51 -06:00
|
|
|
func (p *Peer) HandleOutbound() {
|
|
|
|
out:
|
|
|
|
for {
|
2014-01-09 17:39:16 -06:00
|
|
|
select {
|
2014-01-10 03:58:46 -06:00
|
|
|
// Main message queue. All outbound messages are processed through here
|
2014-01-09 17:39:16 -06:00
|
|
|
case msg := <-p.outputQueue:
|
2014-01-10 03:58:46 -06:00
|
|
|
// TODO Message checking and handle accordingly
|
2014-01-10 14:18:56 -06:00
|
|
|
err := ethwire.WriteMessage(p.conn, msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
|
|
|
|
// Stop the client if there was an error writing to it
|
|
|
|
p.Stop()
|
|
|
|
}
|
2014-01-09 17:39:16 -06:00
|
|
|
|
2014-01-10 03:58:46 -06:00
|
|
|
// Break out of the for loop if a quit message is posted
|
2014-01-09 16:15:51 -06:00
|
|
|
case <- p.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 03:58:46 -06:00
|
|
|
// Inbound handler. Inbound messages are received here and passed to the appropriate methods
|
2014-01-09 16:15:51 -06:00
|
|
|
func (p *Peer) HandleInbound() {
|
2014-01-09 17:39:16 -06:00
|
|
|
defer p.Stop()
|
2014-01-09 16:15:51 -06:00
|
|
|
|
|
|
|
out:
|
|
|
|
for {
|
2014-01-10 03:58:46 -06:00
|
|
|
// Wait for a message from the peer
|
2014-01-10 14:18:56 -06:00
|
|
|
msg, err := ethwire.ReadMessage(p.conn)
|
2014-01-09 16:15:51 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
2014-01-09 17:39:16 -06:00
|
|
|
// TODO
|
2014-01-10 14:18:56 -06:00
|
|
|
data, _ := Decode(msg.Data, 0)
|
|
|
|
log.Printf("%s, %s\n", msg.MsgType, data)
|
2014-01-09 16:15:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the out handler we're quiting
|
|
|
|
p.quit <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) Start() {
|
2014-01-10 03:58:46 -06:00
|
|
|
// Run the outbound handler in a new goroutine
|
2014-01-09 16:15:51 -06:00
|
|
|
go p.HandleOutbound()
|
2014-01-10 03:58:46 -06:00
|
|
|
// Run the inbound handler in a new goroutine
|
2014-01-09 16:15:51 -06:00
|
|
|
go p.HandleInbound()
|
|
|
|
}
|
2014-01-09 17:39:16 -06:00
|
|
|
|
|
|
|
func (p *Peer) Stop() {
|
2014-01-10 14:18:56 -06:00
|
|
|
p.conn.Close()
|
2014-01-09 17:39:16 -06:00
|
|
|
|
|
|
|
p.quit <- true
|
|
|
|
}
|