-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.cpp
145 lines (123 loc) · 4.65 KB
/
command.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
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
#include "headerFiles/command.h"
#include "headerFiles/commandMap.h"
#include "shuntingYard.cpp"
ToolBox toolsinstance;
CommandMap commandMapInstance;
Calculator calInstance;
// Derived class server
void ServerCommand::doCommand(const vector<string> &line)
{
Server instance;
instance.serverThread(line[1], line[2]);
// sending the system a command (using separated thread) to start the drone separated thread
thread t2(ToolBox::systemRun);
t2.detach();
// taking a nap until the drone is ready
cout << "Please wait for 30 seconds...\n"
<< endl;
this_thread::sleep_for(30s);
};
// Derived class client
void ClientCommand::doCommand(const vector<string> &line)
{
Client::getinstance().client(line[1], line[2]);
};
// Derived class var
void VarCommand::doCommand(const vector<string> &line)
{
// checking is it a bind order or a set order.
if (line[3] != "bind")
{
double dValue = toolsinstance.getStatus(line[3]);
// converting the double value to a string in order to insert it to a string based map.
string strValue = to_string(dValue);
SymbolPathMap::getinstance().symbolPathMap(line[1], strValue);
}
else
{
SymbolPathMap::getinstance().symbolPathMap(line[1], line[4]);
}
};
// Derived class set
void SetCommand::doCommand(const vector<string> &line)
{
// connecting multiple string to one string
stringstream ss;
ss << "set"
<< " " << SymbolPathMap::getinstance().umap.at(line[0]) << " " << line[2] << "\r\n";
string oneStrings = ss.str();
// removing the " symbol from the path
char chars[] = "\"";
for (unsigned int i = 0; i < strlen(chars); ++i)
{
oneStrings.erase(remove(oneStrings.begin(), oneStrings.end(), chars[i]), oneStrings.end());
}
// converting the string 'oneString' to a char*
char *charOneString = &oneStrings[0];
Client::getinstance().send(charOneString);
};
// Derived class while
void WhileCommand::doCommand(const vector<string> &line)
{
// use the second part of the pair, that contains only the while body instructions
pair<vector<vector<string>>, vector<vector<string>>> myPair = Lexer::getinstance().lexingMethod("my_file.txt");
vector<vector<string>> whileBody = myPair.second;
// the while condition
while (toolsinstance.operatorInterp((toolsinstance.getStatus(line[1])), line[2], line[3]))
{
// loop over each vector (A.K.A line)
for (int i = 0; i < whileBody.size(); i++)
{
vector<string> line = whileBody[i];
// checking if the command is not in the command map (not print or sleep etc.)
if (CommandMap::umap.find(line[0]) == CommandMap::umap.end())
{
//replacing the var by its current status
line = toolsinstance.replaceByStatus(line);
// connecting multiple string to one string
string s;
for (int j = 2; j < line.size(); j++)
{
s += line[j];
s += " ";
}
// getting the calculation of the string type equasion from the shuntingYard
double shuntVal = calInstance.calculate(s);
// converting the duble resulte of the calculation back to string
string strShuntVal = to_string(shuntVal);
// pushing all the strings into one vector in order to send it to the client
vector<string> finalLine;
finalLine.push_back(line[0]);
finalLine.push_back(" = ");
finalLine.push_back(strShuntVal);
commandMapInstance.umap["set"]->doCommand({finalLine});
}
else
{
// if it is a commant type, send it the polimorphic doCommand (all the othe classes in this file.)
commandMapInstance.umap[line[0]]->doCommand({line});
};
}
}
};
// Derived class print
void PrintCommand::doCommand(const vector<string> &line)
{
// checking what kind of print is it, a plane text or a status.
if ((SymbolPathMap::getinstance().umap.find(line[1]) != SymbolPathMap::getinstance().umap.end()))
cout << "the current " << line[1] << " is: " << toolsinstance.getStatus(line[1]) << endl;
else
{
for (int i = 1; i < line.size(); i++)
cout << line[i] << " ";
}
cout << endl;
};
// Derived class sleep
void SleepCommand::doCommand(const vector<string> &line)
{
// converting string to int
int timeToSleep = stoi(line[1]);
std::chrono::milliseconds timespan(timeToSleep);
std::this_thread::sleep_for(timespan);
};