$darkmode
server.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
23 #pragma once
24 
25 #include <memory> // for unique_ptr<>
26 #include <string> // for string
27 #include <vector> // for vector<>
28 
29 // Requires: cppnetlib
30 #include <boost/network/include/http/server.hpp>
31 #include <boost/network/utils/thread_pool.hpp>
32 
33 #include "oak/registrar.hpp" // for StaticRegistrar, BufferRegistrar
34 #include "oak/route_muxer.hpp" // for Muxer<>
35 
36 namespace oak {
37 
38 // These forward-declarations are a necessary evil:
39 class ServerImplHandler;
40 using ServerImpl = boost::network::http::server<ServerImplHandler>;
41 
42 class RequestStub : public cloe::Request {
43  public:
44  cloe::RequestMethod method() const override { throw ERROR; }
45  cloe::ContentType type() const override { throw ERROR; }
46  const std::string& body() const override { throw ERROR; }
47  const std::string& uri() const override { throw ERROR; }
48  const std::string& endpoint() const override { throw ERROR; }
49  const std::map<std::string, std::string>& query_map() const override { throw ERROR; }
50 
51  private:
52  const std::logic_error ERROR = std::logic_error("using the request in any way is erroneous");
53 };
54 
61  public:
63 
72  void operator()(ServerImpl::request const&, ServerImpl::connection_ptr);
73 
77  void log(const ServerImpl::string_type& msg);
78 
82  void add(const std::string& key, Handler h);
83 
87  std::vector<std::string> endpoints() const { return muxer.routes(); }
88 
92  cloe::Json endpoints_to_json(const std::vector<std::string>& endpoints) const;
93 
94  private:
95  Muxer<Handler> muxer;
96 };
97 
103 class Server {
104  public:
105  Server(const std::string& addr, int port)
106  : listen_addr_(addr), listen_port_(port), listen_threads_(3), listening_(false) {}
107 
108  Server() : listen_addr_("127.0.0.1"), listen_port_(8080), listen_threads_(3), listening_(false) {}
109 
115  if (this->is_listening()) {
116  this->stop();
117  }
118  }
119 
124  void set_threads(int n) { listen_threads_ = n; }
125 
134  void set_address(const std::string& addr) { listen_addr_ = addr; }
135 
139  void set_port(int port) { listen_port_ = port; }
140 
144  bool is_listening() const { return listening_; }
145 
149  void listen();
150 
154  cloe::Json endpoints_to_json(const std::vector<std::string>& endpoints) const {
155  return handler_.endpoints_to_json(endpoints);
156  };
157 
161  void stop();
162 
166  std::vector<std::string> endpoints() const { return handler_.endpoints(); }
167 
168  protected:
169  friend StaticRegistrar;
170  friend LockedRegistrar;
171  friend BufferRegistrar;
172 
176  void add_handler(const std::string& key, Handler h) { handler_.add(key, h); }
177 
178  private:
179  // Configuration
180  std::string listen_addr_;
181  int listen_port_;
182  int listen_threads_;
183 
184  // State
185  bool listening_;
186  ServerImplHandler handler_;
187  std::unique_ptr<ServerImpl::options> options_;
188  std::unique_ptr<ServerImpl> server_;
189  std::unique_ptr<boost::thread> thread_;
190 };
191 
192 } // namespace oak
void set_threads(int n)
Definition: server.hpp:124
RequestMethod
Definition: handler.hpp:55
Definition: registrar.hpp:223
const std::map< std::string, std::string > & query_map() const override
Definition: server.hpp:49
Definition: server.hpp:60
std::vector< std::string > endpoints() const
Definition: server.hpp:166
Definition: server.hpp:42
Definition: handler.hpp:95
cloe::RequestMethod method() const override
Definition: server.hpp:44
std::vector< std::string > endpoints() const
Definition: server.hpp:87
ContentType
Definition: handler.hpp:72
void add_handler(const std::string &key, Handler h)
Definition: server.hpp:176
void set_address(const std::string &addr)
Definition: server.hpp:134
Definition: registrar.hpp:38
const std::string & endpoint() const override
Definition: server.hpp:48
const std::string & uri() const override
Definition: server.hpp:47
Definition: registrar.hpp:186
Definition: registrar.hpp:143
cloe::ContentType type() const override
Definition: server.hpp:45
cloe::Json endpoints_to_json(const std::vector< std::string > &endpoints) const
Definition: server.hpp:154
bool is_listening() const
Definition: server.hpp:144
const std::string & body() const override
Definition: server.hpp:46
void set_port(int port)
Definition: server.hpp:139
~Server()
Definition: server.hpp:114
Definition: server.hpp:103