Electroneum
message.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2019, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "rapidjson/document.h"
33 #include <string>
34 
35 /* I normally hate using macros, but in this case it would be untenably
36  * verbose to not use a macro. This macro saves the trouble of explicitly
37  * writing the below if block for every single RPC call.
38  */
39 #define REQ_RESP_TYPES_MACRO( runtime_str, type, reqjson, resp_message_ptr, handler) \
40  \
41  if (runtime_str == type::name) \
42  { \
43  type::Request reqvar; \
44  type::Response *respvar = new type::Response(); \
45  \
46  reqvar.fromJson(reqjson); \
47  \
48  handler(reqvar, *respvar); \
49  \
50  resp_message_ptr = respvar; \
51  }
52 
53 namespace cryptonote
54 {
55 
56 namespace rpc
57 {
58 
59  class Message
60  {
61  public:
62  static const char* STATUS_OK;
63  static const char* STATUS_RETRY;
64  static const char* STATUS_FAILED;
65  static const char* STATUS_BAD_REQUEST;
66  static const char* STATUS_BAD_JSON;
67 
69 
70  virtual ~Message() { }
71 
72  virtual rapidjson::Value toJson(rapidjson::Document& doc) const;
73 
74  virtual void fromJson(rapidjson::Value& val);
75 
79  };
80 
82  {
83  public:
85 
86  FullMessage(FullMessage&& rhs) noexcept : doc(std::move(rhs.doc)) { }
87 
88  FullMessage(const std::string& json_string, bool request=false);
89 
91 
93 
95 
97 
99 
100  void setID(rapidjson::Value& id);
101 
103 
104  static FullMessage requestMessage(const std::string& request, Message* message);
106 
109 
110  static FullMessage* timeoutMessage();
111  private:
112 
113  FullMessage() = default;
114 
115  FullMessage(const std::string& request, Message* message);
117 
119  };
120 
121 
122  // convenience functions for bad input
123  std::string BAD_REQUEST(const std::string& request);
125 
126  std::string BAD_JSON(const std::string& error_details);
127 
128 
129 } // namespace rpc
130 
131 } // namespace cryptonote
std::string error_details
Definition: message.h:77
static const char * STATUS_BAD_JSON
Definition: message.h:66
std::string status
Definition: message.h:76
::std::string string
Definition: gtest-port.h:1097
std::string BAD_REQUEST(const std::string &request)
Definition: message.cpp:259
virtual rapidjson::Value toJson(rapidjson::Document &doc) const
Definition: message.cpp:57
static const char * STATUS_FAILED
Definition: message.h:64
rapidjson::Value & getID()
Definition: message.cpp:182
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
std::string getRequestType() const
Definition: message.cpp:152
static const char * STATUS_BAD_REQUEST
Definition: message.h:65
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2116
unsigned int uint32_t
Definition: stdint.h:126
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2512
virtual void fromJson(rapidjson::Value &val)
Definition: message.cpp:70
static FullMessage requestMessage(const std::string &request, Message *message)
Definition: message.cpp:214
static FullMessage * timeoutMessage()
Definition: message.cpp:238
std::string message("Message requiring signing")
static const char * STATUS_RETRY
Definition: message.h:63
const T & move(const T &t)
Definition: gtest-port.h:1317
cryptonote::rpc::error getError()
Definition: message.cpp:201
static FullMessage responseMessage(Message *message)
Definition: message.cpp:226
void setID(rapidjson::Value &id)
Definition: message.cpp:188
rapidjson::Value & getMessage()
Definition: message.cpp:158
static const char * STATUS_OK
Definition: message.h:62
FullMessage(FullMessage &&rhs) noexcept
Definition: message.h:86
rapidjson::Value getMessageCopy()
Definition: message.cpp:175
std::string BAD_JSON(const std::string &error_details)
Definition: message.cpp:281