blob: e29b8aa72af4895286ed748edd0fdcb9ba17d96b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_CONFIG_HPP
#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_CONFIG_HPP
#include <map>
#include <memory>
#include <optional>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
#include "gsl-lite/gsl-lite.hpp"
#include "src/buildtool/logging/logger.hpp"
class RemoteExecutionConfig {
public:
[[nodiscard]] static auto ParseAddress(std::string const& address) noexcept
-> std::optional<std::pair<std::string, int>> {
std::istringstream iss(address);
std::string host;
std::string port;
if (not std::getline(iss, host, ':') or
not std::getline(iss, port, ':')) {
return std::nullopt;
}
try {
return std::make_pair(host, std::stoi(port));
} catch (std::out_of_range const& e) {
Logger::Log(LogLevel::Error, "Port raised out_of_range exception.");
return std::nullopt;
} catch (std::invalid_argument const& e) {
Logger::Log(LogLevel::Error,
"Port raised invalid_argument exception.");
return std::nullopt;
}
}
// Obtain global instance
[[nodiscard]] static auto Instance() noexcept -> RemoteExecutionConfig& {
static RemoteExecutionConfig config;
return config;
}
[[nodiscard]] auto IsValidAddress() const noexcept -> bool {
return valid_;
}
[[nodiscard]] auto SetAddress(std::string const& address) noexcept -> bool {
auto pair = ParseAddress(address);
return pair and SetAddress(pair->first, pair->second);
}
[[nodiscard]] auto SetAddress(std::string const& host, int port) noexcept
-> bool {
host_ = host;
port_ = port,
valid_ = (not host.empty() and port >= 0 and port <= kMaxPortNumber);
return valid_;
}
[[nodiscard]] auto Host() const noexcept -> std::string { return host_; }
[[nodiscard]] auto Port() const noexcept -> int { return port_; }
private:
static constexpr int kMaxPortNumber{std::numeric_limits<uint16_t>::max()};
std::string host_{};
int port_{};
bool valid_{false};
};
#endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_REMOTE_CONFIG_HPP
|