1 // Copyright (c) 2016 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "asyncrpcoperation.h"
7 #include <boost/uuid/uuid.hpp>
8 #include <boost/uuid/uuid_generators.hpp>
9 #include <boost/uuid/uuid_io.hpp>
17 static boost::uuids::random_generator uuidgen;
19 static std::map<OperationStatus, std::string> OperationStatusMap = {
20 {OperationStatus::READY, "queued"},
21 {OperationStatus::EXECUTING, "executing"},
22 {OperationStatus::CANCELLED, "cancelled"},
23 {OperationStatus::FAILED, "failed"},
24 {OperationStatus::SUCCESS, "success"}
28 * Every operation instance should have a globally unique id
30 AsyncRPCOperation::AsyncRPCOperation() : error_code_(0), error_message_() {
31 // Set a unique reference for each operation
32 boost::uuids::uuid uuid = uuidgen();
33 id_ = "opid-" + boost::uuids::to_string(uuid);
34 creation_time_ = (int64_t)time(NULL);
35 set_state(OperationStatus::READY);
38 AsyncRPCOperation::AsyncRPCOperation(const AsyncRPCOperation& o) :
39 id_(o.id_), creation_time_(o.creation_time_), state_(o.state_.load()),
40 start_time_(o.start_time_), end_time_(o.end_time_),
41 error_code_(o.error_code_), error_message_(o.error_message_),
46 AsyncRPCOperation& AsyncRPCOperation::operator=( const AsyncRPCOperation& other ) {
47 this->id_ = other.id_;
48 this->creation_time_ = other.creation_time_;
49 this->state_.store(other.state_.load());
50 this->start_time_ = other.start_time_;
51 this->end_time_ = other.end_time_;
52 this->error_code_ = other.error_code_;
53 this->error_message_ = other.error_message_;
54 this->result_ = other.result_;
59 AsyncRPCOperation::~AsyncRPCOperation() {
63 * Override this cancel() method if you can interrupt main() when executing.
65 void AsyncRPCOperation::cancel() {
67 set_state(OperationStatus::CANCELLED);
72 * Start timing the execution run of the code you're interested in
74 void AsyncRPCOperation::start_execution_clock() {
75 std::lock_guard<std::mutex> guard(lock_);
76 start_time_ = std::chrono::system_clock::now();
80 * Stop timing the execution run
82 void AsyncRPCOperation::stop_execution_clock() {
83 std::lock_guard<std::mutex> guard(lock_);
84 end_time_ = std::chrono::system_clock::now();
88 * Implement this virtual method in any subclass. This is just an example implementation.
90 void AsyncRPCOperation::main() {
95 set_state(OperationStatus::EXECUTING);
97 start_execution_clock();
99 // Do some work here..
101 stop_execution_clock();
103 // If there was an error, you might set it like this:
106 setErrorMessage("Murphy's law");
107 setState(OperationStatus::FAILED);
110 // Otherwise, if the operation was a success:
111 UniValue v(UniValue::VSTR, "We have a result!");
113 set_state(OperationStatus::SUCCESS);
117 * Return the error of the completed operation as a UniValue object.
118 * If there is no error, return null UniValue.
120 UniValue AsyncRPCOperation::getError() const {
125 std::lock_guard<std::mutex> guard(lock_);
126 UniValue error(UniValue::VOBJ);
127 error.push_back(Pair("code", this->error_code_));
128 error.push_back(Pair("message", this->error_message_));
133 * Return the result of the completed operation as a UniValue object.
134 * If the operation did not succeed, return null UniValue.
136 UniValue AsyncRPCOperation::getResult() const {
141 std::lock_guard<std::mutex> guard(lock_);
142 return this->result_;
147 * Returns a status UniValue object.
148 * If the operation has failed, it will include an error object.
149 * If the operation has succeeded, it will include the result value.
150 * If the operation was cancelled, there will be no error object or result value.
152 UniValue AsyncRPCOperation::getStatus() const {
153 OperationStatus status = this->getState();
154 UniValue obj(UniValue::VOBJ);
155 obj.push_back(Pair("id", this->id_));
156 obj.push_back(Pair("status", OperationStatusMap[status]));
157 obj.push_back(Pair("creation_time", this->creation_time_));
158 // TODO: Issue #1354: There may be other useful metadata to return to the user.
159 UniValue err = this->getError();
161 obj.push_back(Pair("error", err.get_obj()));
163 UniValue result = this->getResult();
164 if (!result.isNull()) {
165 obj.push_back(Pair("result", result));
167 // Include execution time for successful operation
168 std::chrono::duration<double> elapsed_seconds = end_time_ - start_time_;
169 obj.push_back(Pair("execution_secs", elapsed_seconds.count()));
176 * Return the operation state in human readable form.
178 std::string AsyncRPCOperation::getStateAsString() const {
179 OperationStatus status = this->getState();
180 return OperationStatusMap[status];