rippled
ServerHandler.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_RPC_SERVERHANDLER_H_INCLUDED
21 #define RIPPLE_RPC_SERVERHANDLER_H_INCLUDED
22 
23 #include <ripple/app/main/CollectorManager.h>
24 #include <ripple/core/JobQueue.h>
25 #include <ripple/json/Output.h>
26 #include <ripple/rpc/RPCHandler.h>
27 #include <ripple/rpc/impl/WSInfoSub.h>
28 #include <ripple/server/Server.h>
29 #include <ripple/server/Session.h>
30 #include <ripple/server/WSSession.h>
31 #include <boost/beast/core/tcp_stream.hpp>
32 #include <boost/beast/ssl/ssl_stream.hpp>
33 #include <boost/utility/string_view.hpp>
34 #include <condition_variable>
35 #include <map>
36 #include <mutex>
37 #include <vector>
38 
39 namespace ripple {
40 
41 inline bool
42 operator<(Port const& lhs, Port const& rhs)
43 {
44  return lhs.name < rhs.name;
45 }
46 
48 {
49 public:
50  struct Setup
51  {
52  explicit Setup() = default;
53 
55 
56  // Memberspace
57  struct client_t
58  {
59  explicit client_t() = default;
60 
61  bool secure = false;
68  };
69 
70  // Configuration when acting in client role
72 
73  // Configuration for the Overlay
74  struct overlay_t
75  {
76  explicit overlay_t() = default;
77 
78  boost::asio::ip::address ip;
80  };
81 
83 
84  void
85  makeContexts();
86  };
87 
88 private:
89  using socket_type = boost::beast::tcp_stream;
90  using stream_type = boost::beast::ssl_stream<socket_type>;
91 
104  bool stopped_{false};
106 
107  // A private type used to restrict access to the ServerHandler constructor.
109  {
110  explicit ServerHandlerCreator() = default;
111  };
112 
113  // Friend declaration that allows make_ServerHandler to access the
114  // private type that restricts access to the ServerHandler ctor.
117  Application& app,
118  boost::asio::io_service&,
119  JobQueue&,
120  NetworkOPs&,
122  CollectorManager& cm);
123 
124 public:
125  // Must be public so make_unique can call it.
127  ServerHandlerCreator const&,
128  Application& app,
129  boost::asio::io_service& io_service,
130  JobQueue& jobQueue,
131  NetworkOPs& networkOPs,
132  Resource::Manager& resourceManager,
133  CollectorManager& cm);
134 
135  ~ServerHandler();
136 
138 
139  void
140  setup(Setup const& setup, beast::Journal journal);
141 
142  Setup const&
143  setup() const
144  {
145  return setup_;
146  }
147 
148  void
149  stop();
150 
151  //
152  // Handler
153  //
154 
155  bool
156  onAccept(Session& session, boost::asio::ip::tcp::endpoint endpoint);
157 
158  Handoff
159  onHandoff(
160  Session& session,
162  http_request_type&& request,
163  boost::asio::ip::tcp::endpoint const& remote_address);
164 
165  Handoff
167  Session& session,
168  http_request_type&& request,
169  boost::asio::ip::tcp::endpoint const& remote_address)
170  {
171  return onHandoff(
172  session,
173  {},
174  std::forward<http_request_type>(request),
175  remote_address);
176  }
177 
178  void
179  onRequest(Session& session);
180 
181  void
182  onWSMessage(
185 
186  void
187  onClose(Session& session, boost::system::error_code const&);
188 
189  void
190  onStopped(Server&);
191 
192 private:
195  std::shared_ptr<WSSession> const& session,
197  Json::Value const& jv);
198 
199  void
203 
204  void
206  Port const& port,
207  std::string const& request,
208  beast::IP::Endpoint const& remoteIPAddress,
209  Output&&,
211  boost::string_view forwardedFor,
212  boost::string_view user);
213 
214  Handoff
215  statusResponse(http_request_type const& request) const;
216 };
217 
218 ServerHandler::Setup
219 setup_ServerHandler(Config const& c, std::ostream&& log);
220 
223  Application& app,
224  boost::asio::io_service&,
225  JobQueue&,
226  NetworkOPs&,
227  Resource::Manager&,
228  CollectorManager& cm);
229 
230 } // namespace ripple
231 
232 #endif
ripple::NetworkOPs
Provides server functionality for clients.
Definition: NetworkOPs.h:90
ripple::ServerHandler::stream_type
boost::beast::ssl_stream< socket_type > stream_type
Definition: ServerHandler.h:90
ripple::Application
Definition: Application.h:116
ripple::ServerHandler::Setup::client_t::client_t
client_t()=default
ripple::ServerHandler::onStopped
void onStopped(Server &)
Definition: ServerHandler.cpp:373
ripple::ServerHandler::socket_type
boost::beast::tcp_stream socket_type
Definition: ServerHandler.h:89
ripple::ServerHandler::m_jobQueue
JobQueue & m_jobQueue
Definition: ServerHandler.h:98
ripple::ServerHandler::Setup
Definition: ServerHandler.h:50
std::string
STL class.
std::shared_ptr
STL class.
ripple::ServerHandler::statusResponse
Handoff statusResponse(http_request_type const &request) const
Definition: ServerHandler.cpp:1026
beast::insight::Counter
A metric for measuring an integral value.
Definition: Counter.h:38
ripple::ServerHandler::ServerHandlerCreator
Definition: ServerHandler.h:108
ripple::ServerHandler::onHandoff
Handoff onHandoff(Session &session, std::unique_ptr< stream_type > &&bundle, http_request_type &&request, boost::asio::ip::tcp::endpoint const &remote_address)
Definition: ServerHandler.cpp:174
ripple::ServerHandler::Setup::client_t::user
std::string user
Definition: ServerHandler.h:64
ripple::ServerHandler::Setup::client_t::admin_password
std::string admin_password
Definition: ServerHandler.h:67
ripple::ServerHandler::Output
Json::Output Output
Definition: ServerHandler.h:137
ripple::ServerHandler::setup_
Setup setup_
Definition: ServerHandler.h:97
vector
ripple::CollectorManager
Provides the beast::insight::Collector service.
Definition: CollectorManager.h:29
ripple::ServerHandler::count_
std::map< std::reference_wrapper< Port const >, int > count_
Definition: ServerHandler.h:105
std::function
ripple::ServerHandler::m_resourceManager
Resource::Manager & m_resourceManager
Definition: ServerHandler.h:93
ripple::Port::name
std::string name
Definition: Port.h:52
ripple::ServerHandler::ServerHandler
ServerHandler(ServerHandlerCreator const &, Application &app, boost::asio::io_service &io_service, JobQueue &jobQueue, NetworkOPs &networkOPs, Resource::Manager &resourceManager, CollectorManager &cm)
Definition: ServerHandler.cpp:104
ripple::Session
Persistent state information for a connection session.
Definition: Session.h:40
ripple::ServerHandler::Setup::client_t::ip
std::string ip
Definition: ServerHandler.h:62
ripple::ServerHandler::processRequest
void processRequest(Port const &port, std::string const &request, beast::IP::Endpoint const &remoteIPAddress, Output &&, std::shared_ptr< JobQueue::Coro > coro, boost::string_view forwardedFor, boost::string_view user)
Definition: ServerHandler.cpp:590
ripple::ServerHandler::onAccept
bool onAccept(Session &session, boost::asio::ip::tcp::endpoint endpoint)
Definition: ServerHandler.cpp:152
ripple::ServerHandler::Setup::client_t
Definition: ServerHandler.h:57
ripple::ServerHandler::processSession
Json::Value processSession(std::shared_ptr< WSSession > const &session, std::shared_ptr< JobQueue::Coro > const &coro, Json::Value const &jv)
Definition: ServerHandler.cpp:402
ripple::setup_ServerHandler
ServerHandler::Setup setup_ServerHandler(Config const &config, std::ostream &&log)
Definition: ServerHandler.cpp:1236
ripple::ServerHandler::onHandoff
Handoff onHandoff(Session &session, http_request_type &&request, boost::asio::ip::tcp::endpoint const &remote_address)
Definition: ServerHandler.h:166
ripple::ServerHandler::onClose
void onClose(Session &session, boost::system::error_code const &)
Definition: ServerHandler.cpp:366
ripple::ServerHandler::Setup::client_t::admin_user
std::string admin_user
Definition: ServerHandler.h:66
ripple::ServerHandler::Setup::client
client_t client
Definition: ServerHandler.h:71
ripple::make_ServerHandler
std::unique_ptr< ServerHandler > make_ServerHandler(Application &app, boost::asio::io_service &io_service, JobQueue &jobQueue, NetworkOPs &networkOPs, Resource::Manager &resourceManager, CollectorManager &cm)
Definition: ServerHandler.cpp:1248
ripple::ServerHandler
Definition: ServerHandler.h:47
ripple::operator<
bool operator<(CanonicalTXSet::Key const &lhs, CanonicalTXSet::Key const &rhs)
Definition: CanonicalTXSet.cpp:25
std::ostream
STL class.
ripple::ServerHandler::rpc_requests_
beast::insight::Counter rpc_requests_
Definition: ServerHandler.h:99
ripple::ServerHandler::rpc_size_
beast::insight::Event rpc_size_
Definition: ServerHandler.h:100
ripple::ServerHandler::stopped_
bool stopped_
Definition: ServerHandler.h:104
ripple::ServerHandler::app_
Application & app_
Definition: ServerHandler.h:92
ripple::ServerHandler::Setup::client_t::password
std::string password
Definition: ServerHandler.h:65
ripple::ServerHandler::Setup::client_t::port
std::uint16_t port
Definition: ServerHandler.h:63
ripple::forwardedFor
boost::string_view forwardedFor(http_request_type const &request)
Definition: Role.cpp:261
ripple::Server
A multi-protocol server.
Definition: ServerImpl.h:44
beast::insight::Event
A metric for reporting event timing.
Definition: Event.h:40
ripple::ServerHandler::setup
Setup const & setup() const
Definition: ServerHandler.h:143
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint16_t
ripple::Port
Configuration information for a Server listening port.
Definition: Port.h:48
map
ripple::ServerHandler::Setup::overlay_t
Definition: ServerHandler.h:74
ripple::ServerHandler::Setup::ports
std::vector< Port > ports
Definition: ServerHandler.h:54
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::ServerHandler::Setup::makeContexts
void makeContexts()
Definition: ServerHandler.cpp:1058
ripple::Resource::Manager
Tracks load and resource consumption.
Definition: ResourceManager.h:36
ripple::ServerHandler::make_ServerHandler
friend std::unique_ptr< ServerHandler > make_ServerHandler(Application &app, boost::asio::io_service &, JobQueue &, NetworkOPs &, Resource::Manager &, CollectorManager &cm)
Definition: ServerHandler.cpp:1248
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ServerHandler::Setup::overlay_t::overlay_t
overlay_t()=default
ripple::ServerHandler::m_networkOPs
NetworkOPs & m_networkOPs
Definition: ServerHandler.h:95
ripple::ServerHandler::condition_
std::condition_variable condition_
Definition: ServerHandler.h:103
ripple::ServerHandler::Setup::Setup
Setup()=default
ripple::ServerHandler::Setup::client_t::secure
bool secure
Definition: ServerHandler.h:61
ripple::ServerHandler::ServerHandlerCreator::ServerHandlerCreator
ServerHandlerCreator()=default
ripple::ServerHandler::m_journal
beast::Journal m_journal
Definition: ServerHandler.h:94
condition_variable
ripple::ServerHandler::onRequest
void onRequest(Session &session)
Definition: ServerHandler.cpp:276
ripple::Handoff
Used to indicate the result of a server connection handoff.
Definition: Handoff.h:37
ripple::ServerHandler::~ServerHandler
~ServerHandler()
Definition: ServerHandler.cpp:125
ripple::ServerHandler::stop
void stop()
Definition: ServerHandler.cpp:140
mutex
ripple::ServerHandler::rpc_time_
beast::insight::Event rpc_time_
Definition: ServerHandler.h:101
ripple::ServerHandler::mutex_
std::mutex mutex_
Definition: ServerHandler.h:102
ripple::ServerHandler::Setup::overlay
overlay_t overlay
Definition: ServerHandler.h:82
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::ServerHandler::Setup::overlay_t::port
std::uint16_t port
Definition: ServerHandler.h:79
ripple::ServerHandler::Setup::overlay_t::ip
boost::asio::ip::address ip
Definition: ServerHandler.h:78
ripple::ServerHandler::onWSMessage
void onWSMessage(std::shared_ptr< WSSession > session, std::vector< boost::asio::const_buffer > const &buffers)
Definition: ServerHandler.cpp:316
ripple::http_request_type
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handshake.h:47
std::unique_ptr
STL class.
ripple::ServerHandler::m_server
std::unique_ptr< Server > m_server
Definition: ServerHandler.h:96
Json::Output
std::function< void(boost::beast::string_view const &)> Output
Definition: json/Output.h:30
Json::Value
Represents a JSON value.
Definition: json_value.h:145