Fabcoin Core  0.16.2
P2P Digital Currency
WhisperHost.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #pragma once
23 
24 #include <mutex>
25 #include <array>
26 #include <set>
27 #include <memory>
28 #include <utility>
29 
30 #include <libdevcore/RLP.h>
31 #include <libdevcore/Worker.h>
32 #include <libdevcore/Guards.h>
33 #include <libdevcore/SHA3.h>
34 #include "Common.h"
35 #include "WhisperPeer.h"
36 #include "Interface.h"
37 #include "BloomFilter.h"
38 
39 namespace dev
40 {
41 namespace shh
42 {
43 
44 static const Topics EmptyTopics;
45 
46 class WhisperHost: public HostCapability<WhisperPeer>, public Interface, public Worker
47 {
48  friend class WhisperPeer;
49 
50 public:
51  WhisperHost(bool _storeMessagesInDB = false);
52  virtual ~WhisperHost();
53  unsigned protocolVersion() const { return WhisperProtocolVersion; }
54  void cleanup();
55  std::map<h256, Envelope> all() const { dev::ReadGuard l(x_messages); return m_messages; }
57 
58  virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr) override;
59  virtual Topics const& fullTopics(unsigned _id) const override { try { return m_filters.at(m_watches.at(_id).id).full; } catch (...) { return EmptyTopics; } }
60  virtual unsigned installWatch(Topics const& _filter) override;
61  virtual void uninstallWatch(unsigned _watchId) override;
62  virtual h256s peekWatch(unsigned _watchId) const override { dev::Guard l(m_filterLock); try { return m_watches.at(_watchId).changes; } catch (...) { return h256s(); } }
63  virtual h256s checkWatch(unsigned _watchId) override;
64  virtual h256s watchMessages(unsigned _watchId) override;
65  virtual Envelope envelope(h256 _m) const override { try { dev::ReadGuard l(x_messages); return m_messages.at(_m); } catch (...) { return Envelope(); } }
66 
67 protected:
68  virtual void doWork() override;
70  bool isWatched(Envelope const& _e) const;
71 
72 private:
73  virtual void onStarting() override { startWorking(); }
74  virtual void onStopping() override { stopWorking(); }
75  void streamMessage(h256 _m, RLPStream& _s) const;
76  void saveMessagesToBD();
77  void loadMessagesFromBD();
78 
80  std::map<h256, Envelope> m_messages;
81  std::multimap<unsigned, h256> m_expiryQueue;
82 
84  std::map<h256, InstalledFilter> m_filters;
85  std::map<unsigned, ClientWatch> m_watches;
87 
89 };
90 
91 }
92 }
void noteAdvertiseTopicsOfInterest()
std::map< unsigned, ClientWatch > m_watches
Definition: WhisperHost.h:85
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
dev::SharedMutex x_messages
Definition: WhisperHost.h:79
virtual void inject(Envelope const &_e, WhisperPeer *_from=nullptr) override
Definition: WhisperHost.cpp:55
dev::Mutex m_filterLock
Definition: WhisperHost.h:83
void streamMessage(h256 _m, RLPStream &_s) const
Definition: WhisperHost.cpp:43
virtual void onStarting() override
Definition: WhisperHost.h:73
virtual void onStopping() override
Definition: WhisperHost.h:74
h256s Topics
Definition: Common.h:69
std::map< h256, InstalledFilter > m_filters
Definition: WhisperHost.h:84
virtual h256s watchMessages(unsigned _watchId) override
returns IDs of messages, which match specific watch criteria
void cleanup()
remove old messages
TopicBloomFilterHash bloom() const
Definition: WhisperHost.h:56
std::map< h256, Envelope > all() const
Definition: WhisperHost.h:55
void stopWorking()
Stop worker thread; causes call to stopWorking().
Definition: Worker.cpp:85
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
unsigned protocolVersion() const
Definition: WhisperHost.h:53
TopicBloomFilter m_bloom
Definition: WhisperHost.h:86
WhisperHost(bool _storeMessagesInDB=false)
Definition: WhisperHost.cpp:33
virtual void uninstallWatch(unsigned _watchId) override
virtual Topics const & fullTopics(unsigned _id) const override
Definition: WhisperHost.h:59
boost::shared_lock< boost::shared_mutex > ReadGuard
Definition: Guards.h:44
void startWorking()
Starts worker thread; causes startedWorking() to be called.
Definition: Worker.cpp:30
virtual unsigned installWatch(Topics const &_filter) override
boost::shared_mutex SharedMutex
Definition: Guards.h:39
virtual void doWork() override
Called continuously following sleep for m_idleWaitMs.
bool isWatched(Envelope const &_e) const
bool m_storeMessagesInDB
needed for tests and other special cases
Definition: WhisperHost.h:88
virtual Envelope envelope(h256 _m) const override
Definition: WhisperHost.h:65
std::mutex Mutex
Definition: Guards.h:37
std::multimap< unsigned, h256 > m_expiryQueue
Definition: WhisperHost.h:81
std::map< h256, Envelope > m_messages
Definition: WhisperHost.h:80
virtual h256s checkWatch(unsigned _watchId) override
std::vector< h256 > h256s
Definition: FixedHash.h:345
Class for writing to an RLP bytestream.
Definition: RLP.h:383
virtual h256s peekWatch(unsigned _watchId) const override
Definition: WhisperHost.h:62