2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-04-17 08:45:44 -05:00
|
|
|
// Contains the external API side message filter for watching, pooling and polling
|
2015-04-20 06:56:38 -05:00
|
|
|
// matched whisper messages, also serializing data access to avoid duplications.
|
2015-04-17 08:45:44 -05:00
|
|
|
|
|
|
|
package xeth
|
|
|
|
|
2015-04-20 06:56:38 -05:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
2015-04-17 08:45:44 -05:00
|
|
|
|
|
|
|
// whisperFilter is the message cache matching a specific filter, accumulating
|
|
|
|
// inbound messages until the are requested by the client.
|
|
|
|
type whisperFilter struct {
|
2015-04-20 06:56:38 -05:00
|
|
|
id int // Filter identifier for old message retrieval
|
|
|
|
ref *Whisper // Whisper reference for old message retrieval
|
|
|
|
|
|
|
|
cache []WhisperMessage // Cache of messages not yet polled
|
|
|
|
skip map[common.Hash]struct{} // List of retrieved messages to avoid duplication
|
|
|
|
update time.Time // Time of the last message query
|
|
|
|
|
|
|
|
lock sync.RWMutex // Lock protecting the filter internals
|
|
|
|
}
|
|
|
|
|
|
|
|
// newWhisperFilter creates a new serialized, poll based whisper topic filter.
|
|
|
|
func newWhisperFilter(id int, ref *Whisper) *whisperFilter {
|
|
|
|
return &whisperFilter{
|
|
|
|
id: id,
|
|
|
|
ref: ref,
|
|
|
|
|
|
|
|
update: time.Now(),
|
|
|
|
skip: make(map[common.Hash]struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// messages retrieves all the cached messages from the entire pool matching the
|
|
|
|
// filter, resetting the filter's change buffer.
|
|
|
|
func (w *whisperFilter) messages() []WhisperMessage {
|
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
|
|
|
|
|
|
|
w.cache = nil
|
|
|
|
w.update = time.Now()
|
|
|
|
|
|
|
|
w.skip = make(map[common.Hash]struct{})
|
|
|
|
messages := w.ref.Messages(w.id)
|
|
|
|
for _, message := range messages {
|
|
|
|
w.skip[message.ref.Hash] = struct{}{}
|
|
|
|
}
|
|
|
|
return messages
|
2015-04-17 08:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// insert injects a new batch of messages into the filter cache.
|
2015-04-20 06:56:38 -05:00
|
|
|
func (w *whisperFilter) insert(messages ...WhisperMessage) {
|
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
|
|
|
|
|
|
|
for _, message := range messages {
|
|
|
|
if _, ok := w.skip[message.ref.Hash]; !ok {
|
|
|
|
w.cache = append(w.cache, messages...)
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 08:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve fetches all the cached messages from the filter.
|
|
|
|
func (w *whisperFilter) retrieve() (messages []WhisperMessage) {
|
2015-04-20 06:56:38 -05:00
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
|
|
|
|
2015-04-17 08:45:44 -05:00
|
|
|
messages, w.cache = w.cache, nil
|
2015-04-20 06:56:38 -05:00
|
|
|
w.update = time.Now()
|
|
|
|
|
2015-04-17 08:45:44 -05:00
|
|
|
return
|
|
|
|
}
|
2015-04-20 06:56:38 -05:00
|
|
|
|
|
|
|
// activity returns the last time instance when client requests were executed on
|
|
|
|
// the filter.
|
|
|
|
func (w *whisperFilter) activity() time.Time {
|
|
|
|
w.lock.RLock()
|
|
|
|
defer w.lock.RUnlock()
|
|
|
|
|
|
|
|
return w.update
|
|
|
|
}
|