rippled
ServerHandlerImp.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_SERVERHANDLERIMP_H_INCLUDED
21 #define RIPPLE_RPC_SERVERHANDLERIMP_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 public:
109  Application& app,
110  boost::asio::io_service& io_service,
111  JobQueue& jobQueue,
112  NetworkOPs& networkOPs,
113  Resource::Manager& resourceManager,
114  CollectorManager& cm);
115 
117 
119 
120  void
121  setup(Setup const& setup, beast::Journal journal);
122 
123  Setup const&
124  setup() const
125  {
126  return setup_;
127  }
128 
129  void
130  stop();
131 
132  //
133  // Handler
134  //
135 
136  bool
137  onAccept(Session& session, boost::asio::ip::tcp::endpoint endpoint);
138 
139  Handoff
140  onHandoff(
141  Session& session,
143  http_request_type&& request,
144  boost::asio::ip::tcp::endpoint const& remote_address);
145 
146  Handoff
148  Session& session,
149  http_request_type&& request,
150  boost::asio::ip::tcp::endpoint const& remote_address)
151  {
152  return onHandoff(
153  session,
154  {},
155  std::forward<http_request_type>(request),
156  remote_address);
157  }
158 
159  void
160  onRequest(Session& session);
161 
162  void
163  onWSMessage(
166 
167  void
168  onClose(Session& session, boost::system::error_code const&);
169 
170  void
171  onStopped(Server&);
172 
173 private:
176  std::shared_ptr<WSSession> const& session,
178  Json::Value const& jv);
179 
180  void
184 
185  void
187  Port const& port,
188  std::string const& request,
189  beast::IP::Endpoint const& remoteIPAddress,
190  Output&&,
192  boost::string_view forwardedFor,
193  boost::string_view user);
194 
195  Handoff
196  statusResponse(http_request_type const& request) const;
197 };
198 
199 } // namespace ripple
200 
201 #endif
ripple::ServerHandlerImp::stream_type
boost::beast::ssl_stream< socket_type > stream_type
Definition: ServerHandlerImp.h:90
ripple::NetworkOPs
Provides server functionality for clients.
Definition: NetworkOPs.h:86
ripple::Application
Definition: Application.h:115
ripple::ServerHandlerImp::count_
std::map< std::reference_wrapper< Port const >, int > count_
Definition: ServerHandlerImp.h:105
ripple::ServerHandlerImp::Output
Json::Output Output
Definition: ServerHandlerImp.h:118
ripple::ServerHandlerImp::Setup::client_t::user
std::string user
Definition: ServerHandlerImp.h:64
ripple::ServerHandlerImp::m_journal
beast::Journal m_journal
Definition: ServerHandlerImp.h:94
std::string
STL class.
std::shared_ptr
STL class.
ripple::ServerHandlerImp::ServerHandlerImp
ServerHandlerImp(Application &app, boost::asio::io_service &io_service, JobQueue &jobQueue, NetworkOPs &networkOPs, Resource::Manager &resourceManager, CollectorManager &cm)
Definition: ServerHandlerImp.cpp:104
beast::insight::Counter
A metric for measuring an integral value.
Definition: Counter.h:38
ripple::ServerHandlerImp::m_jobQueue
JobQueue & m_jobQueue
Definition: ServerHandlerImp.h:98
ripple::ServerHandlerImp::setup_
Setup setup_
Definition: ServerHandlerImp.h:97
ripple::ServerHandlerImp::Setup::client_t::secure
bool secure
Definition: ServerHandlerImp.h:61
vector
ripple::ServerHandlerImp::Setup::client_t::admin_user
std::string admin_user
Definition: ServerHandlerImp.h:66
ripple::ServerHandlerImp::stop
void stop()
Definition: ServerHandlerImp.cpp:139
ripple::ServerHandlerImp::Setup
Definition: ServerHandlerImp.h:50
ripple::CollectorManager
Provides the beast::insight::Collector service.
Definition: CollectorManager.h:29
ripple::ServerHandlerImp::rpc_requests_
beast::insight::Counter rpc_requests_
Definition: ServerHandlerImp.h:99
ripple::ServerHandlerImp::Setup::client_t::port
std::uint16_t port
Definition: ServerHandlerImp.h:63
std::function
ripple::Port::name
std::string name
Definition: Port.h:52
ripple::Session
Persistent state information for a connection session.
Definition: Session.h:40
ripple::ServerHandlerImp::Setup::overlay_t::ip
boost::asio::ip::address ip
Definition: ServerHandlerImp.h:78
ripple::ServerHandlerImp::onRequest
void onRequest(Session &session)
Definition: ServerHandlerImp.cpp:275
ripple::ServerHandlerImp::Setup::makeContexts
void makeContexts()
Definition: ServerHandlerImp.cpp:1057
ripple::ServerHandlerImp::statusResponse
Handoff statusResponse(http_request_type const &request) const
Definition: ServerHandlerImp.cpp:1025
ripple::ServerHandlerImp::onClose
void onClose(Session &session, boost::system::error_code const &)
Definition: ServerHandlerImp.cpp:365
ripple::ServerHandlerImp::m_networkOPs
NetworkOPs & m_networkOPs
Definition: ServerHandlerImp.h:95
ripple::ServerHandlerImp::Setup::client_t::ip
std::string ip
Definition: ServerHandlerImp.h:62
ripple::ServerHandlerImp::onHandoff
Handoff onHandoff(Session &session, http_request_type &&request, boost::asio::ip::tcp::endpoint const &remote_address)
Definition: ServerHandlerImp.h:147
ripple::ServerHandlerImp::rpc_time_
beast::insight::Event rpc_time_
Definition: ServerHandlerImp.h:101
ripple::ServerHandlerImp::Setup::overlay_t::port
std::uint16_t port
Definition: ServerHandlerImp.h:79
ripple::ServerHandlerImp::mutex_
std::mutex mutex_
Definition: ServerHandlerImp.h:102
ripple::ServerHandlerImp::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: ServerHandlerImp.cpp:589
ripple::ServerHandlerImp::stopped_
bool stopped_
Definition: ServerHandlerImp.h:104
ripple::operator<
bool operator<(CanonicalTXSet::Key const &lhs, CanonicalTXSet::Key const &rhs)
Definition: CanonicalTXSet.cpp:25
ripple::ServerHandlerImp
Definition: ServerHandlerImp.h:47
ripple::ServerHandlerImp::condition_
std::condition_variable condition_
Definition: ServerHandlerImp.h:103
ripple::ServerHandlerImp::processSession
Json::Value processSession(std::shared_ptr< WSSession > const &session, std::shared_ptr< JobQueue::Coro > const &coro, Json::Value const &jv)
Definition: ServerHandlerImp.cpp:401
ripple::ServerHandlerImp::Setup::client_t::client_t
client_t()=default
ripple::ServerHandlerImp::onAccept
bool onAccept(Session &session, boost::asio::ip::tcp::endpoint endpoint)
Definition: ServerHandlerImp.cpp:151
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::ServerHandlerImp::Setup::client
client_t client
Definition: ServerHandlerImp.h:71
ripple::ServerHandlerImp::rpc_size_
beast::insight::Event rpc_size_
Definition: ServerHandlerImp.h:100
ripple::ServerHandlerImp::socket_type
boost::beast::tcp_stream socket_type
Definition: ServerHandlerImp.h:89
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint16_t
ripple::ServerHandlerImp::Setup::overlay_t
Definition: ServerHandlerImp.h:74
ripple::Port
Configuration information for a Server listening port.
Definition: Port.h:48
map
ripple::ServerHandlerImp::app_
Application & app_
Definition: ServerHandlerImp.h:92
ripple::ServerHandlerImp::m_server
std::unique_ptr< Server > m_server
Definition: ServerHandlerImp.h:96
ripple::ServerHandlerImp::m_resourceManager
Resource::Manager & m_resourceManager
Definition: ServerHandlerImp.h:93
ripple::JobQueue
A pool of threads to perform work.
Definition: JobQueue.h:55
ripple::Resource::Manager
Tracks load and resource consumption.
Definition: ResourceManager.h:36
ripple::ServerHandlerImp::Setup::client_t::admin_password
std::string admin_password
Definition: ServerHandlerImp.h:67
ripple::ServerHandlerImp::onWSMessage
void onWSMessage(std::shared_ptr< WSSession > session, std::vector< boost::asio::const_buffer > const &buffers)
Definition: ServerHandlerImp.cpp:315
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ServerHandlerImp::setup
Setup const & setup() const
Definition: ServerHandlerImp.h:124
condition_variable
ripple::ServerHandlerImp::Setup::ports
std::vector< Port > ports
Definition: ServerHandlerImp.h:54
ripple::Handoff
Used to indicate the result of a server connection handoff.
Definition: Handoff.h:37
mutex
ripple::ServerHandlerImp::~ServerHandlerImp
~ServerHandlerImp()
Definition: ServerHandlerImp.cpp:124
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::ServerHandlerImp::Setup::overlay_t::overlay_t
overlay_t()=default
ripple::ServerHandlerImp::Setup::Setup
Setup()=default
ripple::ServerHandlerImp::Setup::client_t
Definition: ServerHandlerImp.h:57
ripple::ServerHandlerImp::Setup::overlay
overlay_t overlay
Definition: ServerHandlerImp.h:82
ripple::ServerHandlerImp::Setup::client_t::password
std::string password
Definition: ServerHandlerImp.h:65
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::ServerHandlerImp::onStopped
void onStopped(Server &)
Definition: ServerHandlerImp.cpp:372
ripple::ServerHandlerImp::onHandoff
Handoff onHandoff(Session &session, std::unique_ptr< stream_type > &&bundle, http_request_type &&request, boost::asio::ip::tcp::endpoint const &remote_address)
Definition: ServerHandlerImp.cpp:173
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