forked from quotekio/cpp-lsclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.cpp
66 lines (50 loc) · 1.85 KB
/
sample.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
#include "src/lsclient.hpp"
#include <iostream>
#include <string>
void usage(char* prgname) {
cout << "Usage: " << prgname << " <username> <password> <lightstreamer_url>" << endl;
}
int main(int argc, char** argv) {
if ( argc != 4) {
usage(argv[0]);
exit(1);
}
std::string ls_username = std::string(argv[1]);
std::string ls_password = std::string(argv[2]);
std::string ls_url = std::string(argv[3]);
//instanciates new LSClient object with connection parameters
LSClient* lsc = new LSClient(ls_url, ls_username, ls_password);
//Adds a new subscription with 2 items to the client.
lsc->addSubscription(new LSSubscription("MARKET",
{"MARKET:IX.D.CAC.IMF.IP",
"MARKET:IX.D.DAX.IMF.IP"},
{"BID","OFFER"}));
cout << "[LSCLIENT] Connecting to LightStreamer.." ;
//starts stream connection thread
lsc->start();
//waits until LSClient stream is connected.
while ( lsc->getStatus() != LS_STATUS_CONNECTED ) {
cout << "." ;
sleep(1);
}
cout << "[OK]" << endl;
//Sends added subscriptions to Lightstreamer control connection.
lsc->subscribeAll();
//fetches data
while(1) {
std::vector<LSSubscription*>* subscriptions = lsc->getSubscriptions();
for (int i=0;i< subscriptions->size();i++ ) {
//LSTables are the objects containing the LS Data
LSTable* t = subscriptions->at(i)->table_ref;
for (int j=0;j< subscriptions->at(i)->getObjectIds().size(); j++ ) {
vector<string>* values = t->getItemData(j);
if (values->size() > 1 ) {
cout << "ITEM:" << subscriptions->at(i)->getObjectIds()[j] << endl;
cout << "BID:" << values->at(0) << endl;
cout << "OFFER:" << values->at(1) << endl;
}
}
}
sleep(1);
}
}