Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
sim_control.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <nlohmann/json.hpp>
6#include <bit>
7#include <cstddef>
8#include <cstdint>
9#include <memory>
10#include <optional>
11#include <string>
12#include <string_view>
13#include <utility>
14#include <vector>
15
16namespace slick::trader {
17
18// slick-sim's CLI wire format (its docs/cli.md): packed, little-endian, `length` the whole message.
19namespace sim_cli {
20
21static_assert(std::endian::native == std::endian::little);
22
23inline constexpr uint8_t REQUEST = 3;
24inline constexpr uint8_t REPLY = 4;
25inline constexpr uint8_t START_REPLAY = 7;
26inline constexpr uint8_t PCAP_STATUS = 8;
27inline constexpr uint8_t LIST_FEEDS = 9;
28inline constexpr uint8_t GET_VERSION = 10;
29inline constexpr uint8_t WATCH_STATUS = 11; // PCAP_STATUS's answer, then again under its id on each change
30inline constexpr uint8_t STATUS_OK = 0;
31
32#pragma pack(push, 1)
33struct Header {
34 uint16_t length;
35 uint8_t type;
36};
37struct Request {
39 uint32_t request_id; // echoed in the reply
40 uint8_t command;
41 uint8_t flag;
42 char target[32]; // NUL-padded, as is arg
43 char arg[64];
44};
45struct Reply { // then UTF-8 text, a line per feed
47 uint32_t request_id;
48 uint8_t status;
49};
50#pragma pack(pop)
51
52static_assert(sizeof(Header) == 3);
53static_assert(sizeof(Request) == 105);
54static_assert(sizeof(Reply) == 8);
55
56} // namespace sim_cli
57
58// A feed's state as slick-sim names it. FAILED is its ERROR, a macro of windows.h. UNKNOWN is a feed
59// that gave none.
61
62std::string_view to_string(SimFeedState state) noexcept;
63
64// One pcap feed, as slick-sim's `status` reports it.
65struct SimFeed {
66 std::string name; // "<venue>/<feed>"
68 uint32_t permille{0}; // through the window
69 std::string capture; // the capture time reached, and the window, UTC
70 std::string begin;
71 std::string end;
72 std::string speed;
73 std::string message; // the reason in ERROR, or what it said instead of a state
74};
75
76// The feeds in the text of a `status` reply, one a line. A line that names no feed is skipped.
77std::vector<SimFeed> parse_sim_feeds(std::string_view text);
78
79// The feeds slick-sim runs, as its `feeds` lists them.
81 std::vector<std::string> live; // "<venue>[/<instance or feed>]", each once
82 bool pcap{false};
83};
84
85// The text of a `feeds` reply. A line of neither kind is skipped.
86SimFeedList parse_sim_feed_list(std::string_view text);
87
88// The engine's end of slick-sim's CLI port, as PolledLink's session: asks its version and which feeds
89// it runs, then watches the pcap feeds' status and starts their replay. slick-sim sends a watched
90// status as it changes, so nothing more is asked unless the watch fails. With no pcap feed the feeds
91// don't change while slick-sim is up, so nothing more is asked either. A slick-sim too old to watch
92// (0.10.1 and before) gets asked for the status each POLL_NS.
93// Single-threaded: the WebServer's event loop owns it.
95public:
96 static constexpr uint64_t POLL_NS = 1'000'000'000;
97 // slick-sim answers within 2 s, even when a feed doesn't.
98 static constexpr uint64_t REPLY_TIMEOUT_NS = 5'000'000'000;
99 // How long a start it took may go without showing before another may be sent.
100 static constexpr uint64_t STARTING_NS = 5'000'000'000;
101
103 bool ok;
104 std::string text;
105 };
106
107 explicit SimControl(ByteTransport& transport) : transport_(transport) {}
108
109 // PolledLink's session.
110 void on_connected(uint64_t now_ns);
111 size_t on_data(const uint8_t* data, size_t size, uint64_t now_ns);
112 void on_timer(uint64_t now_ns);
113 void on_disconnected();
114 bool ready_to_connect(uint64_t) const noexcept { return true; }
115
116 // The link dropped with nothing left to ask, as slick-sim closes a silent one after its
117 // cli.idle_timeout. SimControlLink makes it again at once, and shows the drop only if that fails.
118 bool held() const noexcept { return held_; }
119 // A held link couldn't be made again, or won't be.
120 void on_lost();
121
122 // slick-sim has answered on this connection what feeds it has, and their status when it has pcap
123 // ones. Its version alone doesn't count. Kept while held().
124 bool connected() const noexcept { return connected_; }
125 // slick-sim's version on this connection, known by connected(). Empty from one too old to say
126 // (0.10.0 and before).
127 const std::string& version() const noexcept { return version_; }
128 // What a Sim environment is with the feeds slick-sim runs, empty with none. Kept through a
129 // dropped link, so the environment doesn't change with it.
130 std::string_view environment() const noexcept {
131 return !list_.live.empty() ? LIVE_SIM_ENVIRONMENT : list_.pcap ? HISTORICAL_SIM_ENVIRONMENT : std::string_view();
132 }
133 const SimFeedList& list() const noexcept { return list_; }
134 // Its pcap feeds' status. Held through a drop, and until the link made again answers.
135 const std::vector<SimFeed>& feeds() const noexcept { return feeds_; }
136
137 // Why the replay can't be started now, empty when it can: every feed READY, or past it when one
138 // was started on its own.
139 std::string start_blocked() const;
140 // Sends the start. False, sending nothing, while start_blocked().
141 bool start_replay(uint64_t now_ns);
142
143 // Whether to_json() or environment() may have changed since the last call.
144 bool take_changed() noexcept { return std::exchange(changed_, false); }
145 // slick-sim's answer to a start, once.
146 std::optional<ReplayAnswer> take_replay_answer() { return std::exchange(replay_answer_, std::nullopt); }
147
148 nlohmann::json to_json() const;
149
150private:
151 struct Pending {
152 uint32_t request_id{0}; // 0: none waits for its reply
153 uint64_t sent_ns{0};
154
155 bool is(uint32_t id) const noexcept { return request_id != 0 && request_id == id; }
156 bool expired(uint64_t now_ns) const noexcept {
157 return request_id != 0 && now_ns - sent_ns >= REPLY_TIMEOUT_NS;
158 }
159 };
160
161 // Nothing is left to ask.
162 bool settled() const noexcept { return listed_ && (!list_.pcap || watch_id_ != 0); }
163 // Whether the status came on this connection. One kept through a held drop is stale until then.
164 bool status_fresh() const noexcept {
165 return last_command_ == sim_cli::WATCH_STATUS || last_command_ == sim_cli::PCAP_STATUS;
166 }
167 Pending send(uint8_t command, uint64_t now_ns);
168 void send_poll(uint64_t now_ns);
169 void on_poll(uint8_t command, bool ok, std::string_view text, uint64_t now_ns);
170 void on_list(bool ok, std::string_view text, uint64_t now_ns);
171 void on_status(std::string_view text);
172 void on_replay(bool ok, std::string_view text, uint64_t now_ns);
173
174 ByteTransport& transport_;
175 uint32_t last_request_id_{0};
176 Pending poll_;
177 uint8_t poll_command_{0}; // what poll_ asks
178 uint8_t last_command_{0}; // and the last poll answered did
179 uint32_t watch_id_{0}; // the watch slick-sim took on this connection, 0 for none
180 bool can_watch_{true}; // false once this connection's slick-sim refused it
181 Pending replay_;
182 Pending version_request_;
183 uint64_t next_poll_ns_{0};
184 uint64_t started_ns_{0};
185 bool starting_{false};
186 bool connected_{false};
187 bool listed_{false}; // the feeds, on this connection, or slick-sim can't
188 bool held_{false};
189 bool changed_{false};
190 bool last_ok_{false};
191 std::string last_text_; // the last poll's reply: the same again changes nothing
192 std::string error_;
193 std::string version_;
194 SimFeedList list_;
195 std::vector<SimFeed> feeds_;
196 std::optional<ReplayAnswer> replay_answer_;
197};
198
199// The connection SimControl talks over, kept up while serviced. Single-threaded, as it is.
201public:
202 explicit SimControlLink(const SimControlConfig& config);
204
205 // Whether to call service() now, as every SERVICE_NS.
206 bool due(uint64_t now_ns) const noexcept { return now_ns >= next_service_ns_; }
207 // Connects, reads and polls.
208 void service(uint64_t now_ns);
209 void disconnect();
210
211 SimControl& control() noexcept;
212 const SimControl& control() const noexcept;
213 const std::string& address() const noexcept { return address_; }
214
215private:
216 static constexpr uint64_t SERVICE_NS = 10'000'000;
217
218 struct Impl; // the socket, whose headers stay out of this one
219 std::unique_ptr<Impl> impl_;
220 std::string address_; // "<addr>:<port>"
221 uint64_t next_service_ns_{0};
222};
223
224} // namespace slick::trader
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)
bool ready_to_connect(uint64_t) const noexcept
std::optional< ReplayAnswer > take_replay_answer()
bool connected() const noexcept
static constexpr uint64_t STARTING_NS
bool take_changed() noexcept
const SimFeedList & list() const noexcept
bool held() const noexcept
SimControl(ByteTransport &transport)
static constexpr uint64_t REPLY_TIMEOUT_NS
std::string_view environment() const noexcept
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 REPLY
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)
Definition event.hpp:78
std::vector< SimFeed > parse_sim_feeds(std::string_view text)
constexpr std::string_view LIVE_SIM_ENVIRONMENT
constexpr std::string_view HISTORICAL_SIM_ENVIRONMENT
std::vector< std::string > live