-
Notifications
You must be signed in to change notification settings - Fork 2
/
StockMatcher.cpp
113 lines (97 loc) · 3.43 KB
/
StockMatcher.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
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
#include<iostream>
#include<string>
#include "StockMatcher.h"
using namespace std;
// The main StockMatcher class implementation.
// Constructor Implementation.
StockMatcher::StockMatcher(const std::string& symbol, const std::string& dataInputFileName, const outputStreamType streamType) : m_symbol(symbol)
{
// Attempt to initialize the input file and output log file.
std::string inputFileName = dataInputFileName;
m_inputFileStream.open(inputFileName, std::ifstream::in);
// Set here the output file stream type if cout or a file.
m_stockMatchEngine.setoutputStreamType(streamType);
// Set the stock symbol Name here.
m_stockMatchEngine.setStockSymbol(symbol);
// populate the Cache Stream here itself.
populateCacheStream();
}
// Copy constructor.
StockMatcher::StockMatcher(const StockMatcher& copyRef)
{
this->m_symbol = copyRef.m_symbol;
this->m_validator = copyRef.m_validator;
this->m_stockMatchEngine = copyRef.m_stockMatchEngine;
this->m_cachedStream = copyRef.m_cachedStream;
}
// Assignment operator.
StockMatcher& StockMatcher::operator=(const StockMatcher& assignRef)
{
this->m_symbol = assignRef.m_symbol;
this->m_validator = assignRef.m_validator;
this->m_stockMatchEngine = assignRef.m_stockMatchEngine;
this->m_cachedStream = assignRef.m_cachedStream;
return(*this);
}
void StockMatcher::populateCacheStream()
{
// This function tries to populate the cache stream from messages of the file and close the file handle.
// This is to avoid accessing ifstream in threaded mode since it is not safe and does not seem to work
// With even locks.
std::string textLine;
while(!m_inputFileStream.eof())
{
getline(m_inputFileStream, textLine);
if(textLine.length() > 0)
{
m_cachedStream.push_back(textLine);
}
}
m_inputFileStream.close();
}
// Implement the stock Matching algorithm call.
void StockMatcher::executeMatching()
{
m_stockMatchEngine.setOutPutStream();
std::string textLine;
long lineCount = 0;
auto cacheIter = m_cachedStream.begin();
while(cacheIter != m_cachedStream.end())
{
textLine = *cacheIter;
lineCount++;
if(textLine.length()>0)
{
std::tuple<std::pair<bool,std::string>,Order> message = m_validator.validateOrder(textLine);
std::pair<bool,std::string> messageStatus = std::get<0>(message);
bool status = messageStatus.first;
if(status)
{
Order order = std::get<1>(message);
if(order.getOrderType() == orderType::buyOrder)
{
m_stockMatchEngine.addIntoBuyQueue(order);
}
else
{
m_stockMatchEngine.addIntoSellQueue(order);
}
// Now run the stockMatching engine.
m_stockMatchEngine.runMatching();
}
else
{
m_stockMatchEngine.pushInBadOrder(textLine, messageStatus.second);
}
}
// There is a need every 10th Message to print the buy and sell queues. do that here.
if(lineCount%10 == 0)
m_stockMatchEngine.displayOrderBook();
cacheIter++;
}
// Display a final summary of any bad orders at the end of execution.
m_stockMatchEngine.writeToLog("Bad order summary : ");
m_stockMatchEngine.displayBadOrders();
m_stockMatchEngine.writeToLog("Final queue summary : ");
m_stockMatchEngine.displayOrderBook();
}