-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_communicator.c
93 lines (69 loc) · 1.98 KB
/
client_communicator.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
// This is an Academic Project, and was published after finishing the lecture.
// @author Joao Elvas @ FCT/UNL
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include "mysocks.h"
#include "client.h"
/*
* Check if the request returns a list
*/
int is_list(Request* request) {
return operation(request) == LIST_ALL ||
operation(request) == LIST ||
operation(request) == LIST_ONGOING_EVENTS;
}
/*
* Handle a request issued from the command line.
* It sends the request to the server and waits for the reply
* request - the request information
*/
int communicate_event_request(Request* request) {
int res = myWriteSocket(request->socket, (char *) &request->event_op, sizeof(EventOp));
if (res == -1)
return -1;
if (operation(request) == ADD) {
myWriteSocket(request->socket, (char *) &request->event, sizeof(Event));
if (res == -1)
return -1;
}
int reply;
res = myReadSocket(request->socket, (char *) &reply, sizeof(int));
if (res == -1)
return -1;
if (reply == -1)
printf ("Error: Could not perform operation.\n");
else if (reply > 0) {
if (is_list(request)) {
EventList* eventList = new_event_list(reply);
res = myReadSocket(request->socket, (char *) eventList->events,
eventList->nEvents * sizeof(Event));
if (res == -1)
return -1;
if (operation(request) == LIST_ONGOING_EVENTS) {
// NOT IMPLEMENTED - START
printf("\n******************\n* NOTIFICATION *\n******************\n");
print_event_list(eventList);
// NOT IMPLEMENTED - END
return 0;
}
else
print_event_list(eventList);
destroy_event_list(eventList);
}
else {
switch (operation(request)) {
case ADD:
printf ("Added event number %d\n", reply);
break;
case REMOVE:
printf("Removed event number %d\n", reply);
break;
default:
printf ("Internal Error - client handling operation %d\n", (-reply));
}
}
}
return 0;
}