-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLinkStreamHandler.cpp
More file actions
118 lines (98 loc) · 4.44 KB
/
MLinkStreamHandler.cpp
File metadata and controls
118 lines (98 loc) · 4.44 KB
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
//
// Created by Anders Cedronius on 2025-07-11.
//
#include "MLinkStreamHandler.h"
int MLinkStreamHandler::startStream(const std::string& rSpiderRockKey,
const std::string& rClause,
const std::function<void(std::unique_ptr<spiderrock::protobuf::api::Observer::CrossTradeInfo>)>& rCrossTradeCallback,
const bool aDumpPrintMessage,
MLinkStreamHandler::StreamType aStreamType
) {
//Start of Spiderrocks example code from their Github-repo
//Got no clue about the first 'rest session' part. it's original fails added port 443 then at least the code continues
//Start of the web socket part (Connect)
mWebSocketSession = std::make_unique<spiderrock::mlink::MLinkSession>(mContext);
bool result = mWebSocketSession->connect("wss://mlink-live.nms.saturn.spiderrockconnect.com/mlink/proto", rSpiderRockKey);
if (!result) {
result = mWebSocketSession->connect("ws://mlink.saturn.srplatform.net:6700/mlink/proto", rSpiderRockKey);
if (!result) {
std::cout << mWebSocketSession->getLastError() << std::endl;
exit(-1);
}
}
//generate the handlers
mSubscriptionManager = std::make_unique<spiderrock::mlink::SubscriptionManager<>>(*mWebSocketSession);
//This callback traverses the incoming data
//the data is feed the observer got callbacks for stockprint ending up in the method 'onCrossTrade' above
mSubscriptionManager->start([&](char *buffer, size_t bytesRead) {
if (buffer[0] == '\r' && buffer[1] == '\n') {
if (buffer[2] == 'P') {
mObserver.process_buffer(buffer, static_cast<int>(bytesRead));
} else {
buffer[bytesRead] = 0;
std::cout << buffer << std::endl;
}
} else {
buffer[bytesRead] = 0;
std::cout << buffer << std::endl;
}
}
);
uint64_t lStreamState = 0;
std::atomic<bool> lStreamCompleted= false;
//The below handler is making sure a stream request passes, begin, active, complete before initiating a new stream request.
mObserver.mMLinkStreamCheckPtCallback = [&lStreamState, &lStreamCompleted](std::unique_ptr<spiderrock::protobuf::api::Observer::StreamData> pStrmData) {
if (pStrmData->mResult == spiderrock::protobuf::api::MLinkStreamState::MLINKSTREAMSTATE_BEGIN) {
if (lStreamState != 0) {
throw std::runtime_error("Stream start failed");
}
lStreamState = 1;
} else if (pStrmData->mResult == spiderrock::protobuf::api::MLinkStreamState::MLINKSTREAMSTATE_ACTIVE) {
if (lStreamState != 1) {
throw std::runtime_error("Stream active failed");
}
lStreamState = 2;
}else if (pStrmData->mResult == spiderrock::protobuf::api::MLinkStreamState::MLINKSTREAMSTATE_COMPLETE) {
if (lStreamState != 2) {
throw std::runtime_error("Stream complete failed");
}
lStreamState = 0;
lStreamCompleted = true;
} else {
std::cout << "Unknown stream state" << std::endl;
lStreamState = 0;
lStreamCompleted = false;
}
};
//get all open and close crosses in the above function: onCrossTrade
mObserver.mCrossTradeCallback = rCrossTradeCallback;
mObserver.mDumpPrints = aDumpPrintMessage;
//Set up the stream basics
int activeLatency = 1;
std::string messageType;
if (aStreamType == StreamType::StockPrint) {
messageType = "StockPrint";
} else if (aStreamType == StreamType::StockPrintSet) {
messageType = "StockPrintSet";
} else {
throw std::runtime_error("Unknown stream type");
}
std::string queryString = messageType + "_Query";
spiderrock::protobuf::api::MLinkStream mlinkStream;
mlinkStream.set_active_latency(activeLatency);
mlinkStream.set_msg_name(messageType);
mlinkStream.set_query_label(queryString);
if (!rClause.empty()) {
mlinkStream.set_where_clause(rClause);
}
mSubscriptionManager->stream(mlinkStream);
uint64_t lTimeout = 5000; //5 seconds timeout
while (!lStreamCompleted) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (!lTimeout--) {
std::cout << "Subscribe string: " << rClause << std::endl;
throw std::runtime_error("Timeout waiting for stream response");
}
}
return 0;
}