Skip to content

Commit

Permalink
Progress FollowMyVote#32: Test connecting app to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielhourt committed Jun 6, 2016
1 parent 32d9af1 commit a0f2300
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
3 changes: 3 additions & 0 deletions GrapheneBackend/BackendConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <kj/debug.h>

#include <fstream>

#include <fcntl.h>
#include <errno.h>

Expand All @@ -19,6 +21,7 @@ void BackendConfiguration::open(kj::StringPtr configFilePath, bool createIfMissi
KJ_REQUIRE(fd >= 0, "Failed to open file for reading", configFilePath, strerror(errno));
else if (fd < 0) {
KJ_LOG(WARNING, "Creating new configuration", configFilePath);
(void)std::ofstream(configFilePath);
filePath = kj::heapString(configFilePath);
save();
config = message.getRoot<Config>();
Expand Down
24 changes: 21 additions & 3 deletions VotingApp/BitsharesWalletBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,27 @@ kj::Promise<void> BWB::BlockchainWalletServer::transfer(TransferContext context)
}
////////////////////////////// END BlockchainWalletServer implementation

BlockchainWallet::Client BitsharesWalletBridge::nextWalletClient() {
KJ_REQUIRE(hasPendingConnections(), "Cannot get next wallet client without any pending connections");
return kj::heap<BlockchainWalletServer>(std::unique_ptr<QWebSocket>(nextPendingConnection()));
kj::Promise<BlockchainWallet::Client> BitsharesWalletBridge::nextWalletClient() {
if (hasPendingConnections()) {
auto server = kj::heap<BlockchainWalletServer>(std::unique_ptr<QWebSocket>(nextPendingConnection()));
return BlockchainWallet::Client(kj::mv(server));
}

auto paf = kj::newPromiseAndFulfiller<BlockchainWallet::Client>();
// Use shared_ptr's to avoid making a noncopyable lambda
auto connection = std::make_shared<QMetaObject::Connection>();
auto fulfiller = std::make_shared<decltype(paf.fulfiller)>(kj::mv(paf.fulfiller));
*connection = connect(this, &QWebSocketServer::newConnection, [this, connection, fulfiller]() mutable {
disconnect(*connection);
connection.reset();
KJ_LOG(DBG, "Fulfilling promise for a BlockchainWallet client");
std::unique_ptr<QWebSocket> peer(nextPendingConnection());
qDebug() << "Connection from" << peer->peerName() << "at" << peer->peerAddress() << ":" << peer->peerPort();
(*fulfiller)->fulfill(kj::heap<BlockchainWalletServer>(kj::mv(peer)));
fulfiller.reset();
});
KJ_LOG(DBG, "Promising a BlockchainWallet client");
return kj::mv(paf.promise);
}

} } // namespace swv::bts
2 changes: 1 addition & 1 deletion VotingApp/BitsharesWalletBridge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BitsharesWalletBridge : public QWebSocketServer {
* @brief Get the nextPendingConnection as a BlockchainWallet::Client
* @return A BlockchainWallet::Client which wraps the next pending connection
*/
BlockchainWallet::Client nextWalletClient();
kj::Promise<BlockchainWallet::Client> nextWalletClient();
};

} } // namespace swv::bts
Expand Down
19 changes: 16 additions & 3 deletions VotingApp/VotingSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,27 +283,40 @@ Promise* VotingSystem::connectToBackend(QString hostname, quint16 port) {
return connectPromise;
}

void VotingSystem::configureChainAdaptor(bool useTestingBackend) {
Promise* VotingSystem::configureChainAdaptor(bool useTestingBackend) {
Q_D(VotingSystem);

Promise* configuredPromise = new Promise(this);

//TODO: make a real implementation of this
if (useTestingBackend) {
auto chain = kj::heap<FakeBlockchain>();
auto backendStub = chain->getBackendStub();
d->chain->setChain(kj::mv(chain));
d->backend = kj::heap<BackendApi>(backendStub, *d->promiseConverter);
emit backendConnectedChanged(true);
configuredPromise->resolve({});
} else {
if (!d->bitsharesBridge)
d->bitsharesBridge = kj::heap<bts::BitsharesWalletBridge>(qApp->applicationName());
if (!d->bitsharesBridge->isListening() && !d->bitsharesBridge->listen(QHostAddress::LocalHost)) {
setLastError(tr("Unable to listen for Bitshares wallet: %1").arg(d->bitsharesBridge->errorString()));
return;
return nullptr;
}
QDesktopServices::openUrl(QStringLiteral("web+bts:connect/%1:%2")
KJ_LOG(DBG, "Listening for Bitshares wallet",
d->bitsharesBridge->serverAddress().toString().toStdString(),
d->bitsharesBridge->serverPort());
d->tasks.add(d->bitsharesBridge->nextWalletClient().then([d, configuredPromise](BlockchainWallet::Client c) {
KJ_LOG(DBG, "Setting blockchain");
d->chain->setChain(c);
configuredPromise->resolve({});
}));
QDesktopServices::openUrl(QStringLiteral("web+bts:%1:%2")
.arg(d->bitsharesBridge->serverAddress().toString())
.arg(d->bitsharesBridge->serverPort()));
}

return configuredPromise;
}

Promise* VotingSystem::castCurrentDecision(swv::data::Contest* contest) {
Expand Down
2 changes: 1 addition & 1 deletion VotingApp/VotingSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class VotingSystem : public QObject
void currentAccountChanged(swv::data::Account* currentAccount);

public slots:
void configureChainAdaptor(bool useTestingBackend = false);
Promise* configureChainAdaptor(bool useTestingBackend = false);

/**
* @brief Cancel changes to the decision on the given contest
Expand Down
4 changes: 3 additions & 1 deletion VotingApp/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ App {
signal connected

Component.onCompleted: {
configureChainAdaptor(true)
configureChainAdaptor(false).then(function() {
connectToBackend("127.0.0.1", 17073)
})
}
onError: {
console.log("Error from Voting System: %1".arg(message))
Expand Down

0 comments on commit a0f2300

Please sign in to comment.