-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathccapi_subscription.h
More file actions
196 lines (163 loc) · 8.34 KB
/
ccapi_subscription.h
File metadata and controls
196 lines (163 loc) · 8.34 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef INCLUDE_CCAPI_CPP_CCAPI_SUBSCRIPTION_H_
#define INCLUDE_CCAPI_CPP_CCAPI_SUBSCRIPTION_H_
#include <set>
#include <string>
#include "ccapi_cpp/ccapi_logger.h"
#include "ccapi_cpp/ccapi_macro.h"
#include "ccapi_cpp/ccapi_util_private.h"
namespace ccapi {
/**
* A single subscription. A 'Subscription' is used when calling 'Session::subscribe()' or 'Session::subscribe'. Subscription objects are created using
* Subscription constructors. A correlation id can be used as the unique identifier to tag all data associated with this subscription.
*/
class Subscription {
public:
explicit Subscription(const std::string& exchange = "", const std::string& instrument = "", const std::string& field = "", const std::string& options = "",
const std::string& correlationId = "", const std::map<std::string, std::string>& credential = {}, const std::string& proxyUrl = "")
: exchange(exchange), instrument(instrument), field(field), correlationId(correlationId), credential(credential), proxyUrl(proxyUrl) {
auto originalInstrumentSet = UtilString::splitToSet(instrument, ",");
std::copy_if(originalInstrumentSet.begin(), originalInstrumentSet.end(), std::inserter(this->instrumentSet, this->instrumentSet.end()),
[](const std::string& value) { return !value.empty(); });
auto originalFieldSet = UtilString::splitToSet(field, ",");
std::copy_if(originalFieldSet.begin(), originalFieldSet.end(), std::inserter(this->fieldSet, this->fieldSet.end()),
[](const std::string& value) { return !value.empty(); });
if (field == CCAPI_GENERIC_PUBLIC_SUBSCRIPTION) {
this->rawOptions = options;
} else {
std::vector<std::string> optionList;
if (!options.empty()) {
optionList = UtilString::split(options, "&");
}
this->optionMap[CCAPI_MARKET_DEPTH_MAX] = CCAPI_MARKET_DEPTH_MAX_DEFAULT;
this->optionMap[CCAPI_CONFLATE_INTERVAL_MILLISECONDS] = CCAPI_CONFLATE_INTERVAL_MILLISECONDS_DEFAULT;
this->optionMap[CCAPI_CONFLATE_GRACE_PERIOD_MILLISECONDS] = CCAPI_CONFLATE_GRACE_PERIOD_MILLISECONDS_DEFAULT;
this->optionMap[CCAPI_MARKET_DEPTH_RETURN_UPDATE] = CCAPI_MARKET_DEPTH_RETURN_UPDATE_DEFAULT;
this->optionMap[CCAPI_FETCH_MARKET_DEPTH_INITIAL_SNAPSHOT_DELAY_MILLISECONDS] = CCAPI_FETCH_MARKET_DEPTH_INITIAL_SNAPSHOT_DELAY_MILLISECONDS_DEFAULT;
this->optionMap[CCAPI_CANDLESTICK_INTERVAL_SECONDS] = CCAPI_CANDLESTICK_INTERVAL_SECONDS_DEFAULT;
for (const auto& option : optionList) {
auto optionKeyValue = UtilString::split(option, "=");
this->optionMap[optionKeyValue.at(0)] = optionKeyValue.at(1);
}
}
std::set<std::string> executionManagementSubscriptionFieldSet = {std::string(CCAPI_EM_ORDER_UPDATE), std::string(CCAPI_EM_PRIVATE_TRADE),
std::string(CCAPI_EM_PRIVATE_TRADE_LITE), std::string(CCAPI_EM_BALANCE_UPDATE),
std::string(CCAPI_EM_POSITION_UPDATE), std::string(CCAPI_EM_WEBSOCKET_ORDER_ENTRY)};
if (field == CCAPI_GENERIC_PUBLIC_SUBSCRIPTION) {
this->serviceName = CCAPI_MARKET_DATA;
} else if (field == CCAPI_FIX || field == CCAPI_FIX_MARKET_DATA) {
this->serviceName = CCAPI_FIX;
} else if (std::includes(executionManagementSubscriptionFieldSet.begin(), executionManagementSubscriptionFieldSet.end(), this->fieldSet.begin(),
this->fieldSet.end())) {
this->serviceName = CCAPI_EXECUTION_MANAGEMENT;
} else if (field == CCAPI_MARKET_DEPTH || field == CCAPI_TRADE || field == CCAPI_AGG_TRADE || field == CCAPI_CANDLESTICK) {
this->serviceName = CCAPI_MARKET_DATA;
}
CCAPI_LOGGER_TRACE("this->serviceName = " + this->serviceName);
if (this->correlationId.empty()) {
this->correlationId = UtilString::generateRandomString(CCAPI_CORRELATION_ID_GENERATED_LENGTH);
}
}
std::string toString() const {
std::map<std::string, std::string> shortCredential;
for (const auto& x : credential) {
shortCredential.insert(std::make_pair(x.first, UtilString::firstNCharacter(x.second, CCAPI_CREDENTIAL_DISPLAY_LENGTH)));
}
std::string output = "Subscription [exchange = " + exchange + ", marginType = " + marginType + ", instrumentType = " + instrumentType +
", instrument = " + instrument + ", field = " + field + ", optionMap = " + ccapi::toString(optionMap) +
", correlationId = " + correlationId + ", credential = " + ccapi::toString(shortCredential) + ", proxyUrl = " + proxyUrl +
", serviceName = " + serviceName + ", timeSent = " + UtilTime::getISOTimestamp(timeSent) + "]";
return output;
}
const std::string& getCorrelationId() const { return correlationId; }
const std::string& getExchange() const { return exchange; }
const std::string& getInstrument() const { return instrument; }
const std::string& getInstrumentType() const { return instrumentType; }
const std::string& getField() const { return field; }
const std::string& getRawOptions() const { return rawOptions; }
const std::map<std::string, std::string>& getOptionMap() const { return optionMap; }
const std::map<std::string, std::string>& getCredential() const { return credential; }
const std::string& getProxyUrl() const { return proxyUrl; }
const std::string& getServiceName() const { return serviceName; }
const std::set<std::string>& getInstrumentSet() const { return instrumentSet; }
const std::set<std::string>& getFieldSet() const { return fieldSet; }
const std::string getSerializedOptions() const {
std::string output;
if (this->rawOptions.empty()) {
int i = 0;
for (const auto& option : this->optionMap) {
output += option.first;
output += "=";
output += option.second;
if (i < this->optionMap.size() - 1) {
output += "&";
}
++i;
}
} else {
output = this->rawOptions;
}
return output;
}
const std::string getSerializedCredential() const { return ccapi::toString(this->credential); }
// 'getTimeSent' only works in C++. For other languages, please use 'getTimeSentISO'.
TimePoint getTimeSent() const { return timeSent; }
std::string getTimeSentISO() const { return UtilTime::getISOTimestamp(timeSent); }
std::pair<long long, long long> getTimeSentPair() const { return UtilTime::divide(timeSent); }
const std::string& getMarginType() const { return marginType; }
void setExchange(const std::string& exchange) { this->exchange = exchange; }
void setInstrument(const std::string& instrument) { this->instrument = instrument; }
void setField(const std::string& field) { this->field = field; }
void setProxyUrl(const std::string& proxyUrl) { this->proxyUrl = proxyUrl; }
void setTimeSent(TimePoint timeSent) { this->timeSent = timeSent; }
void setInstrumentType(const std::string& instrumentType) { this->instrumentType = instrumentType; }
void setMarginType(const std::string& marginType) { this->marginType = marginType; }
enum class Status {
UNKNOWN,
SUBSCRIBING,
SUBSCRIBED,
UNSUBSCRIBING,
UNSUBSCRIBED,
};
static std::string statusToString(Status status) {
std::string output;
switch (status) {
case Status::UNKNOWN:
output = "UNKNOWN";
break;
case Status::SUBSCRIBING:
output = "SUBSCRIBING";
break;
case Status::SUBSCRIBED:
output = "SUBSCRIBED";
break;
case Status::UNSUBSCRIBING:
output = "UNSUBSCRIBING";
break;
case Status::UNSUBSCRIBED:
output = "UNSUBSCRIBED";
break;
default:
CCAPI_LOGGER_FATAL(std::string(CCAPI_UNSUPPORTED_VALUE) + " " + std::to_string(static_cast<int>(status)));
}
return output;
}
#ifndef CCAPI_EXPOSE_INTERNAL
private:
#endif
std::string exchange;
std::string marginType;
std::string instrumentType;
std::string instrument;
std::string field;
std::string rawOptions;
std::map<std::string, std::string> optionMap;
std::string correlationId;
std::map<std::string, std::string> credential;
std::string proxyUrl;
std::string serviceName;
std::set<std::string> instrumentSet;
std::set<std::string> fieldSet;
TimePoint timeSent{std::chrono::seconds{0}};
};
} /* namespace ccapi */
#endif // INCLUDE_CCAPI_CPP_CCAPI_SUBSCRIPTION_H_