Browse Source

Topic TopicMAsk builders.

cl-refactor
Gav Wood 10 years ago
parent
commit
cad2a1579c
  1. 6
      exp/main.cpp
  2. 24
      libwhisper/Common.cpp
  3. 62
      libwhisper/Common.h
  4. 22
      libwhisper/Interface.h
  5. 4
      libwhisper/WhisperHost.cpp
  6. 3
      libwhisper/WhisperHost.h

6
exp/main.cpp

@ -102,12 +102,12 @@ int main(int argc, char** argv)
if (!remoteHost.empty())
ph.connect(remoteHost, remotePort);
/// Only interested in the packet if the lowest bit is 1
auto w = wh->installWatch(TopicFilter(TopicMasks({{Topic("0000000000000000000000000000000000000000000000000000000000000001"), Topic("0000000000000000000000000000000000000000000000000000000000000001")}})));
/// Only interested in odd packets
auto w = wh->installWatch(BuildTopicMask()("odd"));
for (int i = 0; ; ++i)
{
wh->sendRaw(RLPStream().append(i * i).out(), Topic(u256(i)));
wh->sendRaw(RLPStream().append(i * i).out(), BuildTopic(i)(i % 2 ? "odd" : "even"));
for (auto i: wh->checkWatch(w))
{
auto p = wh->envelope(i).open().payload();

24
libwhisper/Common.cpp

@ -21,8 +21,32 @@
#include "Common.h"
#include <libdevcrypto/SHA3.h>
using namespace std;
using namespace dev;
using namespace dev::p2p;
using namespace dev::shh;
BuildTopic& BuildTopic::shiftBytes(bytes const& _b)
{
m_parts.push_back(dev::sha3(_b));
return *this;
}
h256 TopicFilter::sha3() const
{
RLPStream s;
fillStream(s);
return dev::sha3(s.out());
}
TopicMask BuildTopicMask::toTopicMask() const
{
TopicMask ret;
for (auto i = 0; i < 32; ++i)
{
ret.first[i] = m_parts[i * m_parts.size() / 32][i];
ret.second[i] = m_parts[i * m_parts.size() / 32] ? 255 : 0;
}
return ret;
}

62
libwhisper/Common.h

@ -25,7 +25,7 @@
#include <chrono>
#include <libdevcore/Common.h>
#include <libdevcore/Log.h>
#include <libethcore/CommonEth.h>
#include <libdevcore/RLP.h>
#include <libp2p/Capability.h>
namespace dev
@ -48,6 +48,8 @@ class WhisperHost;
class WhisperPeer;
class Whisper;
class Envelope;
enum WhisperPacket
{
StatusPacket = 0,
@ -59,5 +61,63 @@ enum WhisperPacket
using Topic = h256;
class BuildTopic
{
public:
template <class T> BuildTopic(T const& _t) { shift(_t); }
template <class T> BuildTopic& shift(T const& _r) { return shiftBytes(RLPStream().append(_r).out()); }
template <class T> BuildTopic& operator()(T const& _t) { return shift(_t); }
BuildTopic& shiftRaw(h256 const& _part) { m_parts.push_back(_part); return *this; }
operator Topic() const { return toTopic(); }
Topic toTopic() const { Topic ret; for (auto i = 0; i < 32; ++i) ret[i] = m_parts[i * m_parts.size() / 32][i]; return ret; }
protected:
BuildTopic() {}
BuildTopic& shiftBytes(bytes const& _b);
h256s m_parts;
};
using TopicMask = std::pair<Topic, Topic>;
using TopicMasks = std::vector<TopicMask>;
class TopicFilter
{
public:
TopicFilter() {}
TopicFilter(TopicMask const& _m): m_topicMasks(1, _m) {}
TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {}
TopicFilter(RLP const& _r): m_topicMasks((TopicMasks)_r) {}
void fillStream(RLPStream& _s) const { _s << m_topicMasks; }
h256 sha3() const;
bool matches(Envelope const& _m) const;
private:
TopicMasks m_topicMasks;
};
class BuildTopicMask: BuildTopic
{
public:
BuildTopicMask() { shift(); }
template <class T> BuildTopicMask(T const& _t) { shift(_t); }
template <class T> BuildTopicMask& shift(T const& _r) { BuildTopic::shift(_r); return *this; }
BuildTopicMask& shiftRaw(h256 const& _h) { BuildTopic::shiftRaw(_h); return *this; }
BuildTopic& shift() { m_parts.push_back(h256()); return *this; }
BuildTopicMask& operator()() { shift(); return *this; }
template <class T> BuildTopicMask& operator()(T const& _t) { shift(_t); return *this; }
operator TopicMask() const { return toTopicMask(); }
TopicMask toTopicMask() const;
};
}
}

22
libwhisper/Interface.h

@ -43,25 +43,6 @@ namespace shh
Topic mask;
};*/
using TopicMask = std::pair<Topic, Topic>;
using TopicMasks = std::vector<TopicMask>;
class TopicFilter
{
public:
TopicFilter() {}
TopicFilter(TopicMasks const& _m): m_topicMasks(_m) {}
TopicFilter(RLP const& _r): m_topicMasks((TopicMasks)_r) {}
void fillStream(RLPStream& _s) const { _s << m_topicMasks; }
h256 sha3() const { RLPStream s; fillStream(s); return dev::sha3(s.out()); }
bool matches(Envelope const& _m) const;
private:
TopicMasks m_topicMasks;
};
struct InstalledFilter
{
InstalledFilter(TopicFilter const& _f): filter(_f) {}
@ -86,8 +67,9 @@ public:
virtual void inject(Envelope const& _m, WhisperPeer* _from = nullptr) = 0;
unsigned installWatch(TopicMask const& _mask) { return installWatch(TopicFilter(_mask)); }
virtual unsigned installWatch(TopicFilter const& _filter) = 0;
virtual unsigned installWatch(h256 _filterId) = 0;
virtual unsigned installWatchOnId(h256 _filterId) = 0;
virtual void uninstallWatch(unsigned _watchId) = 0;
virtual h256s peekWatch(unsigned _watchId) const = 0;
virtual h256s checkWatch(unsigned _watchId) = 0;

4
libwhisper/WhisperHost.cpp

@ -87,7 +87,7 @@ void WhisperHost::noteChanged(h256 _messageHash, h256 _filter)
}
}
unsigned WhisperHost::installWatch(h256 _h)
unsigned WhisperHost::installWatchOnId(h256 _h)
{
auto ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0;
m_watches[ret] = ClientWatch(_h);
@ -104,7 +104,7 @@ unsigned WhisperHost::installWatch(shh::TopicFilter const& _f)
if (!m_filters.count(h))
m_filters.insert(make_pair(h, _f));
return installWatch(h);
return installWatchOnId(h);
}
void WhisperHost::uninstallWatch(unsigned _i)

3
libwhisper/WhisperHost.h

@ -50,8 +50,9 @@ public:
virtual void inject(Envelope const& _e, WhisperPeer* _from = nullptr);
using Interface::installWatch;
virtual unsigned installWatch(TopicFilter const& _filter);
virtual unsigned installWatch(h256 _filterId);
virtual unsigned installWatchOnId(h256 _filterId);
virtual void uninstallWatch(unsigned _watchId);
virtual h256s peekWatch(unsigned _watchId) const { dev::Guard l(m_filterLock); try { return m_watches.at(_watchId).changes; } catch (...) { return h256s(); } }
virtual h256s checkWatch(unsigned _watchId) { dev::Guard l(m_filterLock); h256s ret; try { ret = m_watches.at(_watchId).changes; m_watches.at(_watchId).changes.clear(); } catch (...) {} return ret; }

Loading…
Cancel
Save