rippled
Loading...
Searching...
No Matches
RPCSub.cpp
1#include <xrpld/rpc/RPCCall.h>
2#include <xrpld/rpc/RPCSub.h>
3
4#include <xrpl/basics/Log.h>
5#include <xrpl/basics/StringUtilities.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/json/to_string.h>
8
9#include <deque>
10
11namespace ripple {
12
13// Subscription object for JSON-RPC
14class RPCSubImp : public RPCSub
15{
16public:
18 InfoSub::Source& source,
19 boost::asio::io_context& io_context,
20 JobQueue& jobQueue,
21 std::string const& strUrl,
22 std::string const& strUsername,
23 std::string const& strPassword,
24 Logs& logs)
25 : RPCSub(source)
26 , m_io_context(io_context)
27 , m_jobQueue(jobQueue)
28 , mUrl(strUrl)
29 , mSSL(false)
30 , mUsername(strUsername)
31 , mPassword(strPassword)
32 , mSending(false)
33 , j_(logs.journal("RPCSub"))
34 , logs_(logs)
35 {
36 parsedURL pUrl;
37
38 if (!parseUrl(pUrl, strUrl))
39 Throw<std::runtime_error>("Failed to parse url.");
40 else if (pUrl.scheme == "https")
41 mSSL = true;
42 else if (pUrl.scheme != "http")
43 Throw<std::runtime_error>("Only http and https is supported.");
44
45 mSeq = 1;
46
47 mIp = pUrl.domain;
48 mPort = (!pUrl.port) ? (mSSL ? 443 : 80) : *pUrl.port;
49 mPath = pUrl.path;
50
51 JLOG(j_.info()) << "RPCCall::fromNetwork sub: ip=" << mIp
52 << " port=" << mPort
53 << " ssl= " << (mSSL ? "yes" : "no") << " path='"
54 << mPath << "'";
55 }
56
57 ~RPCSubImp() = default;
58
59 void
60 send(Json::Value const& jvObj, bool broadcast) override
61 {
63
64 auto jm = broadcast ? j_.debug() : j_.info();
65 JLOG(jm) << "RPCCall::fromNetwork push: " << jvObj;
66
68
69 if (!mSending)
70 {
71 // Start a sending thread.
72 JLOG(j_.info()) << "RPCCall::fromNetwork start";
73
75 jtCLIENT_SUBSCRIBE, "RPCSub::sendThread", [this]() {
76 sendThread();
77 });
78 }
79 }
80
81 void
82 setUsername(std::string const& strUsername) override
83 {
85
86 mUsername = strUsername;
87 }
88
89 void
90 setPassword(std::string const& strPassword) override
91 {
93
94 mPassword = strPassword;
95 }
96
97private:
98 // XXX Could probably create a bunch of send jobs in a single get of the
99 // lock.
100 void
102 {
103 Json::Value jvEvent;
104 bool bSend;
105
106 do
107 {
108 {
109 // Obtain the lock to manipulate the queue and change sending.
111
112 if (mDeque.empty())
113 {
114 mSending = false;
115 bSend = false;
116 }
117 else
118 {
119 auto const [seq, env] = mDeque.front();
120
122
123 jvEvent = env;
124 jvEvent["seq"] = seq;
125
126 bSend = true;
127 }
128 }
129
130 // Send outside of the lock.
131 if (bSend)
132 {
133 // XXX Might not need this in a try.
134 try
135 {
136 JLOG(j_.info()) << "RPCCall::fromNetwork: " << mIp;
137
140 mIp,
141 mPort,
142 mUsername,
143 mPassword,
144 mPath,
145 "event",
146 jvEvent,
147 mSSL,
148 true,
149 logs_);
150 }
151 catch (std::exception const& e)
152 {
153 JLOG(j_.info())
154 << "RPCCall::fromNetwork exception: " << e.what();
155 }
156 }
157 } while (bSend);
158 }
159
160private:
161 boost::asio::io_context& m_io_context;
163
167 bool mSSL;
171
172 int mSeq; // Next id to allocate.
173
174 bool mSending; // Sending threead is active.
175
177
180};
181
182//------------------------------------------------------------------------------
183
185{
186}
187
190 InfoSub::Source& source,
191 boost::asio::io_context& io_context,
192 JobQueue& jobQueue,
193 std::string const& strUrl,
194 std::string const& strUsername,
195 std::string const& strPassword,
196 Logs& logs)
197{
199 std::ref(source),
200 std::ref(io_context),
201 std::ref(jobQueue),
202 strUrl,
203 strUsername,
204 strPassword,
205 logs);
206}
207
208} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
A generic endpoint for log messages.
Definition Journal.h:41
Stream debug() const
Definition Journal.h:309
Stream info() const
Definition Journal.h:315
Abstracts the source of subscription data.
Definition InfoSub.h:49
Manages a client's subscription to data feeds.
Definition InfoSub.h:33
std::mutex mLock
Definition InfoSub.h:220
A pool of threads to perform work.
Definition JobQueue.h:39
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition JobQueue.h:149
Manages partitions for logging.
Definition Log.h:33
~RPCSubImp()=default
std::string mIp
Definition RPCSub.cpp:165
std::deque< std::pair< int, Json::Value > > mDeque
Definition RPCSub.cpp:176
void setPassword(std::string const &strPassword) override
Definition RPCSub.cpp:90
std::string mUsername
Definition RPCSub.cpp:168
beast::Journal const j_
Definition RPCSub.cpp:178
std::uint16_t mPort
Definition RPCSub.cpp:166
JobQueue & m_jobQueue
Definition RPCSub.cpp:162
boost::asio::io_context & m_io_context
Definition RPCSub.cpp:161
void setUsername(std::string const &strUsername) override
Definition RPCSub.cpp:82
std::string mPath
Definition RPCSub.cpp:170
void send(Json::Value const &jvObj, bool broadcast) override
Definition RPCSub.cpp:60
RPCSubImp(InfoSub::Source &source, boost::asio::io_context &io_context, JobQueue &jobQueue, std::string const &strUrl, std::string const &strUsername, std::string const &strPassword, Logs &logs)
Definition RPCSub.cpp:17
std::string mPassword
Definition RPCSub.cpp:169
std::string mUrl
Definition RPCSub.cpp:164
Subscription object for JSON RPC.
Definition RPCSub.h:13
RPCSub(InfoSub::Source &source)
Definition RPCSub.cpp:184
An endpoint that consumes resources.
Definition Consumer.h:17
T empty(T... args)
T front(T... args)
T is_same_v
T make_pair(T... args)
void fromNetwork(boost::asio::io_context &io_context, std::string const &strIp, std::uint16_t const iPort, std::string const &strUsername, std::string const &strPassword, std::string const &strPath, std::string const &strMethod, Json::Value const &jvParams, bool const bSSL, bool const quiet, Logs &logs, std::function< void(Json::Value const &jvInput)> callbackFuncP, std::unordered_map< std::string, std::string > headers)
Definition RPCCall.cpp:1658
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
std::shared_ptr< RPCSub > make_RPCSub(InfoSub::Source &source, boost::asio::io_context &io_context, JobQueue &jobQueue, std::string const &strUrl, std::string const &strUsername, std::string const &strPassword, Logs &logs)
Definition RPCSub.cpp:189
@ jtCLIENT_SUBSCRIBE
Definition Job.h:27
T pop_front(T... args)
T push_back(T... args)
T ref(T... args)
std::optional< std::uint16_t > port
T what(T... args)