-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuqfaceclient.c
More file actions
325 lines (289 loc) · 8.87 KB
/
uqfaceclient.c
File metadata and controls
325 lines (289 loc) · 8.87 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include "protocol.h"
// define the difference exit stiuation
#define EXIT_USAGE 12
#define EXIT_CONNECT_FAIL 14
#define EXIT_SERVER_ERROR 15
#define EXIT_INPUT_FILE 18
#define EXIT_OUTPUT_FILE 10
#define EXIT_COMM_ERROR 8
// define the command line arguments
typedef struct {
char* port;
char* detectFile;
char* outputFile;
char* replaceFile;
} ClientArgs;
// print usage
static void print_usage_and_exit(void)
{
fprintf(stderr,
"Usage: ./uqfaceclient port [--detectfile filename] "
"[--output filename] [--replacefile filename]\n");
exit(EXIT_USAGE);
}
// prase command line
static ClientArgs parse_arguments(int argc, char** argv)
{
ClientArgs args;
memset(&args, 0, sizeof(args));
if (argc < 2) {
print_usage_and_exit();
}
// first argument should be port
if (strlen(argv[1]) == 0) {
print_usage_and_exit();
}
args.port = argv[1];
// process the parse optional
for (int i = 2; i < argc; i++) {
if (strlen(argv[i]) == 0) {
// for empty argument
print_usage_and_exit();
}
if (strcmp(argv[i], "--detectfile") == 0) {
// for duplicate
if (args.detectFile != NULL) {
print_usage_and_exit();
}
// for missing or empty value
if (i + 1 >= argc || strlen(argv[i + 1]) == 0) {
print_usage_and_exit();
}
args.detectFile = argv[++i];
}
// for output part
else if (strcmp(argv[i], "--output") == 0) {
if (args.outputFile != NULL) {
print_usage_and_exit();
}
if (i + 1 >= argc || strlen(argv[i + 1]) == 0) {
print_usage_and_exit();
}
args.outputFile = argv[++i];
}
// for replacement part
else if (strcmp(argv[i], "--replacefile") == 0) {
if (args.replaceFile != NULL) {
print_usage_and_exit();
}
if (i + 1 >= argc || strlen(argv[i + 1]) == 0) {
print_usage_and_exit();
}
args.replaceFile = argv[++i];
}
// last for unexpected one
else {
print_usage_and_exit();
}
}
return args;
}
// check file can be open
static void check_files(ClientArgs* args)
{
// check the input file
if (args->detectFile) {
FILE* file = fopen(args->detectFile, "rb");
if (!file) {
fprintf(stderr,
"uqfaceclient: unable to open the input file "
"\"%s\" for reading\n",
args->detectFile);
exit(EXIT_INPUT_FILE);
}
fclose(file);
}
if (args->replaceFile) {
FILE* file = fopen(args->replaceFile, "rb");
if (!file) {
fprintf(stderr,
"uqfaceclient: unable to open the input file "
"\"%s\" for reading\n",
args->replaceFile);
exit(EXIT_INPUT_FILE);
}
fclose(file);
}
// check output file
if (args->outputFile) {
FILE* file = fopen(args->outputFile, "wb");
if (!file) {
fprintf(stderr,
"uqfaceclient: cannot open the output file "
"\"%s\" for writing\n",
args->outputFile);
exit(EXIT_OUTPUT_FILE);
}
fclose(file);
}
}
// connect to server
// REF: udp-receiver.c
static FILE* connect_to_server(const char* port)
{
struct addrinfo hints, *servinfo;
int sockfd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
int rv = getaddrinfo("localhost", port, &hints, &servinfo);
if (rv != 0) {
fprintf(stderr,
"uqfaceclient: cannot connect to the server "
"on port \"%s\"\n",
port);
exit(EXIT_CONNECT_FAIL);
}
struct addrinfo* p;
for (p = servinfo; p != NULL; p = p->ai_next) {
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sockfd == -1) {
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
continue;
}
// connect successfully
break;
}
freeaddrinfo(servinfo);
if (p == NULL) {
fprintf(stderr,
"uqfaceclient: cannot connect to the server "
"on port \"%s\"\n",
port);
exit(EXIT_CONNECT_FAIL);
}
// convert from socket to file
FILE* stream = fdopen(sockfd, "r+b");
if (!stream) {
close(sockfd);
fprintf(stderr,
"uqfaceclient: cannot connect to the server "
"on port \"%s\"\n",
port);
exit(EXIT_CONNECT_FAIL);
}
return stream;
}
// handle the communicate to server
static void communicate_with_server(FILE* stream, ClientArgs* args)
{
ProtocolMessage request;
memset(&request, 0, sizeof(request));
// set up request
request.prefix = PROTOCOL_PREFIX;
// read detect image
if (args->detectFile) {
request.image1Data
= read_file_data(args->detectFile, &request.image1Size);
} else {
request.image1Data = read_stdin_data(&request.image1Size);
}
if (!request.image1Data) {
fclose(stream);
fprintf(stderr, "uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
// check the replacement request
if (args->replaceFile) {
request.operation = OP_FACE_REPLACE;
request.image2Data
= read_file_data(args->replaceFile, &request.image2Size);
if (!request.image2Data) {
free_message(&request);
fclose(stream);
fprintf(stderr, "uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
} else {
request.operation = OP_FACE_DETECT;
}
// send requestion
if (send_message(stream, &request) != 0) {
free_message(&request);
fclose(stream);
fprintf(stderr, "uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
free_message(&request);
// receive response
ProtocolMessage response;
if (receive_message(stream, &response) != 0) {
fclose(stream);
fprintf(stderr, "uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
// check prefix about the response
if (response.prefix != PROTOCOL_PREFIX) {
free_message(&response);
fclose(stream);
fprintf(stderr, "uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
// handle response on the operation type
if (response.operation == OP_OUTPUT_IMAGE) {
/* Save output image */
if (args->outputFile) {
if (write_file_data(args->outputFile, response.image1Data,
response.image1Size)
!= 0) {
free_message(&response);
fclose(stream);
fprintf(stderr,
"uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
} else {
if (write_stdout_data(response.image1Data, response.image1Size)
!= 0) {
free_message(&response);
fclose(stream);
fprintf(stderr,
"uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
}
free_message(&response);
fclose(stream);
exit(0);
} else if (response.operation == OP_ERROR_MESSAGE) {
// print the error message
char* errorMsg = malloc(response.image1Size + 1);
if (errorMsg) {
memcpy(errorMsg, response.image1Data, response.image1Size);
errorMsg[response.image1Size] = '\0';
fprintf(stderr,
"uqfaceclient: received the following error "
"message: \"%s\"\n",
errorMsg);
free(errorMsg);
}
free_message(&response);
fclose(stream);
exit(EXIT_SERVER_ERROR);
} else {
// for the invalid response
free_message(&response);
fclose(stream);
fprintf(stderr, "uqfaceclient: unexpected communication error\n");
exit(EXIT_COMM_ERROR);
}
}
int main(int argc, char** argv)
{
ClientArgs args = parse_arguments(argc, argv);
check_files(&args);
// connect to server
FILE* stream = connect_to_server(args.port);
communicate_with_server(stream, &args);
return 0;
}