86int main(
int argc,
char *argv[])
89 std::signal(SIGINT, _signal_handler);
91 auto &logger = slick::logger::Logger::instance();
94 logger.add_console_sink(
true,
true);
96 logger.add_file_sink(
"logs/slick-sim.log");
97 logger.init(65535, 16777216);
99#ifdef SLICK_SIM_HAS_SLICK_NET
100 slick::net::set_log_handler(
101 [&logger](slick::net::LogLevel level,
const char *format_text, std::format_args args)
103 logger.log(
static_cast<slick::logger::LogLevel
>(level), format_text, std::move(args));
105 [&logger]() -> slick::net::LogLevel
107 return static_cast<slick::net::LogLevel
>(logger.get_level());
111 std::string config_file =
"slick-sim.json";
114 config_file = argv[1];
117 if (!std::filesystem::exists(config_file))
119 std::cout <<
"Missing configuration JSON file. Usange\n"
120 <<
" slick-sim [config_file]\n"
122 <<
" config_file: config JSON file. Default to slick-sim.json";
123 LOG_ERROR(
"Missing configuration JSON file. Usage\n slick-sim [config_file]\n options:\n config_file: config JSON file. Default to slick-sim.json");
127 std::fstream f(config_file);
131 config = json::parse(f);
133 catch (
const json::parse_error &ex)
135 std::cerr <<
"Parsing failed: \n"
137 LOG_ERROR(
"Parsing failed: \n{}", ex.byte);
141 if (!config.contains(
"exchanges"))
143 std::cerr <<
"Missing exchanges config";
144 LOG_ERROR(
"Missing exchanges config");
148 logger.set_level(slick::logger::to_log_level(config.value(
"log_level",
"info")));
154 std::string built_in;
155 for (
const auto &key : registry.keys())
157 if (!built_in.empty())
163 LOG_INFO(
"Venue adapters built in: {}", built_in.empty() ?
"(none)" : built_in);
165 auto &exch_coinfig = config[
"exchanges"];
166 std::vector<std::unique_ptr<Exchange>> exchanges;
168 std::vector<std::pair<std::string, std::vector<cli::CliEndpoint *>>> cli_groups;
170 std::vector<cli::CliEndpoint *> feed_endpoints;
171 std::vector<exch::FeedInfo> feeds;
172 for (
auto it = exch_coinfig.begin(); it != exch_coinfig.end(); ++it)
177 if (!it.value().value(
"enabled",
true))
179 LOG_INFO(
"Exchange {} is disabled, skipping", it.key());
183 auto factory = registry.find(it.key());
190 std::cerr <<
"No venue adapter for exchanges.\"" << it.key() <<
"\"\n";
191 LOG_ERROR(
"No venue adapter for exchanges.\"{}\". This build has: {}. "
192 "Check the spelling, or configure with -DSLICK_SIM_ENABLE_<VENUE>=ON.",
193 it.key(), built_in.empty() ?
"(none)" : built_in);
200 std::vector<std::unique_ptr<Exchange>> created;
203 created = factory(it.value());
204 for (
const auto &exchange : created)
206 auto enabled = exchange->enabledFeeds();
207 feeds.insert(feeds.end(), enabled.begin(), enabled.end());
211 catch (
const std::exception &ex)
213 std::cerr <<
"exchanges.\"" << it.key() <<
"\": " << ex.what() <<
"\n";
214 LOG_ERROR(
"exchanges.\"{}\": {}", it.key(), ex.what());
217 auto &group = cli_groups.emplace_back(it.key(), std::vector<cli::CliEndpoint *>{}).second;
218 for (
auto &exchange : created)
220 exchanges.emplace_back(std::move(exchange));
221 auto endpoints_of_feeds = exchanges.back()->feedCliEndpoints();
222 feed_endpoints.insert(feed_endpoints.end(), endpoints_of_feeds.begin(), endpoints_of_feeds.end());
223 auto endpoints = exchanges.back()->cliEndpoints();
224 group.insert(group.end(), endpoints.begin(), endpoints.end());
229 for (
auto &
exch : exchanges)
233 auto stop_exchanges = [&exchanges]
235 for (
auto &
exch : exchanges)
244 bool cli_running =
false;
245#ifdef SLICK_SIM_HAS_CLI
246 std::unique_ptr<cli::CliServer> cli_server;
248 if (config.contains(
"cli") && config[
"cli"].value(
"enabled",
true))
250#ifdef SLICK_SIM_HAS_CLI
251 const auto &cli_config = config[
"cli"];
252 slick::socket::TCPServerConfig tcp;
254 tcp.max_connections = cli_config.value(
"max_connections", 4);
255 tcp.idle_timeout = std::chrono::seconds(
257 cli_server = std::make_unique<cli::CliServer>(tcp);
258 cli_server->add_endpoints(
"sim", {&sim_cli});
259 for (
const auto &[key, endpoints] : cli_groups)
261 cli_server->add_endpoints(key, endpoints);
264 cli_server->add_endpoints({}, feed_endpoints);
266 cli_server->set_version(SLICK_SIM_VERSION);
267 if (!cli_server->start())
269 std::cerr <<
"CLI failed to start on port " << tcp.port <<
"\n";
276 LOG_WARN(
"\"cli\" is configured, but this build has no CLI: it needs SLICK_SIM_ENABLE_TCP_GATEWAY");
281 LOG_INFO(
"Press Ctrl+C to stop the server...");
282 while (run.load(std::memory_order_relaxed))
284 sim_cli.
serve(serve_sim_command);
285 std::this_thread::sleep_for(cli_running ? std::chrono::milliseconds(10) : std::chrono::milliseconds(1000));
288#ifdef SLICK_SIM_HAS_CLI
293 cli_server->flush(std::chrono::milliseconds(500));
300 LOG_INFO(
"ExchangeSimulator stopped");
int main(int argc, char *argv[])
slick::sim::exch::Exchange Exchange