interthread communication added
This commit is contained in:
parent
ef76b3daab
commit
bfd7f372b6
3 changed files with 46 additions and 8 deletions
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
#include <fc/rpc/websocket_api.hpp>
|
||||
|
||||
using namespace graphene::app;
|
||||
|
||||
ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent )
|
||||
:QObject(parent),m_thread(&t){}
|
||||
|
||||
|
|
@ -30,32 +32,55 @@ QQmlListProperty<Balance> Account::balances()
|
|||
}
|
||||
|
||||
|
||||
|
||||
GrapheneApplication::GrapheneApplication( QObject* parent )
|
||||
:QObject( parent ),m_thread("app")
|
||||
{
|
||||
connect( this, &GrapheneApplication::queueExecute,
|
||||
this, &GrapheneApplication::execute );
|
||||
|
||||
m_model = new ChainDataModel( m_thread, this );
|
||||
|
||||
start( "", "", "", "" );
|
||||
}
|
||||
|
||||
GrapheneApplication::~GrapheneApplication()
|
||||
{
|
||||
}
|
||||
|
||||
bool GrapheneApplication::start( QString datadir, QString apiurl )
|
||||
void GrapheneApplication::setIsConnected( bool v )
|
||||
{
|
||||
if( v != m_isConnected )
|
||||
{
|
||||
m_isConnected = v;
|
||||
Q_EMIT isConnectedChanged( m_isConnected );
|
||||
}
|
||||
}
|
||||
|
||||
void GrapheneApplication::start( QString datadir, QString apiurl, QString user, QString pass )
|
||||
{
|
||||
if( !m_thread.is_current() )
|
||||
{
|
||||
m_done = m_thread.async( [=](){ return start( datadir, apiurl ); } );
|
||||
return true;
|
||||
m_done = m_thread.async( [=](){ return start( datadir, apiurl, user, pass ); } );
|
||||
return;
|
||||
}
|
||||
try {
|
||||
auto con = m_client.connect( apiurl.toStdString() );
|
||||
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
|
||||
auto remote_api = apic->get_remote_api< login_api >(1);
|
||||
|
||||
if( !remote_api->login( user.toStdString(), pass.toStdString() ) )
|
||||
{
|
||||
// TODO: emit error
|
||||
return;
|
||||
}
|
||||
|
||||
queueExecute( [=](){setIsConnected( true );} );
|
||||
} catch ( const fc::exception& e )
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Q_SLOT void GrapheneApplication::execute( const std::function<void()>& func )const
|
||||
{
|
||||
func();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include <QQmlListProperty>
|
||||
|
||||
|
||||
Q_DECLARE_METATYPE(std::function<void()>)
|
||||
|
||||
|
||||
class Asset : public QObject {
|
||||
|
|
@ -78,7 +79,11 @@ class GrapheneApplication : public QObject {
|
|||
bool m_isConnected = false;
|
||||
|
||||
fc::http::websocket_client m_client;
|
||||
fc::future<bool> m_done;
|
||||
fc::future<void> m_done;
|
||||
|
||||
void setIsConnected( bool v );
|
||||
|
||||
Q_SLOT void execute( const std::function<void()>& )const;
|
||||
public:
|
||||
GrapheneApplication( QObject* parent = nullptr );
|
||||
~GrapheneApplication();
|
||||
|
|
@ -88,7 +93,10 @@ public:
|
|||
return m_model;
|
||||
}
|
||||
|
||||
Q_INVOKABLE bool start(QString dataDirectory, QString apiUrl);
|
||||
Q_INVOKABLE void start( QString dataDirectory,
|
||||
QString apiUrl,
|
||||
QString user,
|
||||
QString pass );
|
||||
|
||||
bool isConnected() const
|
||||
{
|
||||
|
|
@ -97,4 +105,5 @@ public:
|
|||
|
||||
signals:
|
||||
void isConnectedChanged(bool isConnected);
|
||||
void queueExecute( const std::function<void()>& );
|
||||
};
|
||||
|
|
|
|||
|
|
@ -4,10 +4,14 @@
|
|||
|
||||
#include "ClientDataModel.hpp"
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
fc::thread::current().set_name( "main" );
|
||||
QApplication app(argc, argv);
|
||||
|
||||
qRegisterMetaType<std::function<void()>>();
|
||||
|
||||
qmlRegisterType<Asset>("Graphene.Client", 0, 1, "Asset");
|
||||
qmlRegisterType<Balance>("Graphene.Client", 0, 1, "Balance");
|
||||
qmlRegisterType<Account>("Graphene.Client", 0, 1, "Account");
|
||||
|
|
|
|||
Loading…
Reference in a new issue