Statistics
| Branch: | Revision:

streamers / streaming.c @ 666e3817

History | View | Annotate | Download (19.4 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <sys/time.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <stdint.h>
11
#include <stdbool.h>
12
#include <math.h>
13
#include <assert.h>
14
#include <string.h>
15
#include <inttypes.h>
16

    
17
#include <net_helper.h>
18
#include <chunk.h> 
19
#include <chunkbuffer.h> 
20
#include <trade_msg_la.h>
21
#include <trade_msg_ha.h>
22
#include <peerset.h>
23
#include <peer.h>
24
#include <chunkidset.h>
25
#include <limits.h>
26
#include <trade_sig_ha.h>
27
#include <chunkiser_attrib.h>
28

    
29
#include "streaming.h"
30
#include "streamer.h"
31
#include "output.h"
32
#include "input.h"
33
#include "dbg.h"
34
#include "chunk_signaling.h"
35
#include "chunklock.h"
36
#include "topology.h"
37
#include "measures.h"
38
#include "scheduling.h"
39
#include "transaction.h"
40

    
41
#include "scheduler_la.h"
42

    
43
# define CB_SIZE_TIME_UNLIMITED 1e12
44
uint64_t CB_SIZE_TIME = CB_SIZE_TIME_UNLIMITED;        //in millisec, defaults to unlimited
45

    
46
static bool heuristics_distance_maxdeliver = false;
47
static int bcast_after_receive_every = 0;
48
static bool neigh_on_chunk_recv = false;
49
static bool send_bmap_before_push = false;
50

    
51
struct chunk_attributes {
52
  uint64_t deadline;
53
  uint16_t deadline_increment;
54
  uint16_t hopcount;
55
} __attribute__((packed));
56

    
57
extern bool chunk_log;
58

    
59
struct chunk_buffer *cb;
60
static struct input_desc *input;
61
static int cb_size;
62

    
63
static int offer_per_tick = 1;        //N_p parameter of POLITO
64

    
65
int _needs(struct chunkID_set *cset, int cb_size, int cid);
66

    
67
uint64_t gettimeofday_in_us(void)
68
{
69
  struct timeval what_time; //to store the epoch time
70

    
71
  gettimeofday(&what_time, NULL);
72
  return what_time.tv_sec * 1000000ULL + what_time.tv_usec;
73
}
74

    
75
void cb_print()
76
{
77
#ifdef DEBUG
78
  struct chunk *chunks;
79
  int num_chunks, i, id;
80
  chunks = cb_get_chunks(cb, &num_chunks);
81

    
82
  dprintf("\tchbuf :");
83
  i = 0;
84
  if(num_chunks) {
85
    id = chunks[0].id;
86
    dprintf(" %d-> ",id);
87
    while (i < num_chunks) {
88
      if (id == chunks[i].id) {
89
        dprintf("%d",id % 10);
90
        i++;
91
      } else if (chunk_islocked(id)) {
92
        dprintf("*");
93
      } else {
94
        dprintf(".");
95
      }
96
      id++;
97
    }
98
  }
99
  dprintf("\n");
100
#endif
101
}
102

    
103
void stream_init(int size, struct nodeID *myID)
104
{
105
  static char conf[32];
106

    
107
  cb_size = size;
108

    
109
  sprintf(conf, "size=%d", cb_size);
110
  cb = cb_init(conf);
111
  chunkDeliveryInit(myID);
112
  chunkSignalingInit(myID);
113
  init_measures();
114
}
115

    
116
int source_init(const char *fname, struct nodeID *myID, int *fds, int fds_size, int buff_size)
117
{
118
  input = input_open(fname, fds, fds_size);
119
  if (input == NULL) {
120
    return -1;
121
  }
122

    
123
  stream_init(buff_size, myID);
124
  return 0;
125
}
126

    
127
void chunk_attributes_fill(struct chunk* c)
128
{
129
  struct chunk_attributes * ca;
130
  int priority = 1;
131

    
132
  assert((!c->attributes && c->attributes_size == 0) ||
133
         chunk_attributes_chunker_verify(c->attributes, c->attributes_size));
134

    
135
  if (chunk_attributes_chunker_verify(c->attributes, c->attributes_size)) {
136
    priority = ((struct chunk_attributes_chunker*) c->attributes)->priority;
137
    free(c->attributes);
138
    c->attributes = NULL;
139
    c->attributes_size = 0;
140
  }
141

    
142
  c->attributes_size = sizeof(struct chunk_attributes);
143
  c->attributes = ca = malloc(c->attributes_size);
144

    
145
  ca->deadline = c->id;
146
  ca->deadline_increment = priority * 2;
147
  ca->hopcount = 0;
148
}
149

    
150
int chunk_get_hopcount(struct chunk* c) {
151
  struct chunk_attributes * ca;
152

    
153
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
154
    fprintf(stderr,"Warning, chunk %d with strange attributes block. Size:%d expected:%u\n", c->id, c->attributes ? c->attributes_size : 0, sizeof(struct chunk_attributes));
155
    return -1;
156
  }
157

    
158
  ca = (struct chunk_attributes *) c->attributes;
159
  return ca->hopcount;
160
}
161

    
162
void chunk_attributes_update_received(struct chunk* c)
163
{
164
  struct chunk_attributes * ca;
165

    
166
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
167
    fprintf(stderr,"Warning, received chunk %d with strange attributes block. Size:%d expected:%u\n", c->id, c->attributes ? c->attributes_size : 0, sizeof(struct chunk_attributes));
168
    return;
169
  }
170

    
171
  ca = (struct chunk_attributes *) c->attributes;
172
  ca->hopcount++;
173
  dprintf("Received chunk %d with hopcount %hu\n", c->id, ca->hopcount);
174
}
175

    
176
void chunk_attributes_update_sending(const struct chunk* c)
177
{
178
  struct chunk_attributes * ca;
179

    
180
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
181
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
182
    return;
183
  }
184

    
185
  ca = (struct chunk_attributes *) c->attributes;
186
  ca->deadline += ca->deadline_increment;
187
  dprintf("Sending chunk %d with deadline %lu (increment: %d)\n", c->id, ca->deadline, ca->deadline_increment);
188
}
189

    
190
struct chunkID_set *cb_to_bmap(struct chunk_buffer *chbuf)
191
{
192
  struct chunk *chunks;
193
  int num_chunks, i;
194
  struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
195
  chunks = cb_get_chunks(chbuf, &num_chunks);
196

    
197
  for(i=num_chunks-1; i>=0; i--) {
198
    chunkID_set_add_chunk(my_bmap, chunks[i].id);
199
  }
200
  return my_bmap;
201
}
202

    
203
// a simple implementation that request everything that we miss ... up to max deliver
204
struct chunkID_set *get_chunks_to_accept(struct nodeID *fromid, const struct chunkID_set *cset_off, int max_deliver, uint16_t trans_id){
205
  struct chunkID_set *cset_acc, *my_bmap;
206
  int i, d, cset_off_size;
207
  //double lossrate;
208
  struct peer *from = nodeid_to_peer(fromid, 0);
209

    
210
  cset_acc = chunkID_set_init("size=0");
211

    
212
  //reduce load a little bit if there are losses on the path from this guy
213
  //lossrate = get_lossrate_receive(from->id);
214
  //lossrate = finite(lossrate) ? lossrate : 0;        //start agressively, assuming 0 loss
215
  //if (rand()/((double)RAND_MAX + 1) >= 10 * lossrate ) {
216
    my_bmap = cb_to_bmap(cb);
217
    cset_off_size = chunkID_set_size(cset_off);
218
    for (i = 0, d = 0; i < cset_off_size && d < max_deliver; i++) {
219
      int chunkid = chunkID_set_get_chunk(cset_off, i);
220
      //dprintf("\tdo I need c%d ? :",chunkid);
221
      if (!chunk_islocked(chunkid) && _needs(my_bmap, cb_size, chunkid)) {
222
        chunkID_set_add_chunk(cset_acc, chunkid);
223
        chunk_lock(chunkid,from);
224
        dtprintf("accepting %d from %s", chunkid, node_addr(fromid));
225
#ifdef MONL
226
        dprintf(", loss:%f rtt:%f", get_lossrate(fromid), get_rtt(fromid));
227
#endif
228
        dprintf("\n");
229
        d++;
230
      }
231
    }
232
    chunkID_set_free(my_bmap);
233
  //} else {
234
  //    dtprintf("accepting -- from %s loss:%f rtt:%f\n", node_addr(fromid), lossrate, get_rtt(fromid));
235
  //}
236

    
237
  reg_offer_accept_in(chunkID_set_size(cset_acc) > 0 ? 1 : 0);
238

    
239
  return cset_acc;
240
}
241

    
242
void send_bmap(struct nodeID *toid)
243
{
244
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
245
   sendBufferMap(toid,NULL, my_bmap, input ? 0 : cb_size, 0);
246
  chunkID_set_free(my_bmap);
247
}
248

    
249
void bcast_bmap()
250
{
251
  int i, n;
252
  struct peer *neighbours;
253
  struct peerset *pset;
254
  struct chunkID_set *my_bmap;
255

    
256
  pset = get_peers();
257
  n = peerset_size(pset);
258
  neighbours = peerset_get_peers(pset);
259

    
260
  my_bmap = cb_to_bmap(cb);        //cache our bmap for faster processing
261
  for (i = 0; i<n; i++) {
262
    sendBufferMap(neighbours[i].id,NULL, my_bmap, input ? 0 : cb_size, 0);
263
  }
264
  chunkID_set_free(my_bmap);
265
}
266

    
267
void send_ack(struct nodeID *toid, uint16_t trans_id)
268
{
269
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
270
  sendAck(toid, my_bmap,trans_id);
271
  chunkID_set_free(my_bmap);
272
}
273

    
274
double get_average_lossrate_pset(struct peerset *pset)
275
{
276
  int i, n;
277
  struct peer *neighbours;
278

    
279
  n = peerset_size(pset);
280
  neighbours = peerset_get_peers(pset);
281
  {
282
    struct nodeID *nodeids[n];
283
    for (i = 0; i<n; i++) nodeids[i] = neighbours[i].id;
284
#ifdef MONL
285
    return get_average_lossrate(nodeids, n);
286
#else
287
    return 0;
288
#endif
289
  }
290
}
291

    
292
void ack_chunk(struct chunk *c, struct nodeID *from, uint16_t trans_id)
293
{
294
  //reduce load a little bit if there are losses on the path from this guy
295
  double average_lossrate = get_average_lossrate_pset(get_peers());
296
  average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
297
  if (rand()/((double)RAND_MAX + 1) < 1 * average_lossrate ) {
298
    return;
299
  }
300
  send_ack(from, trans_id);        //send explicit ack
301
}
302

    
303
void received_chunk(struct nodeID *from, const uint8_t *buff, int len)
304
{
305
  int res;
306
  static struct chunk c;
307
  struct peer *p;
308
  static int bcast_cnt;
309
  uint16_t transid;
310

    
311
  res = parseChunkMsg(buff + 1, len - 1, &c, &transid);
312
  if (res > 0) {
313
    chunk_attributes_update_received(&c);
314
    chunk_unlock(c.id);
315
    dprintf("Received chunk %d from peer: %s\n", c.id, node_addr(from));
316
    if(chunk_log){fprintf(stderr, "TEO: Received chunk %d from peer: %s at: %"PRIu64" hopcount: %i Size: %d bytes\n", c.id, node_addr(from), gettimeofday_in_us(), chunk_get_hopcount(&c), c.size);}
317
    output_deliver(&c);
318
    res = cb_add_chunk(cb, &c);
319
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c), res==E_CB_OLD, res==E_CB_DUPLICATE);
320
    cb_print();
321
    if (res < 0) {
322
      dprintf("\tchunk too old, buffer full with newer chunks\n");
323
      if(chunk_log){fprintf(stderr, "TEO: Received chunk: %d too old (buffer full with newer chunks) from peer: %s at: %"PRIu64"\n", c.id, node_addr(from), gettimeofday_in_us());}
324
      free(c.data);
325
      free(c.attributes);
326
    }
327
    p = nodeid_to_peer(from, neigh_on_chunk_recv);
328
    if (p) {        //now we have it almost sure
329
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
330
      gettimeofday(&p->bmap_timestamp, NULL);
331
    }
332
    ack_chunk(&c, from, transid);        //send explicit ack
333
    if (bcast_after_receive_every && bcast_cnt % bcast_after_receive_every == 0) {
334
       bcast_bmap();
335
    }
336
  } else {
337
    fprintf(stderr,"\tError: can't decode chunk!\n");
338
  }
339
}
340

    
341
struct chunk *generated_chunk(suseconds_t *delta)
342
{
343
  struct chunk *c;
344

    
345
  c = malloc(sizeof(struct chunk));
346
  if (!c) {
347
    fprintf(stderr, "Memory allocation error!\n");
348
    return NULL;
349
  }
350

    
351
  *delta = input_get(input, c);
352
  if (*delta < 0) {
353
    fprintf(stderr, "Error in input!\n");
354
    exit(-1);
355
  }
356
  if (c->data == NULL) {
357
    free(c);
358
    return NULL;
359
  }
360
  dprintf("Generated chunk %d of %d bytes\n",c->id, c->size);
361
  chunk_attributes_fill(c);
362
  return c;
363
}
364

    
365
int add_chunk(struct chunk *c)
366
{
367
  int res;
368

    
369
  res = cb_add_chunk(cb, c);
370
  if (res < 0) {
371
    free(c->data);
372
    free(c->attributes);
373
    free(c);
374
    return 0;
375
  }
376
  free(c);
377
  return 1;
378
}
379

    
380
uint64_t get_chunk_timestamp(int cid){
381
  const struct chunk *c = cb_get_chunk(cb, cid);
382
  if (!c) return 0;
383

    
384
  return c->timestamp;
385
}
386

    
387
/**
388
 *example function to filter chunks based on whether a given peer needs them.
389
 *
390
 * Looks at buffermap information received about the given peer.
391
 */
392
int needs(struct peer *n, int cid){
393
  struct peer * p = n;
394

    
395
  if (CB_SIZE_TIME < CB_SIZE_TIME_UNLIMITED) {
396
    uint64_t ts;
397
    ts = get_chunk_timestamp(cid);
398
    if (ts && (ts < gettimeofday_in_us() - CB_SIZE_TIME)) {        //if we don't know the timestamp, we accept
399
      return 0;
400
    }
401
  }
402

    
403
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
404
  if (! p->bmap) {
405
    //dprintf("no bmap\n");
406
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
407
  }
408
  return _needs(p->bmap, p->cb_size, cid);
409
}
410

    
411
int _needs(struct chunkID_set *cset, int cb_size, int cid){
412

    
413
  if (cb_size == 0) { //if it declared it does not needs chunks
414
    return 0;
415
  }
416

    
417
  if (CB_SIZE_TIME < CB_SIZE_TIME_UNLIMITED) {
418
    uint64_t ts;
419
    ts = get_chunk_timestamp(cid);
420
    if (ts && (ts < gettimeofday_in_us() - CB_SIZE_TIME)) {        //if we don't know the timestamp, we accept
421
      return 0;
422
    }
423
  }
424

    
425
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
426
    int missing, min;
427
    //@TODO: add some bmap_timestamp based logic
428

    
429
    if (chunkID_set_size(cset) == 0) {
430
      //dprintf("bmap empty\n");
431
      return 1;        // if the bmap seems empty, it needs the chunk
432
    }
433
    missing = cb_size - chunkID_set_size(cset);
434
    missing = missing < 0 ? 0 : missing;
435
    min = chunkID_set_get_earliest(cset);
436
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
437
    return (cid >= min - missing);
438
  }
439

    
440
  //dprintf("has it\n");
441
  return 0;
442
}
443

    
444
double peerWeightReceivedfrom(struct peer **n){
445
  struct peer * p = *n;
446
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
447
}
448

    
449
double peerWeightUniform(struct peer **n){
450
  return 1;
451
}
452

    
453
double peerWeightRtt(struct peer **n){
454
#ifdef MONL
455
  double rtt = get_rtt((*n)->id);
456
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
457
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
458
#else
459
  return 1;
460
#endif
461
}
462

    
463
//ordering function for ELp peer selection, chunk ID based
464
//can't be used as weight
465
double peerScoreELpID(struct nodeID **n){
466
  struct chunkID_set *bmap;
467
  int latest;
468
  struct peer * p = nodeid_to_peer(*n, 0);
469
  if (!p) return 0;
470

    
471
  bmap = p->bmap;
472
  if (!bmap) return 0;
473
  latest = chunkID_set_get_latest(bmap);
474
  if (latest == INT_MIN) return 0;
475

    
476
  return -latest;
477
}
478

    
479
double chunkScoreChunkID(int *cid){
480
  return (double) *cid;
481
}
482

    
483
uint64_t get_chunk_deadline(int cid){
484
  const struct chunk_attributes * ca;
485
  const struct chunk *c;
486

    
487
  c = cb_get_chunk(cb, cid);
488
  if (!c) return 0;
489

    
490
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
491
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
492
    return 0;
493
  }
494

    
495
  ca = (struct chunk_attributes *) c->attributes;
496
  return ca->deadline;
497
}
498

    
499
double chunkScoreDL(int *cid){
500
  return - (double)get_chunk_deadline(*cid);
501
}
502

    
503
double chunkScoreTimestamp(int *cid){
504
  return (double) get_chunk_timestamp(*cid);
505
}
506

    
507
void send_accepted_chunks(struct nodeID *toid, struct chunkID_set *cset_acc, int max_deliver, uint16_t trans_id){
508
  int i, d, cset_acc_size, res;
509
  struct peer *to = nodeid_to_peer(toid, 0);
510

    
511
  transaction_reg_accept(trans_id, toid);
512

    
513
  cset_acc_size = chunkID_set_size(cset_acc);
514
  reg_offer_accept_out(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
515
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
516
    const struct chunk *c;
517
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
518
    c = cb_get_chunk(cb, chunkid);
519
    if (!c) {        // we should have the chunk
520
      dprintf("%s asked for chunk %d we do not own anymore\n", node_addr(toid), chunkid);
521
      continue;
522
    }
523
    if (!to || needs(to, chunkid)) {        //he should not have it. Although the "accept" should have been an answer to our "offer", we do some verification
524
      chunk_attributes_update_sending(c);
525
      res = sendChunk(toid, c, trans_id);
526
      if (res >= 0) {
527
        if(to) chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
528
        d++;
529
        reg_chunk_send(c->id);
530
        if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %"PRIu64" Result: %d Size: %d bytes\n", c->id, node_addr(toid), gettimeofday_in_us(), res, c->size);}
531
      } else {
532
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
533
      }
534
    }
535
  }
536
}
537

    
538
int offer_peer_count()
539
{
540
  return offer_per_tick;
541
}
542

    
543
int offer_max_deliver(struct nodeID *n)
544
{
545

    
546
  if (!heuristics_distance_maxdeliver) return 1;
547

    
548
#ifdef MONL
549
  switch (get_hopcount(n)) {
550
    case 0: return 5;
551
    case 1: return 2;
552
    default: return 1;
553
  }
554
#else
555
  return 1;
556
#endif
557
}
558

    
559
//get the rtt. Currenly only MONL version is supported
560
static double get_rtt_of(struct nodeID* n){
561
#ifdef MONL
562
  return get_rtt(n);
563
#else
564
  return NAN;
565
#endif
566
}
567

    
568
#define DEFAULT_RTT_ESTIMATE 0.5
569

    
570
static struct chunkID_set *compose_offer_cset(struct peer *p)
571
{
572
  int num_chunks, j;
573
  uint64_t smallest_ts, largest_ts;
574
  double dt;
575
  struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
576
  struct chunk *chunks = cb_get_chunks(cb, &num_chunks);
577

    
578
  if (p) {
579
    dt = get_rtt_of(p->id);
580
  } else {
581
    dt = NAN;
582
  }
583
  if (isnan(dt)) dt = DEFAULT_RTT_ESTIMATE;
584
  dt *= 1e6;        //convert to usec
585

    
586
  smallest_ts = chunks[0].timestamp;
587
  largest_ts = chunks[num_chunks-1].timestamp;
588

    
589
  //add chunks in latest...earliest order
590
  if (am_i_source()) {
591
    j = (num_chunks-1) * 3/4;        //do not send offers for the latest chunks from the source
592
  } else {
593
    j = num_chunks-1;
594
  }
595
  for(; j>=0; j--) {
596
    if (chunks[j].timestamp > smallest_ts + dt)
597
    chunkID_set_add_chunk(my_bmap, chunks[j].id);
598
  }
599

    
600
  return my_bmap;
601
}
602

    
603

    
604
void send_offer()
605
{
606
  struct chunk *buff;
607
  int size, res, i, n;
608
  struct peer *neighbours;
609
  struct peerset *pset;
610

    
611
  pset = get_peers();
612
  n = peerset_size(pset);
613
  neighbours = peerset_get_peers(pset);
614
  dprintf("Send Offer: %d neighbours\n", n);
615
  if (n == 0) return;
616
  buff = cb_get_chunks(cb, &size);
617
  if (size == 0) return;
618

    
619
  {
620
    size_t selectedpeers_len = offer_peer_count();
621
    int chunkids[size];
622
    struct peer *nodeids[n];
623
    struct peer *selectedpeers[selectedpeers_len];
624

    
625
    //reduce load a little bit if there are losses on the path from this guy
626
    double average_lossrate = get_average_lossrate_pset(pset);
627
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
628
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
629
      return;
630
    }
631

    
632
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
633
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
634
    schedSelectPeersForChunks(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpeers, &selectedpeers_len, SCHED_NEEDS, SCHED_PEER);
635

    
636
    for (i=0; i<selectedpeers_len ; i++){
637
      int transid = transaction_create(selectedpeers[i]->id);
638
      int max_deliver = offer_max_deliver(selectedpeers[i]->id);
639
      struct chunkID_set *offer_cset = compose_offer_cset(selectedpeers[i]);
640
      dprintf("\t sending offer(%d) to %s, cb_size: %d\n", transid, node_addr(selectedpeers[i]->id), selectedpeers[i]->cb_size);
641
      res = offerChunks(selectedpeers[i]->id, offer_cset, max_deliver, transid++);
642
      chunkID_set_free(offer_cset);
643
    }
644
  }
645
}
646

    
647

    
648
void send_chunk()
649
{
650
  struct chunk *buff;
651
  int size, res, i, n;
652
  struct peer *neighbours;
653
  struct peerset *pset;
654

    
655
  pset = get_peers();
656
  n = peerset_size(pset);
657
  neighbours = peerset_get_peers(pset);
658
  dprintf("Send Chunk: %d neighbours\n", n);
659
  if (n == 0) return;
660
  buff = cb_get_chunks(cb, &size);
661
  dprintf("\t %d chunks in buffer...\n", size);
662
  if (size == 0) return;
663

    
664
  /************ STUPID DUMB SCHEDULING ****************/
665
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
666
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
667
  /************ /STUPID DUMB SCHEDULING ****************/
668

    
669
  /************ USE SCHEDULER ****************/
670
  {
671
    size_t selectedpairs_len = 1;
672
    int chunkids[size];
673
    struct peer *nodeids[n];
674
    struct PeerChunk selectedpairs[1];
675
  
676
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
677
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
678
    SCHED_TYPE(SCHED_WEIGHTING, nodeids, n, chunkids, 1, selectedpairs, &selectedpairs_len, SCHED_NEEDS, SCHED_PEER, SCHED_CHUNK);
679
  /************ /USE SCHEDULER ****************/
680

    
681
    for (i=0; i<selectedpairs_len ; i++){
682
      struct peer *p = selectedpairs[i].peer;
683
      const struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
684
      dprintf("\t sending chunk[%d] to ", c->id);
685
      dprintf("%s\n", node_addr(p->id));
686

    
687
      if (send_bmap_before_push) {
688
        send_bmap(p->id);
689
      }
690

    
691
      chunk_attributes_update_sending(c);
692
      res = sendChunk(p->id, c, 0);        //we do not use transactions in pure push
693
      if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %"PRIu64" Result: %d Size: %d bytes\n", c->id, node_addr(p->id), gettimeofday_in_us(), res, c->size);}
694
      dprintf("\tResult: %d\n", res);
695
      if (res>=0) {
696
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
697
        reg_chunk_send(c->id);
698
      } else {
699
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
700
      }
701
    }
702
  }
703
}