-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.cpp
420 lines (384 loc) · 11.7 KB
/
utilities.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
#include "utilities.h"
#include <string>
bool file_exist(const char *name){
if( access( name, F_OK ) != -1 )
return true;
//file exists
else
return false;
// file doesn't exist
}
int create_directories(char path_name[MAX_STR_SIZE])
{
int mkdir_status = mkdir(path_name, S_IRUSR | S_IWUSR | S_IXUSR | S_IROTH | S_IWOTH | S_IXOTH);
char vfpath[MAX_STR_SIZE];
clear_buff(vfpath, MAX_STR_SIZE);
strcpy(vfpath, path_name);
strcat(vfpath, "/validators.txt");
char path_name_cmp[MAX_STR_SIZE], path_name_cst[MAX_STR_SIZE];
clear_buff(path_name_cmp, MAX_STR_SIZE);
clear_buff(path_name_cst, MAX_STR_SIZE);
strcpy(path_name_cmp, path_name);
strcat(path_name_cmp, "/candidates");
//strcpy(path_name_cst, path_name);
//strcat(path_name_cst, "/Customer");
mkdir_status = mkdir(path_name_cmp, S_IRUSR | S_IWUSR | S_IXUSR| S_IROTH | S_IWOTH | S_IXOTH);
//mkdir_status = mkdir(path_name_cst, S_IRUSR | S_IWUSR | S_IXUSR| S_IROTH | S_IWOTH | S_IXOTH);
return 0;
}
int numDigits(int number)
{
int digits = 0;
if (number < 0) digits = 1; // remove this line if '-' counts as a digit
while (number) {
number /= 10;
digits++;
}
return digits;
}
vector<string> read_available_ca_ports()
{
int open_fd = open("./DB/validators.txt", O_RDONLY);
if(open_fd < 0)
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
char read_buff[MAX_STR_SIZE];
clear_buff(read_buff, MAX_STR_SIZE);
int r_st = read(open_fd, read_buff, MAX_STR_SIZE-1);
vector<string> ports = mytokenizer(read_buff, "\n");
return ports;
}
vector<string> read_available_srvr_ports()
{
int open_fd = open("./DB/servers.txt", O_RDONLY);
if(open_fd < 0)
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
char read_buff[MAX_STR_SIZE];
clear_buff(read_buff, MAX_STR_SIZE);
int r_st = read(open_fd, read_buff, MAX_STR_SIZE-1);
vector<string> ports = mytokenizer(read_buff, "\n");
return ports;
}
int found_in(string str, vector<string> arr)
{
for(int i=0; i<arr.size(); i++)
{
if(str == arr[i])
return i;
}
return -1;
}
bool checkClientCommandValidation(vector<string> args)
{
if(args.size()<=0)
return false;
if(args[0]=="Connect"){
if(args.size()!=5){
cout <<"insufficient number of argument!\tcorrect form=>\tConnect Server 2000 Admin admin's password" << endl;
return false;
}
else
return true;
}
else if(args[0]=="Register"){
if(args.size()!=5){
cout <<"insufficient number of argument!\tcorrect form=>\tRegister 123456789 ghamar" << " ***** *****" << endl;
return false;
}else{
if(args[3]!=args[4]){
cout << "Passwords are not the same!" << endl;
return false;
}
}
}
else if(args[0]=="Show" && strncmp(args[1].c_str(),"Candidates", 10)==0)
{
if (args.size()!=2)
{
cout <<"incorrect number of argument!\tcorrect form=>\tShow Candidates" << endl;
return false;
}
if(strncmp(args[1].c_str(), "Candidates", 10)!=0)
{
cout <<"incorrect form \tcorrect form=>\tShow Candidates" << endl;
return false;
}
}
return true;
}
void show_server_commands()
{
cout<<"*****"<<endl;
cout<<" At first connect to one of Certification Authorizers to get its public key"<<endl;
cout<<" ,then Disconnect from it"<<endl;
cout<<" ,then your serving socket will start working to get votes"<<endl;
cout<<"*****"<<endl;
//show CA available ports
cout<<"\nThese are available port(s) to connect to Certification Authorizer\n"<<endl;
//read available ports
vector<string> avail_ports = read_available_ca_ports();
print_myvector(avail_ports);
sugg_comm_to_server();
}
void showClientCommands()
{
cout<<"*****"<<endl;
cout<<" At first connect to one of Certification Authorizers to get a ca"<<endl;
cout<<" ,then disconnect from it"<<endl;
cout<<" ,then connect to one of servers to choose your candidate"<<endl;
cout<<"*****"<<endl;
//show CA available ports
cout<<"\nThese are available port(s) to connect to Certification Authorizers\n"<<endl;
//read available ports
vector<string> avail_ca_ports = read_available_ca_ports();
print_myvector(avail_ca_ports);
cout<<"\nThese are the valid commands:\n\n";
char comm[][MAX_STR_SIZE] =
{ "Connect\t\t->\tConnect voter to Server\n"
, "Disconnect\t\t->\tDisconnect from the server by admin\n"
, "Register\t->\tRegister yourself to certificate authority\n"
, "Show Log\t->\tShow voters name to admin\n"
, "Vote\t\t->\tVote to specific candidate\n"
, "ShowCandidates\t->\tShow all the available candidates\n"
, "SetVotingTime\t->\tset voting time\n"
, "ExtendVotingTime\t->\textendVotingTime\n"
, "ShowServers\t->\tShow server port\n"
, "ShowCA\t->\tShow CA's port\n"
, "Exit\t\t->\tExit the Program\n\n"};
for(int p = 0; p < 11; p++)
cout<< comm[p];
}
/*{
if(found_in(portno, read_available_ca_ports()) < 0)
{
int fd_to_save_port = open("./DB/validators.txt", O_APPEND | O_RDWR);
cerr<<"args[1] is: "<<portno<< " in " <<portno.length()<<endl;
int dumm = write(fd_to_save_port, portno, portno.length());
dumm = write(fd_to_save_port, "\n", strlen("\n"));
if(dumm <0)
write(STDOUTFD, "Error while writing port\n", sizeof("Error while writing port\n"));
close(fd_to_save_port);
}
}*/
/*void save_srvr_port(string portno)
{
if(found_in(portno, read_available_srvr_ports()) < 0)
{
int fd_to_save_port = open("./DB/servers.txt", O_APPEND | O_RDWR);
cerr<<"args[1] is: "<<portno<< " in " <<portno.length()<<endl;
int dumm = write(fd_to_save_port, portno.c_str(), sizeof(portno));
dumm = write(fd_to_save_port, "\n", strlen("\n"));
if(dumm <0)
write(STDOUTFD, "Error while writing port\n", sizeof("Error while writing port\n"));
close(fd_to_save_port);
}
}*/
vector<string> mytokenizer_char(char* str,string delim){
vector<string> ans;
int j=0;
while(str[j]!='\0'){
string temp="";
for(int i=0; i<delim.length(); i++){
while(str[j]!='\0' && str[j]!=delim[i]){
temp += str[j];
j++;
}
if(str[j]!='\0')
j++;
cout<<"here is temp: " << temp << endl;
ans.push_back(temp);
}
}
return ans;
}
vector<string> mytokenizer(string str, string delim)
{
string tmp;
vector<string> res;
int i, tmp_index = 0, res_index = 0;
for(i = 0; i < str.length(); i++)
{
int cut = 0;
int j;
for(j = 0; j < delim.length(); j++)
{
if (i==str.length()-1 || str[i] == delim[j])
{
cut = 1;
break;
}
}
if(cut == 0)
{
tmp += str[i];
//tmp_index ++;
}
else
{
//int k;
//for(k = 0; k < tmp_index; k++)
// res[res_index][k] = tmp[k];
if(tmp=="")
break;
res.push_back(tmp);
tmp = "";
//res[res_index][k] = '\0';
//res_index ++;
//tmp_index = 0;
}
}
return res;
}
int clear_buff(char* c, int size)
{
if (size == 0) size = MAX_STR_SIZE;
int i;
for(i = 0; i < size; i++)
{
c[i] = 0;
}
}
void print_myvector(vector<string> v)
{
for(int i=0; i<v.size(); i++)
cout<<i+1<<": "<<v[i]<<endl;
}
void sugg_comm_to_voter()
{
string sugg = "\nThese are the valid commands:\n\nConnect\t\t->\tConnect to Server\nDC\t\t->\tDisconnect from the server\nIntroduction\t->\tIntroduce new customer to server\nGet Status\t->\tGets latest customer status from server\nUnregister\t->\tUnregister customer\nExit\t\t->\tExit the Program\n";
cout<<sugg<<endl;
}
void sugg_comm_to_server()
{
cout<<"\nThese are the valid commands:\n"<<endl;
cout<<"Connect\t\t->\tConnect to Server\nDC\t\t->\tDisconnect from the server\nIntroduction\t->\tIntroduce new company to server\nGet Status\t->\tGets latest company status from server\nExit\t\t->\tExit the Program\n"<<endl;
}
int show_candidates_name(char res[MAX_STR_SIZE])
{
int open_fd = open("./DB/candidates_names.txt", O_RDONLY);
if(open_fd < 0)
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
return read(open_fd, res, MAX_STR_SIZE-1);
}
vector<string> read_available_unames()
{
int open_fd = open("./DB/voters.txt", O_RDONLY);
if(open_fd < 0)
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
char read_buff[MAX_STR_SIZE];
clear_buff(read_buff, MAX_STR_SIZE);
int r_st = read(open_fd, read_buff, MAX_STR_SIZE-1);
vector<string> unames = mytokenizer(read_buff, "\n");
return unames;
}
int save_vote_comm(string uname)
{
if(found_in(uname, read_available_unames()) < 0)
{
int fd_to_save_uname = open("./DB/voters.txt", O_APPEND | O_RDWR);
cerr<<"uname is: "<<uname<< " in " <<uname.length()<<endl;
char* cstr = new char [uname.length()+1];
strcpy(cstr, uname.c_str());
int dumm = write(fd_to_save_uname, cstr, strlen(cstr));
dumm = write(fd_to_save_uname, "\n", strlen("\n"));
if(dumm <0)
write(STDOUTFD, "Error while writing port\n", sizeof("Error while writing port\n"));
close(fd_to_save_uname);
return dumm;
}
else
{
cout<<"Any voter can't vote more than once"<<endl;
return -1;
}
}
int voting(vector<string> v)
{
char fpath[MAX_STR_SIZE];
clear_buff(fpath, MAX_STR_SIZE);
strcpy(fpath, "./DB/candidates/");
strcat(fpath, v[2].c_str());
cerr<<"\n\t ### fpath is: "<<fpath<< " in " <<strlen(fpath)<<endl;
int fd_to_save_vote = open(fpath, O_RDWR);
if(fd_to_save_vote < 0)
{
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
return -1;
}
char read_buff[MAX_STR_SIZE];
clear_buff(read_buff, MAX_STR_SIZE);
int r_st = read(fd_to_save_vote, read_buff, MAX_STR_SIZE-1);
vector<string> data = mytokenizer(read_buff, "\n");
int tedaderay = atoi(data[1].c_str());
tedaderay ++;
string Result = static_cast<ostringstream*>( &(ostringstream() << tedaderay) )->str();
/*
char hichi[MAX_STR_SIZE];
clear_buff(hichi, MAX_STR_SIZE);
int dumm = write(fd_to_save_vote, hichi, strlen(hichi));
close(fd_to_save_vote);
fd_to_save_vote = open(fpath, O_APPEND | O_RDWR);
if(fd_to_save_vote < 0)
{
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
return -1;
}*/
char chi[MAX_STR_SIZE];
clear_buff(chi, MAX_STR_SIZE);
strcpy(chi, data[0].c_str());
strcat(chi, "\n");
strcat(chi, Result.c_str());
close(fd_to_save_vote);
fstream f;
f.open(fpath, std::fstream::out | std::fstream::trunc);
f<<"";
f.close();
fd_to_save_vote = open(fpath, O_RDWR);
if(fd_to_save_vote < 0)
{
write(STDOUTFD, "can't open available ports file", sizeof("can't open available ports file"));
return -1;
}
cerr<<"fpath is: "<<fpath<< " in " <<strlen(fpath)<<endl;
//char* cstr = new char [uname.length()+1];
//strcpy(cstr, uname.c_str());
// char hichi[MAX_STR_SIZE];
// clear_buff(hichi, MAX_STR_SIZE);
// dumm = write(fd_to_save_vote, hichi, strlen(hichi));
int dumm = write(fd_to_save_vote, chi, strlen(chi));
dumm = write(fd_to_save_vote, "\n", strlen("\n"));
if(dumm <0)
write(STDOUTFD, "Error while writing port\n", sizeof("Error while writing port\n"));
close(fd_to_save_vote);
return dumm;
}
bool in_time(my_time start_time, my_time stop_time)
{
time_t my_time = time(NULL);
my_time += 3 * 3600 + 30 * 60;
int second = my_time % 60;
int minute = (my_time % 3600) / 60;
int hour = (my_time % (3600 * 24)) / 3600;
if(hour>start_time.hour && hour<stop_time.hour)
return true;
else if(hour==start_time.hour && minute>=start_time.min && hour<stop_time.hour)
return true;
else if(hour>start_time.hour && hour==stop_time.hour && minute<=stop_time.min)
return true;
else
false;
}
/*int getdir (vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir("./DB/Candidates/")) == NULL) {
cout << "Error(" << errno << ") opening $$$$$$$$$$$$$$$$$$$$$$$$$" << "./DB/Candidates" << endl;
return errno;
}
cerr<<"\n dar baz kardane folder ok bude\n\n"<<endl;
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}*/