-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoding.cpp
More file actions
458 lines (427 loc) · 16.3 KB
/
Coding.cpp
File metadata and controls
458 lines (427 loc) · 16.3 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <string.h>
#include <pthread.h>
#include <stdbool.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
//////// DATA BASE Entrys //////////
typedef struct data_base_entry{
char accnum[6];
double fundsav;
char pin[4];
} DB;
//message truct for sending between atm and DBserver.
typedef struct my_msgbuf {
long msgtype;
struct msgContents{
char upin[4];
int uchoice;
char uaccnum[6];
int stat; // 1 if ok, 0 if wrong input
double ufundsav;
double uwithdraw;
}contents;
}my_msgbuf;
void *atmfunc(void *msqid);
void *dbServer(void *msqid);
void *dbEditor(void *zero);
void getInput(char *question, char *returnString, int inputLength);
char* getFeild(char* line, int n, char c);
void getFeild2(char* line, int n, char* retrunStr);
void initBuff(my_msgbuf* buf, int msgtypeNum);
void makeLine(char* line, char* arg1, char* arg2, char* arg3);
//Globals
//constants
#define NUMoTHREADs 3
pthread_t threads[NUMoTHREADs];
///this is a shared variable of type DB//TODO: get rid of this one;
///////////////______MAIN_____////////////////////////
int main(){
///////// Message Queue creation///////
int msqid;
int msgtype;
int msgflg = 0666 | IPC_CREAT;
key_t key;
my_msgbuf sbuf,rbuf;
size_t buf_length;
key=1234;
if ((msqid = msgget(key, msgflg)) < 0) {
perror("msgget");
exit(1);
}else{
(void)fprintf(stderr,"msgget: msgget succeeded: msqid = %i\n", msqid);
}
//IT'S PTHREAD TIME:::
if(pthread_create(&threads[0], NULL, dbEditor, (void *)0)){
printf("ERROR: pthread 2 supposed to return 0\n");
exit(1);
}
pthread_join(threads[0], NULL);
int rc=0;
if(rc=pthread_create(&threads[1], NULL, atmfunc, (void *)(long)msqid)){
printf("ERROR: pthread 0 supposed to return 0\n");
exit(1);
}else{
printf("pthreadsuccessful return code = %i\n",rc);
}
if(pthread_create(&threads[2], NULL, dbServer, (void *)(long)msqid)){
printf("ERROR: pthread 1 supposed to return 0\n");
exit(1);
}else {
printf("New server:\n");
}
pthread_join(threads[1], NULL);
pthread_join(threads[2], NULL);
printf("exiting main\n");
pthread_exit(NULL);
return 0;
};
//***************Functions*************************//
void *atmfunc(void *msq){
printf("reached beginning of atmfunc\n");
my_msgbuf sbuf, rbuf;
char uaccnum1[6];
std::string tempstr;
char tempAccNum[6];
int uchoice1;
char choice[50];
int msqid = (int)(long)msq;
char upin1[4];
printf("Message que id as recieved is: %i\n", msqid);
int toServer = 1;
int fromServer = 2;
int msgLength = sizeof(rbuf.contents);
for(int attemptCount = 3; attemptCount>0; attemptCount--){
initBuff(&sbuf, toServer);
initBuff(&rbuf, fromServer);
getInput((char*)"ATM: Enter your Account Number:", uaccnum1, 5);
if (strstr(uaccnum1, "x")!=NULL || strstr(uaccnum1, "X")!=NULL){
sbuf.contents.stat=2;
break;
}
getInput((char*)"ATM: Enter your pin number:", upin1, 3);
if (strstr(upin1, "x")!=NULL || strstr(upin1, "X")!=NULL){
sbuf.contents.stat=2;
break;
}
if(strcmp(tempAccNum, uaccnum1)!=0) {
attemptCount=3;
strcpy(tempAccNum, uaccnum1);
}
strcpy(sbuf.contents.uaccnum, uaccnum1);
strcpy(sbuf.contents.upin, upin1);
if(attemptCount==1)sbuf.contents.stat=1;
printf("ATM: Sending Message\n");
if(msgsnd(msqid, &sbuf, msgLength, IPC_NOWAIT) < 0 ){// sending account number
perror("ATM: msgsnd\n");
exit(1);
}
printf("ATM: Message sent\n");
////////
if (msgrcv(msqid, &rbuf, msgLength, fromServer, 0) < 0) {//receiving 1 if account exist and 0 if it doesnt
perror("ATM: msgrcv\n");
exit(1);
}
printf("ATM: Message recieved\n");
if(rbuf.contents.stat==1){//then the account number exist
attemptCount = 4; //sets to 4 so that the loop will start again with attemptCount == 3;
uchoice1=3;
while (uchoice1>2 || uchoice1<0){
printf("ATM: Choose from the following menu:\n 1- Display Funds \n 2- Withdraw Funds\n");
scanf("%s", choice);
if(strstr(choice, "x")!=NULL || strstr(choice, "X")!=NULL){
sbuf.contents.stat=2;
break;
}
uchoice1=atoi(choice);
}
if(sbuf.contents.stat==2) break;
sbuf.contents.uchoice=uchoice1;
if(uchoice1==2){//Withdraw amount
printf("ATM: Please enter the amount you would like to withdraw: \n");
scanf("%lf", &sbuf.contents.uwithdraw);
}
if (msgsnd(msqid, &sbuf, msgLength, IPC_NOWAIT) < 0) {// Withdrawing funds
perror("ATM: msgsnd");
exit(1);
}
if (msgrcv(msqid, &rbuf, msgLength, fromServer, 0) < 0) {//receiving available funds
perror("ATM: msgrcv");
exit(1);
}
if(uchoice1==1){//user chose to see funds available
printf("ATM: Account Balance: %lf\n", rbuf.contents.ufundsav);
}else if(uchoice1==2){
if(rbuf.contents.stat==3){
printf("ATM: not enough funds\n");
}else {
printf("ATM: Enough funds\nNew Account Balance: %lf\n", rbuf.contents.ufundsav);
}
}
}
}//ENDFOR
if (msgsnd(msqid, &sbuf, msgLength, IPC_NOWAIT) < 0) {// Withdrawing funds
perror("ATM: msgsnd");
exit(1);
}
};
void *dbServer(void *msq){
my_msgbuf rbuf, sbuf;
int toATM = 2;
int fromATM = 1;
char lines[100][50];
bool found = false;
int msqid = (int)(long)msq;
int msgLength = sizeof(rbuf.contents);
DB shared[100];
FILE* io;
char line[1024];
int sharedSize=0;
printf("Server: entered server\n");
while (rbuf.contents.stat!=2){
found=false;
initBuff(&rbuf, fromATM);
initBuff(&sbuf, toATM);
if (msgrcv(msqid, &rbuf, msgLength, fromATM, 0) < 0) { //what is message type, I wrote it accnum in this case
perror("msgrcv");
exit(1);
}
printf("Server: message recieved \n");
if(rbuf.contents.stat==2) {
printf("Server: breaking\n");
break;
}
//fill shared data
io = fopen("database.txt", "r");
for(sharedSize=0; fgets(line, 1024, io); sharedSize++){
strcpy(lines[sharedSize], line);
getFeild2(line, 1, shared[sharedSize].accnum);
getFeild2(line, 2, shared[sharedSize].pin);
shared[sharedSize].fundsav = atof(getFeild(line, 3, ','));
}
fclose(io);
printf("Server: finished initializing shared\n");
for(int i=0; i<sharedSize && !found; i++){
printf("Server: account number is %s, while account number #%d is %s \n", rbuf.contents.uaccnum, i, shared[i].accnum);
if(strcmp(rbuf.contents.uaccnum, shared[i].accnum)==0){
found=true;
printf("Server: first if reached in accnumber= %s..\n", shared[i].accnum);
printf("the pin we have for this account is: %s, and expected pin is: %s\n", rbuf.contents.upin, shared[i].pin);
if(strcmp(rbuf.contents.upin, shared[i].pin)==0){ //pin nuber correct
printf("Server: seccond if reached stat is one\nServer: sending message\n");
sbuf.contents.stat=1;
if(msgsnd(msqid, &sbuf, msgLength, IPC_NOWAIT) < 0 ){
perror("Server: msgsnd");
exit(1);
}
//User makes choice about weather they want to deposit or withdraw funds and how much they want to withdraw;
if (msgrcv(msqid, &rbuf, msgLength, fromATM, 0) < 0) { //what is message type, I wrote it accnum in this case
perror("Server:msgrcv");
exit(1);
}
if(rbuf.contents.stat==2)break;
printf("Server: Choice message recieved\n");
if(rbuf.contents.uchoice==1){//if it is equal to one then user chose to see available funds
sbuf.contents.ufundsav=shared[i].fundsav;
printf("Server: sending back available funds\n");
}else if(rbuf.contents.uchoice==2){//user chose to withdraw amount
if(rbuf.contents.uwithdraw<=shared[i].fundsav){
shared[i].fundsav = shared[i].fundsav - rbuf.contents.uwithdraw;
sbuf.contents.ufundsav = shared[i].fundsav;
printf("Server: returning successful withdrawral funds left\n..rewriting Database...\n");
stringstream ss;
ss << (double)shared[i].fundsav;
makeLine(lines[i], shared[i].accnum, shared[i].pin, strdup(ss.str().c_str()));
printf("Server: %s\n", lines[i]);
//REWRITE
io = fopen("database.txt", "w+");
for (int j=0; j<sharedSize; j++){
fputs(lines[j], io);
}
fclose(io);
}else{
sbuf.contents.stat=3;
sbuf.fundsav=shared[i].fundsav;
printf("Server: Returning not enough fundsav\n");
}
}
}else{//incorrect pin number
if(rbuf.contents.stat==1){
printf("Server: 3rd Attempt, Locking Account Num %s\n",rbuf.contents.uaccnum);
sbuf.contents.stat=5;
lines[i][0]='x';
//REWRITE
io = fopen("database.txt", "w+");
for (int j=0; j<sharedSize; j++){
fputs(lines[j], io);
}
fclose(io);
}else sbuf.contents.stat=0;
}
}
}
if (!found && rbuf.contents.stat!=2){
sbuf.contents.stat=3;
printf("Server: returning Invalid Account number\n");
}
if(rbuf.contents.stat!=2){
if(msgsnd(msqid, &sbuf, msgLength, IPC_NOWAIT) < 0 ){
perror("msgsnd");
exit(1);
}
}
}//ENDWHILE
printf("Server: exiting\n");
};
void *dbEditor(void *zero){
char newpin[4];
double amount;
char amountStr[50];
char decision=0x00;
char newaccnum[6];
bool found = false;
char temp=0x00;
char line[1024];
int accnumLine=0;
char lines[100][50];
FILE* io;
char *initialArr[4] = {(char*)"00001,107,3443.22,\n", (char*)"00011,323,10089.97,\n", (char*)"00117,259,112.00,\n", (char*)NULL};
printf("enteredDBEditor\n");
printf("would you like to set the database back to default? [Y|n]\n");
scanf("%c", &decision);
if (decision == 'y' || decision == 'Y'){
io = fopen("database.txt", "w+");
for (int i=0; i<3; i++) {
printf("in for %i\n", i);
fputs(initialArr[i], io);
}
fclose(io);
printf("reset file\n");
}
printf("oppenned File data\n");
printf("Make changes to database?? [Y|n]\n");
scanf("%c", &temp);
scanf("%c", &decision);
if (decision == 'y' || decision == 'Y'){
printf("enter an account number, if it exiests you will be asked to delete or edit it\n");
printf("if it does not, it will be added and you will be asked to edit its specifics\n");
scanf("%s", newaccnum);
int lncount=0;
found = false;
io = fopen("database.txt", "r+");
while(fgets(line, 1024, io)){
strcpy(lines[lncount], line);
if (strcmp(getFeild(line, 1, ','), newaccnum)==0){
found = true;
accnumLine = lncount;
}
lncount++;
}
fclose(io);
for(int i=0; i<lncount; i++)printf("line at %i = %s\n", i, lines[i]);
if(found){
printf("Account number found would you like to delete or edit?[d|e]\n");
scanf("%c", &temp);
scanf("%c", &decision);
if(decision == 'd' || decision == 'D'){
//REWRITE
io = fopen("database.txt", "w+");
for (int i=0; i<lncount; i++){
if(i!=accnumLine)fputs(lines[i], io);
}
fclose(io);
}else {
printf("would you like to change the account ballance or the pin? [b/p]\n");
scanf("%c", &temp);
scanf("%c", &decision);
if(decision == 'b' || decision == 'B'){
getInput((char*)"What would you like to change the balance to?\n", amountStr, 49);
strcpy(newpin, getFeild(lines[accnumLine], 2, ','));
makeLine(lines[accnumLine], newaccnum, newpin, amountStr);
printf("line: %s\n", lines[accnumLine]);
//REWRITE
io = fopen("database.txt", "w+");
for (int i=0; i<lncount; i++){
fputs(lines[i], io);
}
fclose(io);
}else{
getInput((char *)"What would you like to change the PIN to?", newpin, 3);
strcpy(amountStr, getFeild(lines[accnumLine], 3, ','));
makeLine(lines[accnumLine], newaccnum, newpin, amountStr);
//REWRITE
io = fopen("database.txt", "w+");
for (int i=0; i<lncount; i++){
fputs(lines[i], io);
}
fclose(io);
}
}
}else {
accnumLine=lncount;
lncount++;
printf("Account not found,\n");
printf("..Adding account number to database\n");
getInput((char *)"Please enter the pin for the account (3 characters please)", newpin, 3);
getInput((char *)"Please enter the new account balance", amountStr, 49);
makeLine(lines[accnumLine], newaccnum, newpin, amountStr);
printf("Printing new line to file\n");
io=fopen("database.txt", "w+");
for (int i=0; i<lncount; i++){
fputs(lines[i], io);
}
fclose(io);
}
}
printf("Exiting database editor\n");
}
void getInput(char *question, char *returnString, int inputLength){
std::string tempstr;
printf("%s *you may only use %d charavters:\n", question, inputLength);
cin >> tempstr;
strcpy(returnString, strdup(tempstr.substr(0,inputLength).c_str()));
};
char* getFeild(char* line, int n, char c){
char* tok;
for (tok = strtok(line, &c); tok && *tok; tok = strtok(NULL, ("%c\n",&c))){
if (!--n)
return tok;
}
return NULL;
}
void getFeild2(char* inLine, int n, char* returnStr){
char* tok;
char* line = strdup(inLine);
for(tok = strtok(line, ","); tok && *tok && n; tok = strtok(NULL, ("%c\n", ","))){
if (!--n)
strcpy(returnStr, tok);
}
return;
}
void initBuff(my_msgbuf* buf, int msgtypeNum){
buf->msgtype=msgtypeNum;
strcpy(buf->contents.upin, (char*)"upi");
buf->contents.uchoice=0;
strcpy(buf->contents.uaccnum, (char*)"*****");
buf->contents.stat=0;
buf->contents.ufundsav=0.0;
buf->contents.uwithdraw=0.0;
}
void makeLine(char* line, char* arg1, char* arg2, char* arg3){
strcpy(line, arg1);
strcat(line,",");
strcat(line, arg2);
strcat(line, ",");
strcat(line, arg3);
strcat(line, ",\n");
}
//void rewrite()