-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.cpp
301 lines (215 loc) · 12.6 KB
/
Commands.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
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
#include "Commands.h"
int travelRequest(string InputWords[], int numMonitors, int socketBufferSize, Info monitorInfo[], BloomNode* bloomListHead, NumOfRequests* req); /* Command 1 */
int travelStats(string InputWords[], int numMonitors, Info monitorInfo[], BloomNode* bloomListHead); /* Command 2 */
int addVaccinationRecords(string InputWords[], int numMonitors, int socketBufferSize , Info monitorInfo[], BloomNode* bloomListHead, string inputDir); /* Command 3 */
int searchVaccinationStatus(string InputWords[], int numMonitors, int socketBufferSize, Info monitorInfo[]); /* Command 4 */
int exit(string InputWords[], int numMonitors, int socketBufferSize, Info monitorInfo[], BloomNode* bloomListHead); /* Command 5 */
int Commands(int bloomSize, int numMonitors, int socketBufferSize, Info monitorInfo[], BloomNode* bloomListHead, NumOfRequests* req, string inputDir){
string InputWords[1024];
string input, word;
cout << endl << "Enter a command: ";
getline(cin, input);
cout << endl;
cin.clear();
istringstream words(input); /* Split the input into words */
int i = 0; /* Gen each word into the array */
while (words >> word)
InputWords[i++] = word;
if (InputWords[0].compare("/travelRequest") == 0){ /* Command 1 - /travelRequest citizenID date countryFrom countryTo virusName */
travelRequest(InputWords,numMonitors,socketBufferSize,monitorInfo,bloomListHead,req);
}else if (InputWords[0].compare("/travelStats") == 0){ /* Command 2 - /travelStats virusName date1 date2 [country] */
travelStats(InputWords, numMonitors, monitorInfo,bloomListHead);
}else if (InputWords[0].compare("/addVaccinationRecords") == 0){ /* Command 3 - /addVaccinationRecords country */
addVaccinationRecords(InputWords,numMonitors,socketBufferSize,monitorInfo,bloomListHead, inputDir);
}else if (InputWords[0].compare("/searchVaccinationStatus") == 0){ /* Command 4 - /searchVaccinationStatus citizenID */
searchVaccinationStatus(InputWords,numMonitors,socketBufferSize,monitorInfo);
}else if (InputWords[0].compare("/exit") == 0){ /* Command 5 - /exit */
return exit(InputWords,numMonitors,socketBufferSize ,monitorInfo,bloomListHead);
}else{ /* ERROR in commands */
cout << "ERROR this command is not valid" << endl; }
return 0;
}
/* Command 1 - /travelRequest citizenID date countryFrom countryTo virusName */
int travelRequest(string InputWords[], int numMonitors, int socketBufferSize, Info monitorInfo[], BloomNode* bloomListHead, NumOfRequests* req){
int citizenId = stoi(InputWords[1]);
string virusName = InputWords[5];
if (BloomListSearch(bloomListHead,virusName) == false){ /* Check if virusName exists in the list => or error */
cout << "There is no virus called: " << virusName << endl;
return 0;
}
Date travelDate;
travelDate.getDateFromStr(InputWords[2]);
if (BloomSearchIdInBloom(bloomListHead,InputWords[1],virusName)){ /* If it exists in bloom */
int foundFlag = -1;
for (int i = 0; i < numMonitors; i++){
if (CountryListSearch(monitorInfo[i].countryListHead,InputWords[3])){ /* Find which monitor has the country called "countryFrom" */
foundFlag = 1;
sendString(monitorInfo[i].socketFd, "/travelRequest", socketBufferSize);
sendInt(monitorInfo[i].socketFd, citizenId); /* Send citizenId */
sendString(monitorInfo[i].socketFd, virusName.c_str(),socketBufferSize); /* Send virusName */
string isVaccinated = readString(monitorInfo[i].socketFd,socketBufferSize); /* Get the answer (if the citizen is vaccinated or not) */
if (isVaccinated.compare("YES") == 0){ /* If citizen is vaccinated */
string dateStr = readString(monitorInfo[i].socketFd, socketBufferSize);
Date vaccinationDate;
vaccinationDate.getDateFromStr(dateStr);
if (vaccinationDate.lessThan6Months(travelDate)){ /* If citizen got vaccinated in the last 6 months */
cout << "REQUEST ACCEPTED – HAPPY TRAVELS" << endl;
req->acceptedReq = req->acceptedReq + 1;
BloomListAddAcceptReq(bloomListHead,virusName,travelDate,InputWords[3]);
}else{ /* If citizen got vaccinated but not in the last 6 months */
cout << "REQUEST REJECTED – YOU WILL NEED ANOTHER VACCINATION BEFORE TRAVEL DATE" << endl;
req->rejectedReq = req->rejectedReq + 1;
BloomListAddRejectReq(bloomListHead,virusName,travelDate,InputWords[3]);
}
}else if(isVaccinated.compare("NO") == 0){ /* If citizen is not vaccinated */
cout << "REQUEST REJECTED – YOU ARE NOT VACCINATED" << endl;
req->rejectedReq = req->rejectedReq + 1;
BloomListAddRejectReq(bloomListHead,virusName,travelDate,InputWords[3]);
}
break;
}
}
if (foundFlag == -1){
cout<< "There is no country called: " << InputWords[3] << endl;
return 0;
}
}else{
cout << "REQUEST REJECTED – YOU ARE NOT VACCINATED" << endl;
req->rejectedReq = req->rejectedReq + 1;
BloomListAddRejectReq(bloomListHead,virusName,travelDate,InputWords[3]);
}
BloomListAddTotalReq(bloomListHead,virusName,travelDate,InputWords[3]);
req->totalReq = req->totalReq + 1;
return 1;
}
/* Command 2 - /travelStats virusName date1 date2 [country] */
int travelStats(string InputWords[], int numMonitors, Info monitorInfo[], BloomNode* bloomListHead){
if ((InputWords[1].compare("\0") == 0) || (InputWords[2].compare("\0") == 0) || (InputWords[3].compare("\0") == 0) || (InputWords[5].compare("\0") != 0)){
cout << "ERROR in command /travelStats" << endl;
return 0;
}
string virusName = InputWords[1];
if (BloomListSearch(bloomListHead,virusName) == false){ /* Check if virusName exists in the list */
cout << "There is no virus called: " << virusName << endl;
return 0;
}
Date date1;
Date date2;
date1.getDateFromStr(InputWords[2]);
date2.getDateFromStr(InputWords[3]);
int totalReqNum, acceptedReqNum, rejectedReqNum; /* Requests counter */
if (InputWords[4].compare("\0") != 0){ /* If a country was given as an argument */
int foundFlag = -1;
string country = InputWords[4];
/* Check if country exists in the list with the countries */
for (int i = 0; i < numMonitors; i++)
if (CountryListSearch(monitorInfo[i].countryListHead,country))
foundFlag = 1;
if (foundFlag == -1){
cout << "There is no country called: " << country << endl;
return 0;
}
totalReqNum = BloomListGetTotalRequests(bloomListHead, virusName, country, date1, date2);
acceptedReqNum = BloomListGetAcceptRequests(bloomListHead,virusName, country, date1, date2);
rejectedReqNum = BloomListGetRejectRequests(bloomListHead,virusName, country, date1, date2);
}else{
totalReqNum = BloomListGetTotalRequests(bloomListHead, virusName, date1, date2);
acceptedReqNum = BloomListGetAcceptRequests(bloomListHead,virusName, date1, date2);
rejectedReqNum = BloomListGetRejectRequests(bloomListHead,virusName, date1, date2);
}
/* Print the number of requests */
cout << "TOTAL REQUESTS " << totalReqNum << endl;
cout << "ACCEPTED " << acceptedReqNum << endl;
cout << "REJECTED " << rejectedReqNum << endl;
return 1;
}
/* Command 3 - /addVaccinationRecords country */
int addVaccinationRecords(string InputWords[], int numMonitors, int socketBufferSize , Info monitorInfo[], BloomNode* bloomListHead, string inputDir){
if ((InputWords[1].compare("\0") == 0) || (InputWords[2].compare("\0") != 0)){ /* There must be exactly 2 arguments */
cout << "ERROR in command /addVaccinationRecords" << endl;
return 0;
}
int foundFlag = -1; /* A flag that indicates if country exists */
string country = InputWords[1];
for (int i = 0; i < numMonitors; i++){
if (CountryListSearch(monitorInfo[i].countryListHead,country)){ /* Find which monitor has the country */
foundFlag = 1;
sendString(monitorInfo[i].socketFd, "/addVaccinationRecords", socketBufferSize);
sendString(monitorInfo[i].socketFd, inputDir.c_str(), socketBufferSize);
sendString(monitorInfo[i].socketFd, country.c_str(), socketBufferSize); /* Send county's name to monitor */
int numOfViruses = readInt(monitorInfo[i].socketFd); /* Get the number of viruses (equals with the number of bloom filters) */
int sizeOfBloom = bloomListHead->bloomFilter->getBloomSize();
char* tempBloom;
for (int j = 0; j < numOfViruses; j++){ /* How many bloom filters to read */
string virusNameStr = readString(monitorInfo[i].socketFd, socketBufferSize); /* Get the virus name */
tempBloom = readBloom(monitorInfo[i].socketFd, sizeOfBloom, socketBufferSize); /* Get the bloom filter */
/* Update the list with the bloom filters */
if (!(BloomListSearch(bloomListHead, virusNameStr))) /* If virusName is not already in the list => add it */
BloomListPush(&bloomListHead, virusNameStr, tempBloom, sizeOfBloom);
else
BloomListMergeBlooms(bloomListHead, virusNameStr, tempBloom);
delete tempBloom;
}
}
}
if (foundFlag == -1)
cout << "There is no country called: " << country << endl;
return 1;
}
/* Command 4 - /searchVaccinationStatus citizenID */
int searchVaccinationStatus(string InputWords[], int numMonitors, int socketBufferSize, Info monitorInfo[]){
if ((InputWords[1].compare("\0") == 0) || (InputWords[2].compare("\0") != 0)){
cout << "ERROR in command /searchVaccinationStatus" << endl;
return 0;
}
int foundFlag = -1; /* A flag that indicates if citizen with id "citizenId" exists */
int citizenId = stoi(InputWords[1]);
for (int i = 0; i < numMonitors; i++){
sendString(monitorInfo[i].socketFd, "/searchVaccinationStatus", socketBufferSize);
sendInt(monitorInfo[i].socketFd, citizenId);
foundFlag = readInt(monitorInfo[i].socketFd);
if (foundFlag == 1){ /* this is the monitor (with index i) that contains citizen with id "citizenId" */
string firstName = readString(monitorInfo[i].socketFd, socketBufferSize);
string lastName = readString(monitorInfo[i].socketFd, socketBufferSize);
string country = readString(monitorInfo[i].socketFd, socketBufferSize);
int age = readInt(monitorInfo[i].socketFd);
cout << endl << citizenId << " " << firstName << " " << lastName << " " << country << endl;
cout << "AGE " << age << endl;
while(1){
string isVaccinated = readString(monitorInfo[i].socketFd, socketBufferSize);
if (isVaccinated.compare("STOPPED") == 0)
break;
if (isVaccinated.compare("YES") == 0){ /* If citizen is in Vaccinated Skip List */
string virusName = readString(monitorInfo[i].socketFd, socketBufferSize);
cout << virusName << " VACCINATED ON ";
string dateStr = readString(monitorInfo[i].socketFd, socketBufferSize);
Date vaccinationDate;
vaccinationDate.getDateFromStr(dateStr);
vaccinationDate.printDate();
cout << endl;
}else if (isVaccinated.compare("NO") == 0){ /* If citizen is in NotVaccinated Skip List */
string virusName = readString(monitorInfo[i].socketFd, socketBufferSize);
cout << virusName << " NOT YET VACCINATED" << endl;
}
}
break;
}
}
if (foundFlag == -1)
cout << "There is no citizen with Id: " << citizenId << endl;
return 1;
}
/* Command 5 - /exit */
int exit(string InputWords[], int numMonitors, int socketBufferSize, Info monitorInfo[], BloomNode* bloomListHead){
/* There must be no other arguments => only eixt */
if ((InputWords[1].compare("\0") != 0)){
cout << "ERROR in command /exit" << endl;
return 0;
}
for (int i = 0; i < numMonitors; i++)
sendString(monitorInfo[i].socketFd, "/exit", socketBufferSize);
BloomListDelete(&bloomListHead);
for (int i = 0; i < numMonitors; i++)
CountryDeleteList(&(monitorInfo[i].countryListHead));
return 1;
}