You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.4 KiB

10 years ago
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file WhisperHost.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "WhisperHost.h"
#include <libdevcore/CommonIO.h>
10 years ago
#include <libdevcore/Log.h>
#include <libp2p/All.h>
using namespace std;
using namespace dev;
using namespace dev::p2p;
using namespace dev::shh;
WhisperHost::WhisperHost(): Worker("shh")
10 years ago
{
}
WhisperHost::~WhisperHost()
{
}
void WhisperHost::streamMessage(h256 _m, RLPStream& _s) const
{
UpgradableGuard l(x_messages);
if (m_messages.count(_m))
{
UpgradeGuard ll(l);
auto const& m = m_messages.at(_m);
cnote << "streamRLP: " << m.expiry() << m.ttl() << m.topic() << toHex(m.data());
m.streamRLP(_s);
10 years ago
}
}
void WhisperHost::inject(Envelope const& _m, WhisperPeer* _p)
10 years ago
{
// this function processes messages originated both by local host (_p == null), and by remote peers (_p != null)
10 years ago
cnote << this << ": inject: " << _m.expiry() << _m.ttl() << _m.topic() << toHex(_m.data());
10 years ago
if (_m.expiry() <= (unsigned)time(0))
10 years ago
return;
10 years ago
auto h = _m.sha3();
{
UpgradableGuard l(x_messages);
if (m_messages.count(h))
return;
UpgradeGuard ll(l);
m_messages[h] = _m;
m_expiryQueue.insert(make_pair(_m.expiry(), h));
10 years ago
}
int rating = 1; // rating for local host is based upon: 1. installed watch; 2. proof of work
//if (bloomfilter.check)
//{
// rating *= 10;
if (_p) // originated externally
DEV_GUARDED(m_filterLock)
{
for (auto const& f: m_filters)
if (f.second.filter.matches(_m))
for (auto& i: m_watches)
if (i.second.id == f.first)
{
i.second.changes.push_back(h);
rating *= 10;
}
}
//}
10 years ago
// TODO p2p: capability-based rating
for (auto i: peerSessions())
{
auto w = i.first->cap<WhisperPeer>().get();
if (w == _p)
w->addRating(rating);
else
w->noteNewMessage(h, _m);
}
10 years ago
}
unsigned WhisperHost::installWatch(shh::Topics const& _t)
10 years ago
{
InstalledFilter f(_t);
10 years ago
h256 h = f.filter.sha3();
unsigned ret = 0;
10 years ago
DEV_GUARDED(m_filterLock)
{
auto it = m_filters.find(h);
if (m_filters.end() == it)
m_filters.insert(make_pair(h, f));
else
it->second.refCount++;
10 years ago
m_bloom.addRaw(f.filter.exportBloom());
ret = m_watches.size() ? m_watches.rbegin()->first + 1 : 0;
m_watches[ret] = ClientWatch(h);
cwatshh << "+++" << ret << h;
}
noteAdvertiseTopicsOfInterest();
return ret;
10 years ago
}
h256s WhisperHost::watchMessages(unsigned _watchId)
{
h256s ret;
auto wit = m_watches.find(_watchId);
if (wit == m_watches.end())
return ret;
TopicFilter f;
{
Guard l(m_filterLock);
auto fit = m_filters.find(wit->second.id);
if (fit == m_filters.end())
return ret;
f = fit->second.filter;
}
ReadGuard l(x_messages);
for (auto const& m: m_messages)
if (f.matches(m.second))
ret.push_back(m.first);
return ret;
}
10 years ago
void WhisperHost::uninstallWatch(unsigned _i)
{
cwatshh << "XXX" << _i;
DEV_GUARDED(m_filterLock)
{
auto it = m_watches.find(_i);
if (it == m_watches.end())
return;
auto id = it->second.id;
m_watches.erase(it);
auto fit = m_filters.find(id);
if (fit != m_filters.end())
{
m_bloom.removeRaw(fit->second.filter.exportBloom());
if (!--fit->second.refCount)
m_filters.erase(fit);
}
}
noteAdvertiseTopicsOfInterest();
10 years ago
}
10 years ago
void WhisperHost::doWork()
{
for (auto i: peerSessions())
10 years ago
i.first->cap<WhisperPeer>().get()->sendMessages();
cleanup();
}
10 years ago
void WhisperHost::cleanup()
{
// remove old messages.
// should be called every now and again.
10 years ago
unsigned now = (unsigned)time(0);
10 years ago
WriteGuard l(x_messages);
for (auto it = m_expiryQueue.begin(); it != m_expiryQueue.end() && it->first <= now; it = m_expiryQueue.erase(it))
m_messages.erase(it->second);
}
void WhisperHost::noteAdvertiseTopicsOfInterest()
{
for (auto i: peerSessions())
i.first->cap<WhisperPeer>().get()->noteAdvertiseTopicsOfInterest();
}