-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulkhandler.h
175 lines (154 loc) · 4.09 KB
/
bulkhandler.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#ifndef BULKHANDLER_H
#define BULKHANDLER_H
#include <cstddef>
#include <iostream>
#include "config.h"
#include <list>
#include <sstream>
#include <functional>
#include <ctime>
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <algorithm>
#include <mutex>
#include <condition_variable>
class Parser
{
enum class ParsingState
{
TopLevel = 0,
InBlock = 1
};
public:
Parser (int bulkSize)
: m_bulkSize(bulkSize) { }
~Parser()
{
publish();
}
void receive(const char *data, std::size_t size)
{
std::string fullData(data, size);
std::istringstream fdStream(fullData);
while (std::getline(fdStream, m_line))
{
if (m_line.size() == 0)
continue;
switch (m_state)
{
case ParsingState::TopLevel:
{
if (m_line != "{")
{
m_commands.push_back(m_line);
if (m_commands.size() == m_bulkSize)
publish();
break;
}
else
{
m_depthCounter++;
publish();
m_state = ParsingState::InBlock;
break;
}
}
case ParsingState::InBlock:
{
if (m_line != "}")
{
if (m_line == "{")
m_depthCounter++;
else
m_commands.push_back(m_line);
}
else
{
m_depthCounter--;
if (m_depthCounter == 0)
{
publish();
m_state = ParsingState::TopLevel;
}
}
break;
}
default:
break;
}
}
}
void subscribe(const std::function<void(std::list<std::string>)>& callback)
{
m_subscribers.push_back(callback);
}
void publish()
{
if (m_commands.size() == 0)
return;
for (const auto& subscriber : m_subscribers)
{
subscriber(m_commands);
}
m_commands.clear();
}
private:
std::list<std::function<void(std::list<std::string>)> > m_subscribers;
int m_bulkSize;
//store data between launches
ParsingState m_state = ParsingState::TopLevel;
std::list<std::string> m_commands;
int m_depthCounter = 0;
std::string m_line;
};
class Executor
{
public:
void execute(std::list<std::string> commands)
{
std::cout << "bulk:";
for (auto command : commands)
std::cout << command << " ";
std::cout << std::endl;
}
};
class LogWriter
{
public:
LogWriter() { }
~LogWriter() { }
void write(std::list<std::string> commands)
{
static int conflictResolverCounter = 0;
static std::mutex conflictMutex;
std::ofstream logFile;
std::time_t result = std::time(nullptr);
char buff[FILENAME_MAX];
getcwd(buff, FILENAME_MAX );
std::string current_working_dir(buff);
{
std::lock_guard<std::mutex> lk(conflictMutex);
logFile.open (std::string(current_working_dir + "/bulk" + std::to_string(result) + "_" + std::to_string(conflictResolverCounter) + ".log"));
std::cout << std::string(current_working_dir + "/bulk" + std::to_string(result) + "_" + std::to_string(conflictResolverCounter) + ".log") << std::endl;
conflictResolverCounter++;
}
logFile << "bulk:";
for (auto command : commands)
logFile << command << " ";
logFile << std::endl;
logFile.close();
}
};
class BulkHandler
{
public:
BulkHandler(std::size_t bulk);
~BulkHandler();
void receive(BulkHandler *handle, const char *data, std::size_t size);
private:
Parser m_parser;
Executor m_executor;
LogWriter m_logWritter;
};
#endif // BULKHANDLER_H