-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
373 lines (248 loc) · 8.02 KB
/
server.c
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
#include <stdlib.h>
#include "message.h"
#include "vector.h"
#include <mqueue.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h> /* flags */
#include <sys/stat.h> /* modes */
#include <stdio.h>
//sudo sh -c 'echo 512 > /proc/sys/fs/mqueue/msg_max'
pthread_mutex_t mutex_msg;
int msg_not_copied = 1; /* TRUE = 1 */
pthread_cond_t cond_msg;
vector messages;
//helper: returns the index of the struct in the messages vector with the corresponding key.
int vector_index(int key){
int i;
for(i = 0;i<vector_total(&messages);i++){
//simple linear search.
struct request * temp = malloc(sizeof(*temp));
temp = (struct request * ) vector_get(&messages, i);
if(key==temp->key){
return i;
}
}
return -1;
}
//thread function for init()
void process_message_init(struct request *msg){
struct request *msg_local = malloc(sizeof( struct request)); /* local message */
mqd_t q_client; /* client queue */
int result = 0;
pthread_mutex_lock(&mutex_msg);
msg_local = msg;
pthread_cond_signal(&cond_msg);
pthread_mutex_unlock(&mutex_msg);
q_client = mq_open(msg_local->str, O_WRONLY);
if (q_client == -1){
result = -1; //so client knows the process failed.
perror("Can’t open client queue");
}
else {
mq_send(q_client, (char *) &result, sizeof(result), 0);
mq_close(q_client);
}
pthread_exit(0);
}
void process_message_set_value(struct request *msg){
struct request *msg_local = malloc(sizeof( struct request));
mqd_t q_client;
int result = 0;
pthread_mutex_lock(&mutex_msg);
msg_local = msg;
int temp = vector_total(&messages);
vector_add(&messages, (void*)msg);
int temp1 = vector_total(&messages);
pthread_cond_signal(&cond_msg);
pthread_mutex_unlock(&mutex_msg);
if(temp==temp1){ //did not add vector succesfully. compares size before and after
result = -1;
printf("Not succesful. Size: %d \n", vector_total(&messages));
}
q_client = mq_open(msg_local->client_name, O_WRONLY);
if (q_client == -1){
result = -1;
perror("Can’t open client queue");
}
else {
mq_send(q_client, (char *) &result, sizeof(result), 0);
mq_close(q_client);
}
pthread_exit(0);
}
void process_message_num_items(struct request *msg)
{
struct request *msg_local = malloc(sizeof( struct request));
mqd_t q_client;
int result = 0;
pthread_mutex_lock(&mutex_msg);
msg_local = msg;
result = vector_total(&messages);
pthread_cond_signal(&cond_msg);
pthread_mutex_unlock(&mutex_msg);
printf("Alternate thread message: %s \n", msg_local->str);
q_client = mq_open(msg_local->client_name, O_WRONLY);
if (q_client == -1){
int result = -1;
perror("Can’t open client queue");
}
else {
mq_send(q_client, (char *) &result, sizeof(result), 0);
mq_close(q_client);
}
}
void process_message_delete_value(struct request *msg){
struct request *msg_local = malloc(sizeof( struct request));
mqd_t q_client;
int result = 0;
pthread_mutex_lock(&mutex_msg);
msg_local = msg;
int temp = vector_total(&messages);
int index = vector_index(msg_local->key);
if(index==-1){
printf("Error, key not found. \n");
}
vector_delete(&messages, index);
printf("After deletion, total number of elements: %i \n", vector_total(&messages));
int temp1 = vector_total(&messages);
pthread_cond_signal(&cond_msg);
pthread_mutex_unlock(&mutex_msg);
if(temp==temp1 || index==-1){ //did not delete vector succesfully.
result = -1;
printf("Not succesful. Size: %d \n", vector_total(&messages));
}
q_client = mq_open(msg_local->client_name, O_WRONLY);
if (q_client == -1){
result = -1;
perror("Can’t open client queue");
}
else {
mq_send(q_client, (char *) &result, sizeof(result), 0);
mq_close(q_client);
}
pthread_exit(0);
}
void process_message_get_value(struct request *msg){
struct request *msg_local = malloc(sizeof( struct request));
mqd_t q_client;
struct request *response = malloc(sizeof( struct request));
pthread_mutex_lock(&mutex_msg);
msg_local = msg;
int index = vector_index(msg->key);
response = (struct request * ) vector_get(&messages, index);
pthread_mutex_unlock(&mutex_msg);
q_client = mq_open(msg_local->client_name, O_WRONLY);
if (q_client == -1){
perror("Can’t open client queue");
}
else {
mq_send(q_client, (char *) response, sizeof(*response), 0);
mq_close(q_client);
}
}
int main(void)
{
//have to init vector:
vector_init(&messages);
mqd_t q_server; /* server queue */
struct request *msg = (struct request *) malloc(sizeof( *msg)); /* message to receive */
struct mq_attr q_attr; /* queue atributes */
pthread_attr_t t_attr[6]; /* thread atributes */
q_attr.mq_maxmsg = 20;
q_attr.mq_msgsize = sizeof(*msg);
pthread_t thid[6]; //need reference to threads
q_server = mq_open("/server", O_RDONLY | O_CREAT, 0777 , &q_attr);
if (q_server == -1) {
perror("Can't create server queue");
return 1;
}
pthread_mutex_init(&mutex_msg, NULL);
pthread_cond_init(&cond_msg, NULL);
int i;
//initialize 1 thread for each thread process function
for( i = 0;i<6;i++){
pthread_attr_setdetachstate(&t_attr[i], PTHREAD_CREATE_DETACHED);
pthread_attr_init(&t_attr[i]);
}
while (1) {
struct request *msg_type = (struct request *) malloc(sizeof( *msg_type));
printf(" waiting for Struct Type (where 1<=k<=6 ) \n");
ssize_t ret = mq_receive(q_server, (char *)msg_type, sizeof( *msg_type), 0);
if(ret==-1){
perror("Error on recieve: ");
}
//We have recieved a type, and will go into one of thee corresponding branches:
if(msg_type->key==1){
//all i have to do is free the vector here.
int x = pthread_create(&thid[0], &t_attr[0], (void *)process_message_init, msg_type );
pthread_join(thid[0],NULL);
if(x!=0){
perror("Error creating init thread!");
}
}
//set_value()
else if(msg_type->key==2){
//wait for second message, containing struct to be inserted
ssize_t ret = mq_receive(q_server, (char *)msg_type, sizeof( *msg_type), 0);
if(ret==-1){
perror("mq_revieve error: ");
}
int x = pthread_create(&thid[1], &t_attr[1], (void *)process_message_set_value, msg_type );
pthread_join(thid[1],NULL);
}
//get_value():
else if(msg_type->key==3){
//wait for second message with key value of desired strucy
ssize_t ret = mq_receive(q_server, (char *)msg_type, sizeof( *msg_type), 0);
int x = pthread_create(&thid[2], &t_attr[2], (void *)process_message_get_value, msg_type );
pthread_join(thid[2],NULL);
}
else if(msg_type->key==4){
//modify value(just delete and than insert. not even necessary on this end, I just make the call
// on keys.c. will keep here to avoid confusion.
}
else if(msg_type->key==5){
//delete
//we wait for second message with actual key value
ssize_t ret = mq_receive(q_server, (char *)msg_type, sizeof( *msg_type), 0);
int x = pthread_create(&thid[4], &t_attr[4], (void *)process_message_delete_value, msg_type );
pthread_join(thid[4],NULL);
}
else if(msg_type->key==6){
//all i have to do is free the vector here.
int test = pthread_create(&thid[5], &t_attr[5], (void *) process_message_num_items, msg_type );
if(test!=1){
perror("error on pthread: ");
}
pthread_join(thid[5],NULL);
printf("Number of messages - messages cleared \n");
}
//**everything else will take place in individual ifs like this, so while(1) loop is always waiting for key values
else{
printf("waiting for real message \n");
printf("Main thread Message: %s \n", msg->str);
if(ret==-1){
printf("MESSAGE SIZE: %ld",sizeof(&msg));
perror("ERR:");
}
//pthread_create(&thid, &t_attr, (void *)process_message, msg);
//pthread_join(thid,NULL);
//msg_not_cpied first init to true. set to 0 in process message(), after message is succesfully copied.
/* wait for thread to copy message */
pthread_mutex_lock(&mutex_msg);
//work out message not copied stuff they are all off
while (msg_not_copied==1){
printf("copying message \n");
pthread_cond_wait(&cond_msg, &mutex_msg);
printf("after con wait \n");
msg_not_copied = 0;
pthread_mutex_unlock(&mutex_msg);
printf("pthread_mutex_unlock(&mutex_msg); \n");
}
printf("**** WILL NOW WAIT FOR NEXT MESSAGE \n");
}
}
//Main done
}
//** most current**** stuck on only being able to send 2 messages.