diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-06-13 15:30:09 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-06-18 12:05:10 +0200 |
commit | 351bafefc45727d6f45579397d71a542a84807d9 (patch) | |
tree | 07319a514513d86deed4321c7da83064bbd367a6 | |
parent | 9a51bf81a860adb2f53794172d049b614739f14d (diff) | |
download | justbuild-351bafefc45727d6f45579397d71a542a84807d9.tar.gz |
Make ServeServerImpl a general class, not a singleton.
3 files changed, 53 insertions, 84 deletions
diff --git a/src/buildtool/main/main.cpp b/src/buildtool/main/main.cpp index 508d980e..4c8add19 100644 --- a/src/buildtool/main/main.cpp +++ b/src/buildtool/main/main.cpp @@ -283,39 +283,6 @@ void SetupExecutionServiceConfig(ServiceArguments const& args) { } } -void SetupServeServiceConfig(ServiceArguments const& args) { - if (args.port) { - if (!ServeServerImpl::SetPort(*args.port)) { - Logger::Log(LogLevel::Error, "Invalid port '{}'", *args.port); - std::exit(kExitFailure); - } - } - if (args.info_file) { - if (!ServeServerImpl::SetInfoFile(*args.info_file)) { - Logger::Log(LogLevel::Error, - "Invalid info-file '{}'", - args.info_file->string()); - std::exit(kExitFailure); - } - } - if (args.interface) { - if (!ServeServerImpl::SetInterface(*args.interface)) { - Logger::Log(LogLevel::Error, - "Invalid interface '{}'", - args.info_file->string()); - std::exit(kExitFailure); - } - } - if (args.pid_file) { - if (!ServeServerImpl::SetPidFile(*args.pid_file)) { - Logger::Log(LogLevel::Error, - "Invalid pid-file '{}'", - args.info_file->string()); - std::exit(kExitFailure); - } - } -} - void SetupHashFunction() { HashFunction::SetHashType(Compatibility::IsCompatible() ? HashFunction::JustHash::Compatible @@ -865,15 +832,19 @@ auto main(int argc, char* argv[]) -> int { } if (arguments.cmd == SubCommand::kServe) { - SetupServeServiceConfig(arguments.service); - auto serve = ServeApi::Create(*serve_config); - if (!ServeServerImpl::Instance().Run( - *serve_config, - serve, - !RemoteExecutionConfig::RemoteAddress())) { - return kExitFailure; + auto serve_server = + ServeServerImpl::Create(arguments.service.interface, + arguments.service.port, + arguments.service.info_file, + arguments.service.pid_file); + if (serve_server) { + auto serve = ServeApi::Create(*serve_config); + bool with_execute = not RemoteExecutionConfig::RemoteAddress(); + return serve_server->Run(*serve_config, serve, with_execute) + ? kExitSuccess + : kExitFailure; } - return kExitSuccess; + return kExitFailure; } // If no execution endpoint was given, the client should default to the diff --git a/src/buildtool/serve_api/serve_service/serve_server_implementation.cpp b/src/buildtool/serve_api/serve_service/serve_server_implementation.cpp index b588fc45..302e2da7 100644 --- a/src/buildtool/serve_api/serve_service/serve_server_implementation.cpp +++ b/src/buildtool/serve_api/serve_service/serve_server_implementation.cpp @@ -60,6 +60,34 @@ auto TryWrite(std::string const& file, T const& content) noexcept -> bool { } } // namespace +auto ServeServerImpl::Create(std::optional<std::string> interface, + std::optional<int> port, + std::optional<std::string> info_file, + std::optional<std::string> pid_file) noexcept + -> std::optional<ServeServerImpl> { + ServeServerImpl server; + if (interface) { + server.interface_ = std::move(*interface); + } + if (port) { + auto parsed_port = ParsePort(*port); + if (parsed_port) { + server.port_ = static_cast<int>(*parsed_port); + } + else { + Logger::Log(LogLevel::Error, "Invalid port '{}'", *port); + return std::nullopt; + } + } + if (info_file) { + server.info_file_ = std::move(*info_file); + } + if (pid_file) { + server.pid_file_ = std::move(*pid_file); + } + return std::move(server); +} + auto ServeServerImpl::Run(RemoteServeConfig const& serve_config, std::optional<ServeApi> const& serve, bool with_execute) -> bool { @@ -159,25 +187,4 @@ auto ServeServerImpl::Run(RemoteServeConfig const& serve_config, return true; } -[[nodiscard]] auto ServeServerImpl::SetInfoFile(std::string const& x) noexcept - -> bool { - Instance().info_file_ = x; - return true; -} - -[[nodiscard]] auto ServeServerImpl::SetPidFile(std::string const& x) noexcept - -> bool { - Instance().pid_file_ = x; - return true; -} - -[[nodiscard]] auto ServeServerImpl::SetPort(int const x) noexcept -> bool { - auto port_num = ParsePort(x); - if (!port_num) { - return false; - } - Instance().port_ = static_cast<int>(*port_num); - return true; -} - #endif // BOOTSTRAP_BUILD_TOOL diff --git a/src/buildtool/serve_api/serve_service/serve_server_implementation.hpp b/src/buildtool/serve_api/serve_service/serve_server_implementation.hpp index b3c0ee51..e0718284 100644 --- a/src/buildtool/serve_api/serve_service/serve_server_implementation.hpp +++ b/src/buildtool/serve_api/serve_service/serve_server_implementation.hpp @@ -15,6 +15,7 @@ #ifndef SERVE_SERVER_IMPLEMENTATION_HPP #define SERVE_SERVER_IMPLEMENTATION_HPP +#include <optional> #include <string> #include "src/buildtool/logging/logger.hpp" @@ -23,31 +24,20 @@ class ServeServerImpl { public: - ServeServerImpl() noexcept = default; - [[nodiscard]] static auto Instance() noexcept -> ServeServerImpl& { - static ServeServerImpl x; - return x; - } - - [[nodiscard]] static auto SetInterface(std::string const& x) noexcept - -> bool { - Instance().interface_ = x; - return true; - } - - [[nodiscard]] static auto SetPidFile(std::string const& x) noexcept -> bool; + [[nodiscard]] static auto Create( + std::optional<std::string> interface, + std::optional<int> port, + std::optional<std::string> info_file, + std::optional<std::string> pid_file) noexcept + -> std::optional<ServeServerImpl>; - [[nodiscard]] static auto SetPort(int x) noexcept -> bool; - - [[nodiscard]] static auto SetInfoFile(std::string const& x) noexcept - -> bool; + ~ServeServerImpl() noexcept = default; ServeServerImpl(ServeServerImpl const&) = delete; - auto operator=(ServeServerImpl const&) noexcept - -> ServeServerImpl& = delete; + auto operator=(ServeServerImpl const&) -> ServeServerImpl& = delete; - ServeServerImpl(ServeServerImpl&&) noexcept = delete; - auto operator=(ServeServerImpl&&) noexcept -> ServeServerImpl& = delete; + ServeServerImpl(ServeServerImpl&&) noexcept = default; + auto operator=(ServeServerImpl&&) noexcept -> ServeServerImpl& = default; /// \brief Start the serve service. /// \param serve_config RemoteServeConfig to be used. @@ -57,9 +47,10 @@ class ServeServerImpl { auto Run(RemoteServeConfig const& serve_config, std::optional<ServeApi> const& serve, bool with_execute) -> bool; - ~ServeServerImpl() = default; private: + ServeServerImpl() noexcept = default; + std::string interface_{"127.0.0.1"}; int port_{0}; std::string info_file_{}; |