diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-08 19:43:33 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-08 19:43:33 +0800 |
commit | ebe2d9d872c5482e02508f1d3e9c3a56e8a41d44 (patch) | |
tree | c26c4e780226851bc699ca182bcb2f4d5da3dc0e /whisper/peer.go | |
parent | f06543fd066e11ac5c36f3eba4f1f82a7a1aefb8 (diff) | |
download | dexon-ebe2d9d872c5482e02508f1d3e9c3a56e8a41d44.tar.gz dexon-ebe2d9d872c5482e02508f1d3e9c3a56e8a41d44.tar.zst dexon-ebe2d9d872c5482e02508f1d3e9c3a56e8a41d44.zip |
First draft of Whisper messages relaying
Diffstat (limited to 'whisper/peer.go')
-rw-r--r-- | whisper/peer.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/whisper/peer.go b/whisper/peer.go new file mode 100644 index 000000000..5fe50ba59 --- /dev/null +++ b/whisper/peer.go @@ -0,0 +1,114 @@ +package whisper + +import ( + "fmt" + "io/ioutil" + "time" + + "github.com/ethereum/go-ethereum/p2p" + "gopkg.in/fatih/set.v0" +) + +const ( + protocolVersion = 0x02 +) + +type peer struct { + host *Whisper + peer *p2p.Peer + ws p2p.MsgReadWriter + + // XXX Eventually this is going to reach exceptional large space. We need an expiry here + known *set.Set + + quit chan struct{} +} + +func NewPeer(host *Whisper, p *p2p.Peer, ws p2p.MsgReadWriter) *peer { + return &peer{host, p, ws, set.New(), make(chan struct{})} +} + +func (self *peer) init() error { + if err := self.handleStatus(); err != nil { + return err + } + + return nil +} + +func (self *peer) start() { + go self.update() +} + +func (self *peer) update() { + relay := time.NewTicker(300 * time.Millisecond) +out: + for { + select { + case <-relay.C: + err := self.broadcast(self.host.envelopes()) + if err != nil { + self.peer.Infoln(err) + break out + } + + case <-self.quit: + break out + } + } +} + +func (self *peer) broadcast(envelopes []*Envelope) error { + envs := make([]interface{}, len(envelopes)) + i := 0 + for _, envelope := range envelopes { + if !self.known.Has(envelope.Hash()) { + envs[i] = envelope + self.known.Add(envelope.Hash()) + i++ + } + } + + msg := p2p.NewMsg(envelopesMsg, envs[:i]...) + if err := self.ws.WriteMsg(msg); err != nil { + return err + } + + return nil +} + +func (self *peer) handleStatus() error { + ws := self.ws + + if err := ws.WriteMsg(self.statusMsg()); err != nil { + return err + } + + msg, err := ws.ReadMsg() + if err != nil { + return err + } + + if msg.Code != statusMsg { + return fmt.Errorf("peer send %x before status msg", msg.Code) + } + + data, err := ioutil.ReadAll(msg.Payload) + if err != nil { + return err + } + + if len(data) == 0 { + return fmt.Errorf("malformed status. data len = 0") + } + + if pv := data[0]; pv != protocolVersion { + return fmt.Errorf("protocol version mismatch %d != %d", pv, protocolVersion) + } + + return nil +} + +func (self *peer) statusMsg() p2p.Msg { + return p2p.NewMsg(statusMsg, protocolVersion) +} |