Move all RPC to connection class
This commit is contained in:
56
src/rpc.h
56
src/rpc.h
@@ -56,64 +56,12 @@ public:
|
||||
void importZPrivKey(QString addr, bool rescan, const std::function<void(json)>& cb);
|
||||
void importTPrivKey(QString addr, bool rescan, const std::function<void(json)>& cb);
|
||||
|
||||
Turnstile* getTurnstile() { return turnstile; }
|
||||
|
||||
// Batch method. Note: Because of the template, it has to be in the header file.
|
||||
template<class T>
|
||||
void getBatchRPC(const QList<T>& payloads,
|
||||
std::function<json(T)> payloadGenerator,
|
||||
std::function<void(QMap<T, json>*)> cb) {
|
||||
auto responses = new QMap<T, json>(); // zAddr -> list of responses for each call.
|
||||
int totalSize = payloads.size();
|
||||
|
||||
for (auto item: payloads) {
|
||||
json payload = payloadGenerator(item);
|
||||
|
||||
QNetworkReply *reply = conn->restclient->post(*conn->request, QByteArray::fromStdString(payload.dump()));
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=] {
|
||||
reply->deleteLater();
|
||||
|
||||
auto all = reply->readAll();
|
||||
auto parsed = json::parse(all.toStdString(), nullptr, false);
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
qDebug() << QString::fromStdString(parsed.dump());
|
||||
qDebug() << reply->errorString();
|
||||
|
||||
(*responses)[item] = json::object(); // Empty object
|
||||
} else {
|
||||
if (parsed.is_discarded()) {
|
||||
(*responses)[item] = json::object(); // Empty object
|
||||
} else {
|
||||
(*responses)[item] = parsed["result"];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
auto waitTimer = new QTimer(main);
|
||||
QObject::connect(waitTimer, &QTimer::timeout, [=]() {
|
||||
if (responses->size() == totalSize) {
|
||||
waitTimer->stop();
|
||||
|
||||
cb(responses);
|
||||
|
||||
waitTimer->deleteLater();
|
||||
}
|
||||
});
|
||||
waitTimer->start(100);
|
||||
}
|
||||
|
||||
|
||||
Turnstile* getTurnstile() { return turnstile; }
|
||||
Connection* getConnection() { return conn; }
|
||||
|
||||
private:
|
||||
void noConnection();
|
||||
|
||||
void doRPC (const json& payload, const std::function<void(json)>& cb);
|
||||
void doSendRPC(const json& payload, const std::function<void(json)>& cb);
|
||||
void doSendRPC(const json& payload, const std::function<void(json)>& cb, const std::function<void(QString)>& err);
|
||||
|
||||
void refreshBalances();
|
||||
|
||||
void refreshTransactions();
|
||||
|
||||
Reference in New Issue
Block a user