-
Notifications
You must be signed in to change notification settings - Fork 2
/
QUANTAnet_rbudpReceiver_c.cxx
464 lines (394 loc) · 14 KB
/
QUANTAnet_rbudpReceiver_c.cxx
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/******************************************************************************
* QUANTA - A toolkit for High Performance Data Sharing
* Copyright (C) 2003 Electronic Visualization Laboratory,
* University of Illinois at Chicago
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either Version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser Public License along
* with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Direct questions, comments etc about Quanta to cavern@evl.uic.edu
*****************************************************************************/
#include "QUANTAnet_rbudpReceiver_c.hxx"
#include <stdarg.h>
// If you want to output debug info on terminals when running, put
// fprintf(stderr, __VA_ARGS__);\
// fprintf(stderr, "\n");\
// into TRACE_DEBUG definition.
// If you want to log the output into a log file, uncomment DEBUG, and put
// fprintf(log, __VA_ARGS__);\
// fprintf(log, "\n");\
// into TRACE_DEBUG definition.
//#define DEBUG
/*#define TRACE_DEBUG(...) do { \
fprintf(stderr, __VA_ARGS__);\
fprintf(stderr, "\n");\
} while(0)
*/
inline void TRACE_DEBUG(char const *format, ...) {
va_list arglist;
va_start(arglist, format);
vfprintf(stderr, format, arglist);
fprintf(stderr, "\n");
va_end(arglist);
}
void QUANTAnet_rbudpReceiver_c::receive(void *buffer, int bufSize, int packetSize) {
int done = 0;
struct timeval curTime, startTime;
gettimeofday(&curTime, NULL);
startTime = curTime;
initReceiveRudp(buffer, bufSize, packetSize);
initErrorBitmap();
while (!done) {
// receive UDP packetsq TRACE_DEBUG("receiving UDP packets");
reportTime(curTime);
udpReceive();
reportTime(curTime);
gettimeofday(&curTime, NULL);
if (verbose > 1) TRACE_DEBUG("Current time: %d %ld", curTime.tv_sec, curTime.tv_usec);
if (updateHashTable() == 0)
done = 1;
if (verbose) {
float dt = (curTime.tv_sec - startTime.tv_sec) +
1e-6 * (curTime.tv_usec - startTime.tv_usec);
int nerrors = updateHashTable();
int got = packetSize * (totalNumberOfPackets - nerrors);
float mbps = 1e-6 * 8 * got / (dt == 0 ? .01 : dt);
TRACE_DEBUG("Error: %d, Loss rate: %.5f Got %d/%dK in %.2fs (%g Mbit/s)",
nerrors,
(double) nerrors / (double) totalNumberOfPackets,
(int) (got >> 10), (int) (bufSize >> 10), dt, mbps);
}
// Send back Error bitmap
if (writen(tcpSockfd, errorBitmap, sizeofErrorBitmap) != sizeofErrorBitmap) {
perror("tcp send");
exit(1);
}
}
free(errorBitmap);
free(hashTable);
}
void QUANTAnet_rbudpReceiver_c::udpReceiveReadv() {
int done;
long long seqno, packetno;
struct timeval timeout;
fd_set rset;
int maxfdpl;
done = 0;
packetno = 0;
timeout.tv_sec = 100;
timeout.tv_usec = 0;
#define QMAX(x, y) ((x)>(y)?(x):(y))
maxfdpl = QMAX(udpSockfd, tcpSockfd) + 1;
FD_ZERO(&rset);
while (!done) {
// These two FD_SET cannot be put outside the while, don't why though
FD_SET(udpSockfd, &rset);
FD_SET(tcpSockfd, &rset);
select(maxfdpl, &rset, NULL, NULL, &timeout);
// receiving a packet
if (FD_ISSET(udpSockfd, &rset)) {
// set seqno to expected sequence number
seqno = hashTable[packetno];
//fprintf(log, "expect %d\t", seqno);
iovRecv[1].iov_base = (char *) mainBuffer + (seqno * payloadSize);
iovRecv[1].iov_len = payloadSize;
if (recvmsg(udpSockfd, &msgRecv, 0) < 0) {
perror("recvmsg error\n");
exit(1);
}
int rcvseq = ptohseq(recvHeader.seq);
// make sure it is the expected packet
if (rcvseq == seqno) {
updateErrorBitmap(rcvseq);
// next expected packet
packetno++;
}
// if some packets lost before this packet, move this packet backward
else if (rcvseq > seqno) {
memmove((char *) mainBuffer + (rcvseq * payloadSize), iovRecv[1].iov_base, iovRecv[1].iov_len);
updateErrorBitmap(rcvseq);
// missed some packets, next expected packet
do packetno++;
while ((rcvseq != hashTable[packetno]) && (packetno <= remainNumberOfPackets));
if (packetno > remainNumberOfPackets) {
fprintf(stderr, "recv error\n");
}
packetno++;
}
//TRACE_DEBUG("received %d -> %d", recvHeader.seq, rcvseq);
}
//receive end of UDP signal
else if (FD_ISSET(tcpSockfd, &rset)) {
done = 1;
if (verbose) TRACE_DEBUG("received TCP signal");
readn(tcpSockfd, (char *) &endOfUdp, sizeof(struct _endOfUdp));
}
else // time out
{
done = 1;
}
}
}
void QUANTAnet_rbudpReceiver_c::udpReceive() {
int done, actualPayloadSize, retval;
long long seqno;
struct timeval start;
char *msg = (char *) malloc(packetSize);
struct timeval timeout;
fd_set rset;
int maxfdpl;
float prog;
int oldprog = 0;
done = 0;
seqno = 0;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
#define QMAX(x, y) ((x)>(y)?(x):(y))
maxfdpl = QMAX(udpSockfd, tcpSockfd) + 1;
FD_ZERO(&rset);
gettimeofday(&start, NULL);
while (!done) {
// These two FD_SET cannot be put outside the while, don't why though
FD_SET(udpSockfd, &rset);
FD_SET(tcpSockfd, &rset);
retval = select(maxfdpl, &rset, NULL, NULL, &timeout);
// receiving a packet
if (FD_ISSET(udpSockfd, &rset)) {
int temp = recv(udpSockfd, msg, packetSize, 0);
memmove(&recvHeader, msg, sizeof(struct _rbudpHeader));
seqno = ptohseq(recvHeader.seq);
// If the packet is the last one,
if (seqno < totalNumberOfPackets - 1) {
actualPayloadSize = payloadSize;
}
else {
actualPayloadSize = lastPayloadSize;
}
memmove((char *) mainBuffer + (seqno * payloadSize), msg + headerSize, actualPayloadSize);
updateErrorBitmap(seqno);
receivedNumberOfPackets++;
prog = (float) receivedNumberOfPackets / (float) totalNumberOfPackets * 100;
if ((int) prog > oldprog) {
oldprog = (int) prog;
if (oldprog > 100) oldprog = 100;
if (progress != 0) {
fseek(progress, 0, SEEK_SET);
fprintf(progress, "%d\n", oldprog);
}
}
}
//receive end of UDP signal
else if (FD_ISSET(tcpSockfd, &rset)) {
done = 1;
readn(tcpSockfd, (char *) &endOfUdp, sizeof(struct _endOfUdp));
}
else // time out
{
//done = 1;
}
}
free(msg);
}
int QUANTAnet_rbudpReceiver_c::getstream(int tofd, int packetSize) {
// Let sender know we're ready.
if (writen(tcpSockfd, "\0", 1) != 1) {
perror("tcp send");
exit(1);
}
long long curSize = -1;
char *buf = 0;
int ok = SUCCESS;
for (; ;) {
long long nbufSize, bufSize;
int n = readn(tcpSockfd, (char *) &nbufSize, sizeof(nbufSize));
if (n < 0) {
fprintf(stderr, "read error.\n");
return (FAILED);
}
bufSize = ntohll(nbufSize);
if (bufSize <= 0)
break;
if (verbose > 1) fprintf(stderr, "accepting %lld byte chunk\n", bufSize);
if (buf == 0 && bufSize != curSize) {
if (buf) free(buf);
buf = (char *) malloc(bufSize);
if (buf == 0) {
fprintf(stderr, "QUANTAnet_rbudpReceiver_c::getstream: Couldn't malloc %lld bytes for buffer\n",
bufSize);
ok = FAILED;
break;
}
curSize = bufSize;
}
receive(buf, bufSize, packetSize);
if (write(tofd, buf, bufSize) < bufSize) {
fprintf(stderr, "QUANTAnet_rbudpReceiver_c::getstream: couldn't write %lld bytes\n", bufSize);
ok = FAILED;
break;
}
}
if (buf != 0)
free(buf);
::close(tofd);
return (ok);
}
int QUANTAnet_rbudpReceiver_c::getfile(char *origFName, char *destFName, int packetSize) {
// Send getfile message.
if (writen(tcpSockfd, origFName, SIZEOFFILENAME) != SIZEOFFILENAME) {
perror("tcp send");
exit(1);
}
long long filesize;
int n = readn(tcpSockfd, (char *) &filesize, sizeof(filesize));
if (n < 0) {
fprintf(stderr, "read error.\n");
return (FAILED);
}
/* Can't use ntohl() on long longs! */
filesize = ntohll(filesize);
fprintf(stderr, "The size of the file is %lld.\n", filesize);
int fd = open(destFName, O_RDWR | O_CREAT | O_TRUNC, 0666);
ftruncate(fd, filesize);
char *buf;
buf = (char *) mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
fprintf(stderr, "mmap failed.\n");
return FAILED;
}
receive(buf, filesize, packetSize);
munmap(buf, filesize);
::close(fd);
return SUCCESS;
}
int QUANTAnet_rbudpReceiver_c::getfilelist(char *fileList, int packetSize) {
FILE *fp = fopen(fileList, "r");
if (fp == NULL) {
fprintf(stderr, "Error open file!\n");
return -1;
}
char str[SIZEOFFILENAME];
char *origFName, *destFName;
while (fgets(str, SIZEOFFILENAME, fp)) {
puts(str);
origFName = strtok(str, " ");
destFName = strtok(NULL, " ");
if ((origFName != NULL) && (destFName != NULL)) {
// Send getfile message.
if (writen(tcpSockfd, origFName, SIZEOFFILENAME) != SIZEOFFILENAME) {
perror("tcp send");
exit(1);
}
long long filesize;
int n = readn(tcpSockfd, (char *) &filesize, sizeof(filesize));
if (n < 0) {
fprintf(stderr, "read error.\n");
return (FAILED);
}
filesize = ntohll(filesize);
fprintf(stderr, "The size of the file is %lld.\n", filesize);
int fd = open(destFName, O_RDWR | O_CREAT | O_TRUNC, 0666);
ftruncate(fd, filesize);
char *buf;
buf = (char *) mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
fprintf(stderr, "mmap failed.\n");
return FAILED;
}
receive(buf, filesize, packetSize);
munmap(buf, filesize);
::close(fd);
}
}
// send all zero block to end the sender.
memset(str, 0, SIZEOFFILENAME);
if (writen(tcpSockfd, str, SIZEOFFILENAME) != SIZEOFFILENAME) {
perror("Error Tcp Send");
exit(1);
}
fclose(fp);
return SUCCESS;
}
/* sendRate: Mbps */
int QUANTAnet_rbudpReceiver_c::initReceiveRudp(void *buffer, int bufSize, int pSize) {
int i;
mainBuffer = (char *) buffer;
dataSize = bufSize;
payloadSize = pSize;
headerSize = sizeof(struct _rbudpHeader);
packetSize = payloadSize + headerSize;
isFirstBlast = 1;
if (dataSize % payloadSize == 0) {
totalNumberOfPackets = dataSize / payloadSize;
lastPayloadSize = payloadSize;
}
else {
totalNumberOfPackets = dataSize / payloadSize + 1; /* the last packet is not full */
lastPayloadSize = dataSize - payloadSize * (totalNumberOfPackets - 1);
}
remainNumberOfPackets = totalNumberOfPackets;
receivedNumberOfPackets = 0;
sizeofErrorBitmap = totalNumberOfPackets / 8 + 2;
errorBitmap = (char *) malloc(sizeofErrorBitmap);
hashTable = (long long *) malloc(totalNumberOfPackets * sizeof(long long));
if (verbose > 1) TRACE_DEBUG("totalNumberOfPackets: %d", totalNumberOfPackets);
if (errorBitmap == NULL) {
fprintf(stderr, "malloc errorBitmap failed\n");
return (-1);
}
if (hashTable == NULL) {
fprintf(stderr, "malloc hashTable failed\n");
return (-1);
}
/* Initialize the hash table */
for (i = 0; i < totalNumberOfPackets; i++) {
hashTable[i] = i;
}
return 0;
}
void QUANTAnet_rbudpReceiver_c::init(char *remoteHost) {
int n;
#ifdef DEBUG
log = fopen("rbudprecv.log","w");
#endif
if (verbose > 2)
progress = fopen("progress.log", "w");
else
progress = 0;
passiveUDP(remoteHost);
if (!hasTcpSock) {
if (verbose) TRACE_DEBUG("try to connect the sender via TCP ...");
n = connectTCP(remoteHost);
if (n < 0) {
fprintf(stderr, "connecting TCP failed, make sure the sender has been started\n");
exit(1);
}
if (verbose) TRACE_DEBUG("tcp connected.");
}
msgRecv.msg_name = NULL;
msgRecv.msg_namelen = 0;
msgRecv.msg_iov = iovRecv;
msgRecv.msg_iovlen = 2;
iovRecv[0].iov_base = reinterpret_cast<char *>(&recvHeader);
iovRecv[0].iov_len = sizeof(struct _rbudpHeader);
}
void QUANTAnet_rbudpReceiver_c::close() {
if (!hasTcpSock)
::close(tcpSockfd);
::close(udpSockfd);
#ifdef DEBUG
fclose(log);
#endif
if (progress != 0)
fclose(progress);
}