-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
367 lines (311 loc) · 10.8 KB
/
Copy pathclient.cpp
File metadata and controls
367 lines (311 loc) · 10.8 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
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <string>
#include <pthread.h>
#include <iostream>
#include <optional>
#include <fstream>
#include <chrono>
#include <unordered_set>
#include "safequeue.h"
#include "common.h"
using namespace std;
using namespace std::chrono;
ThreadsafeQueue<int> send_queue;
char *main_buf;
pthread_mutex_t ack_done_mutex;
pthread_mutex_t thread_done_mutex;
pthread_mutex_t final_ack_recvd_mutex;
pthread_mutex_t measure_time_mutex;
int send_thread_done = 0;
int ack_done = 1;
int final_ack_recvd = 0;
int start_set = 0;
std::chrono::high_resolution_clock::time_point stop;
std::chrono::high_resolution_clock::time_point start;
// UDP Socket setup code from Beej’s Guide to Network Programming
int SetupUDPSocket(const char *ip, const char *port)
{
struct addrinfo hints;
struct addrinfo *res;
int sockfd;
int status;
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints); // make sure hints is empty
hints.ai_family = AF_INET; // use IPv4
hints.ai_socktype = SOCK_DGRAM; // use datagram sockets
// error checking for getaddrinfo
if ((status = getaddrinfo(ip, port, &hints, &res)) != 0)
{
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
// make a socket:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
// error checking for socket creation
if (sockfd == -1)
{
perror("Central UDP: socket");
exit(1);
}
// bind it to the port and IP address we passed in to getaddrinfo():
if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1)
{
close(sockfd);
perror("Central UDP: bind");
exit(1);
}
freeaddrinfo(res); // all done with this structure
return sockfd;
}
void GetUDPServerInfo(const char *ip, const char *port, struct addrinfo *&servinfo)
{
struct addrinfo hints;
int status;
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints); // make sure hints is empty
hints.ai_family = AF_INET; // use IPv4
hints.ai_socktype = SOCK_DGRAM; // use datagram sockets
// error checking for getaddrinfo
// getaddrinfo used to get server address
if ((status = getaddrinfo(ip, port, &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
}
int ReadQueue()
{
optional<int> num = send_queue.pop();
if (!num.has_value())
return -1;
else
return num.value();
}
char *FileMap(int sequence_num, char *main_buffer)
{
return &main_buffer[sequence_num * UDP_DATA_SIZE];
}
// Client sends to server
void *ClientSendTo(void *arg)
{
int thread_idx = (intptr_t)arg;
int sock_fd = SetupUDPSocket(CLIENT_IP, CLIENT_THREAD_PORTS[thread_idx]);
char small_buf[UDP_SIZE];
int numbytes;
struct addrinfo *servinfo;
GetUDPServerInfo(SERVER_IP, SERVER_THREAD_PORTS[thread_idx], servinfo);
int sequence_num;
int file_last_index = (FILE_SIZE - 1) / UDP_DATA_SIZE;
printf("Starting Thread\n");
int local_final_ack_recvd = 0;
pthread_mutex_lock(&measure_time_mutex);
if (!start_set) {
start = std::chrono::high_resolution_clock::now();
start_set = 1;
cout << "Starting Timestamp: " << duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() << endl;
}
pthread_mutex_unlock(&measure_time_mutex);
while (!local_final_ack_recvd) {
int local_ack_done;
pthread_mutex_lock(&ack_done_mutex);
local_ack_done = ack_done;
pthread_mutex_unlock(&ack_done_mutex);
if (!local_ack_done){
continue;
}
int queue_size;
queue_size = send_queue.size();
while ((sequence_num = ReadQueue()) != -1)
{
//printf("Here 1 %d\n", sequence_num);
memcpy(&small_buf[5], &main_buf[sequence_num * UDP_DATA_SIZE], UDP_DATA_SIZE);
//printf("Here 2\n");
memcpy(&small_buf, &sequence_num, sizeof(sequence_num));
small_buf[4] = 0;
int num_duplicate_sends;
if (queue_size > 1000) {
num_duplicate_sends = 1;
} else if (queue_size > 500) {
num_duplicate_sends = 2;
} else if (queue_size > 250) {
num_duplicate_sends = 3;
}
else {
num_duplicate_sends = 4;
}
for (int i = 0; i < num_duplicate_sends; i++) {
if ((numbytes = sendto(sock_fd, small_buf, UDP_SIZE, 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1)
{
perror("Sending Normal Seq num packets");
exit(1);
}
// Measure the time here so we stop measuring as soon as the last bit is sent
pthread_mutex_lock(&measure_time_mutex);
stop = std::chrono::high_resolution_clock::now();
pthread_mutex_unlock(&measure_time_mutex);
// Remove delay if not needed
usleep(400);
}
//printf("sent %d\n", sequence_num);
}
// Each bit of send_thread_done holds an indicator for the thread being done.
// All threads are done sending when all have written a 1 to their respective bit index
//printf("Thread %d done\n", thread_idx);
pthread_mutex_lock(&thread_done_mutex);
send_thread_done |= 0x01 << thread_idx;
pthread_mutex_unlock(&thread_done_mutex);
pthread_mutex_lock(&final_ack_recvd_mutex);
local_final_ack_recvd = final_ack_recvd;
pthread_mutex_unlock(&final_ack_recvd_mutex);
}
printf("Exiting Thread %d\n", thread_idx);
close(sock_fd);
}
void * ReceiveAckFromServer(void *arg)
{
int send_sockfd = (intptr_t)arg;
struct sockaddr_in cliaddr;
socklen_t addr_len = sizeof(cliaddr);
bool ack_received = false;
int numbytes;
char ack_buffer[UDP_SIZE];
// Value of int when all threads have written flags
int THREAD_DONE_VAL = 0;
for (int i = 0; i < NUM_THREADS; i++) {
THREAD_DONE_VAL |= (0x01 << i);
}
// Setup server address info
struct addrinfo *servinfo;
GetUDPServerInfo(SERVER_IP, SERVER_MAIN_PORT, servinfo);
int local_final_ack_recvd = 0;
int itr_num = 0;
while (!local_final_ack_recvd) {
unordered_set<int> hashset;
int local_thread_done;
pthread_mutex_lock(&thread_done_mutex);
local_thread_done = send_thread_done;
pthread_mutex_unlock(&thread_done_mutex);
if (local_thread_done != THREAD_DONE_VAL) {
continue;
}
ack_received = 0;
pthread_mutex_lock(&ack_done_mutex);
ack_done = 0;
pthread_mutex_unlock(&ack_done_mutex);
// Threads are done sending iteration
// Send itr_done packets
// Send Iteration done packet 5 times
char done_buf[] = {'0', '0', '0', '0', '1'};
for (int i = 0; i < 6; i++)
{
if ((numbytes = sendto(send_sockfd, done_buf, 5, 0, servinfo->ai_addr, servinfo->ai_addrlen)) == -1)
{
perror("Send Ack packets to server");
exit(1);
}
}
while (!ack_received)
{
if ((numbytes = recvfrom(send_sockfd, ack_buffer, UDP_SIZE, 0, (struct sockaddr *)&cliaddr, &addr_len)) == -1)
{
perror("Receive from Server");
exit(1);
}
int read_itr_num = 0;
memcpy(&read_itr_num, &ack_buffer[1], 3);
if (read_itr_num != itr_num) {
continue;
}
if (numbytes == 4 && ack_buffer[0] == 1)
{
// Set final ack global flag, then ack done global flag
pthread_mutex_lock(&final_ack_recvd_mutex);
final_ack_recvd = 1;
pthread_mutex_unlock(&final_ack_recvd_mutex);
local_final_ack_recvd = 1;
//pthread_mutex_lock(&ack_done_mutex);
ack_done = 1;
//pthread_mutex_unlock(&ack_done_mutex);
ack_received = 1;
pthread_mutex_lock(&thread_done_mutex);
send_thread_done = 0;
pthread_mutex_unlock(&thread_done_mutex);
break;
}
int temp;
for (int i = 4; i < numbytes; i += 4)
{
memcpy(&temp, &ack_buffer[i], sizeof(int));
hashset.insert(temp);
}
if (ack_buffer[0])
{
ack_received = true;
pthread_mutex_lock(&thread_done_mutex);
send_thread_done = 0;
pthread_mutex_unlock(&thread_done_mutex);
}
}
for (auto it = hashset.begin(); it != hashset.end(); ++it)
{
send_queue.push(*it);
}
// Set ack done global flag
pthread_mutex_lock(&ack_done_mutex);
ack_done = 1;
pthread_mutex_unlock(&ack_done_mutex);
itr_num ++;
}
printf("Recv exiting\n");
}
// Driver code
int main(int argc, char *argv[])
{
int sock_fd = SetupUDPSocket(CLIENT_IP, CLIENT_MAIN_PORT);
// Read file and initialize main buffer
ifstream input_file(argv[1]);
main_buf = new char[FILE_SIZE];
input_file.read(main_buf, FILE_SIZE);
// Initialize queue
int file_last_index = (FILE_SIZE - 1) / UDP_DATA_SIZE;
printf("file last index %d\n", file_last_index);
for (int i = 0; i <= file_last_index; i++)
{
send_queue.push(i);
}
// Begin measuring time
auto start = std::chrono::high_resolution_clock::now();
bool transfer_complete = false;
unordered_set<int> drop_sequence_num;
int numbytes;
pthread_t tid[7];
std::chrono::high_resolution_clock::time_point last_sent_packet_time;
for (int i = 0; i < NUM_THREADS; i++)
{
pthread_create(&tid[i], NULL, ClientSendTo, (void *)(intptr_t)i);
}
pthread_create(&tid[NUM_THREADS], NULL, ReceiveAckFromServer, (void *)(intptr_t)sock_fd);
for (int i = 0; i < NUM_THREADS+1; i++)
{
pthread_join(tid[i], NULL);
}
cout << "Done with Transfer"
<< "\n";
// Stop measuring time and calculate the elapsed time
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
cout << "Transfer Time: " << duration.count() << "\n";
cout << "File Transfer Complete\n";
close(sock_fd);
return 0;
}