-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlsp_client.cpp
More file actions
431 lines (335 loc) · 15.5 KB
/
lsp_client.cpp
File metadata and controls
431 lines (335 loc) · 15.5 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "lsp_client.h"
#include<stdlib.h>
double epoch_delay = _EPOCH_LTH; // number of seconds between epochs
unsigned int num_epochs = _EPOCH_CNT; // number of epochs that are allowed to pass before a connection is terminated
/*
*
*
* LSP RELATED FUNCTIONS
*
*
*/
// Set length of epoch (in seconds)
void lsp_set_epoch_lth(double lth){
if(lth > 0)
epoch_delay = lth;
}
// Set number of epochs before timing out
void lsp_set_epoch_cnt(int cnt){
if(cnt > 0)
num_epochs = cnt;
}
// Set fraction of packets that get dropped along each connection
void lsp_set_drop_rate(double rate){
network_set_drop_rate(rate);
}
/*
*
*
* CLIENT RELATED FUNCTIONS
*
*
*/
lsp_client* lsp_client_create(const char* dest, int port){
lsp_client *client = new lsp_client();
pthread_mutex_init(&(client->mutex),NULL);
client->connection = new Connection();
client->connection->lastSentSeq = 0;
client->connection->lastReceivedSeq = 0;
client->connection->lastReceivedAck = 0;
client->connection->epochsSinceLastMessage = 0;
client->clnt = clnt_create(dest,RPC_PROG,SERVER_VERS,"udp");
// if(client->clnt==NULL)
// printf("RPC failed!!!!");
// kickoff new epoch timer
int res;
//if(network_send_connection_request(client->connection) && network_wait_for_connection(client->connection, epoch_delay * num_epochs))
message *mssg = network_build_message(0,0,NULL,0);
//printf("\nThe connection create message formed is connid is %d seqnum is %d payload is %s\n",mssg->connid,mssg->seqnum,mssg->payload);
//int *val;
bool_t *val;
val = server_recv_mssg_1(mssg,client->clnt);
if(val==NULL)
clnt_perror(client->clnt,"Client Call failed in client create");
if(val)
{
if(*val!=-1){
client->connection->id = *val;
client->connection->status = CONNECTED;
// printf("The connection id returned is %d\n",*val);
}
}
if((res = pthread_create(&(client->epochThread), NULL, ClientEpochThread, (void*)client)) != 0){
printf("Error: Failed to start the epoch thread: %d\n",res);
lsp_client_close(client);
return NULL;
}
pthread_mutex_lock(&(client->mutex));
// connection succeeded, build lsp_client struct
// client->connection->port = port;
// client->connection->host = dest;
//client->connection->status = CONNECTED;
// kick off ReadThread to catch incoming messages
//int res;
if((res = pthread_create(&(client->readThread), NULL, ClientReadThread, (void*)client)) != 0){
printf("Error: Failed to start the read thread: %d\n",res);
lsp_client_close(client);
return NULL;
}
if((res = pthread_create(&(client->writeThread), NULL, ClientWriteThread, (void*)client)) != 0){
printf("Error: Failed to start the write thread: %d\n",res);
lsp_client_close(client);
return NULL;
}
pthread_mutex_unlock(&(client->mutex));
return client;
}
int lsp_client_read(lsp_client* a_client, uint8_t* pld){
// block until a message arrives or the client becomes disconnected
while(true)
{
pthread_mutex_lock(&(a_client->mutex));
Status s = a_client->connection->status;
message *msg = NULL;
if(s == CONNECTED)
{
// try to pop a message off of the inbox queue
if(a_client->inbox.size() > 0){
msg = a_client->inbox.front();
a_client->inbox.pop();
}
}
pthread_mutex_unlock(&(a_client->mutex));
if(s == DISCONNECTED)
break;
// we got a message, so return it
if(msg)
{
std::string payload = msg->payload;
delete msg;
memcpy(pld,payload.c_str(),payload.length()+1);
//printf("\nThe payload is %d\n",payload.length());
return payload.length();
}
// still connected, but no message has arrived...
// sleep for a bit
usleep(10000); // 10 ms = 10,0000 microseconds
}
if(DEBUG) printf("Client was disconnected. Read returning NULL\n");
return 0; // NULL, no bytes read (client disconnected)
}
bool lsp_client_write(lsp_client* a_client, uint8_t* pld, int lth){
// queues up a message to be written by the Write Thread
if(pld == NULL || lth == 0)
return false; // don't send bad messages
pthread_mutex_lock(&(a_client->mutex));
a_client->connection->lastSentSeq++;
if(DEBUG) printf("Client queueing msg %d for write\n",a_client->connection->lastSentSeq);
// build the message
//LSPMessage *msg = network_build_message(a_client->connection->id,a_client->connection->lastSentSeq,pld,lth);
message *msg = network_build_message(a_client->connection->id,a_client->connection->lastSentSeq,pld,lth);
// queue it up
a_client->connection->outbox.push(msg);
pthread_mutex_unlock(&(a_client->mutex));
return true;
}
bool lsp_client_close(lsp_client* a_client){
// returns true if the connected was closed,
// false if it was already previously closed
if(DEBUG) printf("Shutting down the client\n");
pthread_mutex_lock(&(a_client->mutex));
bool alreadyClosed = (a_client->connection && a_client->connection->status == DISCONNECTED);
if(a_client->connection)
a_client->connection->status = DISCONNECTED;
pthread_mutex_unlock(&(a_client->mutex));
cleanup_client(a_client);
return !alreadyClosed;
}
/* Internal Methods */
void* ClientEpochThread(void *params){
lsp_client *client = (lsp_client*)params;
while(true){
usleep(epoch_delay * 1000000); // convert seconds to microseconds
if(DEBUG) printf("Client epoch handler waking up \n");
// epoch is happening; send required messages
pthread_mutex_lock(&(client->mutex));
if(client->connection->status == DISCONNECTED)
break;
if(client->connection->status == CONNECT_SENT)
{
// connect sent already, but not yet acknowledged
//if(DEBUG) printf("Client resending connection request\n");
//network_send_connection_request(client->connection);
message *mssg = network_build_message(0,0,NULL,0);
//printf("\nThe connection create message formed is connid is %d seqnum is %d payload is %s\n",mssg->connid,mssg->seqnum,mssg->payload);
int *vall;
vall = server_recv_mssg_1(mssg,client->clnt);
if(vall==NULL)
clnt_perror(client->clnt,"Client Call failed");
// printf("RPC failed\n");
if(vall)
{
if(*vall!=-1){
client->connection->id = *vall;
client->connection->status = CONNECTED;
// printf("The connection id returned is %d\n",*vall);
//num_epochs = 5;
}
}
} else if(client->connection->status == CONNECTED)
{
// send ACK for most recent message
//if(DEBUG) printf("Client acknowledging last received message: %d\n",client->connection->lastReceivedSeq);
//printf("\nWe will Acknowledge the Packet\n");
network_acknowledge_client(client->connection,client->clnt);
// resend the first message in the outbox, if any
if(client->connection->outbox.size() > 0)
{
if(DEBUG) printf("Client resending msg %d\n",client->connection->outbox.front()->seqnum);
network_send_message(client->connection,client->connection->outbox.front());
}
} else {
if(DEBUG) printf("Unexpected client status: %d\n",client->connection->status);
}
if(++(client->connection->epochsSinceLastMessage) >= num_epochs){
// oops, we haven't heard from the server in a while;
// mark the connection as disconnected
if(DEBUG) printf("Too many epochs have passed since we heard from the server... disconnecting\n");
client->connection->status = DISCONNECTED;
break;
}
pthread_mutex_unlock(&(client->mutex));
}
pthread_mutex_unlock(&(client->mutex));
if(DEBUG) printf("Epoch Thread exiting\n");
return NULL;
}
void* ClientReadThread(void *params){
lsp_client *client = (lsp_client*)params;
// continuously poll for new messages and process them;
// Exit when the client is disconnected
while(true){
pthread_mutex_lock(&(client->mutex));
Status state = client->connection->status;
pthread_mutex_unlock(&(client->mutex));
if(state == DISCONNECTED)
break;
// attempt to read
sockaddr_in addr;
// message *msg = network_read_message(client->connection, 0.5,&addr);
message *recv_msg =NULL;
//printf("The connection id of client is %d\n", client->connection->id);
recv_msg= server_send_mssg_1(&client->connection->id,client->clnt);
if(recv_msg==NULL)
{
//clnt_perror(client->clnt,"Client Call failed in read thread");
usleep(40);
//return NULL;
}
message *msg = NULL;
//printf("\nReceiving message at client side\n");
if(recv_msg)
{
msg = (message*)malloc(sizeof(struct message));
msg->connid = recv_msg->connid;
msg->seqnum = recv_msg->seqnum;
msg->payload = (char *)malloc(recv_msg->payload_length+1);
memset(msg->payload,0,recv_msg->payload_length+1);
msg->payload_length = recv_msg->payload_length;
memcpy(msg->payload,recv_msg->payload,msg->payload_length);
}
if(msg)
{
if(msg->connid == client->connection->id)
{
pthread_mutex_lock(&(client->mutex));
// reset counter for epochs since we have received a message
client->connection->epochsSinceLastMessage = 0;
if(msg->payload_length == 0)
{
// we received an ACK
//if(DEBUG) printf("Client received an ACK for msg %d\n",msg->seqnum);
//printf("\nClient received an ACK for msg with sequence number %d\n",msg->seqnum);
if(msg->seqnum == (client->connection->lastReceivedAck + 1)){
// this sequence number is next in line, even if it overflows
client->connection->lastReceivedAck = msg->seqnum;
}
if(client->connection->outbox.size() > 0 && msg->seqnum == client->connection->outbox.front()->seqnum) {
delete client->connection->outbox.front();
client->connection->outbox.pop();
}
} else {
// data packet
if(DEBUG) printf("Client received msg %d\n",msg->seqnum);
//printf("\nClient received a msg with seqnum %d\n",msg->seqnum);
if(msg->seqnum == (client->connection->lastReceivedSeq + 1)){
// next in the list
client->connection->lastReceivedSeq++;
client->inbox.push(msg);
// send ack for this message
network_acknowledge_client(client->connection,client->clnt);
}
}
pthread_mutex_unlock(&(client->mutex));
}
}
}
if(DEBUG) printf("Read Thread exiting\n");
return NULL;
}
// this write thread will ensure that messages can be sent/received faster than only
// on epoch boundaries. It will continuously poll for messages that are eligible to
// bet sent for the first time, and then send them out.
void* ClientWriteThread(void *params){
lsp_client *client = (lsp_client*)params;
// continuously poll for new messages to send;
// Exit when the client is disconnected
unsigned int lastSent = 0;
while(true)
{
pthread_mutex_lock(&(client->mutex));
Status state = client->connection->status;
if(state == DISCONNECTED)
break;
unsigned int nextToSend = client->connection->lastReceivedAck + 1;
if(nextToSend > lastSent)
{
// we have received an ack for the last message, and we haven't sent the
// next one out yet, so if it exists, let's send it now
if(client->connection->outbox.size() > 0)
{
//printf("\n The sequence number is %d\n",client->connection->outbox.front()->seqnum);
bool_t* value=server_recv_mssg_1(client->connection->outbox.front(),client->clnt);
//printf("The return value of server_recv_mssg_1 is %d \n",*value);
lastSent = client->connection->outbox.front()->seqnum;
}
}
pthread_mutex_unlock(&(client->mutex));
usleep(5000); // 5ms
}
pthread_mutex_unlock(&(client->mutex));
return NULL;
}
void cleanup_client(lsp_client *client){
// wait for threads to close
void *status;
if(client->readThread)
pthread_join(client->readThread,&status);
if(client->writeThread)
pthread_join(client->writeThread,&status);
if(client->epochThread)
pthread_join(client->epochThread,&status);
// cleanup the memory and connection
pthread_mutex_destroy(&(client->mutex));
cleanup_connection(client->connection);
delete client;
}
void cleanup_connection(Connection *s){
if(!s)
return;
// close the file descriptor and free memory
/*if(s->fd != -1)
close(s->fd);
delete s->addr;*/
delete s;
}