20 #include <ripple/json/json_reader.h>
21 #include <ripple/json/to_string.h>
22 #include <ripple/protocol/jss.h>
23 #include <ripple/server/Port.h>
24 #include <boost/beast/core/multi_buffer.hpp>
25 #include <boost/beast/websocket.hpp>
27 #include <test/jtx/WSClient.h>
35 #include <ripple/beast/unit_test.h>
53 static boost::asio::ip::tcp::endpoint
59 auto const ps = v2 ?
"ws2" :
"ws";
68 using namespace boost::asio::ip;
69 if (pp.
ip && pp.
ip->is_unspecified())
70 *pp.
ip = pp.
ip->is_v6() ? address{address_v6::loopback()}
71 : address{address_v4::loopback()};
74 Throw<std::runtime_error>(
"Missing WebSocket port");
78 template <
class ConstBuffers>
82 using boost::asio::buffer;
83 using boost::asio::buffer_size;
86 buffer_copy(buffer(&s[0], s.
size()), b);
90 boost::asio::io_service
ios_;
95 boost::beast::websocket::stream<boost::asio::ip::tcp::socket&>
ws_;
96 boost::beast::multi_buffer
rb_;
118 ws_.async_close({}, strand_.wrap([&](error_code ec) {
123 work_ = std::nullopt;
131 unsigned rpc_version,
144 ws_.set_option(boost::beast::websocket::stream_base::decorator(
145 [&](boost::beast::websocket::request_type& req) {
146 for (
auto const& h : headers)
147 req.set(h.first, h.second);
172 using boost::asio::buffer;
173 using namespace std::chrono_literals;
181 jp[jss::method] = cmd;
182 jp[jss::jsonrpc] =
"2.0";
183 jp[jss::ripplerpc] =
"2.0";
187 jp[jss::command] = cmd;
189 ws_.write_some(
true, buffer(s));
193 return jval[jss::type] == jss::response;
198 jv->removeMember(jss::type);
199 if ((*jv).isMember(jss::status) && (*jv)[jss::status] == jss::error)
202 ret[jss::result] = *jv;
203 if ((*jv).isMember(jss::error))
204 ret[jss::error] = (*jv)[jss::error];
205 ret[jss::status] = jss::error;
208 if ((*jv).isMember(jss::status) && (*jv).isMember(jss::result))
209 (*jv)[jss::result][jss::status] = (*jv)[jss::status];
221 if (!
cv_.
wait_for(lock, timeout, [&] { return !msgs_.empty(); }))
223 m = std::move(
msgs_.back());
226 return std::move(m->
jv);
238 for (auto it = msgs_.begin(); it != msgs_.end(); ++it)
253 return std::move(m->jv);
268 if (ec == boost::beast::websocket::error::closed)
277 auto m = std::make_shared<msg>(std::move(jv));
303 unsigned rpc_version,
306 return std::make_unique<WSClientImpl>(cfg, v2, rpc_version, headers);
unsigned version() const override
Get RPC 1.0 or RPC 2.0.
std::condition_variable cv_
boost::asio::io_service ios_
std::optional< Json::Value > getMsg(std::chrono::milliseconds const &timeout) override
Retrieve a message.
boost::system::error_code error_code
void on_read_msg(error_code const &ec)
boost::asio::ip::tcp::socket stream_
Json::Value invoke(std::string const &cmd, Json::Value const ¶ms) override
Submit a command synchronously.
void parse_Port(ParsedPort &port, Section const §ion, std::ostream &log)
Unserialize a JSON document into a Value.
static std::string buffer_string(ConstBuffers const &b)
boost::beast::websocket::stream< boost::asio::ip::tcp::socket & > ws_
std::vector< std::string > const & values() const
Returns all the values in the section.
void Rethrow()
Rethrow the exception currently being handled.
std::list< std::shared_ptr< msg > > msgs_
std::optional< boost::asio::io_service::work > work_
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::optional< boost::asio::ip::address > ip
std::optional< std::uint16_t > port
bool parse(std::string const &document, Value &root)
Read a Value from a JSON document.
std::unique_ptr< WSClient > makeWSClient(Config const &cfg, bool v2, unsigned rpc_version, std::unordered_map< std::string, std::string > const &headers)
Returns a client operating through WebSockets/S.
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
boost::beast::multi_buffer rb_
std::set< std::string, boost::beast::iless > protocol
std::condition_variable cv0_
boost::asio::io_service::strand strand_
WSClientImpl(Config const &cfg, bool v2, unsigned rpc_version, std::unordered_map< std::string, std::string > const &headers={})
Holds unparsed configuration information.
std::optional< Json::Value > findMsg(std::chrono::milliseconds const &timeout, std::function< bool(Json::Value const &)> pred) override
Retrieve a message that meets the predicate criteria.
static boost::asio::ip::tcp::endpoint getEndpoint(BasicConfig const &cfg, bool v2)
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
Section & section(std::string const &name)
Returns the section with the given name.