-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappserver-coarse.c
More file actions
264 lines (221 loc) · 6.1 KB
/
appserver-coarse.c
File metadata and controls
264 lines (221 loc) · 6.1 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include "appserver.h"
//Delete this later, this is for testing the queue
void display(request *head);
//Function to setup the queue
void queueInit();
//This is the function that processes the users commands stored in the queue
void * processCmd();
queue *q;
account *accounts;
pthread_mutex_t queueMutex;
pthread_mutex_t bankLock;
FILE *output;
int id = 1;
int running =1;
int i;
int main (int argc, char *argv[]){
//Check for valid arguments to the program
if(argc != 4){
printf("Launch the server with the following syntax\n");
printf("./appserver <# of worker thread> <# of accounts> <output file>\n");
exit(1);
}
//Setup the queue
q = (queue*) malloc(sizeof(queue));
queueInit();
pthread_mutex_init(&queueMutex, NULL);
//Set the number of treads and accounts according to the arguments
int workerThreads = atoi(argv[1]);
int numAccounts = atoi(argv[2]);
char outName[strlen(argv[3]+1)];
strcpy(outName, argv[3]);
outName[strlen(argv[3])] = '\0';
output= fopen(outName, "w");
//Allocate memory for the accounts
accounts = (account*) malloc(numAccounts*sizeof(account));
//Setup the accounts
initialize_accounts(numAccounts);
for(i=0; i<numAccounts; i++){
pthread_mutex_init(&(accounts[i].lock), NULL);
accounts[i].value = 0;
}
pthread_t threads[workerThreads];
for(i=0; i<workerThreads; i++){
pthread_create(&threads[i], NULL, processCmd, NULL);
}
pthread_mutex_init(&bankLock, NULL);
printf("Works fine up to the main server loop\n");
//Main server loop that does everthing
while(running){
printf("> ");
char request[1024];
fgets(request, 1024, stdin);
request[strlen(request)-1] = '\0';
if((strcmp(request, "END")) == 0){
running = 0;
break;
}
pthread_mutex_lock(&queueMutex);
push(request, id);
printf("< ID %d\n", id);
pthread_mutex_unlock(&queueMutex);
id++;
}
for(i=0; i<workerThreads; i++){
pthread_join(threads[i], NULL);
}
fclose(output);
return 0;
}
void queueInit(){
q->front = NULL;
q->rear = NULL;
q->count =0;
}
void display(request *head){
if(head == NULL){
printf("NULL\n");
} else {
printf("%s\n", head->command);
display(head->next);
}
}
void push(char *cmd, int requestId){
request *toAdd = malloc(sizeof(request));
toAdd->command = malloc(1024*sizeof(char));
strncpy(toAdd->command, cmd, 1024);
toAdd->requestId = requestId;
gettimeofday(&(toAdd->timeStart),NULL);
toAdd->next = NULL;
if(q->count > 0){
q->rear->next = toAdd;
q->rear = toAdd;
q->count = q->count+1;
} else {
q->front = toAdd;
q->rear = toAdd;
q->count = 1;
}
}
request pop(){
request *temp;
request toPop;
if(q->count >0){
toPop.requestId = q->front->requestId;
toPop.command = malloc(1024 * sizeof(char));
toPop.timeStart = q->front->timeStart;
strncpy(toPop.command, q->front->command, 1024);
toPop.next = NULL;
temp = q->front;
q->front = q->front->next;
free(temp->command);
free(temp);
if(!q->front){
q->rear = NULL;
}
q->count = q->count -1;
} else {
toPop.command = NULL;
}
return toPop;
}
void * processCmd(){
while(running || q->front != NULL){
pthread_mutex_lock(&queueMutex);
if(q->front != NULL){
request req;
req = pop();
pthread_mutex_unlock(&queueMutex);
//Create a string array of the request parameters
int arrayLen = strlen(req.command);
char charArray[arrayLen+1];
strcpy(charArray, req.command);
charArray[arrayLen = '\0'];
int length = strlen(charArray);
int j;
int spaces =0;
for(j =0; j<length; j++){
if(charArray[j] == ' '){
spaces++;
}
}
//Initialize the argument array to have space for a null at the end
char* command[(spaces+2)];
//Initialize the entire array to be null to start
for(j =0; j<spaces+2; j++){
command[j] = NULL;
}
int i = 0;
//Cut the string string up into individual array indexes
char * cut = strtok(charArray, " ");
while(cut != NULL){
command[i] = cut;
cut = strtok(NULL, " ");
i++;
}
//If the request is a check request
if(strcmp(command[0], "CHECK") == 0){
int balance;
int accountNum = atoi(command[1]);
pthread_mutex_lock(&bankLock);
balance = read_account(accountNum);
pthread_mutex_unlock(&bankLock);
struct timeval finished;
gettimeofday(&finished, NULL);
fprintf(output, "%d BAL %d TIME %d.%06d %d.%06d\n", req.requestId, balance, req.timeStart.tv_sec, req.timeStart.tv_usec, finished.tv_sec, finished.tv_usec);
}
else if((strcmp(command[0], "TRANS")) == 0){
int numOfTrans = spaces/2;
int accountNums[numOfTrans];
int amounts[numOfTrans];
int ISF=0;
int i;
int accIndex =0;
int amIndex =0;
//Loop through the command array, starting at 1
//Odd indexes should be the account num, and even should be the ammounts
pthread_mutex_lock(&bankLock);
for(i=1; i<spaces+1; i++){
if(i%2 == 0){
amounts[amIndex] = atoi(command[i]);
amIndex++;
}
else{
accountNums[accIndex] = atoi(command[i]);
accIndex++;
}
}
for(i=0; i<numOfTrans; i++){
int accBalance = read_account(accountNums[i]);
if((accBalance + amounts[i]) < 0){
ISF=1;
break;
}
}
if(ISF){
struct timeval finished;
gettimeofday(&finished, NULL);
fprintf(output, "%d ISF %d TIME %d.%06d %d.%06d\n", req.requestId, accountNums[i], req.timeStart.tv_sec, req.timeStart.tv_usec, finished.tv_sec, finished.tv_usec);
}
else{
for(i=0; i< numOfTrans; i++){
account acc = accounts[accountNums[i]-1];
int accBalance = read_account(accountNums[i]);
write_account(accountNums[i], (accBalance+amounts[i]));
}
struct timeval finished;
gettimeofday(&finished, NULL);
fprintf(output, "%d OK TIME %d.%06d %d.%06d\n", req.requestId, req.timeStart.tv_sec, req.timeStart.tv_usec, finished.tv_sec, finished.tv_usec);
}
pthread_mutex_unlock(&bankLock);
}
} else {
pthread_mutex_unlock(&queueMutex);
}
}
}