Statistics
| Branch: | Revision:

streamers / streaming.c @ d5f60f90

History | View | Annotate | Download (18.6 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:%lu\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:%lu\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
    }
331
    ack_chunk(&c, from, transid);        //send explicit ack
332
    if (bcast_after_receive_every && bcast_cnt % bcast_after_receive_every == 0) {
333
       bcast_bmap();
334
    }
335
  } else {
336
    fprintf(stderr,"\tError: can't decode chunk!\n");
337
  }
338
}
339

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

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

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

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

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

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

    
383
  return c->timestamp;
384
}
385

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
475
  return -latest;
476
}
477

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

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

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

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

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

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

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

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

    
510
  transaction_reg_accept(trans_id, toid);
511

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

    
533
int offer_peer_count()
534
{
535
  return offer_per_tick;
536
}
537

    
538
int offer_max_deliver(struct nodeID *n)
539
{
540

    
541
  if (!heuristics_distance_maxdeliver) return 1;
542

    
543
#ifdef MONL
544
  switch (get_hopcount(n)) {
545
    case 0: return 5;
546
    case 1: return 2;
547
    default: return 1;
548
  }
549
#else
550
  return 1;
551
#endif
552
}
553

    
554
static struct chunkID_set * compose_offer_cset(void)
555
{
556
  if (am_i_source()) {
557
    struct chunk *chunks;
558
    int num_chunks, j;
559
    struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
560
    chunks = cb_get_chunks(cb, &num_chunks);
561
    for(j=((num_chunks-1)*3)/4; j>=0; j--) {
562
      chunkID_set_add_chunk(my_bmap, chunks[j].id);
563
    }
564
    return my_bmap;
565
  } else {
566
    return cb_to_bmap(cb);
567
  }
568
}
569

    
570

    
571
void send_offer()
572
{
573
  struct chunk *buff;
574
  int size, res, i, n;
575
  struct peer *neighbours;
576
  struct peerset *pset;
577

    
578
  pset = get_peers();
579
  n = peerset_size(pset);
580
  neighbours = peerset_get_peers(pset);
581
  dprintf("Send Offer: %d neighbours\n", n);
582
  if (n == 0) return;
583
  buff = cb_get_chunks(cb, &size);
584
  if (size == 0) return;
585

    
586
  {
587
    size_t selectedpeers_len = offer_peer_count();
588
    int chunkids[size];
589
    struct peer *nodeids[n];
590
    struct peer *selectedpeers[selectedpeers_len];
591

    
592
    //reduce load a little bit if there are losses on the path from this guy
593
    double average_lossrate = get_average_lossrate_pset(pset);
594
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
595
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
596
      return;
597
    }
598

    
599
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
600
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
601
    selectPeersForChunks(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpeers, &selectedpeers_len, SCHED_NEEDS, SCHED_PEER);
602

    
603
    for (i=0; i<selectedpeers_len ; i++){
604
      int transid = transaction_create(selectedpeers[i]->id);
605
      int max_deliver = offer_max_deliver(selectedpeers[i]->id);
606
      struct chunkID_set *offer_cset = compose_offer_cset();
607
      dprintf("\t sending offer(%d) to %s, cb_size: %d\n", transid, node_addr(selectedpeers[i]->id), selectedpeers[i]->cb_size);
608
      res = offerChunks(selectedpeers[i]->id, offer_cset, max_deliver, transid++);
609
      chunkID_set_free(offer_cset);
610
    }
611
  }
612
}
613

    
614

    
615
void send_chunk()
616
{
617
  struct chunk *buff;
618
  int size, res, i, n;
619
  struct peer *neighbours;
620
  struct peerset *pset;
621

    
622
  pset = get_peers();
623
  n = peerset_size(pset);
624
  neighbours = peerset_get_peers(pset);
625
  dprintf("Send Chunk: %d neighbours\n", n);
626
  if (n == 0) return;
627
  buff = cb_get_chunks(cb, &size);
628
  dprintf("\t %d chunks in buffer...\n", size);
629
  if (size == 0) return;
630

    
631
  /************ STUPID DUMB SCHEDULING ****************/
632
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
633
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
634
  /************ /STUPID DUMB SCHEDULING ****************/
635

    
636
  /************ USE SCHEDULER ****************/
637
  {
638
    size_t selectedpairs_len = 1;
639
    int chunkids[size];
640
    struct peer *nodeids[n];
641
    struct PeerChunk selectedpairs[1];
642
  
643
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
644
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
645
    SCHED_TYPE(SCHED_WEIGHTING, nodeids, n, chunkids, 1, selectedpairs, &selectedpairs_len, SCHED_NEEDS, SCHED_PEER, SCHED_CHUNK);
646
  /************ /USE SCHEDULER ****************/
647

    
648
    for (i=0; i<selectedpairs_len ; i++){
649
      struct peer *p = selectedpairs[i].peer;
650
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
651
      dprintf("\t sending chunk[%d] to ", c->id);
652
      dprintf("%s\n", node_addr(p->id));
653

    
654
      if (send_bmap_before_push) {
655
        send_bmap(p->id);
656
      }
657

    
658
      chunk_attributes_update_sending(c);
659
      res = sendChunk(p->id, c, 0);        //we do not use transactions in pure push
660
      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);}
661
      dprintf("\tResult: %d\n", res);
662
      if (res>=0) {
663
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
664
        reg_chunk_send(c->id);
665
      } else {
666
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
667
      }
668
    }
669
  }
670
}