189 lines
5.9 KiB
C++
189 lines
5.9 KiB
C++
#include "SessionStore.h"
|
|
#include "lib/jetfix/store/session_name.h"
|
|
#include "lib/orderman/actors/ilink3/IL3GW.h"
|
|
#include "lib/orderman/actors/ilink3/IL3SecDB.h"
|
|
#include "lib/orderman/orderman_utils/ExecutionReport.h"
|
|
#include "lib/utils/base64.hpp"
|
|
#include "lib/utils/bts_chrono.h"
|
|
#include "lib/utils/file_utils.h"
|
|
#include "lib/utils/thread_manager.h"
|
|
|
|
#include <boost/filesystem.hpp>
|
|
#include <nlohmann/json.hpp>
|
|
#include <tbb/concurrent_queue.h>
|
|
#include <thread>
|
|
|
|
namespace bts {
|
|
namespace orderman {
|
|
namespace ilink3 {
|
|
|
|
using Action = std::function<void()>;
|
|
|
|
static std::string base_store_path(std::string base_path,
|
|
std::string const &connection_name,
|
|
std::string const &session_name) {
|
|
if (base_path.empty())
|
|
base_path = os::bts_data_path();
|
|
boost::filesystem::path result = utils::normalize_data_path(base_path);
|
|
result /= "ilink3";
|
|
result /= connection_name;
|
|
result /= session_name;
|
|
if (!boost::filesystem::exists(result))
|
|
bts_assert(boost::filesystem::create_directories(result));
|
|
return result.string();
|
|
}
|
|
|
|
static std::string raw_msg_log_path(std::string const &store_path) {
|
|
boost::filesystem::path result = store_path;
|
|
result /= "log";
|
|
return result.string();
|
|
}
|
|
|
|
static std::string session_path(std::string const &store_path) {
|
|
boost::filesystem::path result = store_path;
|
|
result /= "seqnum";
|
|
return result.string();
|
|
}
|
|
|
|
static std::vector<ExecutionReport>
|
|
load_replay(IL3SecDB &db, config::JetFIX::Connection const *cfg,
|
|
std::string const &path) {
|
|
|
|
IL3Parser parser(cfg, &db);
|
|
|
|
std::vector<ExecutionReport> result;
|
|
if (boost::filesystem::exists(path)) {
|
|
|
|
std::ifstream f(path);
|
|
nlohmann::json j;
|
|
for (;;) {
|
|
while (!f.eof() && f.get() != '{')
|
|
;
|
|
if (f.eof())
|
|
break;
|
|
else {
|
|
f.unget();
|
|
f >> j;
|
|
if (j["direction"] == "<") {
|
|
auto ts = datetime::parse_FIX_timestamp(j["time"]);
|
|
if (!datetime::is_at_least_secs_old(ts,
|
|
cfg->seconds_to_replay())) {
|
|
|
|
auto msg_bytes = websocketpp::base64_decode(j["msg_sbe"]);
|
|
if (msg_bytes.size()) {
|
|
auto maybe_ex_rep = parser.try_parse_exe_report(msg_bytes);
|
|
if (maybe_ex_rep)
|
|
result.push_back(maybe_ex_rep.value());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static void save_raw_msg(std::ofstream &out, RawMessageDirection d, size_t sn,
|
|
RawMessageData const &msg) {
|
|
nlohmann::json j;
|
|
j["direction"] = d == RawMessageDirection::INCOMING ? "<" : ">";
|
|
j["sn"] = sn;
|
|
j["time"] = datetime::FIX_timestamp(datetime::now());
|
|
j["msg_sbe"] = websocketpp::base64_encode(msg.binary);
|
|
try {
|
|
j["msg_json"] = nlohmann::json::parse(msg.json);
|
|
} catch (...) {
|
|
LOG(iLink3, Error, "Skipping troublesome json encoded message: {}",
|
|
msg.json);
|
|
j["msg_json"] = nlohmann::json{};
|
|
}
|
|
out << j << std::endl;
|
|
out.flush();
|
|
}
|
|
|
|
void save_session(std::string const &path, Session const &session) {
|
|
std::ofstream f(path);
|
|
f << session.uuid << " " << session.to_cme << " " << session.from_cme
|
|
<< std::endl;
|
|
}
|
|
|
|
Session load_session(jetfix::SessionName const &sn, std::string const &path) {
|
|
auto result = Session{sn.as_uint64_t, 0, 0};
|
|
if (boost::filesystem::exists(path)) {
|
|
std::ifstream f(path);
|
|
uint64_t dummy;
|
|
f >> dummy >> result.to_cme >> result.from_cme;
|
|
} else
|
|
save_session(path, result);
|
|
return result;
|
|
}
|
|
|
|
struct SessionStore::PImpl {
|
|
PImpl(IL3SecDB &db, jetfix::SessionName const &session_name,
|
|
config::JetFIX::Connection const *cfg, std::string const &base_path)
|
|
: cfg(cfg), store_path(base_store_path(base_path, cfg->name(),
|
|
session_name.as_string)),
|
|
replay_messages(load_replay(db, cfg, raw_msg_log_path(store_path))),
|
|
starting_session_descriptor(
|
|
load_session(session_name, session_path(store_path))),
|
|
raw_msg_store() {
|
|
raw_msg_store.open(raw_msg_log_path(store_path), std::ofstream::app);
|
|
}
|
|
|
|
const config::JetFIX::Connection *cfg;
|
|
const std::string store_path;
|
|
const std::vector<ExecutionReport> replay_messages;
|
|
const Session starting_session_descriptor;
|
|
std::atomic<bool> done = false;
|
|
tbb::concurrent_queue<Action> event_queue;
|
|
std::thread storage_thread;
|
|
std::ofstream raw_msg_store;
|
|
};
|
|
|
|
SessionStore::SessionStore(IL3SecDB &db,
|
|
jetfix::SessionName const &session_name,
|
|
config::JetFIX::Connection const *cfg,
|
|
std::string base_path)
|
|
: me(new PImpl(db, session_name, cfg, base_path)) {
|
|
me->storage_thread = threading::make_thread_from_function(
|
|
"il3_store", [this]() { do_work(); });
|
|
}
|
|
|
|
std::vector<ExecutionReport> const &SessionStore::replay() const {
|
|
return me->replay_messages;
|
|
}
|
|
|
|
Session SessionStore::starting_session_descriptor() const {
|
|
return me->starting_session_descriptor;
|
|
}
|
|
|
|
void SessionStore::async_update_session(Session const &s) {
|
|
me->event_queue.push(
|
|
Action{[this, s]() { save_session(session_path(me->store_path), s); }});
|
|
}
|
|
|
|
void SessionStore::async_store_raw_msg(RawMessageDirection d, size_t sn,
|
|
RawMsg const &ms) {
|
|
me->event_queue.push(Action{
|
|
[this, d, sn, ms]() { save_raw_msg(me->raw_msg_store, d, sn, ms()); }});
|
|
}
|
|
|
|
void SessionStore::do_work() {
|
|
while (!me->done) {
|
|
Action a;
|
|
if (me->event_queue.try_pop(a))
|
|
a();
|
|
else
|
|
std::this_thread::sleep_for(100ms);
|
|
}
|
|
}
|
|
|
|
SessionStore::~SessionStore() {
|
|
me->done = true;
|
|
me->storage_thread.join();
|
|
}
|
|
|
|
} // namespace ilink3
|
|
} // namespace orderman
|
|
} // namespace bts
|