-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbankingServer.c
More file actions
343 lines (315 loc) · 10.4 KB
/
bankingServer.c
File metadata and controls
343 lines (315 loc) · 10.4 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "banking.h"
pthread_mutex_t mut;
boolean isAccessing = FALSE;
boolean caughtInterupt = FALSE;
LLofFD* activeFD;
account* bankAccountsLL = NULL;
LLofFD* addFD(LLofFD* activeFD,int fd){
LLofFD* node = (LLofFD*)malloc(sizeof(LLofFD));
node->fd = fd;
node->next = NULL;
if(activeFD == NULL){
activeFD = node;
return activeFD;
}
LLofFD* ptr = activeFD;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = node;
return activeFD;
}
//searches linked list and returns pointer to account with the given name
account* search(char* searchName){
account* ptr = bankAccountsLL;
while(ptr != NULL){
if(strcmp(ptr->accountName,searchName) == 0)
return ptr;
ptr = ptr->next;
}
return NULL;
}
void addAccount(char* name){
//make a new node
account* newAccount = (account*)malloc(sizeof(account));
newAccount->accountName = (char*)malloc(sizeof(char)*255);
strcpy(newAccount->accountName, name);
newAccount->inSession = FALSE;
newAccount->currBalance = 0.0;
//point to front of list and set front to created node
newAccount->next = bankAccountsLL;
bankAccountsLL = newAccount;
}
//returns account name if it is being served, and NULL otherwise
char* communicateBankAccounts(char* command,char* accountName, int socketNum){
char* userArg = (char*)malloc(sizeof(char)*255);
account* nodePtr;
//if acountName is null, no account is being served
//otherwise accountName contains the current account
if(accountName != NULL){
nodePtr = search(accountName);
if(nodePtr == NULL){
//write(STDERR && socketNum,"Error:account not found",23);
send(socketNum,"Error: account not found",23,0);
}
}
//check which command is being recieved
if(strncmp(command,"create",6) == 0){
//get argument from command
strcpy(userArg,&command[7]);
if(accountName != NULL){
//write(STDERR && socketNum,"Error:Can't create account while in another account",53);
send(socketNum,"Error:Can't create account while in another account",53,0);
}
else if(search(userArg) != NULL){
//write(STDERR && socketNum,"Error: Account already exists",30);
send(socketNum,"Error: Account already exists",30,0);
}
else{
addAccount(userArg);
char* buffer = "Account created\n";
send(socketNum,"Account created",strlen(buffer),0);
//write(socketNum,buffer,15);
return NULL;
}
}
else if(strncmp(command,"serve",5) == 0){
strcpy(userArg,&command[6]);
nodePtr = search(userArg);
if(accountName != NULL){
//write(STDERR && socketNum,"Error:Can't serve account while in another account",51);
send(socketNum,"Error:Can't serve account while in another account",51,0);
}
else if(nodePtr == NULL){
send(socketNum, "Error: Account not found", 25, 0);
}
else if(nodePtr != NULL && nodePtr->inSession == TRUE){
send(socketNum, "Error: Account in session", 25, 0);
}
else{
send(socketNum,"In service",11,0);
//write(STDOUT && socketNum,"In service\n",12);
strcpy(userArg,&command[6]);
accountName = userArg;
nodePtr = search(userArg);
nodePtr->inSession = TRUE;
}
}
else if(strncmp(command,"deposit",7) == 0){
if(accountName == NULL){
//write(STDERR && socketNum,"Error: Can't deposit without an account",51);
send(socketNum,"Error: Can't deposit without an account",51,0);
}
else{
//write(STDOUT && socketNum,"Deposit completed\n",19);
send(socketNum,"Deposit completed",18,0);
strcpy(userArg, &command[8]);
double amount = atof(userArg);
nodePtr->currBalance = nodePtr->currBalance + amount;
}
}
else if(strncmp(command,"withdraw",8) == 0){
if(accountName == NULL){
//write(STDERR && socketNum,"Error: Can't withdraw without an account",51);
send(socketNum,"Error: Can't withdraw without an account",51,0);
}
else if(nodePtr->currBalance < atof(&command[9])){
send(socketNum, "Error: Balance cannot go negative",33, 0);
}
else{
//write(STDOUT && socketNum,"Withdraw completed",18);
send(socketNum,"Withdraw completed",18,0);
strcpy(userArg, &command[9]);
double amount = atof(userArg);
nodePtr->currBalance = nodePtr->currBalance - amount;
}
}
else if(strncmp(command,"query", 5) == 0){
if(accountName == NULL){
//write(STDERR && socketNum,"Error: Can't query without an account",51);
send(socketNum,"Error: Can't query without an account",51,0);
}
else{
char balanceToSend[500];
sprintf(balanceToSend, "$%.2f", nodePtr->currBalance);
send(socketNum, balanceToSend, strlen(balanceToSend), 0);
//write(socketNum,balanceToSend,strlen(balanceToSend));
}
}
else if(strncmp(command,"end", 3) == 0){
if(accountName == NULL){
//write(STDERR && socketNum,"Error: Can't exit without a session",51);
send(socketNum,"Error: Can't exit without a session",51,0);
}
else{
//write(STDOUT && socketNum,"Account exited",14);
send(socketNum,"Session ended", 13,0);
nodePtr->inSession = FALSE;
accountName = NULL;
//nodePtr = bankAccountsLL;
return NULL;
}
}
else{
//write(STDERR && socketNum,"Command not found",18);
send(socketNum,"Command not found",18,0);
}
return accountName;
}
//recieves commands from clients
void clientServerCommunication(void* args){
int fd = *(int *)args;
char buffer[1500];
char* currAccount = NULL;
int var = 0;
boolean isRunning = TRUE;
while(isRunning == TRUE){
/*
if(caughtInterupt == TRUE){
isRunning = FALSE;
send(fd, "Server exited", 15, 0);
exit(0);
}
*/
var = recv(fd,buffer,1500,0);
//receives error
if(var == -1){
printf("something went wrong\n");
isRunning = FALSE;
break;
}
//check data from server and perform command
buffer[var] = '\0';
if(strncmp(buffer, "quit", 4) == 0){
printf("Disconnecting from client\n");
if(currAccount != NULL){
communicateBankAccounts("end", currAccount, fd);
}
send(fd,"Client exiting",14,0);
isRunning = FALSE;
}
else{
if(currAccount){
//printf("Currently serving:%s\n", currAccount);
}
//wait while data is being accessed
while(isAccessing == TRUE){};
isAccessing = TRUE;
pthread_mutex_lock(&mut);
currAccount = communicateBankAccounts(buffer, currAccount, fd);
pthread_mutex_unlock(&mut);
isAccessing = FALSE;
}
sleep(1);
}
close(fd);
}
//accepts connections and spawns a thread for each client
void sessionAcceptor(void* args){
char* port = (char*)args;
struct sockaddr_in serv;
int socketNum;
socketNum = socket(AF_INET, SOCK_STREAM, 0);
//zero out server info struct and set type to TCP
bzero(&serv, sizeof(serv));
//set server address and port number
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port = htons(atoi(port));
//bind information to socket
bind(socketNum, (struct sockaddr *)&serv, sizeof(struct sockaddr));
//listen for connection, up to 1 connection
listen(socketNum, 1);
//pthread_t clientThreads[500];
//int clientCount = 0;
//accept new clients
while(caughtInterupt == FALSE){
//printf("listening...");
listen(socketNum,1);
//printf("listened");
int newFd = accept(socketNum,(struct sockaddr*)NULL, NULL);
printf("Successfully connected to client.\n");
write(newFd,"Successfully connected to server.",34);
activeFD = addFD(activeFD,newFd);
pthread_t clientServerCom;
pthread_create(&clientServerCom,0,clientServerCommunication,&newFd);
}
//wait for child threads
//int i;
//for(i = 0; i < clientCount; i++){
// pthread_join(clientThreads[i], NULL);
//}
printf("Exiting connection acceptor\n");
close(socketNum);
}
//print out data from list
//invoked by SIGALRM every 15 seconds
void timedOutput(){
//wait until other threads are not accessing data
//printf("%d\n",isAccessing);
while(isAccessing == TRUE){};
isAccessing = TRUE;
write(STDOUT, "\nDiagnostic Output:\n", 20);
account* ptr = bankAccountsLL;
if(ptr == NULL)
write(STDOUT, "List is empty\n", 14);
while(ptr != NULL){
char buffer[350];
int n = sprintf(buffer, "%s\t$%.2f\t", ptr->accountName, ptr->currBalance);
write(STDOUT, buffer, n);
if(ptr->inSession == TRUE)
printf("IN SESSION");
printf("\n");
ptr = ptr->next;
}
isAccessing = FALSE;
}
void setInterupt(){
write(STDOUT, "Caught user interupt. Exiting...\n", 34);
caughtInterupt = TRUE;
LLofFD* ptr = activeFD;
while(ptr != NULL){
send(ptr->fd,"Server exited",14,0);
close(ptr->fd);
ptr = ptr->next;
}
exit(0);
}
int main(int argc, char *argv[])
{
if(argc != 2){
write(STDOUT,"Error: Incorrect amount of arguments",37);
return 0;
}
char* portNum = (char*)malloc(sizeof(char) * 20);
strcpy(portNum,argv[1]);
//create new thread to accept clients
pthread_t saThread;
void (*saPtr)(void*) = sessionAcceptor;
pthread_create(&saThread, NULL, saPtr, (void *)portNum);
//set up signal handler and timer
signal(SIGALRM, timedOutput);
struct itimerval it, oldIt;
it.it_interval.tv_sec = 15;
it.it_interval.tv_usec = 0;
it.it_value = it.it_interval;
if(setitimer(ITIMER_REAL, &it, &oldIt) == -1)
write(STDERR, "error setting timer",19);
//set up sighandler for sigint
signal(SIGINT, setInterupt);
//wait for signals
while(caughtInterupt == FALSE)
sleep(1);
pthread_join(saThread, NULL);
return EXIT_SUCCESS;
}