]>
Commit | Line | Data |
---|---|---|
fc72c078 S |
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 | ||
15 | using namespace std; | |
16 | using namespace json_spirit; | |
17 | ||
18 | static boost::uuids::random_generator uuidgen; | |
19 | ||
20 | std::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 | ||
3b54bf58 S |
28 | /** |
29 | * Every operation instance should have a globally unique id | |
30 | */ | |
31 | AsyncRPCOperation::AsyncRPCOperation() : error_code_(0), error_message_() { | |
fc72c078 S |
32 | // Set a unique reference for each operation |
33 | boost::uuids::uuid uuid = uuidgen(); | |
e2574666 | 34 | id_ = "opid-" + boost::uuids::to_string(uuid); |
3b54bf58 | 35 | creation_time_ = (int64_t)time(NULL); |
e2574666 | 36 | set_state(OperationStatus::READY); |
fc72c078 S |
37 | } |
38 | ||
61ea2aba | 39 | AsyncRPCOperation::AsyncRPCOperation(const AsyncRPCOperation& o) : |
e2574666 S |
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_) | |
fc72c078 S |
44 | { |
45 | } | |
46 | ||
fc72c078 | 47 | AsyncRPCOperation& AsyncRPCOperation::operator=( const AsyncRPCOperation& other ) { |
e2574666 | 48 | this->id_ = other.id_; |
3b54bf58 S |
49 | this->creation_time_ = other.creation_time_; |
50 | this->state_.store(other.state_.load()); | |
e2574666 S |
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_; | |
fc72c078 S |
56 | return *this; |
57 | } | |
58 | ||
59 | ||
60 | AsyncRPCOperation::~AsyncRPCOperation() { | |
61 | } | |
62 | ||
3b54bf58 S |
63 | /** |
64 | * Override this cancel() method if you can interrupt main() when executing. | |
65 | */ | |
fc72c078 | 66 | void AsyncRPCOperation::cancel() { |
3b54bf58 S |
67 | if (isReady()) { |
68 | set_state(OperationStatus::CANCELLED); | |
69 | } | |
fc72c078 S |
70 | } |
71 | ||
3b54bf58 S |
72 | /** |
73 | * Start timing the execution run of the code you're interested in | |
74 | */ | |
75 | void AsyncRPCOperation::start_execution_clock() { | |
e2574666 | 76 | std::lock_guard<std::mutex> guard(lock_); |
3b54bf58 | 77 | start_time_ = std::chrono::system_clock::now(); |
fc72c078 S |
78 | } |
79 | ||
3b54bf58 S |
80 | /** |
81 | * Stop timing the execution run | |
82 | */ | |
83 | void AsyncRPCOperation::stop_execution_clock() { | |
e2574666 | 84 | std::lock_guard<std::mutex> guard(lock_); |
3b54bf58 | 85 | end_time_ = std::chrono::system_clock::now(); |
fc72c078 S |
86 | } |
87 | ||
3b54bf58 S |
88 | /** |
89 | * Implement this virtual method in any subclass. This is just an example implementation. | |
90 | */ | |
fc72c078 | 91 | void AsyncRPCOperation::main() { |
3b54bf58 | 92 | if (isCancelled()) { |
fc72c078 | 93 | return; |
3b54bf58 | 94 | } |
fc72c078 | 95 | |
3b54bf58 | 96 | set_state(OperationStatus::EXECUTING); |
fc72c078 | 97 | |
3b54bf58 | 98 | start_execution_clock(); |
fc72c078 | 99 | |
3b54bf58 | 100 | // Do some work here.. |
fc72c078 | 101 | |
3b54bf58 | 102 | stop_execution_clock(); |
fc72c078 | 103 | |
3b54bf58 S |
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 | */ | |
fc72c078 | 110 | |
3b54bf58 | 111 | // Otherwise, if the operation was a success: |
fc72c078 | 112 | Value v("We have a result!"); |
3b54bf58 S |
113 | set_result(v); |
114 | set_state(OperationStatus::SUCCESS); | |
fc72c078 S |
115 | } |
116 | ||
3b54bf58 S |
117 | /** |
118 | * Return the error of the completed operation as a Value object. | |
61ea2aba | 119 | * If there is no error, return null Value. |
3b54bf58 | 120 | */ |
fc72c078 | 121 | Value AsyncRPCOperation::getError() const { |
3b54bf58 | 122 | if (!isFailed()) { |
fc72c078 | 123 | return Value::null; |
3b54bf58 | 124 | } |
fc72c078 | 125 | |
e2574666 | 126 | std::lock_guard<std::mutex> guard(lock_); |
fc72c078 | 127 | Object error; |
3b54bf58 S |
128 | error.push_back(Pair("code", this->error_code_)); |
129 | error.push_back(Pair("message", this->error_message_)); | |
fc72c078 S |
130 | return Value(error); |
131 | } | |
132 | ||
3b54bf58 S |
133 | /** |
134 | * Return the result of the completed operation as a Value object. | |
61ea2aba | 135 | * If the operation did not succeed, return null Value. |
3b54bf58 | 136 | */ |
fc72c078 | 137 | Value AsyncRPCOperation::getResult() const { |
3b54bf58 | 138 | if (!isSuccess()) { |
fc72c078 | 139 | return Value::null; |
3b54bf58 | 140 | } |
fc72c078 | 141 | |
e2574666 | 142 | std::lock_guard<std::mutex> guard(lock_); |
3b54bf58 | 143 | return this->result_; |
fc72c078 S |
144 | } |
145 | ||
146 | ||
3b54bf58 | 147 | /** |
fc72c078 S |
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. | |
61ea2aba | 151 | * If the operation was cancelled, there will be no error object or result value. |
fc72c078 S |
152 | */ |
153 | Value AsyncRPCOperation::getStatus() const { | |
154 | OperationStatus status = this->getState(); | |
155 | Object obj; | |
e2574666 | 156 | obj.push_back(Pair("id", this->id_)); |
fc72c078 | 157 | obj.push_back(Pair("status", OperationStatusMap[status])); |
3b54bf58 | 158 | obj.push_back(Pair("creation_time", this->creation_time_)); |
61ea2aba | 159 | // TODO: Issue #1354: There may be other useful metadata to return to the user. |
fc72c078 S |
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 | |
3b54bf58 | 169 | std::chrono::duration<double> elapsed_seconds = end_time_ - start_time_; |
fc72c078 S |
170 | obj.push_back(Pair("execution_secs", elapsed_seconds.count())); |
171 | ||
172 | } | |
173 | return Value(obj); | |
174 | } | |
175 | ||
3b54bf58 S |
176 | /** |
177 | * Return the operation state in human readable form. | |
178 | */ | |
c72a4272 S |
179 | std::string AsyncRPCOperation::getStateAsString() const { |
180 | OperationStatus status = this->getState(); | |
181 | return OperationStatusMap[status]; | |
182 | } |