From 34689cb3f369ad71164b81d0c05238d78cb67945 Mon Sep 17 00:00:00 2001 From: obscuren Date: Mon, 12 Jan 2015 20:36:45 +0100 Subject: Added manual triggering of filters --- whisper/whisper.go | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'whisper') 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), + } +} -- cgit