-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommandparser.cpp
More file actions
368 lines (337 loc) · 10.8 KB
/
commandparser.cpp
File metadata and controls
368 lines (337 loc) · 10.8 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "commandparser.h"
#include <thread>
#include <iostream>
#if defined(_WIN32) || defined(_WIN64)
#include <WS2tcpip.h>
#elif defined (__linux__)
#include <string.h>
// remove this if linux ever implements memcpy_s
#define memcpy_s(dest, destsz, src, srcsz) memcpy(dest, src, destsz)
#endif
#define CompareArg(Arg, Val) (std::string(Arg) == Val)
#define CheckIndexValid(idx, arg) if(idx >= ArgCount) { std::cout << "Invalid end of Argument: " << arg << std::endl; break; }
void CommandParser::PrintHelp()
{
std::cout << "Primebot" << std::endl;
std::cout << "primebot Usage:" << std::endl;
std::cout << "primebot [[-c | -s] <addr>] [-a] [-t <cnt>] [-p <path>] [-i <seed>] [-b <cnt>]" << std::endl;
std::cout << " [-r] [--batchsize <size>] [--batches <cnt>] [--start <start>]" << std::endl;
std::cout << " [--startbase <base>]" << std::endl;
std::cout << "primebot [-h | -?]" << std::endl;
std::cout << " -c / --client: Configure as a client, connection to server at <address>" << std::endl;
std::cout << " -s / --server: Configure as a server" << std::endl;
std::cout << " -a / --async: Use async routines instead of threadpool" << std::endl;
std::cout << " -t / --threads: Threadcount for threadpool and async" << std::endl;
std::cout << " -p / --path: Path to save discovered primes at. Otherwise print to console" << std::endl;
std::cout << " -i / --seed: Use <seed> for RNG to find starting number" << std::endl;
std::cout << " -b / --bits: Search numbers using at most <count> bits" << std::endl;
std::cout << " -r / --reverse: Search numbers in reverse, i.e. down towards zero" << std::endl;
std::cout << " --print: Print primes stored in a file given with -p/--path" << std::endl;
std::cout << " --text: Store primes written with -p/--path as text instead of binary" << std::endl;
std::cout << " --batchsize: Count of numbers each thread will test for primality" << std::endl;
std::cout << " --batches: Count of batches to search" << std::endl;
std::cout << " --start: Use <start> to start searching from. Overrides RNG." << std::endl;
std::cout << " --startbase: Numeric base to interpret Start as (valid range is 2-62)" << std::endl;
std::cout << std::endl;
}
bool CommandParser::ConfigureSocketAddress(AllPrimebotSettings & Settings, int Flags, std::string Address)
{
#if defined(_WIN32) || defined(_WIN64)
WSAData WsData;
int Error = WSAStartup(MAKEWORD(2, 2), &WsData);
if (Error != NO_ERROR)
{
std::cout << gai_strerror(Error) << " at " << __FUNCTION__ << std::endl;
}
#endif
addrinfo Hints = { 0 };
addrinfo* Results;
int Result = 0;
Hints.ai_flags = Flags;
Hints.ai_socktype = SOCK_STREAM;
Hints.ai_protocol = IPPROTO_TCP;
Result = getaddrinfo(
Address.c_str(),
nullptr,
&Hints,
&Results
);
if (Result != 0)
{
std::cout << gai_strerror(Result) << " on argument: " << Address << std::endl;
freeaddrinfo(Results);
return false;
}
if (Results->ai_family == AF_INET)
{
memcpy_s(
&Settings.NetworkSettings.IPv4,
sizeof(Settings.NetworkSettings.IPv4),
Results->ai_addr,
Results->ai_addrlen);
Settings.NetworkSettings.IPv4.sin_port = SERVER_PORT;
}
else
{
memcpy_s(
&Settings.NetworkSettings.IPv6,
sizeof(Settings.NetworkSettings.IPv6),
Results->ai_addr,
Results->ai_addrlen);
Settings.NetworkSettings.IPv6.sin6_port = SERVER_PORT;
}
freeaddrinfo(Results);
return true;
}
bool CommandParser::ConfigureServer(AllPrimebotSettings& Settings, std::string Address)
{
Settings.NetworkSettings.Server = true;
return ConfigureSocketAddress(Settings, AI_V4MAPPED | AI_PASSIVE, Address);
}
bool CommandParser::ConfigureClient(AllPrimebotSettings& Settings, std::string Address)
{
Settings.NetworkSettings.Client = true;
return ConfigureSocketAddress(Settings, AI_V4MAPPED, Address);
}
bool CommandParser::ConfigureAsync(AllPrimebotSettings& Settings)
{
Settings.PrimeSettings.UseAsync = true;
return true;
}
bool CommandParser::ConfigureReverse(AllPrimebotSettings& Settings)
{
Settings.PrimeSettings.Reverse = true;
return true;
}
bool CommandParser::ConfigureThreads(AllPrimebotSettings& Settings, std::string Threads)
{
try
{
Settings.PrimeSettings.ThreadCount = std::stoul(Threads);
}
catch (std::invalid_argument ia)
{
std::cout << ia.what() << std::endl;
return false;
}
return true;
}
bool CommandParser::ConfigurePath(AllPrimebotSettings& Settings, std::string Path)
{
Settings.FileSettings.Path = Path;
return true;
}
bool CommandParser::ConfigureSeed(AllPrimebotSettings& Settings, std::string Seed)
{
try
{
Settings.PrimeSettings.RngSeed = std::stoul(Seed);
}
catch (std::invalid_argument ia)
{
std::cout << ia.what() << std::endl;
return false;
}
return true;
}
bool CommandParser::ConfigureBits(AllPrimebotSettings& Settings, std::string Bits)
{
try
{
Settings.PrimeSettings.Bitsize = std::stoul(Bits);
}
catch (std::invalid_argument& ia)
{
std::cout << ia.what() << std::endl;
return false;
}
return true;
}
bool CommandParser::ConfigurePrint(AllPrimebotSettings & Settings)
{
Settings.FileSettings.Flags.Print = true;
return true;
}
bool CommandParser::ConfigureText(AllPrimebotSettings & Settings)
{
Settings.FileSettings.Flags.Binary = false;
return true;
}
bool CommandParser::ConfigureBatchSize(AllPrimebotSettings & Settings, std::string Size)
{
try
{
Settings.PrimeSettings.BatchSize = std::stoul(Size);
}
catch (std::invalid_argument& ia)
{
std::cout << ia.what() << std::endl;
return false;
}
return true;
}
bool CommandParser::ConfigureBatches(AllPrimebotSettings & Settings, std::string Batches)
{
try
{
Settings.PrimeSettings.BatchCount = std::stoull(Batches);
}
catch (std::invalid_argument& ia)
{
std::cout << ia.what() << std::endl;
return false;
}
return true;
}
bool CommandParser::ConfigureStartValue(AllPrimebotSettings & Settings, std::string Start)
{
Settings.PrimeSettings.StartValue = Start;
return true;
}
bool CommandParser::ConfigureStartValueBase(AllPrimebotSettings & Settings, std::string StartBase)
{
try
{
Settings.PrimeSettings.StartValueBase = std::stoull(StartBase);
if (Settings.PrimeSettings.StartValueBase < 2 || Settings.PrimeSettings.StartValueBase > 62)
{
std::cout << "Base for start must be between 2 and 62! You entered: "
<< Settings.PrimeSettings.StartValueBase << std::endl;
return false;
}
}
catch (std::invalid_argument& ia)
{
std::cout << ia.what() << std::endl;
return false;
}
return true;
}
CommandParser::CommandParser(int argc, char ** argv) :
ArgCount(argc),
Args(argv)
{
}
AllPrimebotSettings CommandParser::ParseArguments()
{
AllPrimebotSettings Settings;
bool Result = true;
for (int i = 1; i < ArgCount && Result; i++)
{
if (i == 0)
{
std::cout << Args[0] << std::endl;
continue;
}
if (CompareArg("--client", Args[i]) || CompareArg("-c", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureClient(Settings, Args[i]);
}
else if (CompareArg("--server", Args[i]) || CompareArg("-s", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureServer(Settings, Args[i]);
}
else if (CompareArg("--async", Args[i]) || CompareArg("-a", Args[i]))
{
Result = ConfigureAsync(Settings);
}
else if (CompareArg("--threads", Args[i]) || CompareArg("-t", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureThreads(Settings, Args[i]);
}
else if (CompareArg("--path", Args[i]) || CompareArg("-p", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigurePath(Settings, Args[i]);
}
else if (CompareArg("--seed", Args[i]) || CompareArg("-i", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureSeed(Settings, Args[i]);
}
else if (CompareArg("--bits", Args[i]) || CompareArg("-b", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureBits(Settings, Args[i]);
}
else if (CompareArg("--reverse", Args[i]) || CompareArg("-r", Args[i]))
{
Result = ConfigureReverse(Settings);
}
else if (CompareArg("--print", Args[i]))
{
Result = ConfigurePrint(Settings);
}
else if (CompareArg("--text", Args[i]))
{
Result = ConfigureText(Settings);
}
else if (CompareArg("--batchsize", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureBatchSize(Settings, Args[i]);
}
else if (CompareArg("--batches", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureBatches(Settings, Args[i]);
}
else if (CompareArg("--start", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureStartValue(Settings, Args[i]);
}
else if (CompareArg("--startbase", Args[i]))
{
CheckIndexValid(i + 1, Args[i]);
i++;
Result = ConfigureStartValueBase(Settings, Args[i]);
}
else if (CompareArg("--help", Args[i]) || CompareArg("-h", Args[i]) ||
CompareArg("-?", Args[i]))
{
PrintHelp();
Settings.Run = false;
}
else if (CompareArg("--", Args[i]))
{
break;
}
else
{
std::cout << "Invalid argument: " << Args[i] << std::endl;
Settings.Run = false;
break; // out of loop
}
}
if (!Result)
{
// Error occurred, throw
std::cout << "Invalid argument on command line. See above messages for details." << std::endl;
Settings.Run = false;
}
return Settings;
}
PrimebotSettings::PrimebotSettings() :
UseAsync(false),
Reverse(false),
ThreadCount(std::thread::hardware_concurrency()),
Bitsize(512),
RngSeed(1234),
StartValue(),
StartValueBase(0),
BatchSize(1000),
BatchCount(-1)
{}