WhisperHost.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file WhisperHost.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <mutex>
  20. #include <array>
  21. #include <set>
  22. #include <memory>
  23. #include <utility>
  24. #include <libdevcore/RLP.h>
  25. #include <libdevcore/Worker.h>
  26. #include <libdevcore/Guards.h>
  27. #include <libdevcore/SHA3.h>
  28. #include "Common.h"
  29. #include "WhisperPeer.h"
  30. #include "Interface.h"
  31. #include "BloomFilter.h"
  32. namespace dev
  33. {
  34. namespace shh
  35. {
  36. static const Topics EmptyTopics;
  37. class WhisperHost: public HostCapability<WhisperPeer>, public Interface, public Worker
  38. {
  39. friend class WhisperPeer;
  40. public:
  41. WhisperHost(bool _storeMessagesInDB = false);
  42. virtual ~WhisperHost();
  43. unsigned protocolVersion() const { return WhisperProtocolVersion; }
  44. void cleanup(); ///< remove old messages
  45. std::map<h256, Envelope> all() const { dev::ReadGuard l(x_messages); return m_messages; }
  46. TopicBloomFilterHash bloom() const { dev::Guard l(m_filterLock); return m_bloom; }
  47. virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr) override;
  48. virtual Topics const& fullTopics(unsigned _id) const override { try { return m_filters.at(m_watches.at(_id).id).full; } catch (...) { return EmptyTopics; } }
  49. virtual unsigned installWatch(Topics const& _filter) override;
  50. virtual void uninstallWatch(unsigned _watchId) override;
  51. virtual h256s peekWatch(unsigned _watchId) const override { dev::Guard l(m_filterLock); try { return m_watches.at(_watchId).changes; } catch (...) { return h256s(); } }
  52. virtual h256s checkWatch(unsigned _watchId) override;
  53. virtual h256s watchMessages(unsigned _watchId) override; ///< returns IDs of messages, which match specific watch criteria
  54. virtual Envelope envelope(h256 _m) const override { try { dev::ReadGuard l(x_messages); return m_messages.at(_m); } catch (...) { return Envelope(); } }
  55. protected:
  56. virtual void doWork() override;
  57. void noteAdvertiseTopicsOfInterest();
  58. bool isWatched(Envelope const& _e) const;
  59. private:
  60. virtual void onStarting() override { startWorking(); }
  61. virtual void onStopping() override { stopWorking(); }
  62. void streamMessage(h256 _m, RLPStream& _s) const;
  63. void saveMessagesToBD();
  64. void loadMessagesFromBD();
  65. mutable dev::SharedMutex x_messages;
  66. std::map<h256, Envelope> m_messages;
  67. std::multimap<unsigned, h256> m_expiryQueue;
  68. mutable dev::Mutex m_filterLock;
  69. std::map<h256, InstalledFilter> m_filters;
  70. std::map<unsigned, ClientWatch> m_watches;
  71. TopicBloomFilter m_bloom;
  72. bool m_storeMessagesInDB; ///< needed for tests and other special cases
  73. };
  74. }
  75. }