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