4#include <slick/logger.hpp>
5#include <slick/queue.hpp>
15inline std::string
instance_path(std::string_view venue, std::string_view instance) {
16 std::string path(venue);
17 if (instance != venue) {
25inline std::string
venue_path(std::string_view venue, std::string_view rest = {}) {
27 for (
char c : venue) {
28 path += (c >=
'A' && c <= 'Z') ? static_cast<char>(c -
'A' +
'a') : c;
39inline std::string
pcap_feed_path(std::string_view venue, std::string_view feed) {
44[[nodiscard]]
constexpr std::string_view
cut_text(std::string_view text,
size_t size)
noexcept {
45 if (text.size() <= size) {
49 while (end != 0 && size - end < 3 && (
static_cast<unsigned char>(text[end]) & 0xC0) == 0x80) {
52 return {text.data(), end};
62 [[nodiscard]] std::string_view
arg_view() const noexcept {
return {
arg, strnlen(
arg,
sizeof(
arg))}; }
68 out +=
" is not served here";
75 return polled ? slick::logger::LogLevel::L_DEBUG : slick::logger::LogLevel::L_INFO;
98 [[nodiscard]]
const std::string &
name() const noexcept {
return name_; }
105 template <
typename Handler>
108 auto [command, count] = inbox_.read(inbox_cursor_);
109 if (command ==
nullptr) {
113 const bool ok = handler(
static_cast<const Command &
>(*command), text_);
114 SLICK_LOGGER_LOG_IF_ENABLED(
log_level(command->command),
"CLI {} {}: {}", name_,
115 ok ?
"did" :
"refused", text_);
126 const auto index = inbox_.reserve();
127 *inbox_[index] = command;
128 inbox_.publish(index);
138 template <
typename OnReply>
141 auto [data, size] = outbox_.read(outbox_cursor_);
142 if (data ==
nullptr) {
147 std::memcpy(&header, data,
sizeof(header));
148 on_reply(header.ticket, header.status,
149 std::string_view(
reinterpret_cast<const char *
>(data) +
sizeof(header), size -
sizeof(header)));
161 static constexpr uint32_t SLOT_BYTES = 64;
162 static constexpr uint32_t OUTBOX_BYTES = 1u << 17;
165 void reply(uint64_t ticket,
CliStatus status, std::string_view text) {
170 const auto size =
static_cast<uint32_t
>(
sizeof(ReplyHeader) + text.size() + (cut ? 3 : 0));
171 const auto index = outbox_.reserve(size);
172 uint8_t *out = outbox_[index];
173 const ReplyHeader header{ticket, status};
174 std::memcpy(out, &header,
sizeof(header));
175 std::memcpy(out +
sizeof(header), text.data(), text.size());
177 std::memcpy(out +
sizeof(header) + text.size(),
"...", 3);
179 outbox_.publish(index, size);
184 slick::queue<uint8_t> outbox_{OUTBOX_BYTES, SLOT_BYTES};
185 uint64_t inbox_cursor_ = 0;
187 uint64_t outbox_cursor_ = 0;
188 uint32_t in_flight_ = 0;
void set_name(std::string name)
Before the endpoint is given to the CLI server.
void serve(Handler &&handler)
Owner thread.
CliEndpoint(const CliEndpoint &)=delete
bool post(const Command &command)
CLI server thread. False, posting nothing, while busy().
bool busy() const noexcept
CLI server thread.
const std::string & name() const noexcept
How the CLI addresses it, /-separated from the venue down: cme/<segment>.
CliEndpoint & operator=(const CliEndpoint &)=delete
static constexpr uint32_t MAX_IN_FLIGHT
static constexpr uint32_t MAX_REPLY_TEXT
A longer reply is cut, ending in "...".
CliEndpoint(std::string name={})
void take_replies(OnReply &&on_reply)
CLI server thread.
The CLI commands that switch a gateway's business rejects per ID: REJECT_LOGON, REJECT_NEW_ORDERS and...
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.
std::string pcap_feed_path(std::string_view venue, std::string_view feed)
pcap/<venue>/<feed>.
std::string instance_path(std::string_view venue, std::string_view instance)
<venue>/<instance>, or <venue> alone for the one instance of a venue configured without a list of the...
constexpr slick::logger::LogLevel log_level(CliCommandType command) noexcept
What a command and its answer are logged at: DEBUG for what clients poll.
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 venue_path(std::string_view venue, std::string_view rest={})
<venue>[/<rest>], the venue lowercased (CME -> cme).
CliCommandType
What a CLI request asks for. The values are on the wire: append, never renumber.
@ PCAP_STATUS
pcap. Each feed's state, progress and window.
@ LIST_FEEDS
Any target. The CLI server answers with every enabled market-data feed.
constexpr const char * to_string(FeedState state) noexcept
char arg[64]
The ID or level. NUL-padded.
One command, as the CLI server hands it to the thread that runs it.
char arg[sizeof(CliRequestMessage::arg)]
NUL-padded, as on the wire.
uint64_t ticket
The server's id for the request, echoed in the reply.
std::string_view arg_view() const noexcept