-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
96 lines (82 loc) · 2.74 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
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
#include "rpc.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int exit_code = 0;
rpc_client *state = rpc_init_client("::1", 3000);
if (state == NULL) {
exit(EXIT_FAILURE);
}
char name[] = "add3";
rpc_handle *handle_add3 = rpc_find(state, name);
if (handle_add3 == NULL) {
fprintf(stderr, "ERROR: Function add3 does not exist\n");
}
else{
printf("successfully found add3 on server\n");
}
rpc_handle *handle_multi2 = rpc_find(state, "multi2");
if (handle_multi2 == NULL) {
fprintf(stderr, "ERROR: Function multi2 does not exist\n");
}
else{
printf("successfully found multi2 on server\n");
}
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;
}
else{
printf("successfully found add2 on server\n");
}
for (int i = 127; i < 128; i++) {
/* Prepare request */
char left_operand = i;
char right_operand = 127;
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);
}
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_multi2, &request_data);
if (response_data == NULL) {
fprintf(stderr, "Function call of multi2 failed\n");
exit_code = 1;
goto cleanup;
}
/* Interpret response */
assert(response_data->data2_len == 0);
assert(response_data->data2 == NULL);
printf("Result of multipling %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;
}