2#include <slick/logger.hpp>
13std::string utc(uint64_t ns) {
16 std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::nanoseconds(ns))));
20void describe(
const FeedStatusMessage &status, std::string_view message, std::string &out) {
23 const unsigned permille = status.progress_permille;
24 const uint64_t current_ns = status.current_ns;
25 const uint64_t begin_ns = status.begin_ns;
26 const uint64_t end_ns = status.end_ns;
27 const double speed = status.speed;
28 std::format_to(std::back_inserter(out),
"{} {}.{}%, capture {}, window {}..{}, speed {}",
to_string(state),
29 permille / 10, permille % 10, utc(current_ns), utc(begin_ns), utc(end_ns), speed);
30 if (!message.empty()) {
40 : Server(name, config)
41 , name_(std::move(name))
42 , cli_(std::move(cli_name))
51 if (!Server::start()) {
52 LOG_ERROR(
"{} control channel failed to start on port {}", name_, Server::port());
55 LOG_INFO(
"{} control channel listening on port {}", name_, Server::port());
64 const auto index = outbox_.reserve();
65 StatusSlot &slot = *outbox_[index];
66 std::memcpy(slot.bytes, &status,
sizeof(status));
67 slot.message_size =
static_cast<uint16_t
>(
68 cli::cut_text(message,
sizeof(slot.message)).copy(slot.message,
sizeof(slot.message)));
69 outbox_.publish(index);
73 LOG_INFO(
"{} control channel: connection {} from {}", name_, client_id, client_address);
77 clients_.insert(client_id);
79 Server::send_data(client_id, last_status_->bytes,
sizeof(last_status_->bytes));
84 LOG_INFO(
"{} control channel: connection {} closed", name_, client_id);
85 clients_.erase(client_id);
86 streams_.erase(client_id);
90 const bool ok = streams_.feed(client_id, data, length, [
this](
const ControlHeader &header,
const uint8_t *) {
98 LOG_WARN(
"{} control channel: malformed message from connection {}, closing it", name_, client_id);
99 Server::disconnect_client(client_id);
105 cli_.serve([
this](
const cli::Command &command, std::string &out) {
return serve_cli(command, out); });
108void PcapControlServer::deliver() {
110 auto [slot, size] = outbox_.read(outbox_cursor_);
111 if (slot ==
nullptr) {
114 last_status_ = *slot;
115 for (
int client : clients_) {
116 Server::send_data(client, slot->bytes,
sizeof(slot->bytes));
121bool PcapControlServer::serve_cli(
const cli::Command &command, std::string &out) {
126 FeedStatusMessage status{};
127 std::string_view message;
129 std::memcpy(&status, last_status_->bytes,
sizeof(status));
130 message = {last_status_->message, last_status_->message_size};
132 switch (command.command) {
134 describe(status, message, out);
137 switch (status.state) {
139 out =
"cannot start; ";
140 describe(status, message, out);
145 default: out =
"starts once READY; ";
break;
149 describe(status, message, out);
void publish_status(const FeedStatusMessage &status, std::string_view message)
Sends status to every client now connected, and to each one that connects before the next.
void onClientDisconnected(int client_id)
void onClientConnected(int client_id, const std::string &client_address)
void onClientData(int client_id, const uint8_t *data, size_t length)
bool start()
False when the port could not be bound.
PcapControlServer(std::string name, const slick::socket::TCPServerConfig &config, std::string cli_name)
cli_name is the CLI endpoint's (cli::pcap_feed_path).
bool refuse_unserved(const Command &command, std::string &out)
A handler's answer to a command it doesn't serve: says so in out, false.
constexpr std::string_view cut_text(std::string_view text, size_t size) noexcept
text cut to size bytes, backing off up to 3 so as not to split a UTF-8 character.
std::string format_timestamp_iso8601(const std::chrono::system_clock::time_point &tp, int num_sebsecond_digits=3)
@ START_REPLAY
pcap. Starts each feed's replay, as a control-channel START does.
@ PCAP_STATUS
pcap. Each feed's state, progress and window.
FeedState
Where a pcap replay feed is. The values are on the wire: append, never renumber.
@ READY
The book is built and held. Replay starts when a client sends START.
@ FINISHED
The window is replayed out.
@ REPLAYING
Publishing the capture.
@ ERROR
The feed failed; message says why.
constexpr const char * to_string(FeedState state) noexcept
@ START
Client to sim: start the replay.
Starts the replay of every loaded instrument.
One command, as the CLI server hands it to the thread that runs it.