2#include <slick/logger.hpp>
3#include <slick/socket/polled_tcp_client.h>
15constexpr std::string_view TARGET =
"pcap";
16constexpr std::string_view FEED_PREFIX =
"pcap/";
17constexpr std::string_view LIVE_PREFIX =
"live ";
18constexpr std::string_view PCAP_PREFIX =
"pcap ";
20constexpr std::string_view NO_COMMAND =
"unknown command";
23 std::string_view name;
26constexpr std::array STATES{
37std::string_view take(std::string_view& text, std::string_view delimiter) {
38 const size_t at = text.find(delimiter);
39 const std::string_view taken = text.substr(0, at);
40 text.remove_prefix(at == std::string_view::npos ? text.size() : at + delimiter.size());
46 const auto it = std::ranges::find(STATES, answer.substr(0, answer.find(
' ')), &StateName::name);
51uint32_t to_permille(std::string_view percent) {
54 const char*
const end = percent.data() + percent.size();
55 const auto [next, error] = std::from_chars(percent.data(), end, whole);
56 if (error == std::errc() && next != end && *next ==
'.') {
57 std::from_chars(next + 1, end, tenth);
59 return std::min(whole, 100u) * 10 + std::min(tenth, 9u);
63void parse_answer(std::string_view answer,
SimFeed& feed) {
64 feed.state = leading_state(answer);
66 feed.message = answer;
70 feed.permille = to_permille(take(answer,
", capture "));
71 feed.capture = take(answer,
", window ");
72 feed.begin = take(answer,
"..");
73 feed.end = take(answer,
", speed ");
74 feed.speed = take(answer,
" - ");
75 feed.message = answer;
80size_t name_length(std::string_view line) {
81 const size_t first = line.find(
": ");
82 for (
size_t at = first; at != std::string_view::npos; at = line.find(
": ", at + 1)) {
90bool loaded(
const SimFeed& feed) {
95bool has_state(
const std::vector<SimFeed>& feeds,
SimFeedState state) {
96 return std::ranges::find(feeds, state, &
SimFeed::state) != feeds.end();
102 const auto it = std::ranges::find(STATES, state, &StateName::state);
103 return it == STATES.end() ?
"UNKNOWN" : it->name;
107 std::vector<SimFeed> feeds;
108 while (!text.empty()) {
109 std::string_view line = take(text,
"\n");
110 if (!line.starts_with(FEED_PREFIX)) {
113 line.remove_prefix(FEED_PREFIX.size());
114 const size_t length = name_length(line);
115 if (length == std::string_view::npos) {
118 const std::string_view name = line.substr(0, length);
119 const std::string_view answer = line.substr(length + 2);
122 feeds.back().message +=
' ';
123 feeds.back().message += answer;
126 SimFeed& feed = feeds.emplace_back();
128 parse_answer(answer, feed);
135 while (!text.empty()) {
136 std::string_view line = take(text,
"\n");
137 if (line.starts_with(PCAP_PREFIX)) {
139 }
else if (line.starts_with(LIVE_PREFIX)) {
140 line.remove_prefix(LIVE_PREFIX.size());
141 if (std::ranges::find(list.
live, line) == list.
live.end()) {
142 list.
live.emplace_back(line);
149SimControl::Pending SimControl::send(uint8_t command, uint64_t now_ns) {
150 if (++last_request_id_ == 0) {
153 sim_cli::Request request{};
154 request.header = {
static_cast<uint16_t
>(
sizeof(request)),
sim_cli::REQUEST};
155 request.request_id = last_request_id_;
156 request.command = command;
158 std::memcpy(request.target, TARGET.data(), TARGET.size());
160 transport_.send(
reinterpret_cast<const uint8_t*
>(&request),
sizeof(request));
161 return {last_request_id_, now_ns};
164void SimControl::send_poll(uint64_t now_ns) {
168 poll_ = send(poll_command_, now_ns);
169 next_poll_ns_ = now_ns +
POLL_NS;
182 std::memcpy(&reply, data + used,
sizeof(reply));
185 LOG_WARN(
"SimControl: malformed reply from slick-sim, reconnecting");
189 if (size - used < length) {
192 const std::string_view text(
reinterpret_cast<const char*
>(data) + used +
sizeof(reply),
193 length -
sizeof(reply));
200 on_poll(poll_command_, ok, text, now_ns);
201 }
else if (watch_id_ != 0 && reply.
request_id == watch_id_) {
205 on_replay(ok, text, now_ns);
206 }
else if (version_request_.is(reply.
request_id)) {
207 version_request_ = {};
208 if (
const auto version = ok ? text : std::string_view();
version != version_) {
219 if (poll_.expired(now_ns) || replay_.expired(now_ns)) {
220 LOG_WARN(
"SimControl: no reply from slick-sim, reconnecting");
224 if (poll_.request_id == 0 && now_ns >= next_poll_ns_ && !settled()) {
227 if (starting_ && replay_.request_id == 0 && now_ns - started_ns_ >=
STARTING_NS) {
234 held_ = settled() && poll_.request_id == 0;
235 if (replay_.request_id != 0) {
236 replay_answer_ =
ReplayAnswer{
false,
"the link to slick-sim dropped before it answered"};
240 version_request_ = {};
253 if (connected_ || !version_.empty()) {
263void SimControl::on_poll(uint8_t command,
bool ok, std::string_view text, uint64_t now_ns) {
269 if (connected_ && command == last_command_ && ok == last_ok_ && text == last_text_) {
273 last_command_ = command;
278 on_list(ok, text, now_ns);
284void SimControl::on_list(
bool ok, std::string_view text, uint64_t now_ns) {
292 listed_ = text.starts_with(NO_COMMAND);
293 error_ = listed_ ? std::string_view(
"too old to list its feeds, update it") : text;
304void SimControl::on_status(std::string_view text) {
307 feeds_ = std::move(
feeds);
313 error_ = text.empty() ? std::string_view(
"slick-sim gave no status") : text;
317void SimControl::on_replay(
bool ok, std::string_view text, uint64_t now_ns) {
319 started_ns_ = now_ns;
321 next_poll_ns_ = now_ns;
323 std::string line(text);
324 std::ranges::replace(line,
'\n',
';');
326 LOG_INFO(
"SimControl: replay started: {}", line);
328 LOG_ERROR(
"SimControl: slick-sim refused the replay: {}", line);
334 return "Not connected to slick-sim";
336 if (!error_.empty()) {
339 if (feeds_.empty()) {
340 return list_.pcap ?
"Waiting for the feeds' status" :
"slick-sim has no pcap feed";
342 if (!status_fresh()) {
344 return "Reconnecting to slick-sim";
350 return failed->name +
" is in ERROR";
352 if (
const auto count = std::ranges::count_if(feeds_, loaded);
static_cast<size_t>(count) != feeds_.size()) {
353 return std::format(
"{} of {} feeds ready", count, feeds_.size());
372 auto feeds = nlohmann::json::array();
373 for (
const auto& feed : feeds_) {
377 {
"progress", feed.permille / 10.0},
378 {
"capture", feed.capture},
379 {
"begin", feed.begin},
381 {
"speed", feed.speed},
382 {
"message", feed.message}
387 {
"connected", connected_},
388 {
"version", version_},
389 {
"live_feeds", list_.live},
390 {
"pcap", list_.pcap},
391 {
"can_start", blocked.empty()},
392 {
"start_blocked", blocked},
393 {
"starting", starting_},
395 {
"feeds", std::move(
feeds)}
400 using Client = slick::socket::PolledTCPClient;
405 slick::socket::PolledTCPClientConfig tcp;
406 tcp.server_address = config.
addr;
407 tcp.server_port = config.
port;
408 tcp.connection_timeout = std::chrono::milliseconds(3000);
409 tcp.send_timeout = std::chrono::milliseconds(100);
410 return std::make_unique<Client>(
"SimControl", tcp);
421 : impl_(std::make_unique<
Impl>(config))
422 , address_(std::format(
"{}:{}", config.addr, config.port))
428 next_service_ns_ = now_ns + SERVICE_NS;
429 auto& [link,
control, buffer] = *impl_;
430 const bool was_connected = link.connected();
431 link.service(
control, now_ns, buffer);
432 if (
control.held()) [[unlikely]] {
435 }
else if (link.client().state() == Impl::Client::State::Disconnected) {
442 impl_->link.disconnect();
443 impl_->control.on_disconnected();
444 impl_->control.on_lost();
SimControl & control() noexcept
SimControlLink(const SimControlConfig &config)
void service(uint64_t now_ns)
void on_connected(uint64_t now_ns)
nlohmann::json to_json() const
bool start_replay(uint64_t now_ns)
static constexpr uint64_t POLL_NS
size_t on_data(const uint8_t *data, size_t size, uint64_t now_ns)
static constexpr uint64_t STARTING_NS
std::string start_blocked() const
const std::vector< SimFeed > & feeds() const noexcept
const std::string & version() const noexcept
void on_timer(uint64_t now_ns)
constexpr uint8_t REQUEST
constexpr uint8_t PCAP_STATUS
constexpr uint8_t LIST_FEEDS
constexpr uint8_t GET_VERSION
constexpr uint8_t WATCH_STATUS
constexpr uint8_t STATUS_OK
constexpr uint8_t START_REPLAY
SimFeedList parse_sim_feed_list(std::string_view text)
const char * to_string(BrokerId bid)
std::vector< SimFeed > parse_sim_feeds(std::string_view text)
std::array< uint8_t, 16384 > buffer
Impl(const SimControlConfig &config)
static std::unique_ptr< Client > make_client(const SimControlConfig &config)
slick::socket::PolledTCPClient Client
PolledLink< Client > link
static constexpr uint64_t RECONNECT_NS
std::vector< std::string > live