Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (68 loc) · 2.45 KB
/
Copy pathmain.cpp
File metadata and controls
78 lines (68 loc) · 2.45 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
#include "KeychainHelper.hpp"
#include "Logger.hpp"
#include "LogWebServer.hpp"
#include "TwelveWSClient.hpp"
#include <iostream>
#include <thread>
#include <vector>
#include <csignal>
#include <atomic>
/**
* @brief Entry point of the Twelve Data WebSocket client example.
*
* Demonstrates dynamic subscription management using 8 WS credits.
*/
int main(int argc, char** argv) {
static std::atomic<bool> interrupted{false};
auto signalHandler = [](int) {
interrupted = true;
};
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);
Logger::instance().setLogFile("twelve_ws_client.log");
LogWebServer logServer(8080, 9002, "web");
const bool serverStarted = logServer.start();
if (serverStarted) {
Logger::instance().setBroadcastCallback([&logServer](const std::string& line) {
logServer.broadcast(line);
});
Logger::instance().info("Log server available at http://localhost:8080 (WebSocket ws://localhost:9002)");
} else {
Logger::instance().warn("Failed to start log server; continuing without web dashboard.");
}
const std::string API_KEY = getApiKeyFromKeychain("TwelveDataService", "TwelveDataAccount");
if (API_KEY.empty()) {
Logger::instance().error("Failed to load API key from keychain (service=TwelveDataService, account=TwelveDataAccount).");
return 1;
}
TwelveWSClient client(API_KEY);
// Subscribe to requested symbols
std::vector<std::string> symbols = {"AAPL", "TRP", "QQQ", "EUR/USD", "BTC/USD"};
if (argc > 1) {
symbols.clear();
for (int i = 1; i < argc; ++i) {
symbols.push_back(argv[i]);
}
}
for (const auto& s : symbols)
client.addSymbol(s);
std::string symbolList;
for (size_t i = 0; i < symbols.size(); ++i) {
symbolList += symbols[i];
if (i + 1 < symbols.size()) symbolList += ", ";
}
Logger::instance().info("Starting Twelve Data WebSocket client (symbols=" + symbolList + ")...");
client.start();
Logger::instance().info("Streaming... Press Ctrl+C or ENTER to stop.");
while (!interrupted) {
if (std::cin.rdbuf()->in_avail() > 0) break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
client.stop();
Logger::instance().info("Client stopped.");
Logger::instance().setBroadcastCallback(nullptr);
if (serverStarted) {
logServer.stop();
}
return 0;
}