]> Git Repo - VerusCoin.git/blame - src/asyncrpcoperation.cpp
Remove debug outs
[VerusCoin.git] / src / asyncrpcoperation.cpp
CommitLineData
fc72c078
S
1// Copyright (c) 2016 The Zcash developers
2// Distributed under the MIT software license, see the accompanying
bc909a7a 3// file COPYING or https://www.opensource.org/licenses/mit-license.php .
fc72c078
S
4
5#include "asyncrpcoperation.h"
6
7#include <boost/uuid/uuid.hpp>
8#include <boost/uuid/uuid_generators.hpp>
9#include <boost/uuid/uuid_io.hpp>
10
11#include <string>
12#include <ctime>
13#include <chrono>
14
15using namespace std;
fc72c078
S
16
17static boost::uuids::random_generator uuidgen;
18
39d2e9e0 19static std::map<OperationStatus, std::string> OperationStatusMap = {
fc72c078
S
20 {OperationStatus::READY, "queued"},
21 {OperationStatus::EXECUTING, "executing"},
22 {OperationStatus::CANCELLED, "cancelled"},
23 {OperationStatus::FAILED, "failed"},
24 {OperationStatus::SUCCESS, "success"}
25};
26
3b54bf58
S
27/**
28 * Every operation instance should have a globally unique id
29 */
30AsyncRPCOperation::AsyncRPCOperation() : error_code_(0), error_message_() {
fc72c078
S
31 // Set a unique reference for each operation
32 boost::uuids::uuid uuid = uuidgen();
e2574666 33 id_ = "opid-" + boost::uuids::to_string(uuid);
3b54bf58 34 creation_time_ = (int64_t)time(NULL);
e2574666 35 set_state(OperationStatus::READY);
fc72c078
S
36}
37
61ea2aba 38AsyncRPCOperation::AsyncRPCOperation(const AsyncRPCOperation& o) :
e2574666
S
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_),
42 result_(o.result_)
fc72c078
S
43{
44}
45
fc72c078 46AsyncRPCOperation& AsyncRPCOperation::operator=( const AsyncRPCOperation& other ) {
e2574666 47 this->id_ = other.id_;
3b54bf58
S
48 this->creation_time_ = other.creation_time_;
49 this->state_.store(other.state_.load());
e2574666
S
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_;
fc72c078
S
55 return *this;
56}
57
58
59AsyncRPCOperation::~AsyncRPCOperation() {
60}
61
3b54bf58
S
62/**
63 * Override this cancel() method if you can interrupt main() when executing.
64 */
fc72c078 65void AsyncRPCOperation::cancel() {
3b54bf58
S
66 if (isReady()) {
67 set_state(OperationStatus::CANCELLED);
68 }
fc72c078
S
69}
70
3b54bf58
S
71/**
72 * Start timing the execution run of the code you're interested in
73 */
74void AsyncRPCOperation::start_execution_clock() {
e2574666 75 std::lock_guard<std::mutex> guard(lock_);
3b54bf58 76 start_time_ = std::chrono::system_clock::now();
fc72c078
S
77}
78
3b54bf58
S
79/**
80 * Stop timing the execution run
81 */
82void AsyncRPCOperation::stop_execution_clock() {
e2574666 83 std::lock_guard<std::mutex> guard(lock_);
3b54bf58 84 end_time_ = std::chrono::system_clock::now();
fc72c078
S
85}
86
3b54bf58
S
87/**
88 * Implement this virtual method in any subclass. This is just an example implementation.
89 */
fc72c078 90void AsyncRPCOperation::main() {
3b54bf58 91 if (isCancelled()) {
fc72c078 92 return;
3b54bf58 93 }
fc72c078 94
3b54bf58 95 set_state(OperationStatus::EXECUTING);
fc72c078 96
3b54bf58 97 start_execution_clock();
fc72c078 98
3b54bf58 99 // Do some work here..
fc72c078 100
3b54bf58 101 stop_execution_clock();
fc72c078 102
3b54bf58
S
103 // If there was an error, you might set it like this:
104 /*
105 setErrorCode(123);
106 setErrorMessage("Murphy's law");
107 setState(OperationStatus::FAILED);
108 */
fc72c078 109
3b54bf58 110 // Otherwise, if the operation was a success:
0d37ae3a 111 UniValue v(UniValue::VSTR, "We have a result!");
3b54bf58
S
112 set_result(v);
113 set_state(OperationStatus::SUCCESS);
fc72c078
S
114}
115
3b54bf58 116/**
0d37ae3a
JG
117 * Return the error of the completed operation as a UniValue object.
118 * If there is no error, return null UniValue.
3b54bf58 119 */
0d37ae3a 120UniValue AsyncRPCOperation::getError() const {
3b54bf58 121 if (!isFailed()) {
0d37ae3a 122 return NullUniValue;
3b54bf58 123 }
fc72c078 124
e2574666 125 std::lock_guard<std::mutex> guard(lock_);
0d37ae3a 126 UniValue error(UniValue::VOBJ);
3b54bf58
S
127 error.push_back(Pair("code", this->error_code_));
128 error.push_back(Pair("message", this->error_message_));
0d37ae3a 129 return error;
fc72c078
S
130}
131
3b54bf58 132/**
0d37ae3a
JG
133 * Return the result of the completed operation as a UniValue object.
134 * If the operation did not succeed, return null UniValue.
3b54bf58 135 */
0d37ae3a 136UniValue AsyncRPCOperation::getResult() const {
3b54bf58 137 if (!isSuccess()) {
0d37ae3a 138 return NullUniValue;
3b54bf58 139 }
fc72c078 140
e2574666 141 std::lock_guard<std::mutex> guard(lock_);
3b54bf58 142 return this->result_;
fc72c078
S
143}
144
145
3b54bf58 146/**
0d37ae3a 147 * Returns a status UniValue object.
fc72c078
S
148 * If the operation has failed, it will include an error object.
149 * If the operation has succeeded, it will include the result value.
61ea2aba 150 * If the operation was cancelled, there will be no error object or result value.
fc72c078 151 */
0d37ae3a 152UniValue AsyncRPCOperation::getStatus() const {
fc72c078 153 OperationStatus status = this->getState();
0d37ae3a 154 UniValue obj(UniValue::VOBJ);
e2574666 155 obj.push_back(Pair("id", this->id_));
fc72c078 156 obj.push_back(Pair("status", OperationStatusMap[status]));
3b54bf58 157 obj.push_back(Pair("creation_time", this->creation_time_));
61ea2aba 158 // TODO: Issue #1354: There may be other useful metadata to return to the user.
0d37ae3a
JG
159 UniValue err = this->getError();
160 if (!err.isNull()) {
fc72c078
S
161 obj.push_back(Pair("error", err.get_obj()));
162 }
0d37ae3a
JG
163 UniValue result = this->getResult();
164 if (!result.isNull()) {
fc72c078
S
165 obj.push_back(Pair("result", result));
166
167 // Include execution time for successful operation
3b54bf58 168 std::chrono::duration<double> elapsed_seconds = end_time_ - start_time_;
fc72c078
S
169 obj.push_back(Pair("execution_secs", elapsed_seconds.count()));
170
171 }
0d37ae3a 172 return obj;
fc72c078
S
173}
174
3b54bf58
S
175/**
176 * Return the operation state in human readable form.
177 */
c72a4272
S
178std::string AsyncRPCOperation::getStateAsString() const {
179 OperationStatus status = this->getState();
180 return OperationStatusMap[status];
181}
This page took 0.141013 seconds and 4 git commands to generate.