-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (97 loc) · 3.62 KB
/
Copy pathmain.cpp
File metadata and controls
114 lines (97 loc) · 3.62 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
#include "Order.h"
#include "OrderBook.h"
#include "MatchingEngine.h"
#include "ThreadSafeQueue.h"
#include "OrderProducer.h"
#include "EngineWorker.h"
#include "ConsoleRenderer.h"
#include <iostream>
#include <thread>
#include <csignal>
#include <atomic>
#include <string>
// Global flag for graceful shutdown
std::atomic<bool> g_shutdownRequested(false);
void signalHandler(int signum) {
std::cout << "\nShutdown signal received (" << signum << "). Exiting gracefully..." << std::endl;
g_shutdownRequested = true;
}
void printUsage(const char* programName) {
std::cout << "Usage: " << programName << " [--mode=<random|stdin>]" << std::endl;
std::cout << " --mode=random : Generate random orders automatically (default)" << std::endl;
std::cout << " --mode=stdin : Read orders from standard input" << std::endl;
std::cout << std::endl;
std::cout << "Stdin format: <BUY|SELL> <price> <quantity>" << std::endl;
std::cout << "Example: BUY 100.50 1000" << std::endl;
}
int main(int argc, char *argv[]) {
// Parse command-line arguments
ProducerMode mode = ProducerMode::Random;
for (int i = 1; i < argc; ++i) {
std::string arg(argv[i]);
if (arg == "--help" || arg == "-h") {
printUsage(argv[0]);
return 0;
} else if (arg.substr(0, 7) == "--mode=") {
std::string modeStr = arg.substr(7);
if (modeStr == "random") {
mode = ProducerMode::Random;
} else if (modeStr == "stdin") {
mode = ProducerMode::Stdin;
} else {
std::cerr << "Invalid mode: " << modeStr << std::endl;
printUsage(argv[0]);
return 1;
}
} else {
std::cerr << "Unknown argument: " << arg << std::endl;
printUsage(argv[0]);
return 1;
}
}
// Set up signal handler for graceful shutdown (Ctrl+C)
std::signal(SIGINT, signalHandler);
std::signal(SIGTERM, signalHandler);
std::cout << "Starting Limit Order Book Matching Engine..." << std::endl;
std::cout << "Mode: " << (mode == ProducerMode::Random ? "Random" : "Stdin") << std::endl;
std::cout << "Press Ctrl+C to exit" << std::endl;
std::cout << std::endl;
// Wait a moment before starting
std::this_thread::sleep_for(std::chrono::seconds(2));
// Create core components
ThreadSafeQueue<Order> orderQueue;
OrderBook orderBook;
MatchingEngine matchingEngine(orderBook);
// Create worker objects
OrderProducer producer(orderQueue, mode);
EngineWorker engineWorker(orderQueue, matchingEngine);
ConsoleRenderer renderer(orderBook, matchingEngine, orderQueue);
// Launch threads
std::thread producerThread([&producer]() { producer.run(); });
std::thread engineThread([&engineWorker]() { engineWorker.run(); });
std::thread rendererThread([&renderer]() { renderer.run(); });
// Wait for shutdown signal
while (!g_shutdownRequested) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Graceful shutdown
std::cout << "Shutting down threads..." << std::endl;
producer.stop();
engineWorker.stop();
renderer.stop();
// Push a dummy order to unblock the engine thread
Order dummyOrder(0, OrderSide::Buy, 0.0, 0);
orderQueue.push(dummyOrder);
// Join threads
if (producerThread.joinable()) {
producerThread.join();
}
if (engineThread.joinable()) {
engineThread.join();
}
if (rendererThread.joinable()) {
rendererThread.join();
}
std::cout << "Shutdown complete." << std::endl;
return 0;
}