diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-13 03:36:45 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-13 03:36:45 +0800 |
commit | 34689cb3f369ad71164b81d0c05238d78cb67945 (patch) | |
tree | 63b0d68be8d3d719e3a46ef73d39bb49b6de9f5f /whisper | |
parent | 8a1b51c716abdc21a8af43282e106d77eda3706b (diff) | |
download | dexon-34689cb3f369ad71164b81d0c05238d78cb67945.tar.gz dexon-34689cb3f369ad71164b81d0c05238d78cb67945.tar.zst dexon-34689cb3f369ad71164b81d0c05238d78cb67945.zip |
Added manual triggering of filters
Diffstat (limited to 'whisper')
-rw-r--r-- | whisper/whisper.go | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/whisper/whisper.go b/whisper/whisper.go index 3ff4bac5a..bdc69f199 100644 --- a/whisper/whisper.go +++ b/whisper/whisper.go @@ -126,6 +126,20 @@ func (self *Whisper) Watch(opts Filter) int { }) } +func (self *Whisper) Trigger(id int) { + filter := self.filters.Get(id) + if filter != nil { + for _, e := range self.messages { + if msg, key := self.open(e); msg != nil { + f := createFilter(msg, e.Topics, key) + if self.filters.Match(filter, f) { + self.filters.Notify(f, msg) + } + } + } + } +} + // Main handler for passing whisper messages to whisper peer objects func (self *Whisper) msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error { wpeer := NewPeer(self, peer, ws) @@ -227,19 +241,28 @@ func (self *Whisper) envelopes() (envelopes []*Envelope) { } func (self *Whisper) postEvent(envelope *Envelope) { + if message, key := self.open(envelope); message != nil { + self.filters.Notify(createFilter(message, envelope.Topics, key), message) + } +} + +func (self *Whisper) open(envelope *Envelope) (*Message, *ecdsa.PrivateKey) { for _, key := range self.keys { if message, err := envelope.Open(key); err == nil || (err != nil && err == ecies.ErrInvalidPublicKey) { - self.filters.Notify(filter.Generic{ - Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())), - Data: bytesToMap(envelope.Topics), - }, message) - break - } else { - wlogger.Infoln(err) + return message, key } } + + return nil, nil } func (self *Whisper) Protocol() p2p.Protocol { return self.protocol } + +func createFilter(message *Message, topics [][]byte, key *ecdsa.PrivateKey) filter.Filter { + return filter.Generic{ + Str1: string(crypto.FromECDSA(key)), Str2: string(crypto.FromECDSAPub(message.Recover())), + Data: bytesToMap(topics), + } +} |