$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 #include <cloe/cloe_fwd.hpp> // for Handler
30 #include <fable/fable_fwd.hpp> // for Json
31 #include <oatpp/network/Server.hpp> // for Server
32 
33 #include "oak/registrar.hpp" // for StaticRegistrar, BufferRegistrar
34 
35 namespace oak {
36 
37 class GreedyHandler;
38 
44 class Server {
45  public:
46  Server(const std::string& addr, int port);
47  Server();
48 
53  ~Server();
54 
62  void set_threads(int n) { listen_threads_ = n; }
63 
72  void set_address(const std::string& addr) { listen_addr_ = addr; }
73 
74  [[nodiscard]] const std::string& address() const { return listen_addr_; }
75 
79  void set_port(int port) { listen_port_ = port; }
80 
81  [[nodiscard]] int port() const { return listen_port_; }
82 
86  [[nodiscard]] bool is_listening() const { return listening_; }
87 
91  void listen();
92 
96  [[nodiscard]] fable::Json endpoints_to_json(const std::vector<std::string>& endpoints) const;
97 
101  void stop();
102 
106  [[nodiscard]] std::vector<std::string> endpoints() const;
107 
108  protected:
109  friend StaticRegistrar;
110  friend LockedRegistrar;
111  friend BufferRegistrar;
112 
116  void add_handler(const std::string& key, cloe::Handler h);
117 
118  private:
119  // Configuration
120  std::string listen_addr_;
121  int listen_port_;
122  int listen_threads_;
123 
124  // State
125  bool listening_;
126  std::shared_ptr<oatpp::network::Server> server_;
127  std::shared_ptr<GreedyHandler> handler_;
128 };
129 
130 } // namespace oak
Definition: registrar.hpp:217
Definition: registrar.hpp:180
Definition: server.hpp:44
bool is_listening() const
Definition: server.hpp:86
void listen()
Definition: server.cpp:182
void set_threads(int n)
Definition: server.hpp:62
void stop()
Definition: server.cpp:204
void add_handler(const std::string &key, cloe::Handler h)
Definition: server.cpp:216
void set_port(int port)
Definition: server.hpp:79
void set_address(const std::string &addr)
Definition: server.hpp:72
~Server()
Definition: server.cpp:238
fable::Json endpoints_to_json(const std::vector< std::string > &endpoints) const
Definition: server.cpp:220
std::vector< std::string > endpoints() const
Definition: server.cpp:218
Definition: registrar.hpp:137
std::function< void(const Request &, Response &)> Handler
Definition: cloe_fwd.hpp:65
nlohmann::json Json
Definition: fable_fwd.hpp:35