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