client.c 14.2 KB
Newer Older
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * Frisbee client.
 */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <signal.h>
#include <stdarg.h>
#include <pthread.h>
#include "decls.h"

int		debug = 0;
int		portnum;
struct in_addr	mcastaddr;
struct in_addr	mcastif;

/* Forward Decls */
static void	PlayFrisbee(void);
static void	GotBlock(Packet_t *p);
static void	RequestChunk(int timedout);
static int	ImageUnzip(int chunk);
extern int	ImageUnzipInit(char *filename, int slice, int debug);

/*
 * The chunker data structure. For each chunk in progress, we maintain this
 * array of blocks (plus meta info). This serves as a cache to receive
 * blocks from the server while we write completed chunks to disk. The child
 * thread reads packets and updates this cache, while the parent thread
 * simply looks for completed blocks and writes them. The "inprogress" slot
 * serves a free/allocated flag, while the ready bit indicates that a chunk
 * is complete and ready to write to disk.
 */
typedef struct {
	int	thischunk;		/* Which chunk in progress */
	int	inprogress;		/* Chunk in progress? Free/Allocated */
	int	ready;			/* Ready to write to disk? */
	int	blockcount;		/* Number of blocks not received yet */
	char	bitmap[CHUNKSIZE];	/* Which blocks have been received */
	struct {
		char	data[BLOCKSIZE];
	} blocks[CHUNKSIZE];		/* Actual block data */
} ChunkBuffer_t;
ChunkBuffer_t   *ChunkBuffer;		/* The cache */
char		*ChunkBitmap;		/* Bitmap of which chunks finished */
int		*ChunkRequestList;	/* Randomized chunk request order */
int		LastReceiveChunk;	/* Chunk of last data packet received*/
int		TotalChunkCount;	/* Total number of chunks in file */
int		IdleCounter;		/* Countdown to request more data */

char *usagestr = 
 "usage: frisbee [-d] [-s #] <-p #> <-m mcastaddr> <output filename>\n"
 " -d              Turn on debugging. Multiple -d options increase output.\n"
 " -p portnum      Specify a port number.\n"
 " -m mcastaddr    Specify a multicast address in dotted notation.\n"
 " -i mcastif      Specify a multicast interface in dotted notation.\n"
 " -s slice        Output to DOS slice (DOS numbering 1-4)\n"
 "                 NOTE: Must specify a raw disk device for output filename.\n"
 "\n";

void
usage()
{
	fprintf(stderr, usagestr);
	exit(1);
}

int
main(int argc, char **argv)
{
	int	ch, slice = 0;
	char   *filename;

	while ((ch = getopt(argc, argv, "dhp:m:s:i:")) != -1)
		switch(ch) {
		case 'd':
			debug++;
			break;
			
		case 'p':
			portnum = atoi(optarg);
			break;
			
		case 'm':
			inet_aton(optarg, &mcastaddr);
			break;

		case 'i':
			inet_aton(optarg, &mcastif);
			break;

		case 's':
			slice = atoi(optarg);
			break;

		case 'h':
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	if (argc != 1)
		usage();
	filename = argv[0];

	if (!portnum || ! mcastaddr.s_addr)
		usage();

	ClientLogInit();
	ImageUnzipInit(filename, slice, debug);
	ClientNetInit();
	PlayFrisbee();
	exit(0);
}

/*
 * The client receive thread. This thread takes in packets from the server.
 */
void *
ClientRecvThread(void *arg)
{
	Packet_t	packet, *p = &packet;

	if (debug)
		log("Receive pthread starting up ...");

	IdleCounter = CLIENT_IDLETIMER_COUNT;

	while (1) {
		/*
		 * If we go too long without getting a block, we want
		 * to make another chunk request.
		 */
		 if (PacketReceive(p) < 0) {
			 pthread_testcancel();
			 IdleCounter--;
			 if (! IdleCounter) {
				 RequestChunk(1);
				 IdleCounter = CLIENT_IDLETIMER_COUNT;
			 }
			 continue;
		 }
		 pthread_testcancel();

		 switch (p->hdr.subtype) {
		 case PKTSUBTYPE_BLOCK:
			 GotBlock(p);
			 break;

		 case PKTSUBTYPE_JOIN:
		 case PKTSUBTYPE_LEAVE:
		 case PKTSUBTYPE_REQUEST:
			 /* Ignore these. They are from other clients. */
			 break;

		 default:
			 fatal("ClientRecvThread: Bad packet type!");
			 break;
		 }
	}
}

/*
 * The heart of the game.
 */
static void
ChunkerStartup(void)
{
	pthread_t		child_pid;
	void		       *ignored;
	int			chunkcount = TotalChunkCount;
	int			i;

	/*
	 * Allocate the Chunk Buffer and Chunk Bitmap.
	 */
	if ((ChunkBitmap =
	     (char *) calloc(chunkcount, sizeof(*ChunkBitmap))) == NULL)
		fatal("ChunkBitmap: No more memory");

	if ((ChunkRequestList =
	     (int *) calloc(chunkcount, sizeof(*ChunkRequestList))) == NULL)
		fatal("ChunkRequestList: No more memory");

	if ((ChunkBuffer =
	     (ChunkBuffer_t *) malloc(MAXCHUNKBUFS * sizeof(ChunkBuffer_t)))
	    == NULL)
		fatal("ChunkBuffer: No more memory");

	/*
	 * Set all the buffers to "free"
	 */
	for (i = 0; i < MAXCHUNKBUFS; i++) {
		ChunkBuffer[i].inprogress = 0;
		ChunkBuffer[i].ready      = 0;
	}

	/*
	 * We randomize the block selection so that multiple clients
	 * do not end up getting stalled by each other. That is, if
	 * all the clients were requesting blocks in order, then all
	 * the clients would end up waiting until the last client was
	 * done (since the server processes client requests in FIFO
	 * order).
	 */
	for (i = 0; i < TotalChunkCount; i++)
		ChunkRequestList[i] = i;
	
	for (i = 0; i < 50 * TotalChunkCount; i++) {
		int c1 = random() % TotalChunkCount;
		int c2 = random() % TotalChunkCount;
		int t1 = ChunkRequestList[c1];
		int t2 = ChunkRequestList[c2];

		ChunkRequestList[c2] = t1;
		ChunkRequestList[c1] = t2;
	}

	if (pthread_create(&child_pid, NULL,
			   ClientRecvThread, (void *)0)) {
		fatal("Failed to create pthread!");
	}

	/*
	 * Loop until all chunks have been received and written to disk.
	 */
	while (chunkcount) {
		/*
		 * Search the chunk cache for a chunk that is ready to write.
		 */
		for (i = 0; i < MAXCHUNKBUFS; i++) {
			if (ChunkBuffer[i].ready)
				break;
		}

		/*
		 * If nothing to do, then get out of the way for a while.
		 */
		if (i == MAXCHUNKBUFS) {
			log("No chunks ready to write!");
			fsleep(100000);
			continue;
		}

		/*
		 * We have a completed chunk. Write it to disk.
		 */
		log("Writing chunk %d (buffer %d)",
		    ChunkBuffer[i].thischunk, i);

		ImageUnzip(i);

		/*
		 * Okay, free the slot up for another chunk.
		 */
		ChunkBuffer[i].ready      = 0;
		ChunkBuffer[i].inprogress = 0;
		chunkcount--;
	}
	/*
	 * Kill the child and wait for it before returning. We do not
	 * want the child absorbing any more packets, cause that would
	 * mess up the termination handshake with the server. 
	 */
	pthread_cancel(child_pid);
	pthread_join(child_pid, &ignored);
}

/*
 * Receive a single data block. If the block is for a chunk in progress, then
 * insert the data and check for a completed chunk. It will be up to the main
 * thread to process that chunk.
 *
 * If the block is the first of some chunk, then try to allocate a new chunk.
 * If the chunk buffer is full, then drop the block. If this happens, it
 * indicates the chunk buffer is not big enough, and should be increased.
 */
static void
GotBlock(Packet_t *p)
{
	int	chunk = p->msg.block.chunk;
	int	block = p->msg.block.block;
	int	i, free = -1;

	/*
	 * Search the chunk buffer for a match (or a free one).
	 */
	for (i = 0; i < MAXCHUNKBUFS; i++) {
		if (!ChunkBuffer[i].inprogress) {
			free = i;
			continue;
		}
		
		if (!ChunkBuffer[i].ready &&
		    ChunkBuffer[i].thischunk == chunk)
			break;
	}
	if (i == MAXCHUNKBUFS) {
		/*
		 * Did not find it. Allocate the free one, or drop the
		 * packet if there is no free chunk.
		 */
		if (free == -1) {
			if (debug)
				log("No more free buffer slots for chunk %d!",
				    chunk);
			return;
		}

		/*
		 * Was this chunk already processed? 
		 */
		if (ChunkBitmap[chunk]) {
			if (0)
				log("Duplicate chunk %d ignored!", chunk);
			return;
		}
		ChunkBitmap[chunk] = 1;

		if (debug)
			log("Allocating chunk buffer %d to chunk %d",
			    free, chunk);

		i = free;
		ChunkBuffer[i].ready      = 0;
		ChunkBuffer[i].inprogress = 1;
		ChunkBuffer[i].thischunk  = chunk;
		ChunkBuffer[i].blockcount = CHUNKSIZE;
		bzero(ChunkBuffer[i].bitmap, sizeof(ChunkBuffer[i].bitmap));
	}

	/*
	 * Insert the block and update the metainfo. We have to watch for
	 * duplicate blocks in the same chunk since another client may
	 * issue a request for a lost block, and we will see that even if
	 * we do not need it (cause of broadcast/multicast).
	 */
	if (ChunkBuffer[i].bitmap[block]) {
		if (0)
			log("Duplicate block %d in chunk %d", block, chunk);
		return;
	}
	ChunkBuffer[i].bitmap[block] = 1;
	ChunkBuffer[i].blockcount--;
	memcpy(ChunkBuffer[i].blocks[block].data, p->msg.block.buf, BLOCKSIZE);
	LastReceiveChunk = chunk;

	/*
	 * Anytime we receive a packet thats needed, reset the idle counter.
	 * This will prevent us from sending too many requests.
	 */
	IdleCounter = CLIENT_IDLETIMER_COUNT;

	/*
	 * Is the chunk complete? If so, then release it to the main thread.
	 */
	if (ChunkBuffer[i].blockcount == 0) {
		if (debug)
			log("Releasing chunk %d to main thread", chunk);
		ChunkBuffer[i].ready = 1;

		/*
		 * Send off a request for a chunk we do not have yet. This
		 * should be enough to ensure that there is more work to do
		 * by the time the main thread finishes the chunk we just
		 * released.
		 */
		RequestChunk(0);
	}
}

/*
 * Request a chunk/block/range we do not have.
 */
static void
RequestRange(int chunk, int block, int count)
{
	Packet_t	packet, *p = &packet;

	if (debug)
		log("Requesting chunk:%d block:%d count:%d",
		    chunk, block, count);
	
	p->hdr.type       = PKTTYPE_REQUEST;
	p->hdr.subtype    = PKTSUBTYPE_REQUEST;
	p->hdr.datalen    = sizeof(p->msg.request);
	p->msg.request.chunk = chunk;
	p->msg.request.block = block;
	p->msg.request.count = count;
	PacketSend(p);
}

static void
RequestChunk(int timedout)
{
	int		i, j, k;

	/*
	 * Look for unfinished chunks.
	 */
	for (i = 0; i < MAXCHUNKBUFS; i++) {
		if (! ChunkBuffer[i].inprogress || ChunkBuffer[i].ready)
			continue;

		/*
		 * If we timed out, then its okay to send in another request
		 * for the last chunk in progress. The point of this test
		 * is to prevent requesting a chunk we are currently getting
		 * packets for. But once we time out, we obviously want to
		 * get more blocks for it.
		 */
		if (ChunkBuffer[i].thischunk == LastReceiveChunk && !timedout)
			continue;

		/*
		 * Scan the bitmap. We do not want to request single blocks,
		 * but rather small groups of them.
		 */
		j = 0;
		while (j < CHUNKSIZE) {
			/*
			 * When we find a missing block, scan forward from
			 * that point till we get to a block that is present.
			 * Send a request for the intervening range.
			 */
			if (! ChunkBuffer[i].bitmap[j]) {
				for (k = j; k < CHUNKSIZE; k++) {
					if (ChunkBuffer[i].bitmap[k])
						break;
				}
				RequestRange(ChunkBuffer[i].thischunk,
					     j, k - j);
				j = k;
			}
			j++;
		}
	}

	/*
	 * Make sure we have a place to put new data.
	 */
	for (i = 0, k = 0; i < MAXCHUNKBUFS; i++) {
		if (ChunkBuffer[i].inprogress)
			k++;
	}
	if (MAXCHUNKBUFS - k < MAXREADAHEAD || k >= MAXINPROGRESS)
		return;

	/*
	 * Look for a chunk we are not working on yet.
	 */
	for (i = 0, j = 0; i < TotalChunkCount && j < MAXREADAHEAD; i++) {
		int chunk = ChunkRequestList[i];

		if (! ChunkBitmap[chunk]) {
			/*
			 * Not working on this one yet, so send off a
			 * request for it.
			 */
			RequestRange(chunk, 0, CHUNKSIZE);
			j++;
		}
	}
}

/*
 * Join the Frisbee team, and then go into the main loop above.
 */
static void
PlayFrisbee(void)
{
	Packet_t	packet, *p = &packet;
	struct timeval  stamp, estamp;
	unsigned int	myid;

	gettimeofday(&stamp, 0);

	/*
	 * Init the random number generator. We randomize the block request
	 * sequence above, and its important that each client have a different
	 * sequence!
	 */
	srandomdev();

	/*
	 * A random number ID. I do not think this is really necessary,
	 * but perhaps might be useful for determining when a client has
	 * crashed and returned.
	 */
	myid = random();
	
	/*
	 * Send a join the team message. We block waiting for a reply
	 * since we need to know the total block size. We resend the
	 * message (dups are harmless) if we do not get a reply back.
	 */
	while (1) {
		int	countdown = 0;

		if (countdown <= 0) {
			p->hdr.type       = PKTTYPE_REQUEST;
			p->hdr.subtype    = PKTSUBTYPE_JOIN;
			p->hdr.datalen    = sizeof(p->msg.join);
			p->msg.join.clientid = myid;
			PacketSend(p);
			countdown = 3;
		}

		/*
		 * Throw away any data packets. We cannot start until
		 * we get a reply back. Wait several receive delays
		 * before resending the join message.
		 */
		if (PacketReceive(p) < 0) {
			countdown--;
			continue;
		}

		if (p->hdr.subtype == PKTSUBTYPE_JOIN &&
		    p->hdr.type == PKTTYPE_REPLY) {
			break;
		}
	}
	TotalChunkCount = p->msg.join.blockcount / BLOCKSIZE;
	
	log("Joined the team. ID is %u. File is %d chunks (%d blocks)",
	    myid, TotalChunkCount, p->msg.join.blockcount);

	ChunkerStartup();

	/*
	 * Done! Handshake our exit with the server.
	 */
	while (1) {
		int	countdown = 0;

		if (countdown <= 0) {
			p->hdr.type       = PKTTYPE_REQUEST;
			p->hdr.subtype    = PKTSUBTYPE_LEAVE;
			p->hdr.datalen    = sizeof(p->msg.join);
			p->msg.join.clientid = myid;
			PacketSend(p);
			countdown = 3;
		}

		/*
		 * Throw away any data packets until we get a reply back.
		 * Wait several receive delays before resending the message.
		 */
		if (PacketReceive(p) < 0) {
			countdown--;
			continue;
		}

		if (p->hdr.subtype == PKTSUBTYPE_LEAVE &&
		    p->hdr.type == PKTTYPE_REPLY) {
			break;
		}
	}
	gettimeofday(&estamp, 0);
	estamp.tv_sec -= stamp.tv_sec;
	log("Left the team after %ld seconds on the field!", estamp.tv_sec);
}

/*
 * Supply a read function to the imageunzip library. Kinda hokey right
 * now. The original imageunzip code depends on read to keep track of
 * the seek offset within the input file. Well, in our case we know what
 * chunk the inflator is working on, and we keep track of the block offset
 * as well.
 *
 * XXX We copy out the data. Need to change this interface to avoid that.
 */
static int	ImageUnzipOffset;
static int	ImageUnzipBuffer;
extern int	inflate_subblock(void);

static int
ImageUnzip(int slot)
{
	ImageUnzipBuffer = slot;
	ImageUnzipOffset = 0;

	if (inflate_subblock())
		pfatal("ImageUnzip: inflate_subblock failed");

	return 0;
}

int
FrisbeeRead(void **buf, size_t count)
{
	char	*data = (char *) &ChunkBuffer[ImageUnzipBuffer].blocks;
	
	data += ImageUnzipOffset;	
#if 1
	*buf = data;
#else
	memcpy(buf, data, count);
#endif
	ImageUnzipOffset += count;
	return count;
}