|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | + */ |
| 18 | +/** |
| 19 | + * \file server_mock.cpp |
| 20 | + * \see server.hpp |
| 21 | + * \see server.cpp |
| 22 | + * |
| 23 | + * This file is included in the build when the server component is disabled. |
| 24 | + * This allows us to decouple the engine from the oak dependency with all |
| 25 | + * it entails in one place. |
| 26 | + * |
| 27 | + * We still need to implement the interfaces defined in server.hpp though. |
| 28 | + * |
| 29 | + * Having a separate file makes it easier to diff the two implmentation files. |
| 30 | + */ |
| 31 | + |
| 32 | +#include "server.hpp" |
| 33 | + |
| 34 | +#include <memory> // for unique_ptr<>, make_unique |
| 35 | + |
| 36 | +#include <cloe/registrar.hpp> // for HandlerType |
| 37 | + |
| 38 | +namespace engine { |
| 39 | + |
| 40 | +class ServerRegistrarImpl : public ServerRegistrar { |
| 41 | + public: |
| 42 | + ServerRegistrarImpl(const std::string& static_prefix, const std::string& api_prefix) |
| 43 | + : static_prefix_(static_prefix), api_prefix_(api_prefix) {} |
| 44 | + |
| 45 | + std::unique_ptr<ServerRegistrar> clone() const override { |
| 46 | + return std::make_unique<ServerRegistrarImpl>(static_prefix_, api_prefix_); |
| 47 | + } |
| 48 | + |
| 49 | + std::unique_ptr<ServerRegistrar> with_prefix(const std::string& static_prefix, |
| 50 | + const std::string& api_prefix) const override { |
| 51 | + assert(static_prefix.empty() || static_prefix[0] == '/'); |
| 52 | + assert(api_prefix.empty() || api_prefix[0] == '/'); |
| 53 | + return std::make_unique<ServerRegistrarImpl>(static_prefix_ + static_prefix, |
| 54 | + api_prefix_ + api_prefix); |
| 55 | + } |
| 56 | + |
| 57 | + void register_static_handler(const std::string& endpoint, cloe::Handler) override { |
| 58 | + logger()->warn("Unregistered static endpoint: {}", endpoint); |
| 59 | + } |
| 60 | + |
| 61 | + void register_api_handler(const std::string& endpoint, cloe::HandlerType t, |
| 62 | + cloe::Handler) override { |
| 63 | + switch (t) { |
| 64 | + case cloe::HandlerType::STATIC: |
| 65 | + logger()->warn("Unregistered static endpoint: {}", endpoint); |
| 66 | + break; |
| 67 | + case cloe::HandlerType::DYNAMIC: |
| 68 | + logger()->warn("Unregistered dynamic endpoint: {}", endpoint); |
| 69 | + break; |
| 70 | + case cloe::HandlerType::BUFFERED: |
| 71 | + logger()->warn("Unregistered buffered endpoint: {}", endpoint); |
| 72 | + break; |
| 73 | + default: |
| 74 | + throw std::runtime_error("unknown handler type"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + cloe::Logger logger() const { |
| 79 | + return cloe::logger::get("cloe"); |
| 80 | + } |
| 81 | + |
| 82 | + private: |
| 83 | + std::string static_prefix_{"/"}; |
| 84 | + std::string api_prefix_{"/"}; |
| 85 | +}; |
| 86 | + |
| 87 | +class ServerImpl : public Server { |
| 88 | + public: |
| 89 | + ServerImpl(const cloe::ServerConf& config) : Server(config) { |
| 90 | + if (config.listen) { |
| 91 | + logger()->error("Server unavailable, but configuration value /server/listen = true"); |
| 92 | + logger()->error("Server unavailable, feature is not compiled into engine."); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + ~ServerImpl() {} |
| 97 | + |
| 98 | + public: |
| 99 | + bool is_listening() const override { return false; } |
| 100 | + |
| 101 | + bool is_streaming() const override { return false; } |
| 102 | + |
| 103 | + void start() override { |
| 104 | + logger()->error("Server unavailable, cannot start."); |
| 105 | + } |
| 106 | + |
| 107 | + void init_stream(const std::string& ) override { |
| 108 | + logger()->error("Server unavailable, cannot initialize stream."); |
| 109 | + } |
| 110 | + |
| 111 | + void stop() override { |
| 112 | + } |
| 113 | + |
| 114 | + void enroll(cloe::Registrar& ) override { |
| 115 | + } |
| 116 | + |
| 117 | + std::unique_ptr<ServerRegistrar> server_registrar() override { |
| 118 | + return server_registrar_.clone(); |
| 119 | + } |
| 120 | + |
| 121 | + void refresh_buffer_start_stream() override { |
| 122 | + } |
| 123 | + |
| 124 | + void refresh_buffer() override { |
| 125 | + } |
| 126 | + |
| 127 | + Defer lock() override { |
| 128 | + return Defer([]() {}); |
| 129 | + } |
| 130 | + |
| 131 | + private: |
| 132 | + ServerRegistrarImpl server_registrar_{ "", "" }; |
| 133 | +}; |
| 134 | + |
| 135 | +std::unique_ptr<Server> make_server(const cloe::ServerConf& c) { |
| 136 | + return std::make_unique<ServerImpl>(c); |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace engine |
0 commit comments