rippled
ServerImpl.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_SERVER_SERVERIMPL_H_INCLUDED
21 #define RIPPLE_SERVER_SERVERIMPL_H_INCLUDED
22 
23 #include <ripple/basics/chrono.h>
24 #include <ripple/beast/core/List.h>
25 #include <ripple/server/Server.h>
26 #include <ripple/server/impl/Door.h>
27 #include <ripple/server/impl/io_list.h>
28 #include <boost/asio.hpp>
29 #include <array>
30 #include <chrono>
31 #include <mutex>
32 #include <optional>
33 
34 namespace ripple {
35 
37 
44 class Server
45 {
46 public:
51  virtual ~Server() = default;
52 
54  virtual beast::Journal
55  journal() = 0;
56 
60  virtual Endpoints
61  ports(std::vector<Port> const& v) = 0;
62 
71  virtual void
72  close() = 0;
73 };
74 
75 template <class Handler>
76 class ServerImpl : public Server
77 {
78 private:
80 
81  enum { historySize = 100 };
82 
83  Handler& handler_;
85  boost::asio::io_service& io_service_;
86  boost::asio::io_service::strand strand_;
88 
92  int high_ = 0;
94 
96 
97 public:
98  ServerImpl(
99  Handler& handler,
100  boost::asio::io_service& io_service,
102 
103  ~ServerImpl();
104 
106  journal() override
107  {
108  return j_;
109  }
110 
111  Endpoints
112  ports(std::vector<Port> const& ports) override;
113 
114  void
115  close() override;
116 
117  io_list&
118  ios()
119  {
120  return ios_;
121  }
122 
123  boost::asio::io_service&
125  {
126  return io_service_;
127  }
128 
129  bool
130  closed();
131 
132 private:
133  static int
134  ceil_log2(unsigned long long x);
135 };
136 
137 template <class Handler>
139  Handler& handler,
140  boost::asio::io_service& io_service,
141  beast::Journal journal)
142  : handler_(handler)
143  , j_(journal)
144  , io_service_(io_service)
145  , strand_(io_service_)
146  , work_(io_service_)
147 {
148 }
149 
150 template <class Handler>
152 {
153  // Handler::onStopped will not be called
154  work_ = std::nullopt;
155  ios_.close();
156  ios_.join();
157 }
158 
159 template <class Handler>
160 Endpoints
162 {
163  if (closed())
164  Throw<std::logic_error>("ports() on closed Server");
165  ports_.reserve(ports.size());
166  Endpoints eps;
167  eps.reserve(ports.size());
168  for (auto const& port : ports)
169  {
170  ports_.push_back(port);
171  if (auto sp = ios_.emplace<Door<Handler>>(
172  handler_, io_service_, ports_.back(), j_))
173  {
174  list_.push_back(sp);
175  eps.push_back(sp->get_endpoint());
176  sp->run();
177  }
178  }
179  return eps;
180 }
181 
182 template <class Handler>
183 void
185 {
186  ios_.close([&] {
187  work_ = std::nullopt;
188  handler_.onStopped(*this);
189  });
190 }
191 
192 template <class Handler>
193 bool
195 {
196  return ios_.closed();
197 }
198 } // namespace ripple
199 
200 #endif
std::chrono::system_clock
std::vector::reserve
T reserve(T... args)
std::vector
STL class.
std::vector::size
T size(T... args)
ripple::ServerImpl::strand_
boost::asio::io_service::strand strand_
Definition: ServerImpl.h:86
ripple::Server::~Server
virtual ~Server()=default
Destroy the server.
ripple::ServerImpl::ceil_log2
static int ceil_log2(unsigned long long x)
ripple::Door
A listening socket.
Definition: Door.h:47
ripple::ServerImpl::io_service_
boost::asio::io_service & io_service_
Definition: ServerImpl.h:85
ripple::ServerImpl
Definition: ServerImpl.h:76
ripple::ServerImpl::get_io_service
boost::asio::io_service & get_io_service()
Definition: ServerImpl.h:124
ripple::ServerImpl::handler_
Handler & handler_
Definition: ServerImpl.h:83
std::vector::push_back
T push_back(T... args)
ripple::ServerImpl::ServerImpl
ServerImpl(Handler &handler, boost::asio::io_service &io_service, beast::Journal journal)
Definition: ServerImpl.h:138
ripple::ServerImpl::close
void close() override
Close the server.
Definition: ServerImpl.h:184
ripple::ServerImpl::hist_
std::array< std::size_t, 64 > hist_
Definition: ServerImpl.h:93
chrono
ripple::Server::close
virtual void close()=0
Close the server.
array
ripple::Server
A multi-protocol server.
Definition: ServerImpl.h:44
ripple::ServerImpl::ports_
std::vector< Port > ports_
Definition: ServerImpl.h:90
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::ServerImpl::work_
std::optional< boost::asio::io_service::work > work_
Definition: ServerImpl.h:87
ripple::ServerImpl::closed
bool closed()
Definition: ServerImpl.h:194
ripple::ServerImpl::ports
Endpoints ports(std::vector< Port > const &ports) override
Set the listening port settings.
Definition: ServerImpl.h:161
ripple::ServerImpl::j_
const beast::Journal j_
Definition: ServerImpl.h:84
ripple::Server::journal
virtual beast::Journal journal()=0
Returns the Journal associated with the server.
ripple::ServerImpl::journal
beast::Journal journal() override
Returns the Journal associated with the server.
Definition: ServerImpl.h:106
ripple::ServerImpl::list_
std::vector< std::weak_ptr< Door< Handler > > > list_
Definition: ServerImpl.h:91
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::ServerImpl::ios
io_list & ios()
Definition: ServerImpl.h:118
ripple::Server::ports
virtual Endpoints ports(std::vector< Port > const &v)=0
Set the listening port settings.
ripple::ServerImpl::historySize
@ historySize
Definition: ServerImpl.h:81
optional
mutex
ripple::ServerImpl::high_
int high_
Definition: ServerImpl.h:92
ripple::ServerImpl::m_
std::mutex m_
Definition: ServerImpl.h:89
ripple::io_list
Manages a set of objects performing asynchronous I/O.
Definition: io_list.h:35
ripple::ServerImpl::~ServerImpl
~ServerImpl()
Definition: ServerImpl.h:151
ripple::ServerImpl::ios_
io_list ios_
Definition: ServerImpl.h:95