-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
69 lines (59 loc) · 1.82 KB
/
client.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
#include "rpc.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char* address;
int port, opt;
int exit_code = 0;
// parse the arguments from the command line and read them into variables
while ((opt = getopt(argc, argv, "i:p:")) != -1) {
switch(opt) {
case 'i':
address = optarg;
break;
case 'p':
port = atoi(optarg);
break;
}
}
rpc_client *state = rpc_init_client(address, port);
if (state == NULL) {
fprintf(stderr, "Fail\n");
exit(EXIT_FAILURE);
}
rpc_handle *handle_add2 = rpc_find(state, "add2");
if (handle_add2 == NULL) {
fprintf(stderr, "ERROR: Function add2 does not exist\n");
exit_code = 1;
goto cleanup;
}
for (int i = 0; i < 2; i++) {
/* Prepare request */
char left_operand = i;
char right_operand = 100;
rpc_data request_data = {
.data1 = left_operand, .data2_len = 1, .data2 = &right_operand};
/* Call and receive response */
rpc_data *response_data = rpc_call(state, handle_add2, &request_data);
if (response_data == NULL) {
fprintf(stderr, "Function call of add2 failed\n");
exit_code = 1;
goto cleanup;
}
/* Interpret response */
assert(response_data->data2_len == 0);
assert(response_data->data2 == NULL);
printf("Result of adding %d and %d: %d\n", left_operand, right_operand,
response_data->data1);
rpc_data_free(response_data);
}
cleanup:
if (handle_add2 != NULL) {
free(handle_add2);
}
rpc_close_client(state);
state = NULL;
return exit_code;
}