Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
cli_endpoint.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <slick/logger.hpp>
5#include <slick/queue.hpp>
6#include <cstdint>
7#include <cstring>
8#include <string>
9#include <string_view>
10
11namespace slick::sim::cli {
12
15inline std::string instance_path(std::string_view venue, std::string_view instance) {
16 std::string path(venue);
17 if (instance != venue) {
18 path += '/';
19 path += instance;
20 }
21 return path;
22}
23
25inline std::string venue_path(std::string_view venue, std::string_view rest = {}) {
26 std::string path;
27 for (char c : venue) {
28 path += (c >= 'A' && c <= 'Z') ? static_cast<char>(c - 'A' + 'a') : c;
29 }
30 if (!rest.empty()) {
31 path += '/';
32 path += rest;
33 }
34 return path;
35}
36
39inline std::string pcap_feed_path(std::string_view venue, std::string_view feed) {
40 return "pcap/" + venue_path(venue, feed);
41}
42
44[[nodiscard]] constexpr std::string_view cut_text(std::string_view text, size_t size) noexcept {
45 if (text.size() <= size) {
46 return text;
47 }
48 size_t end = size;
49 while (end != 0 && size - end < 3 && (static_cast<unsigned char>(text[end]) & 0xC0) == 0x80) {
50 --end;
51 }
52 return {text.data(), end};
53}
54
56struct Command {
57 uint64_t ticket;
59 uint8_t flag;
61
62 [[nodiscard]] std::string_view arg_view() const noexcept { return {arg, strnlen(arg, sizeof(arg))}; }
63};
64
66inline bool refuse_unserved(const Command &command, std::string &out) {
67 out = to_string(command.command);
68 out += " is not served here";
69 return false;
70}
71
73[[nodiscard]] constexpr slick::logger::LogLevel log_level(CliCommandType command) noexcept {
74 const bool polled = command == CliCommandType::PCAP_STATUS || command == CliCommandType::LIST_FEEDS;
75 return polled ? slick::logger::LogLevel::L_DEBUG : slick::logger::LogLevel::L_INFO;
76}
77
88public:
89 static constexpr uint32_t MAX_IN_FLIGHT = 16;
91 static constexpr uint32_t MAX_REPLY_TEXT = 4096;
92
93 explicit CliEndpoint(std::string name = {}) : name_(std::move(name)) {}
94 CliEndpoint(const CliEndpoint &) = delete;
95 CliEndpoint &operator=(const CliEndpoint &) = delete;
96
98 [[nodiscard]] const std::string &name() const noexcept { return name_; }
100 void set_name(std::string name) { name_ = std::move(name); }
101
105 template <typename Handler>
106 void serve(Handler &&handler) {
107 for (;;) {
108 auto [command, count] = inbox_.read(inbox_cursor_);
109 if (command == nullptr) {
110 return;
111 }
112 text_.clear();
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_);
116 reply(command->ticket, ok ? CliStatus::OK : CliStatus::FAILED, text_);
117 }
118 }
119
121 [[nodiscard]] bool post(const Command &command) {
122 if (busy()) {
123 return false;
124 }
125 ++in_flight_;
126 const auto index = inbox_.reserve();
127 *inbox_[index] = command;
128 inbox_.publish(index);
129 return true;
130 }
131
134 [[nodiscard]] bool busy() const noexcept { return in_flight_ >= MAX_IN_FLIGHT; }
135
138 template <typename OnReply>
139 void take_replies(OnReply &&on_reply) {
140 for (;;) {
141 auto [data, size] = outbox_.read(outbox_cursor_);
142 if (data == nullptr) {
143 return;
144 }
145 --in_flight_;
146 ReplyHeader header;
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)));
150 }
151 }
152
153private:
154 struct ReplyHeader {
155 uint64_t ticket;
156 CliStatus status;
157 };
158
161 static constexpr uint32_t SLOT_BYTES = 64;
162 static constexpr uint32_t OUTBOX_BYTES = 1u << 17;
163 static_assert(OUTBOX_BYTES >= MAX_IN_FLIGHT * (sizeof(ReplyHeader) + MAX_REPLY_TEXT + SLOT_BYTES));
164
165 void reply(uint64_t ticket, CliStatus status, std::string_view text) {
166 const bool cut = text.size() > MAX_REPLY_TEXT;
167 if (cut) {
168 text = cut_text(text, MAX_REPLY_TEXT - 3);
169 }
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());
176 if (cut) {
177 std::memcpy(out + sizeof(header) + text.size(), "...", 3);
178 }
179 outbox_.publish(index, size);
180 }
181
182 std::string name_;
183 slick::queue<Command> inbox_{MAX_IN_FLIGHT};
184 slick::queue<uint8_t> outbox_{OUTBOX_BYTES, SLOT_BYTES};
185 uint64_t inbox_cursor_ = 0;
186 std::string text_;
187 uint64_t outbox_cursor_ = 0;
188 uint32_t in_flight_ = 0;
189};
190
191} // namespace slick::sim::cli
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