-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
437 lines (415 loc) · 14 KB
/
main.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <mpi.h>
#include "avl.h"
#include <unistd.h>
#include <cstdlib>
using namespace std;
Patient_Record get_patient_record(const string &line)
{
Patient_Record patient;
stringstream s_stream(line);
getline(s_stream, patient.id, ',');
s_stream >> patient.age;
s_stream.ignore();
s_stream >> patient.gender;
s_stream.ignore();
getline(s_stream, patient.blood_type, ',');
getline(s_stream, patient.date, ',');
getline(s_stream, patient.diagnosis);
return patient;
}
void log_file(const string &message)
{
ofstream logFile;
logFile.open("log.txt", ios::app);
if (!logFile)
{
cerr << "Failed to open the log file for appending." << std::endl;
return;
}
logFile << message;
logFile << endl;
logFile.close();
}
int extractPatientID(const string &patientID)
{
if (patientID.size() <= 1)
return 0;
try
{
return stoi(patientID.substr(1));
}
catch (const invalid_argument &e)
{
cerr << "Invalid patient ID format: " << patientID << endl;
return 0;
}
}
void read_file(const vector<string>& filenames, vector<string>& string_rec)
{
for (const auto& filename : filenames)
{
ifstream file(filename);
string line;
while (getline(file, line))
{
string_rec.push_back(line);
}
if (file.bad())
{
std::cerr << "Error: An error occurred while reading file " << filename << std::endl;
}
file.close();
}
}
string record_to_string(const Patient_Record &record)
{
stringstream s_stream;
s_stream << record.id << ",";
s_stream << record.age << ",";
s_stream << record.gender << ",";
s_stream << record.blood_type << ",";
s_stream << record.date << ",";
s_stream << record.diagnosis;
return s_stream.str();
}
void inorderTraversal(TreeNode *root, ofstream &file)
{
if (root != NULL)
{
inorderTraversal(root->left, file);
file << record_to_string(root->record) << endl;
inorderTraversal(root->right, file);
}
}
void create_backup(PatientAVL &patient_avl)
{
ofstream file("backup.txt", ios::app);
if (!file.is_open())
{
cout << "Error opening file!" << endl;
return;
}
inorderTraversal(patient_avl.get_root(), file);
file.close();
}
Patient_Record string_to_obj(const string &serialized)
{
Patient_Record record;
stringstream s_stream(serialized);
getline(s_stream, record.id, ',');
s_stream >> record.age;
s_stream.ignore();
s_stream >> record.gender;
s_stream.ignore();
getline(s_stream, record.blood_type, ',');
getline(s_stream, record.date, ',');
getline(s_stream, record.diagnosis);
return record;
}
void add_patient(PatientAVL &patient_avl, const Patient_Record &new_record)
{
string message;
patient_avl.insert(new_record);
cout << "Patient record added successfully." << endl;
message = "Patient record with ID '" + new_record.id + "' added successfully.";
log_file(message);
}
bool del_patient(PatientAVL &patient_avl, const string &del_id)
{
string message;
if (patient_avl.remove(del_id))
{
cout << "Patient record with ID '" << del_id << "' deleted successfully." << endl;
message = "Patient record with ID '" + del_id + "' deleted successfully.";
log_file(message);
return true;
}
else
{
cout << "Patient record with ID '" << del_id << "' not found." << endl;
message = "Patient record with ID '" + del_id + "' not found.";
log_file(message);
return false;
}
}
bool search_patient(PatientAVL &patient_avl, string search_id)
{
Patient_Record *node = patient_avl.search(search_id);
string message;
if (node != nullptr)
{
cout << "Patient record with ID '" << search_id << "' found successfully." << endl;
message = "Patient record with ID '" + search_id + "' found successfully.";
log_file(message);
cout << "Patient ID:" << search_id << endl;
cout << "Patient Age:" << node->age << endl;
cout << "Patient Gender:" << node->gender << endl;
cout << "Patient Blood Type:" << node->blood_type << endl;
cout << "Patient Date:" << node->date << endl;
cout << "Patient Diagnosis:" << node->diagnosis << endl;
return true;
}
else
{
cout << "Patient record with ID '" << search_id << "' not found." << endl;
message = "Patient record with ID '" + search_id + "' not found.";
log_file(message);
return false;
}
}
bool update_patient(PatientAVL &patient_avl, const Patient_Record &new_record)
{
Patient_Record *existing_record = patient_avl.search(new_record.id);
string message;
if (existing_record != nullptr)
{
existing_record->age = new_record.age;
existing_record->gender = new_record.gender;
existing_record->blood_type = new_record.blood_type;
existing_record->date = new_record.date;
existing_record->diagnosis = new_record.diagnosis;
cout << "Patient record with ID '" << new_record.id << "' updated successfully." << endl;
message = "Patient record with ID '" + new_record.id + "' updated successfully.";
log_file(message);
return true;
}
else
{
cout << "Patient record with ID '" << new_record.id << "' not found." << endl;
message = "Patient record with ID '" + new_record.id + "' not found.";
log_file(message);
return false;
}
}
int print_menu()
{
system("clear");
int choice;
cout<<endl<<endl<<endl;
cout << "\t\t\t\t\t\t\t\t1. Add a Patient\n";
cout << "\t\t\t\t\t\t\t\t2. Update a Patient\n";
cout << "\t\t\t\t\t\t\t\t3. Delete a Patient\n";
cout << "\t\t\t\t\t\t\t\t4. Search a Patient\n";
cout << "\t\t\t\t\t\t\t\t5. Create a Backup\n";
cout << "\t\t\t\t\t\t\t\t6. Recover from Backup\n";
cout<<"\t\t\t\t\t\t\t\tEnter Choice:";
cin >> choice;
return choice;
}
int main()
{
int rank, size, choice, file_count=3;
vector<string>file_arr(file_count);
file_arr.push_back("Patient_Records.txt");
file_arr.push_back("Additional_Patients.txt");
file_arr.push_back("Extra_Patient_Records.txt");
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int record_size;
vector<string> string_records;
PatientAVL patient_avl;
if (rank == 0)
{
read_file(file_arr, string_records);
record_size = string_records.size() / size;
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&record_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (rank==0){
for (int i = 0; i < record_size; ++i)
{
if (i + record_size < string_records.size()){
const auto &serial = string_records[i + record_size];
const char *buffer = serial.c_str();
int buffer_size = serial.size() + 1;
MPI_Send(buffer, buffer_size, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
}
auto &str = string_records[i];
patient_avl.insert(string_to_obj(str));
}
int pat_id;
string str_id;
const char *cstr;
int str_length;
Patient_Record new_record;
string str_record;
do
{
choice = print_menu();
MPI_Send(&choice, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
switch (choice)
{
case 1:
{
cout << "Enter patient ID (P0000): ";
cin >> new_record.id;
cout << "Enter patient age: ";
cin >> new_record.age;
cout << "Enter patient gender (M/F): ";
cin >> new_record.gender;
cout << "Enter patient blood type: ";
cin >> new_record.blood_type;
cout << "Enter diagnosis date (MM/DD/YYYY): ";
cin >> new_record.date;
cout << "Enter diagnosis: ";
cin.ignore();
getline(cin, new_record.diagnosis);
string str_record = record_to_string(new_record);
const char *buff = str_record.c_str();
int buff_size = str_record.size() + 1;
MPI_Send(buff, buff_size, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
}
break;
case 2:
{
cout << "Enter the ID of the patient you want to update: ";
cin >> str_id;
pat_id = extractPatientID(str_id);
new_record.id = str_id;
cout << "Enter patient age: ";
cin >> new_record.age;
cout << "Enter patient gender (M/F): ";
cin >> new_record.gender;
cout << "Enter patient blood type: ";
cin >> new_record.blood_type;
cout << "Enter diagnosis date (MM/DD/YYYY): ";
cin >> new_record.date;
cout << "Enter diagnosis: ";
cin.ignore();
getline(cin, new_record.diagnosis);
string str_record = record_to_string(new_record);
const char *buff = str_record.c_str();
int buff_size = str_record.size() + 1;
MPI_Send(buff, buff_size, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
update_patient(patient_avl, new_record);
}
break;
case 3:
cout << "Enter ID to Delete (P0000):";
cin >> str_id;
cstr = str_id.c_str();
str_length = str_id.length() + 1;
MPI_Send(cstr, str_length, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
del_patient(patient_avl, str_id);
break;
case 4:
cout << "Enter ID to search (P0000):";
cin >> str_id;
pat_id = extractPatientID(str_id);
cstr = str_id.c_str();
str_length = str_id.length() + 1;
MPI_Send(cstr, str_length, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
search_patient(patient_avl, str_id);
break;
case 5:
create_backup(patient_avl);
break;
case 6:
{
ifstream file("backup.txt");
if (file.is_open())
{
patient_avl.deleteTree();
string_records.clear();
file_arr.clear();
file_arr.push_back("backup.txt");
read_file(file_arr, string_records);
record_size = string_records.size() / size;
MPI_Bcast(&record_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
for (int i = 0; i < record_size; ++i)
{
if (i + record_size < string_records.size()){
const auto &serial = string_records[i + record_size];
const char *buffer = serial.c_str();
int buffer_size = serial.size() + 1;
MPI_Send(buffer, buffer_size, MPI_CHAR, 1, 0, MPI_COMM_WORLD);
}
auto &str = string_records[i];
patient_avl.insert(string_to_obj(str));
}
}
}
break;
default:
cout << "Invalid Choice\n";
break;
}
sleep(7);
} while (choice != 7);
}
if(rank==1){
PatientAVL patient_avl;
Patient_Record new_record;
string id;
for (int i = 0; i < record_size; ++i)
{
char buffer[1024];
MPI_Recv(buffer, sizeof(buffer), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
string recv_records(buffer);
Patient_Record record = string_to_obj(recv_records);
patient_avl.insert(record);
}
while (choice != 7)
{
MPI_Recv(&choice, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (choice <= 2)
{
char buffer[1024];
MPI_Recv(buffer, sizeof(buffer), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
string recv_records(buffer);
new_record = string_to_obj(recv_records);
}
else if (choice > 2 && choice < 5)
{
char buffer[1024];
MPI_Recv(buffer, sizeof(buffer), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
buffer[sizeof(buffer) - 1] = '\0';
string str_id(buffer);
id = str_id;
}
switch (choice)
{
case 1:
add_patient(patient_avl, new_record);
break;
case 2:
update_patient(patient_avl, new_record);
break;
case 3:
del_patient(patient_avl, id);
break;
case 4:
search_patient(patient_avl, id);
break;
case 5:
create_backup(patient_avl);
cout << "Backup completed successfully!" << endl;
break;
case 6:
patient_avl.deleteTree();
for (int i = 0; i < record_size; ++i)
{
char buffer[1024];
MPI_Recv(buffer, sizeof(buffer), MPI_CHAR, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
string recv_records(buffer);
Patient_Record record = string_to_obj(recv_records);
patient_avl.insert(record);
}
cout<<"Recovery Successful"<<endl;
break;
default:
cout << "Invalid Choice\n";
break;
}
}
}
MPI_Finalize();
return 0;
}