Refactored JetFIX

This commit is contained in:
Pedro Pinto committed 2017-08-25 15:25:00 -04:00
1 parent 394f3895da
commit c8bd96e9e0
10 files changed
+50 -43

No files matched your search

+1 -1
View File
@@ -30,7 +30,7 @@ set (Sparkd_Sources
jetfix/gateway/CoreGateway.cpp
jetfix/gateway/JetFIXGateway.cpp
jetfix/gateway/Session.cpp
jetfix/store/FileStore.cpp
jetfix/store/FIXFileStore.cpp
jetfix/store/session_name.cpp
jetfix/tests/async_reader_tests.cpp
jetfix/tests/decoding_tests.cpp
@@ -5,7 +5,7 @@
#include "server/sparkd/jetfix/connection/IConnection.h"
#include "server/sparkd/jetfix/gateway/ICoreGatewayObserver.h"
#include "server/sparkd/jetfix/gateway/Session.h"
#include "server/sparkd/jetfix/store/IStore.h"
#include "server/sparkd/jetfix/store/IFIXStore.h"
#include "lib/logging/logging.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/datetime.h"
@@ -16,6 +16,7 @@
#include <fstream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <moodycamel/concurrentqueue.h>
#include <fmt/format.h>
namespace bts {
namespace jetfix {
@@ -28,7 +29,7 @@ struct CoreGateway::SessionInfo
{}
std::unique_ptr<IConnection> connection;
std::unique_ptr<IStore> store;
std::unique_ptr<IFIXStore> store;
std::unique_ptr<AsyncReader> reader;
std::unique_ptr<Session> session;
};
@@ -197,6 +198,7 @@ void CoreGateway::dispatch_events ()
void CoreGateway::call_time ()
{
os::set_thread_name(fmt::format("{0}_timer",me->cfg.name()).c_str());
while (!me->destroy) {
std::this_thread::sleep_for(10ms);
auto ts = std::make_shared<std::string> (
@@ -12,14 +12,14 @@ namespace bts {
namespace jetfix {
class ICoreGatewayObserver;
class IConnection;
class IStore;
class IFIXStore;
class OutMsgsBuffer;
class CoreGateway : ICoreGatewayEventSink
{
public:
typedef std::function<IConnection* (config::JetFIX::Connection const &cfg)> ConnectionFactory;
typedef std::function<IStore* (config::JetFIX::Connection const &cfg)> StoreFactory;
typedef std::function<IFIXStore* (config::JetFIX::Connection const &cfg)> StoreFactory;
CoreGateway(config::JetFIX::Connection const &cfg,
ICoreGatewayObserver &observer,
@@ -4,7 +4,7 @@
#include "server/sparkd/jetfix/gateway/CoreGateway.h"
#include "server/sparkd/jetfix/gateway/ICoreGatewayObserver.h"
#include "server/sparkd/jetfix/connection/SocketConnection.h"
#include "server/sparkd/jetfix/store/FileStore.h"
#include "server/sparkd/jetfix/store/FIXFileStore.h"
#include "lib/utils/meta_programming.h"
#include "lib/config/protos/jetfix.pb.h"
@@ -25,7 +25,7 @@ public:
{return new SocketConnection (cfg);},
[] (config::JetFIX::Connection const &cfg)
{ return new FileStore (cfg);}),
{ return new FIXFileStore (cfg);}),
_dispatchers (),
_dispatcher_idx (),
_on_ready ([](){}),
+4 -4
View File
@@ -8,7 +8,7 @@
#include "server/sparkd/jetfix/common/OutFIX.h"
#include "server/sparkd/jetfix/connection/IConnection.h"
#include "server/sparkd/jetfix/gateway/ICoreGatewayEventSink.h"
#include "server/sparkd/jetfix/store/IStore.h"
#include "server/sparkd/jetfix/store/IFIXStore.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/datetime.h"
#include "lib/logging/logging.h"
@@ -46,7 +46,7 @@ struct Session::PImpl
{
PImpl(config::JetFIX::Connection const &cfg,
IConnection &connection,
IStore &store,
IFIXStore &store,
ICoreGatewayEventSink &sink,
Clock const &clock)
:
@@ -73,7 +73,7 @@ struct Session::PImpl
config::JetFIX::Connection cfg;
State state;
IConnection &connection;
IStore &store;
IFIXStore &store;
ICoreGatewayEventSink &event_sink;
Clock now;
uint32_t begin_of_this_session;
@@ -90,7 +90,7 @@ struct Session::PImpl
Session::Session(config::JetFIX::Connection const &cfg,
IConnection &connection,
IStore &store,
IFIXStore &store,
ICoreGatewayEventSink &sink,
Clock const &clock)
: me (new PImpl(cfg,connection,store,sink,clock))
+2 -2
View File
@@ -14,7 +14,7 @@ namespace bts {
namespace utils {class TimeStamp;}
namespace jetfix {
class IConnection;
class IStore;
class IFIXStore;
class ICoreGatewayEventSink;
class Session
@@ -24,7 +24,7 @@ public:
Session(config::JetFIX::Connection const &cfg,
IConnection &connection,
IStore &store,
IFIXStore &store,
ICoreGatewayEventSink &sink,
Clock const &clock);
~Session();
@@ -1,4 +1,4 @@
#include "FileStore.h"
#include "FIXFileStore.h"
#include "lib/config/config.h"
#include "server/sparkd/jetfix/codec/FIXEncodingStream.h"
@@ -16,11 +16,12 @@
#include <boost/filesystem.hpp>
#include <boost/date_time.hpp>
#include <moodycamel/readerwriterqueue.h>
#include <fmt/format.h>
namespace bts {
namespace jetfix {
struct FileStore::Event
struct FIXFileStore::Event
{
Event ()
: out_msgs (), in_msg (),
@@ -72,9 +73,9 @@ struct FileStore::Event
uint32_t last_msg_processed;
};
struct FileStore::PImpl
struct FIXFileStore::PImpl
{
PImpl() :
PImpl(config::JetFIX::Connection const&cfg) :
active (false),
store_called (false),
replay_messages (),
@@ -83,7 +84,8 @@ struct FileStore::PImpl
seq_num_path (),
fix_store (),
writer_thread (),
event_queue ()
event_queue (),
cfg(cfg)
{}
bool active;
@@ -95,6 +97,7 @@ struct FileStore::PImpl
std::ofstream fix_store;
std::thread writer_thread;
moodycamel::ReaderWriterQueue<Event> event_queue;
config::JetFIX::Connection const cfg;
};
std::string fix_log_path (std::string const&base_path)
@@ -194,7 +197,8 @@ void load_seq_nums (std::string const&path,uint32_t* last_seq_num, uint32_t *las
}
}
FileStore::FileStore (config::JetFIX::Connection const&cfg,std::string base_path) : me (new PImpl()) {
FIXFileStore::FIXFileStore (config::JetFIX::Connection const&cfg,std::string base_path)
: me (new PImpl(cfg)) {
auto sn = session_name (cfg);
me->active = sn.size () > 0;
if (me->active) {
@@ -225,11 +229,11 @@ FileStore::FileStore (config::JetFIX::Connection const&cfg,std::string base_path
me->fix_store.open (fix_log_p,std::ofstream::app);
create_current_simlink (path);
me->writer_thread = std::thread (std::bind (&FileStore::write,this));
me->writer_thread = std::thread (std::bind (&FIXFileStore::write,this));
}
}
FileStore::~FileStore ()
FIXFileStore::~FIXFileStore ()
{
if (me->active) {
Event ev;
@@ -239,28 +243,28 @@ FileStore::~FileStore ()
}
}
bool FileStore::session_active () const
bool FIXFileStore::session_active () const
{return me->active;}
std::vector<InMsgBuffer> FileStore::replay () const
std::vector<InMsgBuffer> FIXFileStore::replay () const
{
bts_assert (me->active && !me->store_called);
return me->replay_messages;
}
uint32_t FileStore::last_seq_num () const
uint32_t FIXFileStore::last_seq_num () const
{
bts_assert (me->active && !me->store_called);
return me->last_seq_num;
}
uint32_t FileStore::last_seq_num_processed () const
uint32_t FIXFileStore::last_seq_num_processed () const
{
bts_assert (me->active && !me->store_called);
return me->last_seq_num_processed;
}
void FileStore::store (OutFIXPtr const&msgs)
void FIXFileStore::store (OutFIXPtr const&msgs)
{
bts_assert (me->active);
me->store_called = true;
@@ -269,7 +273,7 @@ void FileStore::store (OutFIXPtr const&msgs)
me->event_queue.enqueue (ev);
}
void FileStore::store (InMsgBuffer const&msg)
void FIXFileStore::store (InMsgBuffer const&msg)
{
bts_assert (me->active);
me->store_called = true;
@@ -278,7 +282,7 @@ void FileStore::store (InMsgBuffer const&msg)
me->event_queue.enqueue (ev);
}
void FileStore::set_last_msg_processed (uint32_t last_msg_processed)
void FIXFileStore::set_last_msg_processed (uint32_t last_msg_processed)
{
bts_assert (me->active);
me->store_called = true;
@@ -288,9 +292,10 @@ void FileStore::set_last_msg_processed (uint32_t last_msg_processed)
me->event_queue.enqueue (ev);
}
void FileStore::write ()
void FIXFileStore::write ()
{
Event ev;
os::set_thread_name(fmt::format("{0}_store",me->cfg.name()).c_str());
while (true)
if (me->event_queue.try_dequeue (ev)) {
@@ -1,17 +1,17 @@
#pragma once
#include <memory>
#include "server/sparkd/jetfix/store/IStore.h"
#include "server/sparkd/jetfix/store/IFIXStore.h"
#include "lib/config/protos/jetfix.pb.h"
namespace bts {
namespace jetfix {
class FileStore : public IStore
class FIXFileStore : public IFIXStore
{
public:
FileStore(config::JetFIX::Connection const&cfg,std::string base_path="");
~FileStore();
FIXFileStore(config::JetFIX::Connection const&cfg,std::string base_path="");
~FIXFileStore();
bool session_active () const override;
std::vector<InMsgBuffer> replay () const override;
@@ -9,10 +9,10 @@ namespace bts {
namespace jetfix {
class InMsgBuffer;
class IStore
class IFIXStore
{
public:
virtual ~IStore() {};
virtual ~IFIXStore() {};
virtual bool session_active () const = 0;
virtual std::vector<InMsgBuffer> replay () const = 0;
@@ -4,7 +4,7 @@
#include "server/sparkd/jetfix/common/InMsgBuffer.h"
#include "server/sparkd/jetfix/common/OutFIX.h"
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
#include "server/sparkd/jetfix/store/FileStore.h"
#include "server/sparkd/jetfix/store/FIXFileStore.h"
#include "server/sparkd/jetfix/store/session_name.h"
#include "lib/utils/TempDir.h"
#include "lib/utils/TimeStamp.h"
@@ -149,7 +149,7 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]")
utils::TempDir td;
FileStore fs (c,td.name ());
FIXFileStore fs (c,td.name ());
REQUIRE (fs.session_active ());
REQUIRE (fs.replay ().size ()==0);
REQUIRE (fs.last_seq_num ()==0);
@@ -182,12 +182,12 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]")
auto fix = stream.to_str ();
{
FileStore fs (c,td->name ());
FIXFileStore fs (c,td->name ());
fs.store (InMsgBuffer (fix));
fs.set_last_msg_processed (3);
}
FileStore fs (c,td->name ());
FIXFileStore fs (c,td->name ());
auto replay = fs.replay ();
REQUIRE (replay.size ()==1);
REQUIRE (replay[0].type ()==MessageTypes::Logon);
@@ -222,11 +222,11 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]")
stream.stamp (2,3,datetime::FIX_timestamp (datetime::now ()));
{
FileStore fs (c,td->name ());
FIXFileStore fs (c,td->name ());
fs.store (msg_vec);
}
FileStore fs (c,td->name ());
FIXFileStore fs (c,td->name ());
auto replay = fs.replay ();
REQUIRE (replay.size ()==0);
REQUIRE (fs.last_seq_num ()==3);
@@ -248,7 +248,7 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]")
auto start = utils::TimeStamp::now ();
{
FileStore fs (c,td->name ());
FIXFileStore fs (c,td->name ());
for (int i = 0; i < 10000;i++) {
auto msg_vec = std::make_shared<std::vector<FIXEncodingStream>>();
msg_vec->emplace_back (c);
@@ -269,7 +269,7 @@ TEST_CASE ("JetFIX FileStoreTests", "[jetfix]")
std::cout << "\tjetfix store gen: " << (stop-start).milliseconds () << "ms\n";
start = utils::TimeStamp::now ();
FileStore fs (c,td->name ());
FIXFileStore fs (c,td->name ());
stop = utils::TimeStamp::now ();
std::cout << "\tjetfix store load: " << (stop-start).milliseconds () << "ms\n";
}