-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathManager.cpp
More file actions
263 lines (230 loc) · 5.92 KB
/
Manager.cpp
File metadata and controls
263 lines (230 loc) · 5.92 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "Manager.h"
#include "Spider.h"
void Manager::FireCommand(std::string in)
{
m_working = true;
std::stringstream ss;
ss << in.data();
std::string temp;
std::getline(ss, temp, ' ');
if (m_CommandList.find(temp) == m_CommandList.end())
{
std::cerr << "err, " << temp << " is not a valid command\n"; //hmm Manager::instance().RegisterCommand("err", [=](std::string err) { display->WriteOut(err); });
}
else
{
if (temp == "connect") // ultra stupid, instead of looking for specific commands, perhaps a better use would be to have an Event class which held templated data, and an id with enum/guid.
{
Logger::log.SetLog();
std::string temp2;
std::getline(ss, temp2, ' ');
m_CommandList[temp](static_cast<void*>(&temp2));
}
else
{
m_CommandList[temp](nullptr);
}
}
ss.clear();
m_working = false;
}
std::vector<std::string> Manager::ListCommands(const std::string & in)
{
std::vector<std::string> vec;
auto it = m_CommandList.lower_bound(in);
if (it == m_CommandList.end()) { return vec; }
for (; it != m_CommandList.end(); ++it)
{
auto res = std::mismatch(in.begin(), in.end(), it->first.begin(), it->first.end());
if (res.first == in.end())
{
vec.push_back(it->first);
}
else
{
break;
}
}
return vec;
}
void Manager::ReadConfig()
{
Config = std::make_unique<Settings>();
std::ifstream file("Settings.json", std::ifstream::in);
if (file.is_open())
{
std::string s;
file.seekg(0, std::ios::end);
s.reserve(file.tellg());
file.seekg(0, std::ios::beg);
s.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
rapidjson::Document doc;
doc.Parse(s.c_str());
this->Config->textspeed = doc["textspeed"].GetInt();
this->Config->depth = doc["depth"].GetInt();
this->Config->debug = !!doc["debug"].GetInt();
this->Config->polite = !!doc["polite"].GetInt();
this->Config->show_http = !!doc["show_http"].GetInt();
auto it = this->Config->eMap.find(doc["type"].GetString());
if (it != this->Config->eMap.end())
{
this->Config->type = it->second;
}
else
{
std::cerr << "Wrong type, available types are \"unchanged, allsmall, firstcapital, fullcapital\".";
Logger::log << "Wrong settings type error";
}
}
else
{
std::cerr << "Error opening settings file.";
Logger::log << "Error opening settings file.";
}
file.close();
}
void Manager::WriteToFile(const std::set<std::string>& data)
{
Logger::log << "Writing to file..";
std::ofstream file("Output.txt", std::ifstream::out);
if (file.is_open())
{
std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(file, "\n"));
}
else
{
std::cerr << "Error saving the file.";
Logger::log << "Error saving the file.";
}
file.close();
}
void Manager::SetDisplay(std::shared_ptr<Console> console)
{
m_display = console;
}
void Manager::CheckProgress() // POOLING SUCKS, THINK OBSERVER OR CONDITION_VARIABLE
{
std::thread t1;
bool active = false;
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
if (Manager::instance().m_working == true)
{
if (Manager::instance().m_display->progress.load() == false)
{
t1 = std::thread(std::bind(&Console::ProgressStar, Manager::instance().m_display)); //Spin the star
active = true;
}
if (m_SpiderSet.size() != 0)
for (auto s : m_SpiderSet)
{
auto x = s->AmIStuck;
auto timenow = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = timenow - x;// FOR EACH SPIDER, AmIStuck?
if (diff.count() > 10)
{
std::cerr << &s << " Is taking " << std::chrono::duration_cast<std::chrono::seconds>(diff).count() << "s to check: " << s->CurrentUrl << std::endl;
}//More than 10s have passed, so it's stuck in a loop?
}
}
if (active)
{
t1.join();
active = false;
}
}
}
std::set<std::shared_ptr<Spider>> Manager::m_SpiderSet;
Manager::Manager()
{
}
Manager::~Manager()
{
}
Settings::Settings()
{
eMap.insert(std::pair<std::string, SortingType>("unchanged", unchanged));
eMap.insert(std::pair<std::string, SortingType>("lowercase", lowercase));
eMap.insert(std::pair<std::string, SortingType>("capitalize", capitalize));
eMap.insert(std::pair<std::string, SortingType>("uppercase", uppercase));
}
/* Inlined code
/////////////////////////////////
template <typename T>
inline typename std::enable_if<!std::is_integral<T>::value, Logger&>::type operator<<(const T in)
{
if (Manager::instance().Config->debug == true)
{
std::string instring = (std::string)in;
m_InternalBuffer.append(instring);
try {
if (m_InternalBuffer.substr(m_InternalBuffer.size() - 1) == "\n")
{
m_Log(m_InternalBuffer);
Manager::instance().m_display->WriteCurrentEvent(m_InternalBuffer);
m_InternalBuffer.clear();
}
}
catch (std::out_of_range& e)
{
std::cerr << e.what();
//Do nuffin
}
}
return *this;
}
template <typename T>
inline typename std::enable_if<std::is_integral<T>::value, Logger&>::type operator<<(const T in)
{
Logger::operator<< (std::to_string(in));
return *this;
}
/////////////////////////////////
*/
void Logger::Logger::m_Log(std::string in)
{
if (!m_file.is_open())
{
m_file.open(m_logname);
}
if (m_file.is_open())
{
m_file << in;
m_file.flush();
}
else
{
std::cerr << "Error saving the file.";
std::cerr << strerror(errno);
}
}
//Logger::Logger::operator internal::Liner() const { return {}; }
Logger::Logger &Logger::log = Logger::Logger::instance();
void Logger::Logger::SetLog()
{
if (m_file.is_open())
{
m_file.close();
}
auto now = std::chrono::system_clock::now();
std::time_t start_time = std::chrono::system_clock::to_time_t(now);
std::stringstream tempstrs;
char timedisplay[256];
auto tm = localtime(&start_time);
if (std::strftime(timedisplay, sizeof(timedisplay), "%F_%H.%M.%S", tm))
{
tempstrs << timedisplay;
}
m_logname = "Log_" + tempstrs.str() + ".txt";
}
Logger::Logger::Logger()
{
}
Logger::Logger::~Logger()
{
if (m_file.is_open())
{
m_file.close();
}
}