-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
executable file
·232 lines (206 loc) · 7.96 KB
/
client.c
File metadata and controls
executable file
·232 lines (206 loc) · 7.96 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
////////////////////////////////////////////////////////////////////////////////
// Main File: client.c
// This File: client.c
// Semester: CS 537 Spring 2023
// Instructor: Shivaram
//
// Author: Zelong Jiang
// wisc ID: 9082157588
// Email: zjiang287@wisc.edu
// CS Login: zjiang dipaksi
//
/////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdbool.h>
#include "client.h"
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>
#define MAX_SEQ_NUM 10000
// initializes the RPC connection to the server
struct rpc_connection RPC_init(int src_port, int dst_port, char dst_addr[])
{
//printf("RPC init\n");
// Create a socket to receive responses from the server
struct socket recv_socket = init_socket(src_port);
// Populate the destination address struct
struct sockaddr_storage dst;
socklen_t dst_len;
populate_sockaddr(AF_INET, dst_port, dst_addr, &dst, &dst_len);
// Create the RPC connection struct
struct rpc_connection rpc;
rpc.recv_socket = recv_socket;
//memcpy(&rpc.dst_addr, &dst, sizeof(struct sockaddr));
rpc.dst_addr = *((struct sockaddr *)(&dst));
rpc.dst_len = dst_len;
rpc.seq_number = 0;
// Initialize the random number generator with the current time as the seed
//srand(time(NULL));
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
rpc.client_id = rand() % MAX_SEQ_NUM;
printf("client id: %d\n", rpc.client_id);
return rpc;
}
// Sleeps the server thread for a few seconds
void RPC_idle(struct rpc_connection *rpc, int time)
{
//printf("RPC idle\n");
char payload[BUFLEN];
int payload_length = sprintf(payload, "IDLE %d %d %d", rpc->seq_number, rpc->client_id, time);
//struct packet_info response = send_request(rpc, payload, payload_length);
//printf("seq_num: %d, id: %d\n", rpc->seq_number, rpc->client_id);
rpc->seq_number++;
// attempt to send the packet to the server RETRY_COUNT times
for (int i = 0; i < RETRY_COUNT; i++)
{
//printf("client %d sent message\n", rpc->client_id);
send_packet(rpc->recv_socket, rpc->dst_addr, rpc->dst_len, payload, payload_length);
// wait for a response from the server for TIMEOUT_TIME seconds
struct packet_info response = receive_packet_timeout(rpc->recv_socket, time + TIMEOUT_TIME);
if (response.recv_len <= 0)
{
// no response was received, retry the request
continue;
}
//printf("response: %s\n", response.buf);
// parse the response from the server
int ack_seq_number, ack_client_id, ack_result;
char ack_type[BUFLEN];
sscanf(response.buf, "%s %d %d %d", ack_type, &ack_seq_number, &ack_client_id, &ack_result);
//printf("ask_seq_numnber %d, rpc->seq_number %d, client id %d\n", ack_seq_number, rpc->seq_number, rpc->client_id);
// check if the response is for our request and from the correct client id
if (ack_seq_number != rpc->seq_number - 1 || ack_client_id != rpc->client_id)
{
// printf("wrong seq number\n");
// response is not for our request, retry the request
continue;
}
// check if the response is an ACK
if (strcmp(ack_type, "ACK") == 0)
{
// printf("Acknowledged client id %d\n", rpc->client_id);
// ACK received, delay for 1 second
sleep(1);
return;
}
else
{
// printf("not Acknowledged\n");
// response is not an ACK, retry the request
continue;
}
}
}
// gets the value of a key on the server store
int RPC_get(struct rpc_connection *rpc, int key)
{
//printf("RPC get\n");
char payload[BUFLEN];
int payload_length = sprintf(payload, "GET %d %d %d", rpc->seq_number, rpc->client_id, key);
//printf("seq_num: %d, id: %d\n", rpc->seq_number, rpc->client_id);
// printf("payload: %s\n", payload);
rpc->seq_number++;
// struct packet_info response = send_request(rpc, payload, payload_length);
// attempt to send the packet to the server RETRY_COUNT times
for (int i = 0; i < RETRY_COUNT; i++)
{
send_packet(rpc->recv_socket, rpc->dst_addr, rpc->dst_len, payload, payload_length);
// wait for a response from the server for TIMEOUT_TIME seconds
struct packet_info response = receive_packet_timeout(rpc->recv_socket, TIMEOUT_TIME);
if (response.recv_len <= 0)
{
// no response was received, retry the request
continue;
}
// printf("response: %s\n", response.buf);
// parse the response from the server
int ack_seq_number, ack_client_id, ack_result;
char ack_type[BUFLEN];
sscanf(response.buf, "%s %d %d %d", ack_type, &ack_seq_number, &ack_client_id, &ack_result);
// printf("ask_seq_numnber %d, rpc->seq_number %d, client id %d\n", ack_seq_number, rpc->seq_number, rpc->client_id);
// check if the response is for our request and from the correct client id
if (ack_seq_number != rpc->seq_number - 1 || ack_client_id != rpc->client_id)
{
// response is not for our request, retry the request
// printf("wrong seq number\n");
continue;
}
// check if the response is an ACK
if (strcmp(ack_type, "ACK") == 0)
{
// printf("Acknowledged\n");
// ACK received, delay for 1 second
sleep(1);
return ack_result;
}
else
{
// printf("not Acknowledged\n");
// response is not an ACK, retry the request
continue;
}
}
// maximum number of retries reached, return failure
return -1;
}
// sets the value of a key on the server store
int RPC_put(struct rpc_connection *rpc, int key, int value)
{
//printf("RPC put\n");
char payload[BUFLEN];
int payload_length = sprintf(payload, "PUT %d %d %d %d", rpc->seq_number, rpc->client_id, key, value);
//printf("seq_num: %d, id: %d\n", rpc->seq_number, rpc->client_id);
rpc->seq_number++;
// attempt to send the packet to the server RETRY_COUNT times
for (int i = 0; i < RETRY_COUNT; i++)
{
send_packet(rpc->recv_socket, rpc->dst_addr, rpc->dst_len, payload, payload_length);
// wait for a response from the server for TIMEOUT_TIME seconds
struct packet_info response = receive_packet_timeout(rpc->recv_socket, TIMEOUT_TIME);
if (response.recv_len <= 0)
{
// no response was received, retry the request
continue;
}
//printf("response: %s\n", response.buf);
// parse the response from the server
int ack_seq_number, ack_client_id, ack_result;
char ack_type[BUFLEN];
sscanf(response.buf, "%s %d %d %d", ack_type, &ack_seq_number, &ack_client_id, &ack_result);
// printf("ask_seq_numnber %d, rpc->seq_number %d, client id %d\n", ack_seq_number, rpc->seq_number, rpc->client_id);
// check if the response is for our request and from the correct client id
if (ack_seq_number != rpc->seq_number - 1 || ack_client_id != rpc->client_id)
{
// printf("wrong seq number\n");
// response is not for our request, retry the request
continue;
}
// check if the response is an ACK
if (strcmp(ack_type, "ACK") == 0)
{
// printf("Acknowledged\n");
// ACK received, delay for 1 second
sleep(1);
return ack_result;
}
else
{
// printf("not Acknowledged\n");
// response is not an ACK, retry the request
continue;
}
}
// maximum number of retries reached, return failure
return -1;
}
// closes the RPC connection to the server
void RPC_close(struct rpc_connection *rpc)
{
// printf("RPC close\n");
close_socket(rpc->recv_socket);
}