From de8ccf0ea4225fb76715dd37da1a0a68c9c578bc Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Wed, 12 Aug 2015 14:56:33 -0400 Subject: [PATCH] [FWN] Create BlockChain class --- programs/full_web_node/BlockChain.cpp | 20 ++++++++++++ programs/full_web_node/BlockChain.hpp | 19 +++++++++++ programs/full_web_node/CMakeLists.txt | 7 ++-- programs/full_web_node/main.cpp | 46 +++++++++++++++++++++++++-- programs/full_web_node/qml/.gitignore | 1 + programs/full_web_node/qml/main.qml | 9 ++++-- 6 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 programs/full_web_node/BlockChain.cpp create mode 100644 programs/full_web_node/BlockChain.hpp create mode 100644 programs/full_web_node/qml/.gitignore diff --git a/programs/full_web_node/BlockChain.cpp b/programs/full_web_node/BlockChain.cpp new file mode 100644 index 00000000..51cd30a9 --- /dev/null +++ b/programs/full_web_node/BlockChain.cpp @@ -0,0 +1,20 @@ +#include "BlockChain.hpp" + +#include + +#include + +#include +#include +#include + +BlockChain::BlockChain() + : chainThread(fc::thread::current()), + fcTaskScheduler(new QTimer(this)), + grapheneApp(new graphene::app::application) +{ + fcTaskScheduler->setInterval(100); + fcTaskScheduler->setSingleShot(false); + connect(fcTaskScheduler, &QTimer::timeout, [] {fc::yield();}); + fcTaskScheduler->start(); +} diff --git a/programs/full_web_node/BlockChain.hpp b/programs/full_web_node/BlockChain.hpp new file mode 100644 index 00000000..ef281706 --- /dev/null +++ b/programs/full_web_node/BlockChain.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +class QTimer; +class QThread; +namespace fc { class thread; } +namespace graphene { namespace app { class application; } } +class BlockChain : public QObject { + Q_OBJECT + + fc::thread& chainThread; + QTimer* fcTaskScheduler; + graphene::app::application* grapheneApp; + +public: + BlockChain(); + virtual ~BlockChain(){} +}; diff --git a/programs/full_web_node/CMakeLists.txt b/programs/full_web_node/CMakeLists.txt index c758eaaf..71bda79a 100644 --- a/programs/full_web_node/CMakeLists.txt +++ b/programs/full_web_node/CMakeLists.txt @@ -5,7 +5,7 @@ project(full_web_node) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMake to run moc automatically when needed. -#set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTOMOC ON) find_package(Qt5Core) find_package(Qt5Quick) @@ -14,9 +14,10 @@ find_package(Qt5WebEngine) file(GLOB QML qml/*) qt5_add_resources(QML_QRC qml/qml.qrc) -add_executable(full_web_node main.cpp ${QML_QRC} ${QML}) +add_executable(full_web_node BlockChain.cpp main.cpp ${QML_QRC} ${QML}) -target_link_libraries(full_web_node PRIVATE Qt5::Core Qt5::Quick Qt5::WebEngine graphene_chain graphene_egenesis_brief graphene_utilities fc graphene_app ) +target_link_libraries(full_web_node PRIVATE Qt5::Core Qt5::Quick Qt5::WebEngine + graphene_chain graphene_egenesis_brief graphene_utilities fc graphene_account_history graphene_market_history graphene_app ) install( TARGETS full_web_node diff --git a/programs/full_web_node/main.cpp b/programs/full_web_node/main.cpp index 949270da..1f6bdcc7 100644 --- a/programs/full_web_node/main.cpp +++ b/programs/full_web_node/main.cpp @@ -1,7 +1,13 @@ +#include "BlockChain.hpp" + #include +#include +#include +#include #include #include +#include #include #include @@ -14,11 +20,45 @@ int main(int argc, char *argv[]) app.setOrganizationDomain("cryptonomex.org"); app.setOrganizationName("Cryptonomex, Inc."); + QThread chainThread; + fc::promise::ptr chain = new fc::promise; + QObject::connect(&chainThread, &QThread::started, [&app, &chain] { + chain->set_value(new BlockChain); + }); + chainThread.start(); + QtWebEngine::initialize(); + fc::http::server webGuiServer; + fc::thread serverThread("HTTP server thread"); + serverThread.async([&webGuiServer] { + webGuiServer.listen(fc::ip::endpoint::from_string("127.0.0.1:8080")); + webGuiServer.on_request([](const fc::http::request& request, const fc::http::server::response& response) { + QString path = QStringLiteral("/Users/nathan/graphene-ui/web/bundle") + QString::fromStdString(request.path); + if (path.endsWith('/')) + path.append(QStringLiteral("index.html")); + QFile file(path); + + if (file.exists()) { + file.open(QIODevice::ReadOnly); + auto buffer = file.readAll(); + response.set_status(fc::http::reply::OK); + response.set_length(buffer.size()); + response.write(buffer.data(), buffer.size()); + } else { + response.set_status(fc::http::reply::NotFound); + response.set_length(18); + response.write("I can't find that.", 18); + } + }); + }); + QQmlApplicationEngine engine; + auto chainPtr = chain->wait(); + engine.rootContext()->setContextProperty(QStringLiteral("blockChain"), chainPtr); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); - return app.exec(); + auto ret = app.exec(); + chainPtr->deleteLater(); + chainThread.exit(ret); + return ret; } - -#include "main.moc" diff --git a/programs/full_web_node/qml/.gitignore b/programs/full_web_node/qml/.gitignore new file mode 100644 index 00000000..bd58b541 --- /dev/null +++ b/programs/full_web_node/qml/.gitignore @@ -0,0 +1 @@ +qml.qrc.depends diff --git a/programs/full_web_node/qml/main.qml b/programs/full_web_node/qml/main.qml index 1368fbda..fc469d55 100644 --- a/programs/full_web_node/qml/main.qml +++ b/programs/full_web_node/qml/main.qml @@ -3,11 +3,14 @@ import QtQuick.Window 2.2 import QtWebEngine 1.0 Window { - width: 800 - height: 600 - visible: true + id: window + width: Screen.width / 2 + height: Screen.height / 2 + Rectangle { anchors.fill: parent; color: "#1F1F1F" } WebEngineView { anchors.fill: parent + url: "http://localhost:8080" + onLoadProgressChanged: if (loadProgress === 100) window.visible = true } }