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 xrpl {
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 << " port=" << mPort
52 << " ssl= " << (mSSL ? "yes" : "no") << " path='" << mPath << "'";
53 }
54
55 ~RPCSubImp() = default;
56
57 void
58 send(Json::Value const& jvObj, bool broadcast) override
59 {
61
62 auto jm = broadcast ? j_.debug() : j_.info();
63 JLOG(jm) << "RPCCall::fromNetwork push: " << jvObj;
64
66
67 if (!mSending)
68 {
69 // Start a sending thread.
70 JLOG(j_.info()) << "RPCCall::fromNetwork start";
71
72 mSending = m_jobQueue.addJob(jtCLIENT_SUBSCRIBE, "RPCSubSendThr", [this]() { sendThread(); });
73 }
74 }
75
76 void
77 setUsername(std::string const& strUsername) override
78 {
80
81 mUsername = strUsername;
82 }
83
84 void
85 setPassword(std::string const& strPassword) override
86 {
88
89 mPassword = strPassword;
90 }
91
92private:
93 // XXX Could probably create a bunch of send jobs in a single get of the
94 // lock.
95 void
97 {
98 Json::Value jvEvent;
99 bool bSend;
100
101 do
102 {
103 {
104 // Obtain the lock to manipulate the queue and change sending.
106
107 if (mDeque.empty())
108 {
109 mSending = false;
110 bSend = false;
111 }
112 else
113 {
114 auto const [seq, env] = mDeque.front();
115
117
118 jvEvent = env;
119 jvEvent["seq"] = seq;
120
121 bSend = true;
122 }
123 }
124
125 // Send outside of the lock.
126 if (bSend)
127 {
128 // XXX Might not need this in a try.
129 try
130 {
131 JLOG(j_.info()) << "RPCCall::fromNetwork: " << mIp;
132
134 m_io_context, mIp, mPort, mUsername, mPassword, mPath, "event", jvEvent, mSSL, true, logs_);
135 }
136 catch (std::exception const& e)
137 {
138 JLOG(j_.info()) << "RPCCall::fromNetwork exception: " << e.what();
139 }
140 }
141 } while (bSend);
142 }
143
144private:
145 boost::asio::io_context& m_io_context;
147
151 bool mSSL;
155
156 int mSeq; // Next id to allocate.
157
158 bool mSending; // Sending thread is active.
159
161
164};
165
166//------------------------------------------------------------------------------
167
169{
170}
171
174 InfoSub::Source& source,
175 boost::asio::io_context& io_context,
176 JobQueue& jobQueue,
177 std::string const& strUrl,
178 std::string const& strUsername,
179 std::string const& strPassword,
180 Logs& logs)
181{
183 std::ref(source), std::ref(io_context), std::ref(jobQueue), strUrl, strUsername, strPassword, logs);
184}
185
186} // namespace xrpl
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:301
Stream info() const
Definition Journal.h:307
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:205
A pool of threads to perform work.
Definition JobQueue.h:38
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition JobQueue.h:146
Manages partitions for logging.
Definition Log.h:33
beast::Journal const j_
Definition RPCSub.cpp:162
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
void send(Json::Value const &jvObj, bool broadcast) override
Definition RPCSub.cpp:58
std::string mIp
Definition RPCSub.cpp:149
std::string mUsername
Definition RPCSub.cpp:152
std::deque< std::pair< int, Json::Value > > mDeque
Definition RPCSub.cpp:160
std::string mPath
Definition RPCSub.cpp:154
void setUsername(std::string const &strUsername) override
Definition RPCSub.cpp:77
void setPassword(std::string const &strPassword) override
Definition RPCSub.cpp:85
boost::asio::io_context & m_io_context
Definition RPCSub.cpp:145
std::string mUrl
Definition RPCSub.cpp:148
std::string mPassword
Definition RPCSub.cpp:153
JobQueue & m_jobQueue
Definition RPCSub.cpp:146
~RPCSubImp()=default
void sendThread()
Definition RPCSub.cpp:96
std::uint16_t mPort
Definition RPCSub.cpp:150
Subscription object for JSON RPC.
Definition RPCSub.h:14
RPCSub(InfoSub::Source &source)
Definition RPCSub.cpp:168
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:1590
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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:173
@ jtCLIENT_SUBSCRIBE
Definition Job.h:26
bool parseUrl(parsedURL &pUrl, std::string const &strUrl)
T pop_front(T... args)
T push_back(T... args)
T ref(T... args)
std::optional< std::uint16_t > port
T what(T... args)