Browse Source

listening and peer count separated to QPeer2Peer

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
3b9ac153a0
  1. 4
      alethzero/MainWin.cpp
  2. 2
      alethzero/MainWin.h
  3. 1
      libethrpc/EthStubServer.cpp
  4. 4
      libethrpc/EthStubServer.h
  5. 50
      libqethereum/QEthereum.cpp
  6. 41
      libqethereum/QEthereum.h
  7. 1
      libwebthree/WebThree.h
  8. 4
      third/MainWin.cpp

4
alethzero/MainWin.cpp

@ -137,13 +137,15 @@ Main::Main(QWidget *parent) :
// NOTE: no need to delete as QETH_INSTALL_JS_NAMESPACE adopts it. // NOTE: no need to delete as QETH_INSTALL_JS_NAMESPACE adopts it.
m_ethereum = new QEthereum(this, ethereum(), owned()); m_ethereum = new QEthereum(this, ethereum(), owned());
m_whisper = new QWhisper(this, whisper()); m_whisper = new QWhisper(this, whisper());
m_p2p = new QPeer2Peer(this, peer2peer());
QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
QWebFrame* f = ui->webView->page()->mainFrame(); QWebFrame* f = ui->webView->page()->mainFrame();
f->disconnect(SIGNAL(javaScriptWindowObjectCleared())); f->disconnect(SIGNAL(javaScriptWindowObjectCleared()));
auto qeth = m_ethereum; auto qeth = m_ethereum;
auto qshh = m_whisper; auto qshh = m_whisper;
connect(f, &QWebFrame::javaScriptWindowObjectCleared, QETH_INSTALL_JS_NAMESPACE(f, qeth, qshh, this)); auto qp2p = m_p2p;
connect(f, &QWebFrame::javaScriptWindowObjectCleared, QETH_INSTALL_JS_NAMESPACE(f, qeth, qshh, qp2p, this));
}); });
connect(ui->webView, &QWebView::loadFinished, [=]() connect(ui->webView, &QWebView::loadFinished, [=]()

2
alethzero/MainWin.h

@ -75,6 +75,7 @@ public:
dev::WebThreeDirect* web3() const { return m_webThree.get(); } dev::WebThreeDirect* web3() const { return m_webThree.get(); }
dev::eth::Client* ethereum() const { return m_webThree->ethereum(); } dev::eth::Client* ethereum() const { return m_webThree->ethereum(); }
dev::p2p::Host* peer2peer() const { return m_webThree->peer2peer(); }
std::shared_ptr<dev::shh::WhisperHost> whisper() const { return m_webThree->whisper(); } std::shared_ptr<dev::shh::WhisperHost> whisper() const { return m_webThree->whisper(); }
QList<dev::KeyPair> const& owned() const { return m_myKeys; } QList<dev::KeyPair> const& owned() const { return m_myKeys; }
@ -249,4 +250,5 @@ private:
QEthereum* m_ethereum = nullptr; QEthereum* m_ethereum = nullptr;
QWhisper* m_whisper = nullptr; QWhisper* m_whisper = nullptr;
QPeer2Peer* m_p2p = nullptr;
}; };

1
libethrpc/EthStubServer.cpp

@ -17,6 +17,7 @@
/** @file EthStubServer.cpp /** @file EthStubServer.cpp
* @authors: * @authors:
* Gav Wood <i@gavwood.com> * Gav Wood <i@gavwood.com>
* Marek Kotewicz <marek@ethdev.com>
* @date 2014 * @date 2014
*/ */

4
libethrpc/EthStubServer.h

@ -15,7 +15,9 @@
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file EthStubServer.h /** @file EthStubServer.h
* @author Gav Wood <i@gavwood.com> * @authors:
* Gav Wood <i@gavwood.com>
* Marek Kotewicz <marek@ethdev.com>
* @date 2014 * @date 2014
*/ */

50
libqethereum/QEthereum.cpp

@ -5,6 +5,7 @@
#include <liblll/Compiler.h> #include <liblll/Compiler.h>
#include <libethereum/Client.h> #include <libethereum/Client.h>
#include <libethereum/EthereumHost.h> #include <libethereum/EthereumHost.h>
#include <libp2p/Host.h>
#include "QEthereum.h" #include "QEthereum.h"
using namespace std; using namespace std;
using namespace dev; using namespace dev;
@ -447,11 +448,6 @@ bool QEthereum::isMining() const
return m_client ? client()->isMining() : false; return m_client ? client()->isMining() : false;
} }
bool QEthereum::isListening() const
{
return /*m_client ? client()->haveNetwork() :*/ false;
}
void QEthereum::_private_setMining(bool _l) void QEthereum::_private_setMining(bool _l)
{ {
if (m_client) if (m_client)
@ -463,21 +459,6 @@ void QEthereum::_private_setMining(bool _l)
} }
} }
void QEthereum::_private_setListening(bool)
{
if (!m_client)
return;
/* if (_l)
client()->startNetwork();
else
client()->stopNetwork();*/
}
unsigned QEthereum::peerCount() const
{
return /*m_client ? (unsigned)client()->peerCount() :*/ 0;
}
QString QEthereum::doCreate(QString _secret, QString _amount, QString _init, QString _gas, QString _gasPrice) QString QEthereum::doCreate(QString _secret, QString _amount, QString _init, QString _gas, QString _gasPrice)
{ {
if (!m_client) if (!m_client)
@ -577,6 +558,35 @@ void QEthereum::poll()
emit watchChanged(w); emit watchChanged(w);
} }
QPeer2Peer::QPeer2Peer(QObject *_p, dev::p2p::Host *_p2p): QObject(_p), m_p2p(_p2p)
{
}
QPeer2Peer::~QPeer2Peer()
{
}
bool QPeer2Peer::isListening() const
{
return m_p2p ? m_p2p->isStarted() : false;
}
void QPeer2Peer::_private_setListening(bool _l)
{
if (!m_p2p)
return;
if (_l)
m_p2p->start();
else
m_p2p->stop();
}
unsigned QPeer2Peer::peerCount() const
{
return m_p2p ? (unsigned)m_p2p->peerCount() : 0;
}
// TODO: repot and hook all these up. // TODO: repot and hook all these up.
QWhisper::QWhisper(QObject* _p, std::shared_ptr<dev::shh::Interface> const& _c): QObject(_p), m_face(_c) QWhisper::QWhisper(QObject* _p, std::shared_ptr<dev::shh::Interface> const& _c): QObject(_p), m_face(_c)

41
libqethereum/QEthereum.h

@ -13,7 +13,11 @@ class Interface;
namespace shh { namespace shh {
class Interface; class Interface;
} }
namespace p2p {
class Host;
} }
}
class QJSEngine; class QJSEngine;
class QWebFrame; class QWebFrame;
@ -164,7 +168,6 @@ public:
Q_INVOKABLE void killWatch(unsigned _w); Q_INVOKABLE void killWatch(unsigned _w);
void clearWatches(); void clearWatches();
bool isListening() const;
bool isMining() const; bool isMining() const;
QString/*dev::Address*/ coinbase() const; QString/*dev::Address*/ coinbase() const;
@ -177,12 +180,9 @@ public:
QString/*dev::Address*/ account() const; QString/*dev::Address*/ account() const;
QStringList/*list of dev::Address*/ accounts() const; QStringList/*list of dev::Address*/ accounts() const;
unsigned peerCount() const;
public slots: public slots:
void _private_setCoinbase(QString/*dev::Address*/); void _private_setCoinbase(QString/*dev::Address*/);
void _private_setMining(bool _l); void _private_setMining(bool _l);
void _private_setListening(bool _l);
void setDefault(int _block); void setDefault(int _block);
/// Check to see if anything has changed, fire off signals if so. /// Check to see if anything has changed, fire off signals if so.
@ -190,20 +190,17 @@ public slots:
void poll(); void poll();
signals: signals:
void netChanged();
void watchChanged(unsigned _w); void watchChanged(unsigned _w);
void coinbaseChanged(); void coinbaseChanged();
void keysChanged(); void keysChanged();
void netChanged();
void miningChanged();
private: private:
Q_PROPERTY(QString coinbase READ coinbase WRITE _private_setCoinbase NOTIFY coinbaseChanged) Q_PROPERTY(QString coinbase READ coinbase WRITE _private_setCoinbase NOTIFY coinbaseChanged)
Q_PROPERTY(bool listening READ isListening WRITE _private_setListening NOTIFY netChanged)
Q_PROPERTY(bool mining READ isMining WRITE _private_setMining NOTIFY netChanged) Q_PROPERTY(bool mining READ isMining WRITE _private_setMining NOTIFY netChanged)
Q_PROPERTY(QString gasPrice READ gasPrice) Q_PROPERTY(QString gasPrice READ gasPrice)
Q_PROPERTY(QString key READ key NOTIFY keysChanged) Q_PROPERTY(QString key READ key NOTIFY keysChanged)
Q_PROPERTY(QStringList keys READ keys NOTIFY keysChanged) Q_PROPERTY(QStringList keys READ keys NOTIFY keysChanged)
Q_PROPERTY(unsigned peerCount READ peerCount NOTIFY miningChanged)
Q_PROPERTY(int defaultBlock READ getDefault WRITE setDefault) Q_PROPERTY(int defaultBlock READ getDefault WRITE setDefault)
Q_PROPERTY(unsigned number READ number NOTIFY watchChanged) Q_PROPERTY(unsigned number READ number NOTIFY watchChanged)
@ -213,6 +210,28 @@ private:
dev::FixedHash<32> numberOrHash(QString const &_json) const; dev::FixedHash<32> numberOrHash(QString const &_json) const;
}; };
class QPeer2Peer : public QObject
{
Q_OBJECT
public:
QPeer2Peer(QObject *_p, dev::p2p::Host *_p2p);
virtual ~QPeer2Peer();
bool isListening() const;
void _private_setListening(bool _l);
unsigned peerCount() const;
signals:
void netChanged();
void miningChanged();
private:
Q_PROPERTY(bool listening READ isListening WRITE _private_setListening NOTIFY netChanged)
Q_PROPERTY(unsigned peerCount READ peerCount NOTIFY miningChanged)
dev::p2p::Host* m_p2p;
};
class QWhisper: public QObject class QWhisper: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -252,14 +271,17 @@ private:
std::vector<unsigned> m_watches; std::vector<unsigned> m_watches;
}; };
#define QETH_INSTALL_JS_NAMESPACE(frame, eth, shh, env) [frame, eth, shh, env]() \
#define QETH_INSTALL_JS_NAMESPACE(frame, eth, shh, p2p, env) [frame, eth, shh, p2p, env]() \
{ \ { \
frame->disconnect(); \ frame->disconnect(); \
frame->addToJavaScriptWindowObject("env", env, QWebFrame::QtOwnership); \ frame->addToJavaScriptWindowObject("env", env, QWebFrame::QtOwnership); \
frame->addToJavaScriptWindowObject("eth", eth, QWebFrame::ScriptOwnership); \ frame->addToJavaScriptWindowObject("eth", eth, QWebFrame::ScriptOwnership); \
frame->addToJavaScriptWindowObject("shh", eth, QWebFrame::ScriptOwnership); \ frame->addToJavaScriptWindowObject("shh", eth, QWebFrame::ScriptOwnership); \
frame->addToJavaScriptWindowObject("p2p", p2p, QWebFrame::ScriptOwnership); \
frame->evaluateJavaScript("eth.setCoinbase = function(a, f) { window.setTimeout(function () { eth.coinbase = a; if (f) {f(true);}}, 0); }"); \ frame->evaluateJavaScript("eth.setCoinbase = function(a, f) { window.setTimeout(function () { eth.coinbase = a; if (f) {f(true);}}, 0); }"); \
frame->evaluateJavaScript("eth.getCoinbase = function(f) { window.setTimeout(function () { if (f) {f(eth.coinbase);}}, 0); }"); \ frame->evaluateJavaScript("eth.getCoinbase = function(f) { window.setTimeout(function () { if (f) {f(eth.coinbase);}}, 0); }"); \
frame->evaluateJavaScript("eth.listening = {get listening() {return p2p.listening}, set listening(l) {p2p.listening = l}}"); \
frame->evaluateJavaScript("eth.setListening = function(a, f) { window.setTimeout(function () { eth.listening = a; if (f) {f(true);}}, 0); }"); \ frame->evaluateJavaScript("eth.setListening = function(a, f) { window.setTimeout(function () { eth.listening = a; if (f) {f(true);}}, 0); }"); \
frame->evaluateJavaScript("eth.getListening = function(f) { window.setTimeout(function () { if (f) {f(eth.listening);}}, 0); }"); \ frame->evaluateJavaScript("eth.getListening = function(f) { window.setTimeout(function () { if (f) {f(eth.listening);}}, 0); }"); \
frame->evaluateJavaScript("eth.setMining = function(a, f) { window.setTimeout(function () { eth.mining = a; if (f) {f(true);}}, 0); }"); \ frame->evaluateJavaScript("eth.setMining = function(a, f) { window.setTimeout(function () { eth.mining = a; if (f) {f(true);}}, 0); }"); \
@ -267,6 +289,7 @@ private:
frame->evaluateJavaScript("eth.getGasPrice = function(f) { window.setTimeout(function () { if (f) {f(eth.gasPrice);}}, 0); }"); \ frame->evaluateJavaScript("eth.getGasPrice = function(f) { window.setTimeout(function () { if (f) {f(eth.gasPrice);}}, 0); }"); \
frame->evaluateJavaScript("eth.getKey = function(f) { window.setTimeout(function () { if(f) {f(eth.key);}}, 0); }"); \ frame->evaluateJavaScript("eth.getKey = function(f) { window.setTimeout(function () { if(f) {f(eth.key);}}, 0); }"); \
frame->evaluateJavaScript("eth.getKeys = function(f) { window.setTimeout(function () { if (f) {f(eth.keys);}}, 0); }"); \ frame->evaluateJavaScript("eth.getKeys = function(f) { window.setTimeout(function () { if (f) {f(eth.keys);}}, 0); }"); \
frame->evaluateJavaScript("eth.peerCount = {get peerCount() {return p2p.peerCount}}"); \
frame->evaluateJavaScript("eth.getPeerCount = function(f) { window.setTimeout(function () { if (f) {f(eth.peerCount);}}, 0); }"); \ frame->evaluateJavaScript("eth.getPeerCount = function(f) { window.setTimeout(function () { if (f) {f(eth.peerCount);}}, 0); }"); \
frame->evaluateJavaScript("eth.getDefaultBlock = function(f) { window.setTimeout(function () { if (f) {f(eth.defaultBlock);}}, 0); }"); \ frame->evaluateJavaScript("eth.getDefaultBlock = function(f) { window.setTimeout(function () { if (f) {f(eth.defaultBlock);}}, 0); }"); \
frame->evaluateJavaScript("eth.getNumber = function(f) { window.setTimeout(function () { if (f) {f(eth.number);}}, 0); }"); \ frame->evaluateJavaScript("eth.getNumber = function(f) { window.setTimeout(function () { if (f) {f(eth.number);}}, 0); }"); \

1
libwebthree/WebThree.h

@ -76,6 +76,7 @@ public:
eth::Client* ethereum() const { if (!m_ethereum) BOOST_THROW_EXCEPTION(InterfaceNotSupported("eth")); return m_ethereum.get(); } eth::Client* ethereum() const { if (!m_ethereum) BOOST_THROW_EXCEPTION(InterfaceNotSupported("eth")); return m_ethereum.get(); }
std::shared_ptr<shh::WhisperHost> whisper() const { auto w = m_whisper.lock(); if (!w) BOOST_THROW_EXCEPTION(InterfaceNotSupported("shh")); return w; } std::shared_ptr<shh::WhisperHost> whisper() const { auto w = m_whisper.lock(); if (!w) BOOST_THROW_EXCEPTION(InterfaceNotSupported("shh")); return w; }
bzz::Interface* swarm() const { BOOST_THROW_EXCEPTION(InterfaceNotSupported("bzz")); } bzz::Interface* swarm() const { BOOST_THROW_EXCEPTION(InterfaceNotSupported("bzz")); }
p2p::Host* peer2peer() { return &m_net; };
// Misc stuff: // Misc stuff:

4
third/MainWin.cpp

@ -107,12 +107,14 @@ Main::Main(QWidget *parent) :
// NOTE: no need to delete as QETH_INSTALL_JS_NAMESPACE adopts it. // NOTE: no need to delete as QETH_INSTALL_JS_NAMESPACE adopts it.
m_ethereum = new QEthereum(this, ethereum(), owned()); m_ethereum = new QEthereum(this, ethereum(), owned());
m_whisper = new QWhisper(this, whisper()); m_whisper = new QWhisper(this, whisper());
m_p2p = new QPeer2Peer(this, peer2peer());
QWebFrame* f = ui->webView->page()->mainFrame(); QWebFrame* f = ui->webView->page()->mainFrame();
f->disconnect(SIGNAL(javaScriptWindowObjectCleared())); f->disconnect(SIGNAL(javaScriptWindowObjectCleared()));
auto qeth = m_ethereum; auto qeth = m_ethereum;
auto qshh = m_whisper; auto qshh = m_whisper;
connect(f, &QWebFrame::javaScriptWindowObjectCleared, QETH_INSTALL_JS_NAMESPACE(f, qeth, qshh, this)); auto qp2p = m_p2p;
connect(f, &QWebFrame::javaScriptWindowObjectCleared, QETH_INSTALL_JS_NAMESPACE(f, qeth, qshh, qp2p, this));
}); });
connect(ui->webView, &QWebView::loadFinished, [=]() connect(ui->webView, &QWebView::loadFinished, [=]()

Loading…
Cancel
Save