forked from antoinewaugh/cme-sbe-latency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectivity_plugin.cpp
153 lines (120 loc) · 4.83 KB
/
connectivity_plugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "config.h"
#include "DepthBook.h"
#include "Trade.h"
#include "SessionStatistics.h"
#include "SecurityStatus.h"
#include <sag_connectivity_plugins.hpp>
#include <thread>
#include "boost/asio.hpp"
#include "ChannelImpl.h"
#include <chrono>
using namespace sp::lltp::cme;
using namespace com::softwareag::connectivity;
timing timings = {0,0,0,0};
namespace sp {
namespace atp {
namespace cme {
using sp::lltp::cme::SecurityEvent;
using sp::lltp::cme::SecurityStatus ;
using sp::lltp::cme::SessionStatistics;
class HandlerImpl: public MarketDataListener {
public:
HandlerImpl(HostSide::ptr_t* host):host(host) {}
HandlerImpl() = default;
void OnQuote(uint64_t securityid,DepthBook const& book);
void OnTrade(uint64_t securityid, Trade& trade);
void OnStatus(uint64_t securityid, SecurityStatus status, SecurityEvent event);
void OnStatistics(uint64_t securityid, SessionStatistics &statistics);
private:
void Send(DepthBook const& book);
HostSide::ptr_t* host;
};
// void Subscribe(HostSide::ptr_t* hostSide) {
// map_t payload;
// payload.insert(data_t("symbol"), data_t("ESH8"));
//
// list_t bidp, bidv, askp, askv;
//
// payload[data_t("bid_prices")] = data_t(std::move(bidp));
// payload[data_t("bid_volumes")] = data_t(std::move(bidv));
// payload[data_t("ask_prices")] = data_t(std::move(askp));
// payload[data_t("ask_volumes")] = data_t(std::move(askv));
//
// com::softwareag::connectivity::Message msg(data_t(std::move(payload)));
// msg.putMetadataValue(com::softwareag::connectivity::Message::HOST_MESSAGE_TYPE(), "com.apama.marketdata.Depth");
// (*hostSide)->sendBatchTowardsHost(&msg, (&msg)+1);
//
// }
void Subscribe(MarketDataListener* handler, boost::asio::io_service *io_service) {
auto listen_address = boost::asio::ip::address::from_string("0.0.0.0");
auto configs = Config::load("config.xml");
auto channel = ChannelImpl::make_channel("310", configs, *io_service, listen_address);
channel->RegisterMarketDataListener(handler);
channel->Subscribe("ESH8");
io_service->run();
}
class CMEConnectivityPlugin: public AbstractTransport {
public:
CMEConnectivityPlugin(const TransportConstructorParameters& params)
: AbstractTransport(params)
{
}
virtual void sendBatchTowardsTransport(com::softwareag::connectivity::Message* start, com::softwareag::connectivity::Message* end) {}
virtual void start() { logger.info("CME SBE plugin started."); }
virtual void shutdown() { io_service.stop(); logger.info("CME SBE plugin shutting down."); }
virtual void hostReady() {
handler = HandlerImpl(&hostSide);
std::thread t(Subscribe, &handler, &io_service);
// std::thread t(Subscribe, &hostSide);
t.detach();
logger.info("HostReady received.");
}
private:
HandlerImpl handler;
boost::asio::io_service io_service;
};
void HandlerImpl::OnQuote(uint64_t securityid, DepthBook const &book) {
Send(book);
}
void HandlerImpl::Send(DepthBook const &book) {
timings.presend_ts = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
map_t payload;
payload.insert(data_t("symbol"), data_t("ESH8"));
list_t bidp, bidv, askp, askv;
auto bids = book.Bids();
for(int i = 0; i < bids.Size(); ++i) {
auto level = bids.Get(i);
bidp.push_back(data_t(level.price));
bidv.push_back(data_t(static_cast<int64_t>(level.quantity)));
}
auto asks = book.Asks();
for(int i = 0; i < asks.Size(); ++i) {
auto level = asks.Get(i);
askp.push_back(data_t(level.price));
askv.push_back(data_t(static_cast<int64_t>(level.quantity)));
}
payload[data_t("bid_prices")] = data_t(std::move(bidp));
payload[data_t("bid_volumes")] = data_t(std::move(bidv));
payload[data_t("ask_prices")] = data_t(std::move(askp));
payload[data_t("ask_volumes")] = data_t(std::move(askv));
list_t timestamps;
timings.send_ts = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
timestamps.push_back(data_t(timings.rec_ts));
timestamps.push_back(data_t(timings.dec_ts));
timestamps.push_back(data_t(timings.presend_ts));
timestamps.push_back(data_t(timings.send_ts));
payload[data_t("timestamps")] = data_t(std::move(timestamps));
com::softwareag::connectivity::Message msg(data_t(std::move(payload)));
msg.putMetadataValue(com::softwareag::connectivity::Message::HOST_MESSAGE_TYPE(), "com.apama.marketdata.Depth");
(*host)->sendBatchTowardsHost(&msg, (&msg) + 1);
}
void HandlerImpl::OnTrade(uint64_t securityid, Trade &trade) {
}
void HandlerImpl::OnStatus(uint64_t securityid, SecurityStatus status, SecurityEvent event) {
}
void HandlerImpl::OnStatistics(uint64_t securityid, SessionStatistics &statistics) {
}
SAG_DECLARE_CONNECTIVITY_TRANSPORT_CLASS(CMEConnectivityPlugin);
}}} // namespace