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