-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
293 lines (265 loc) · 8.27 KB
/
Copy pathmain.cpp
File metadata and controls
293 lines (265 loc) · 8.27 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
#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>
#include <thread>
#include "BigLexer.h"
#include "SmallLexer.h"
#include "DataReaderServer.h"
#include "LineParser.h"
#include "SymbolTable.h"
#include "BlockParser.h"
#include "DataSender.h"
#include <fstream>
using namespace std;
// create mutex lock.
mutex mutex1;
void shared_print(string toPrint) {
mutex1.lock();
cout << toPrint << endl;
mutex1.unlock();
}
void *pthreadFunc(void *arg) {
int *idPtr = (int *) arg;
int id = *idPtr;
for (int i = 0; i < 1000; i++) {
if (id == 1) {
shared_print("hello, this is the NUMBER ONE thread");
} else {
shared_print("i am not the number one...");
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
pthread_exit(nullptr); // the thread which runs this func finishes.
}
/**
* Shared memory try.
* @param toPrint
*/
//void sharedMemory(map<string, double> *mapy, double id, string toPut) {
//
//// mutex1.lock();
// mapy->insert(pair<string, double>("this", id));
// cout << to_string(mapy->at("this")) + "\n";
//// mutex1.unlock();
//}
//
//void *pthreadFunc2(void *arg) {
// map<string, double> mapy;
// int *idPtr = (int *) arg;
// double id = *idPtr;
// mutex1.lock();
// for (int i = 0; i < 100; i++) {
// if (id == 1) {
// sharedMemory(&mapy, 1);
// } else {
// sharedMemory(&mapy, 2);
// }
// }
// mutex1.unlock();
//
//
// pthread_exit(nullptr); // the thread which runs this func finishes.
//}
int main(int argc, char *argv[]) {
/**
* Pthread try:
* this try is using mutex in order to lock data usage from other threads.
*/
// // create 1st thread
// pthread_t threadID;
// pthread_attr_t attr;
// pthread_attr_init(&attr);
// int valueForFunc = 1;
// pthread_create(&threadID, &attr, pthreadFunc2, &valueForFunc);
//
// // create 2nd thread
// pthread_t thread2ID;
// pthread_attr_t attr2;
// pthread_attr_init(&attr2);
// int secondValue = 2;
// pthread_create(&thread2ID, &attr2, pthreadFunc2, &secondValue);
//
// pthread_join(threadID, nullptr); // wait for thread to join.
// pthread_join(thread2ID, nullptr); // wait for thread to join.
/**
* 2nd Pthread try:
* this try is trying to use thread on a class method.
*/
// DataReaderServer dataReaderServer;
// dataReaderServer.threadListen();
/**
* Lexer try:
*/
// BigLexer lexer1;
// string str;
// vector<string> vec;
// while (true) {
// getline(cin, str); // client input will be sent to lexer function to receive string array.
// vec = lexer1.lexer(str);
// for (int i = 0; i < vec.size(); i++) {
// cout << vec.at(i) << "\n";
// }
// }
// SmallLexer lexer1;
// string str;
// vector<string> vec;
// while (true) {
// getline(cin, str); // client input will be sent to lexer function to receive string array.
// vec = lexer1.lexer(str);
// for (int i = 0; i < vec.size(); i++) {
// cout << vec.at(i) << "\n";
// }
// }
/**
* "REAL" main for sending commands.
* this main simulates the real main used for the flight simulator.
*/
// SymbolTable st;
// LineParser lineParser(&st); // parser gets a pointer to the shared symbol table.
// BlockParser blockParser(&st); // block parser gets a pointer to the shared symbol table.
// BigLexer bl;
// string userInput;
// vector<string> stringVector;
//
// while (true) {
// getline(cin, userInput);
// stringVector = bl.lexer(userInput);
//
// // if the user wants to exit the process.
// if (userInput == "exit") {
// break;
// }
// // if command is block command.
// else if (stringVector.at(0) == "while" || stringVector.at(0) == "if") {
// string blockString = userInput.substr(0, userInput.length() - 2); // exclude curly bracket.
// // get all of the user input until the end of the if/while statement.
// int openedBrackets = 1;
// while (openedBrackets != 0) {
// getline(cin, userInput);
// if (userInput.back() == '}') {
// openedBrackets -= 1;
// continue;
// } else if (userInput.back() == '{') {
// openedBrackets += 1;
// }
// blockString += userInput;
// }
//
// stringVector = bl.lexer(blockString);
// blockParser.parse(stringVector);
// }
// // command was a line command.
// else {
// lineParser.parse(bl.lexer(userInput), 0);
// }
// }
/**
* "REAL" main for sending command - WITH FILEEE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* this main simulates the real main used for the flight simulator.
*/
SymbolTable st;
DataReaderServer dataReaderServer;
DataSender dataSender;
LineParser lineParser(&st, &dataReaderServer, &dataSender); // parser gets a pointer to the shared symbol table and DRS
BlockParser blockParser(&st, &dataReaderServer, &dataSender); // block parser gets pointers to the shared symbol table and DRS
BigLexer bl;
string userInput;
vector<string> stringVector;
bool fromFile = false;
string line;
ifstream myFile(argv[1]);
if (argc == 2) {
// ifstream myFile(argv[1]);
if (myFile.is_open()) {
fromFile = true;
}
}
while (true) {
if (fromFile) {
if (getline(myFile, line)) {
userInput = line;
} else {
break;
}
}
if (!fromFile) {
getline(cin, userInput);
}
stringVector = bl.lexer(userInput);
// if the user wants to exit the process.
if (userInput == "exit") {
break;
}
// if command is block command.
else if (stringVector.at(0) == "while" || stringVector.at(0) == "if") {
string blockString = userInput.substr(0, userInput.length() - 2); // exclude curly bracket.
// get all of the user input until the end of the if/while statement.
int openedBrackets = 1;
while (openedBrackets != 0) {
if (fromFile) {
if (getline(myFile, line)) {
userInput = line;
} else {
break;
}
}
if (!fromFile) {
getline(cin, userInput);
}
if (userInput.back() == '}') {
openedBrackets -= 1;
continue;
} else if (userInput.back() == '{') {
openedBrackets += 1;
}
blockString += " " + userInput;
}
vector<string> lexedVec1 = bl.lexer(blockString);
stringVector = bl.lexer(blockString);
blockParser.parse(stringVector);
}
// command was a line command.
else {
cout << "user *Line Command input* is :" << userInput << endl;
vector<string> lexedVec = bl.lexer(userInput);
lineParser.parse(bl.lexer(userInput), 0);
}
}
if (fromFile) {
myFile.close();
}
/**
* Test with Ido (worked open server).
*
* And now also the client can connect to the flight simulator with it's own pthread.
* After it connects it can send commands to the simulator.
*/
// DataReaderServer dataReaderServer;
// pthread_t t1ID;
// pthread_create(&t1ID, NULL, &DataReaderServer::runOpen, &dataReaderServer);
//
// dataReaderServer.set("5402", "10");
//
// // unconnected print...
// cout << "parallel activity" << endl;
//
// DataSender dataSender;
// pthread_t t2ID;
// pthread_create(&t2ID, NULL, &DataReaderServer::runOpen, &dataSender);
//
// sleep(18);
//
// dataSender.set("127.0.0.1", "5401");
// dataSender.openPipe();
//
// sleep(18);
// dataSender.sendCommand("ls");
//
// // unconnected print...
// cout << "parallel activity" << endl;
//
// pthread_join(t1ID, nullptr); // wait for thread to join.
// pthread_join(t2ID, nullptr); // wait for thread to join.
return 0;
}