Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (59 loc) · 2.09 KB
/
Copy pathmain.cpp
File metadata and controls
81 lines (59 loc) · 2.09 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
#include "adapter/eadapter.h"
#include <iostream>
#include <thread>
#include <csignal>
#include <cstdlib>
#include <string>
#include <atomic>
std::atomic<bool> adapterRunning{true};
std::unique_ptr<EAdapter> adapter;
void signal_handler(int) {
adapterRunning = false;
if (adapter) adapter->stop();
}
static ExchangeType parse_exchange(const std::string& name) {
if (name == "JUPITER" || name == "jupiter" || name == "2")
return ExchangeType::JUPITER;
return ExchangeType::BINANCE; // default
}
int main(int argc, char* argv[]) {
signal(SIGINT, signal_handler);
std::string exchange_name = "BINANCE";
if (argc >= 2) exchange_name = argv[1];
else if (const char* env = std::getenv("EXCHANGE")) exchange_name = env;
ExchangeType type = parse_exchange(exchange_name);
adapter = std::make_unique<EAdapter>(type);
if (type == ExchangeType::BINANCE)
adapter->set_symbols({"BTCUSDT"});
else
adapter->set_symbols({"BTC"});
std::thread adapterThread([&]() {
adapter->run();
});
std::cout << "[Broker] Running. Type 'BINANCE' or 'JUPITER' to switch.\n";
std::string line;
while (adapterRunning && std::getline(std::cin, line)) {
if (line == "BINANCE" || line == "JUPITER") {
ExchangeType newType = parse_exchange(line);
if (newType != adapter->get_exchange()) {
adapter->stop();
adapterThread.join();
adapter = std::make_unique<EAdapter>(newType);
if (newType == ExchangeType::BINANCE)
adapter->set_symbols({"BTCUSDT"});
else
adapter->set_symbols({"BTC"});
adapterThread = std::thread([&]() {
adapter->run();
});
std::cout << "[Broker] Switched to " << line << "\n";
}
} else if (line == "quit") {
break;
}
}
adapterRunning = false;
adapter->stop();
if (adapterThread.joinable()) adapterThread.join();
return 0;
}