-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.cpp
198 lines (162 loc) · 6.31 KB
/
helper.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
#include "helper.h"
using namespace std;
// global
std::vector<int> vector_clocks; // vector clocks of knowledge of all processess/nodes
// operation arguemnt: 0 - fetech curr proc count; 1 increase counter by 1; -1 decrease counter by 1
int processes_counter(int operation){
int proc_ctr_current_value;
string line;
ifstream proc_ctr_file;
proc_ctr_file.open(PROCESSES_COUNTER_FILE_NAME);
if (proc_ctr_file.is_open()){
try{
getline(proc_ctr_file, line);
proc_ctr_current_value = stoi(line.c_str());
if (operation == 0){ // only read current value
proc_ctr_file.close();
return proc_ctr_current_value;
}
}catch(std::exception const & e) {
cout << "Exception Caught '" << e.what() << "'.\n";
}
proc_ctr_file.close();
} else {
cout << "Unable to open the '" << PROCESSES_COUNTER_FILE_NAME << "' file, please check the name!\n";
exit(-1);
}
// increase or decrease the processes counter value
ofstream proc_ctr_file_modify;
proc_ctr_file_modify.open(PROCESSES_COUNTER_FILE_NAME, ios::trunc); // ios::trunc: previous content is deleted
if (proc_ctr_file_modify.is_open()){
if (operation == 1){// increase by 1
proc_ctr_current_value ++;
}else if (operation == -1){// decrease by 1
proc_ctr_current_value --;
}
// write into
proc_ctr_file_modify << proc_ctr_current_value;
proc_ctr_file_modify.close();
} else {
cout << "Unable to open the '" << PROCESSES_COUNTER_FILE_NAME << "' file, please check the name!\n";
exit(-1);
}
return proc_ctr_current_value;
}
int declare_processes_amount(int declared_amount){
ofstream proc_amount_declared;
proc_amount_declared.open(PROCESSES_AMOUNT_DECLARED_FILE_NAME, ios::trunc); // ios::trunc: previous content is deleted
if (proc_amount_declared.is_open()){
// write into
proc_amount_declared << declared_amount;
proc_amount_declared.close();
} else {
cout << "Unable to open the '" << PROCESSES_AMOUNT_DECLARED_FILE_NAME << "' file, please check the name!\n";
exit(-1);
}
return declared_amount;
}
int fetch_processes_declared_amount(){
string line;
ifstream proc_amount_declared;
proc_amount_declared.open(PROCESSES_AMOUNT_DECLARED_FILE_NAME);
if (proc_amount_declared.is_open()){
try{
getline(proc_amount_declared, line);
int proc_ctr_current_value = stoi(line.c_str());
proc_amount_declared.close();
return proc_ctr_current_value;
}catch(std::exception const & e) {
cout << "Exception Caught '" << e.what() << "'.\n";
}
} else {
cout << "Unable to open the '" << PROCESSES_AMOUNT_DECLARED_FILE_NAME << "' file, please check the name!\n";
exit(-1);
}
}
int ask_user_input_declared_proc_amt(){
int declared_amount = fetch_processes_declared_amount();
if (declared_amount <= 2) {
cout << "\nHow many processes(nodes) are you going to create (which must >= 3 and you must follow)? Please input a positive integer: ";
try{
cin >> declared_amount;
}catch(std::exception const & e) {
cout << "Invalid input. Exception Caught '" << e.what() << "'.\n";
}
cout << "\nYou declared you will create " << declared_amount << " processes(nodes). You must follow it.\n" << endl;
// bc this must be first process so also clean processes_counter to double ensure
// This avoid the effect of sb closed terminal windows directly at last running
int proc_ctr = processes_counter(0);
if(proc_ctr != 0){
ofstream proc_ctr_cleaner;
proc_ctr_cleaner.open(PROCESSES_COUNTER_FILE_NAME, ios::trunc); // ios::trunc: previous content is deleted
if (proc_ctr_cleaner.is_open()){
// write into
proc_ctr_cleaner << 0;
proc_ctr_cleaner.close();
} else {
cout << "Unable to open the '" << PROCESSES_COUNTER_FILE_NAME << "' file, please check the name!\n";
exit(-1);
}
}
}
return declared_amount;
}
string vectorint2str(vector<int>& vc){
std::stringstream ss;
for (int i = 0; i < vc.size()-1; i++) {
ss << vc.at(i) << ", ";
}
ss << vc.back();
std::string s = ss.str();
return s;
}
string vectorstr2str(vector<string>& vc){
std::stringstream ss;
for (int i = 0; i < vc.size()-1; i++) {
ss << vc.at(i) << ", ";
}
ss << vc.back();
std::string s = ss.str();
return s;
}
void print_vecotr_clocks(vector<int>& vc){
// Print out the vector
std::cout << "\nvector_clocks = {";
std::cout << vectorint2str(vc) << "}; \n\n";
}
// function for string delimiter
vector<string> split(string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;
while ((pos_end = s.find (delimiter, pos_start)) != string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back( token );
}
res.push_back( s.substr(pos_start) );
return res;
}
void clean_proc_ctr(){
// clean the processes counter value
ofstream proc_ctr_file_modify;
proc_ctr_file_modify.open(PROCESSES_COUNTER_FILE_NAME, ios::trunc); // ios::trunc: previous content is deleted
if (proc_ctr_file_modify.is_open()){
// write into
proc_ctr_file_modify << 0;
proc_ctr_file_modify.close();
} else {
cout << "Unable to open the '" << PROCESSES_COUNTER_FILE_NAME << "' file, please check the name!\n";
exit(-1);
}
}
// Define the function to be called when ctrl-c (SIGINT) is sent to process
void signal_callback_handler(int signum) {
cout << "\n\nCaught Ctrl+c signal " << signum << endl;
declare_processes_amount(0); // clean the declared proc amount
clean_proc_ctr(); // clean the proc ctr
printf("Multicast: process stopped. \n");
cout << "There are still " << processes_counter(0) << " processes/nodes left in " << PROCESSES_COUNTER_FILE_NAME << " now." << endl;
// Terminate program
exit(signum);
}