-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlModule.cpp
116 lines (106 loc) · 2.87 KB
/
ControlModule.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
113
114
115
116
#include "ControlModule.h"
void print_instruction() {
std::cout << "---------- Console instruction ----------\n"
<< "In random mode corresponding commands are:\n"
<< "\t* <PARAMETER_NAME>=<VALUE> - change parameters\n"
<< "\t* apply - apply changes\n"
<< "\t* switch - switch to the reading mode\n"
<< "In reading mode corresponding commands are:\n"
<< "\t* switch - switch to the random mode\n"
<< "Any other form of input would be conidered as invalid\n" << std::endl;
std::cout << "---------- List of parameters ----------\n"
<< "R - generating time(milliseconds)\n"
<< "T - receiving time(milliseconds)\n"
<< "M - max array length\n"
<< "N - max number of blocks\n" << std::endl;
}
ControlModule::ControlModule(SharedParameters* ptr_params) {
if (ptr_params != nullptr) {
ptr_parameters = ptr_params;
}
print_instruction();
};
ControlModule::~ControlModule() {
};
void ControlModule::processInput(Generator& g) {
auto isAccessible = checkIfAccessible(g);
if (isAccessible)
processIfAccessible(g);
else
processIfInaccessible(g);
};
void ControlModule::processIfAccessible(Generator& g) {
std::string temp_str = "";
Parameters temp_params = *ptr_parameters;
std::cout << prompt;
while (std::cin >> temp_str) {
if (temp_str[1] == '=') {
switch (temp_str[0]) {
case 'R': {
//à åñëè ìåíüøå 0 âðåìÿ?
temp_params.R = atoi(&((temp_str.c_str())[2]));
std::cin.clear();
break;
}
case 'T': {
temp_params.T = atoi(&((temp_str.c_str())[2]));
std::cin.clear();
break;
}
case 'M': {
temp_params.M = atoi(&((temp_str.c_str())[2]));
std::cin.clear();
break;
}
case 'N': {
// à åñëè áîëüøå size_t?
temp_params.N = atoi(&((temp_str.c_str())[2]));
std::cin.clear();
break;
}
default: {
std::cerr << "Invalid name of parameter, try again" << std::endl;
break;
}
}
}
else if (temp_str == "apply") {
std::lock_guard<std::mutex> locker(ptr_parameters->mu_param);
*ptr_parameters = temp_params;
}
else if (temp_str == "switch") {
g.switchMode();
processInput(g);
}
else if (temp_str == "exit") {
//std::terminate();
}
else {
std::cerr << "Invalid input\n";
std::cin.clear();
}
std::cout << prompt;
}
};
void ControlModule::processIfInaccessible(Generator& g) {
std::string temp_str = "";
Parameters temp_params = *ptr_parameters;
std::cout << prompt;
while (std::cin >> temp_str) {
if (temp_str == "switch") {
g.switchMode();
//std::cout << "switch completed, beginning processing\n";
processInput(g);
}
else if (temp_str == "exit") {
//std::terminate();
}
else {
std::cerr << "Invalid command\n";
}
std::cout << prompt;
}
};
bool ControlModule::checkIfAccessible(Generator& g) {
return g.canChangeParameters();
};