-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_client.c
More file actions
204 lines (165 loc) · 5.14 KB
/
rpc_client.c
File metadata and controls
204 lines (165 loc) · 5.14 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
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <err.h>
#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "rpc.h"
static void serve_client(char *, int);
static void sighandler(int);
static void *emalloc(size_t);
static char *argv0;
static volatile sig_atomic_t f_quit = 0;
void dot_product_prog(char *host, int sock)
{
CLIENT *clnt;
scalar_product_args args;
int *result, n;
clnt = clnt_create(host, VECTOR_CALC_PROG, VECTOR_CALC_VERS, "tcp");
if (clnt == NULL) { clnt_pcreateerror(host); exit(1); }
recv(sock, &n, sizeof(int), 0);
printf("\033[38;2;255;140;0m[rpc_client] Received dot product request for n = %d\033[0m\n", n);
args.n = n;
args.x.x_len = n;
args.y.y_len = n;
args.x.x_val = emalloc(n * sizeof(int));
args.y.y_val = emalloc(n * sizeof(int));
recv(sock, args.x.x_val, n * sizeof(int), 0);
recv(sock, args.y.y_val, n * sizeof(int), 0);
result = dot_product_1(&args, clnt);
if (result == NULL) { clnt_perror(clnt, "call failed"); }
printf("\033[38;2;255;95;31m[rpc_client] Calling RPC dot_product_1...\033[0m\n");
send(sock, result, sizeof(int), 0);
free(args.x.x_val);
free(args.y.y_val);
clnt_destroy(clnt);
}
void calc_averages_prog(char *host, int sock)
{
CLIENT *clnt;
two_vectors args;
struct avg_result *result;
int n;
clnt = clnt_create(host, VECTOR_CALC_PROG, VECTOR_CALC_VERS, "tcp");
if (clnt == NULL) { clnt_pcreateerror(host); exit(1); }
recv(sock, &n, sizeof(int), 0);
printf("\033[38;2;255;140;0m[rpc_client] Received average request for n = %d\033[0m\n", n);
args.n = n;
args.x.x_len = n;
args.y.y_len = n;
args.x.x_val = emalloc(n * sizeof(int));
args.y.y_val = emalloc(n * sizeof(int));
recv(sock, args.x.x_val, n * sizeof(int), 0);
recv(sock, args.y.y_val, n * sizeof(int), 0);
result = calc_averages_1(&args, clnt);
if (result == NULL) { clnt_perror(clnt, "call failed"); }
printf("\033[38;2;255;95;31m[rpc_client] RPC returned: avg_x = %.3f, avg_y = %.3f\033[0m\n", result->ex, result->ey);
send(sock, result, 2 * sizeof(float), 0);
free(args.x.x_val);
free(args.y.y_val);
clnt_destroy(clnt);
}
void scale_vector_prog(char *host, int sock)
{
CLIENT *clnt;
scale_product_args args;
struct float_array *result;
int n;
clnt = clnt_create(host, VECTOR_CALC_PROG, VECTOR_CALC_VERS, "tcp");
if (clnt == NULL) { clnt_pcreateerror(host); exit(1); }
recv(sock, &args.r, sizeof(float), 0);
recv(sock, &n, sizeof(int), 0);
args.n = n;
args.x.x_len = n;
args.x.x_val = emalloc(n * sizeof(int));
recv(sock, args.x.x_val, n * sizeof(int), 0);
printf("\033[38;2;255;140;0m[rpc_client] Received scale_vector request: r = %.2f, n = %d\033[0m\n", args.r, n);
result = scale_vector_1(&args, clnt);
if (result == NULL) { clnt_perror(clnt, "call failed"); }
printf("\033[38;2;255;95;31m[rpc_client] RPC returned scaled vector:\n");
for (int i = 0; i < n; i++)
printf(" %.3f\n", result->arr.arr_val[i]);
printf("\033[0m");
send(sock, result->arr.arr_val, n * sizeof(float), 0);
free(args.x.x_val);
clnt_destroy(clnt);
}
static void serve_client(char *host, int cfd)
{
int choice;
for (;;) {
if (recv(cfd, &choice, sizeof(int), 0) <= 0)
break;
switch (choice) {
case 1: dot_product_prog(host, cfd); break;
case 2: calc_averages_prog(host, cfd); break;
case 3: scale_vector_prog(host, cfd); break;
case 4: close(cfd); return;
default: break;
}
}
}
static void sighandler(int sig) {
f_quit = 1;
}
static void *emalloc(size_t nb)
{
void *p;
if ((p = malloc(nb)) == NULL)
err(1, "malloc");
return (p);
}
int main(int argc, char *argv[])
{
struct sockaddr_in sin;
struct hostent *hp;
struct sigaction sa;
int backlog = 5;
int port = 9999;
int sfd, cfd;
char *host, ch;
argv0 = basename(*argv);
while ((ch = getopt(argc, argv, "b:p:")) != -1) {
switch (ch) {
case 'b': backlog = atoi(optarg); break;
case 'p': port = atoi(optarg); break;
default: exit(1);
}
}
argc -= optind;
argv += optind;
if (argc < 1)
exit(1);
host = *argv;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sighandler;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
err(1, "socket(AF_INET)");
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
err(1, "bind");
if (listen(sfd, backlog) < 0)
err(1, "listen");
for (;;) {
if (f_quit) break;
if ((cfd = accept(sfd, NULL, NULL)) < 0)
continue;
if (fork() == 0) {
close(sfd);
serve_client(host, cfd);
exit(0);
}
close(cfd);
}
close(sfd);
return 0;
}