Statistics
| Branch: | Revision:

streamers / streaming.c @ 08f76adc

History | View | Annotate | Download (17.1 KB)

1 8fed7779 CsabaKiraly
/*
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 1cd06c26 CsabaKiraly
#include <sys/time.h>
8 89e893e2 Luca
#include <stdlib.h>
9
#include <stdio.h>
10
#include <stdint.h>
11 30a6e902 Csaba Kiraly
#include <stdbool.h>
12 4c0ba13e Csaba Kiraly
#include <math.h>
13 e99600d8 Csaba Kiraly
#include <assert.h>
14 f14985ba Luca Abeni
#include <string.h>
15 84ff82ba CsabaKiraly
#include <inttypes.h>
16 89e893e2 Luca
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 0f35d029 Csaba Kiraly
#include <peerset.h>
23
#include <peer.h>
24 e98d8f50 Csaba Kiraly
#include <chunkidset.h>
25 ea084625 Csaba Kiraly
#include <limits.h>
26 6546a0c0 Alessandro Russo
#include <trade_sig_ha.h>
27 58811a24 Csaba Kiraly
#include <chunkiser_attrib.h>
28 89e893e2 Luca
29
#include "streaming.h"
30
#include "output.h"
31
#include "input.h"
32 e64fc7e5 Luca
#include "dbg.h"
33 b44ae8d2 CsabaKiraly
#include "chunk_signaling.h"
34 abd2ef3b Csaba Kiraly
#include "chunklock.h"
35 fcb5c29b Csaba Kiraly
#include "topology.h"
36 4c0ba13e Csaba Kiraly
#include "measures.h"
37 0fec1310 Csaba Kiraly
#include "scheduling.h"
38 89e893e2 Luca
39 4367dafd Csaba Kiraly
#include "scheduler_la.h"
40
41 d88f86de Csaba Kiraly
static bool heuristics_distance_maxdeliver = false;
42 a204d648 Csaba Kiraly
static int bcast_after_receive_every = 0;
43 59b87dad Csaba Kiraly
static bool neigh_on_chunk_recv = false;
44 10fd812c Csaba Kiraly
45 e99600d8 Csaba Kiraly
struct chunk_attributes {
46 f5d9663e Csaba Kiraly
  uint64_t deadline;
47
  uint16_t deadline_increment;
48 e99600d8 Csaba Kiraly
  uint16_t hopcount;
49
} __attribute__((packed));
50
51 1cd06c26 CsabaKiraly
extern bool chunk_log;
52 bc1ddc15 MatteoSammarco
53 03dca3bf ArpadBakay
struct chunk_buffer *cb;
54 709f774c Luca
static struct input_desc *input;
55 8c1b2832 Csaba Kiraly
static int cb_size;
56 7ca3b176 Csaba Kiraly
static int transid=0;
57 89e893e2 Luca
58 dc87dca9 Csaba Kiraly
static int offer_per_tick = 1;        //N_p parameter of POLITO
59
60 742dfaec Csaba Kiraly
int _needs(struct chunkID_set *cset, int cb_size, int cid);
61
62 1cd06c26 CsabaKiraly
uint64_t gettimeofday_in_us(void)
63
{
64
  struct timeval what_time; //to store the epoch time
65
66
  gettimeofday(&what_time, NULL);
67
  return what_time.tv_sec * 1000000ULL + what_time.tv_usec;
68
}
69
70 d3a242ab Csaba Kiraly
void cb_print()
71
{
72 b45e7201 Csaba Kiraly
#ifdef DEBUG
73 d3a242ab Csaba Kiraly
  struct chunk *chunks;
74
  int num_chunks, i, id;
75
  chunks = cb_get_chunks(cb, &num_chunks);
76
77
  dprintf("\tchbuf :");
78
  i = 0;
79
  if(num_chunks) {
80
    id = chunks[0].id;
81
    dprintf(" %d-> ",id);
82
    while (i < num_chunks) {
83
      if (id == chunks[i].id) {
84
        dprintf("%d",id % 10);
85
        i++;
86 2314ccb7 Csaba Kiraly
      } else if (chunk_islocked(id)) {
87
        dprintf("*");
88 d3a242ab Csaba Kiraly
      } else {
89
        dprintf(".");
90
      }
91
      id++;
92
    }
93
  }
94
  dprintf("\n");
95 b45e7201 Csaba Kiraly
#endif
96 d3a242ab Csaba Kiraly
}
97
98 6920fdab Luca
void stream_init(int size, struct nodeID *myID)
99 89e893e2 Luca
{
100 8612d586 CsabaKiraly
  static char conf[32];
101 89e893e2 Luca
102 8c1b2832 Csaba Kiraly
  cb_size = size;
103
104
  sprintf(conf, "size=%d", cb_size);
105 89e893e2 Luca
  cb = cb_init(conf);
106 1b6a3ea7 Csaba Kiraly
  chunkDeliveryInit(myID);
107 513e75ef Alessandro Russo
  chunkSignalingInit(myID);
108 95faef91 CsabaKiraly
  init_measures();
109 7442ecb3 Luca
}
110
111 c9370421 CsabaKiraly
int source_init(const char *fname, struct nodeID *myID, bool loop, int *fds, int fds_size)
112 7442ecb3 Luca
{
113 f14985ba Luca Abeni
  int flags = 0;
114
115
  if (memcmp(fname, "udp:", 4) == 0) {
116
    fname += 4;
117
    flags = INPUT_UDP;
118
  }
119
  if (loop) {
120
    flags |= INPUT_LOOP;
121
  }
122
  input = input_open(fname, flags, fds, fds_size);
123 7442ecb3 Luca
  if (input == NULL) {
124
    return -1;
125
  }
126
127
  stream_init(1, myID);
128
  return 0;
129 89e893e2 Luca
}
130
131 e99600d8 Csaba Kiraly
void chunk_attributes_fill(struct chunk* c)
132
{
133
  struct chunk_attributes * ca;
134 58811a24 Csaba Kiraly
  int priority = 1;
135 e99600d8 Csaba Kiraly
136 58811a24 Csaba Kiraly
  assert((!c->attributes && c->attributes_size == 0) ||
137
         chunk_attributes_chunker_verify(c->attributes, c->attributes_size));
138
139
  if (chunk_attributes_chunker_verify(c->attributes, c->attributes_size)) {
140
    priority = ((struct chunk_attributes_chunker*) c->attributes)->priority;
141
    free(c->attributes);
142
    c->attributes = NULL;
143
    c->attributes_size = 0;
144
  }
145 e99600d8 Csaba Kiraly
146
  c->attributes_size = sizeof(struct chunk_attributes);
147 74a5d4ae CsabaKiraly
  c->attributes = ca = malloc(c->attributes_size);
148 e99600d8 Csaba Kiraly
149 e13e2d0e Csaba Kiraly
  ca->deadline = c->id;
150 58811a24 Csaba Kiraly
  ca->deadline_increment = priority * 2;
151 e99600d8 Csaba Kiraly
  ca->hopcount = 0;
152
}
153
154 ccfc425d Csaba Kiraly
int chunk_get_hopcount(struct chunk* c) {
155
  struct chunk_attributes * ca;
156
157
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
158 84ff82ba CsabaKiraly
    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));
159 ccfc425d Csaba Kiraly
    return -1;
160
  }
161
162
  ca = (struct chunk_attributes *) c->attributes;
163
  return ca->hopcount;
164
}
165
166 74a5d4ae CsabaKiraly
void chunk_attributes_update_received(struct chunk* c)
167 e99600d8 Csaba Kiraly
{
168
  struct chunk_attributes * ca;
169
170
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
171 84ff82ba CsabaKiraly
    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));
172 e99600d8 Csaba Kiraly
    return;
173
  }
174
175
  ca = (struct chunk_attributes *) c->attributes;
176
  ca->hopcount++;
177
  dprintf("Received chunk %d with hopcount %hu\n", c->id, ca->hopcount);
178
}
179
180 43dd1a10 Csaba Kiraly
void chunk_attributes_update_sending(const struct chunk* c)
181 f5d9663e Csaba Kiraly
{
182
  struct chunk_attributes * ca;
183
184
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
185 cf6aaf5b Csaba Kiraly
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
186 f5d9663e Csaba Kiraly
    return;
187
  }
188
189
  ca = (struct chunk_attributes *) c->attributes;
190
  ca->deadline += ca->deadline_increment;
191
  dprintf("Sending chunk %d with deadline %lu\n", c->id, ca->deadline);
192
}
193
194 2b97cbf1 Csaba Kiraly
struct chunkID_set *cb_to_bmap(struct chunk_buffer *chbuf)
195
{
196
  struct chunk *chunks;
197 74a5d4ae CsabaKiraly
  int num_chunks, i;
198 b31bd3a2 CsabaKiraly
  struct chunkID_set *my_bmap = chunkID_set_init("type=bitmap");
199 2b97cbf1 Csaba Kiraly
  chunks = cb_get_chunks(chbuf, &num_chunks);
200
201 b5462b05 Csaba Kiraly
  for(i=num_chunks-1; i>=0; i--) {
202 2b97cbf1 Csaba Kiraly
    chunkID_set_add_chunk(my_bmap, chunks[i].id);
203
  }
204
  return my_bmap;
205
}
206
207 d447f71d Csaba Kiraly
// a simple implementation that request everything that we miss ... up to max deliver
208 8e750be6 Csaba Kiraly
struct chunkID_set *get_chunks_to_accept(struct nodeID *fromid, const struct chunkID_set *cset_off, int max_deliver, uint16_t trans_id){
209 2b97cbf1 Csaba Kiraly
  struct chunkID_set *cset_acc, *my_bmap;
210 d447f71d Csaba Kiraly
  int i, d, cset_off_size;
211 8dd1eccd Csaba Kiraly
  //double lossrate;
212 f9630d20 Csaba Kiraly
  struct peer *from = nodeid_to_peer(fromid, 0);
213 d447f71d Csaba Kiraly
214 0781f344 Csaba Kiraly
  cset_acc = chunkID_set_init("size=0");
215 e735a1b1 Csaba Kiraly
216
  //reduce load a little bit if there are losses on the path from this guy
217 8dd1eccd Csaba Kiraly
  //lossrate = get_lossrate_receive(from->id);
218
  //lossrate = finite(lossrate) ? lossrate : 0;        //start agressively, assuming 0 loss
219
  //if (rand()/((double)RAND_MAX + 1) >= 10 * lossrate ) {
220 e735a1b1 Csaba Kiraly
    my_bmap = cb_to_bmap(cb);
221
    cset_off_size = chunkID_set_size(cset_off);
222
    for (i = 0, d = 0; i < cset_off_size && d < max_deliver; i++) {
223
      int chunkid = chunkID_set_get_chunk(cset_off, i);
224 702769ac Csaba Kiraly
      //dprintf("\tdo I need c%d ? :",chunkid);
225 e735a1b1 Csaba Kiraly
      if (!chunk_islocked(chunkid) && _needs(my_bmap, cb_size, chunkid)) {
226
        chunkID_set_add_chunk(cset_acc, chunkid);
227
        chunk_lock(chunkid,from);
228 f9630d20 Csaba Kiraly
        dtprintf("accepting %d from %s", chunkid, node_addr(fromid));
229 cff93a07 Csaba Kiraly
#ifdef MONL
230 f9630d20 Csaba Kiraly
        dprintf(", loss:%f rtt:%f", get_lossrate(fromid), get_rtt(fromid));
231 cff93a07 Csaba Kiraly
#endif
232
        dprintf("\n");
233 e735a1b1 Csaba Kiraly
        d++;
234
      }
235 d447f71d Csaba Kiraly
    }
236 e735a1b1 Csaba Kiraly
    chunkID_set_free(my_bmap);
237 8dd1eccd Csaba Kiraly
  //} else {
238 f9630d20 Csaba Kiraly
  //    dtprintf("accepting -- from %s loss:%f rtt:%f\n", node_addr(fromid), lossrate, get_rtt(fromid));
239 8dd1eccd Csaba Kiraly
  //}
240 d447f71d Csaba Kiraly
241
  return cset_acc;
242
}
243
244 b0225995 Csaba Kiraly
void send_bmap(struct nodeID *toid)
245 22ebd96d Csaba Kiraly
{
246
  struct chunkID_set *my_bmap = cb_to_bmap(cb);
247 b0225995 Csaba Kiraly
   sendBufferMap(toid,NULL, my_bmap, input ? 0 : cb_size, 0);
248 c8c4c779 Csaba Kiraly
  chunkID_set_free(my_bmap);
249 a1a9e662 Csaba Kiraly
}
250
251 a204d648 Csaba Kiraly
void bcast_bmap()
252
{
253
  int i, n;
254
  struct peer *neighbours;
255
  struct peerset *pset;
256 ba99da01 Csaba Kiraly
  struct chunkID_set *my_bmap;
257 a204d648 Csaba Kiraly
258
  pset = get_peers();
259
  n = peerset_size(pset);
260
  neighbours = peerset_get_peers(pset);
261
262 ba99da01 Csaba Kiraly
  my_bmap = cb_to_bmap(cb);        //cache our bmap for faster processing
263 a204d648 Csaba Kiraly
  for (i = 0; i<n; i++) {
264 ba99da01 Csaba Kiraly
    sendBufferMap(neighbours[i].id,NULL, my_bmap, input ? 0 : cb_size, 0);
265 a204d648 Csaba Kiraly
  }
266 ba99da01 Csaba Kiraly
  chunkID_set_free(my_bmap);
267 a204d648 Csaba Kiraly
}
268
269 68330740 Csaba Kiraly
double get_average_lossrate_pset(struct peerset *pset)
270
{
271
  int i, n;
272
  struct peer *neighbours;
273
274
  n = peerset_size(pset);
275
  neighbours = peerset_get_peers(pset);
276
  {
277 74a5d4ae CsabaKiraly
    struct nodeID *nodeids[n];
278 68330740 Csaba Kiraly
    for (i = 0; i<n; i++) nodeids[i] = neighbours[i].id;
279 5fc04950 Csaba Kiraly
#ifdef MONL
280 68330740 Csaba Kiraly
    return get_average_lossrate(nodeids, n);
281 5fc04950 Csaba Kiraly
#else
282
    return 0;
283
#endif
284 68330740 Csaba Kiraly
  }
285
}
286
287 fa3d2720 Csaba Kiraly
void ack_chunk(struct chunk *c, struct nodeID *from)
288 5b95417d Csaba Kiraly
{
289
  //reduce load a little bit if there are losses on the path from this guy
290
  double average_lossrate = get_average_lossrate_pset(get_peers());
291
  average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
292
  if (rand()/((double)RAND_MAX + 1) < 1 * average_lossrate ) {
293
    return;
294
  }
295 fa3d2720 Csaba Kiraly
  send_bmap(from);        //send explicit ack
296 5b95417d Csaba Kiraly
}
297
298 74a5d4ae CsabaKiraly
void received_chunk(struct nodeID *from, const uint8_t *buff, int len)
299 89e893e2 Luca
{
300
  int res;
301 52b7c5ea Luca
  static struct chunk c;
302 30c9739a Csaba Kiraly
  struct peer *p;
303 a204d648 Csaba Kiraly
  static int bcast_cnt;
304 397f3860 Csaba Kiraly
  uint16_t transid;
305 89e893e2 Luca
306 a1c01ccf Csaba Kiraly
  res = parseChunkMsg(buff + 1, len - 1, &c, &transid);
307 89e893e2 Luca
  if (res > 0) {
308 e99600d8 Csaba Kiraly
    chunk_attributes_update_received(&c);
309 6ac1e106 Csaba Kiraly
    chunk_unlock(c.id);
310 13d85fc6 Csaba Kiraly
    dprintf("Received chunk %d from peer: %s\n", c.id, node_addr(from));
311 84ff82ba CsabaKiraly
    if(chunk_log){fprintf(stderr, "TEO: Received chunk %d from peer: %s at: %"PRIu64" hopcount: %i\n", c.id, node_addr(from), gettimeofday_in_us(), chunk_get_hopcount(&c));}
312 89e893e2 Luca
    output_deliver(&c);
313
    res = cb_add_chunk(cb, &c);
314 14e5c21e Csaba Kiraly
    reg_chunk_receive(c.id, c.timestamp, chunk_get_hopcount(&c), res==E_CB_OLD, res==E_CB_DUPLICATE);
315 fc0260a4 Csaba Kiraly
    cb_print();
316 89e893e2 Luca
    if (res < 0) {
317 13d85fc6 Csaba Kiraly
      dprintf("\tchunk too old, buffer full with newer chunks\n");
318 84ff82ba CsabaKiraly
      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());}
319 89e893e2 Luca
      free(c.data);
320
      free(c.attributes);
321
    }
322 13d4c180 Csaba Kiraly
    p = nodeid_to_peer(from, neigh_on_chunk_recv);
323 30c9739a Csaba Kiraly
    if (p) {        //now we have it almost sure
324
      chunkID_set_add_chunk(p->bmap,c.id);        //don't send it back
325
    }
326 c3e8369f Csaba Kiraly
    ack_chunk(&c,from);        //send explicit ack
327 a204d648 Csaba Kiraly
    if (bcast_after_receive_every && bcast_cnt % bcast_after_receive_every == 0) {
328
       bcast_bmap();
329
    }
330 ae3c4aa9 Csaba Kiraly
  } else {
331
    fprintf(stderr,"\tError: can't decode chunk!\n");
332 89e893e2 Luca
  }
333
}
334
335 685225b2 CsabaKiraly
struct chunk *generated_chunk(suseconds_t *delta)
336 89e893e2 Luca
{
337 685225b2 CsabaKiraly
  struct chunk *c;
338
339
  c = malloc(sizeof(struct chunk));
340
  if (!c) {
341
    fprintf(stderr, "Memory allocation error!\n");
342
    return NULL;
343
  }
344 89e893e2 Luca
345 685225b2 CsabaKiraly
  *delta = input_get(input, c);
346 afdc8db4 Luca Abeni
  if (*delta < 0) {
347 4bb789ed Luca
    fprintf(stderr, "Error in input!\n");
348
    exit(-1);
349
  }
350 685225b2 CsabaKiraly
  if (c->data == NULL) {
351
    free(c);
352
    return NULL;
353 4bb789ed Luca
  }
354 685225b2 CsabaKiraly
  dprintf("Generated chunk %d of %d bytes\n",c->id, c->size);
355
  chunk_attributes_fill(c);
356
  return c;
357
}
358
359
int add_chunk(struct chunk *c)
360
{
361
  int res;
362
363
  res = cb_add_chunk(cb, c);
364 5fb5ecd4 Luca
  if (res < 0) {
365 685225b2 CsabaKiraly
    free(c->data);
366
    free(c->attributes);
367
    free(c);
368
    return 0;
369 89e893e2 Luca
  }
370 685225b2 CsabaKiraly
  free(c);
371 afdc8db4 Luca Abeni
  return 1;
372 89e893e2 Luca
}
373
374 4367dafd Csaba Kiraly
/**
375 d74bc79c Csaba Kiraly
 *example function to filter chunks based on whether a given peer needs them.
376
 *
377
 * Looks at buffermap information received about the given peer.
378 4367dafd Csaba Kiraly
 */
379 7b86e7d9 Csaba Kiraly
int needs(struct peer *n, int cid){
380
  struct peer * p = n;
381 1b7da906 Csaba Kiraly
382 3cd33bb0 Csaba Kiraly
  //dprintf("\t%s needs c%d ? :",node_addr(p->id),c->id);
383 d74bc79c Csaba Kiraly
  if (! p->bmap) {
384 3cd33bb0 Csaba Kiraly
    //dprintf("no bmap\n");
385 d74bc79c Csaba Kiraly
    return 1;        // if we have no bmap information, we assume it needs the chunk (aggressive behaviour!)
386
  }
387 b44ae8d2 CsabaKiraly
  return _needs(p->bmap, p->cb_size, cid);
388 742dfaec Csaba Kiraly
}
389 d74bc79c Csaba Kiraly
390 742dfaec Csaba Kiraly
int _needs(struct chunkID_set *cset, int cb_size, int cid){
391 7c2ac59e CsabaKiraly
  if (cb_size == 0) { //if it declared it does not needs chunks
392
    return 0;
393
  }
394
395 742dfaec Csaba Kiraly
  if (chunkID_set_check(cset,cid) < 0) { //it might need the chunk
396 d74bc79c Csaba Kiraly
    int missing, min;
397
    //@TODO: add some bmap_timestamp based logic
398
399 742dfaec Csaba Kiraly
    if (chunkID_set_size(cset) == 0) {
400 3cd33bb0 Csaba Kiraly
      //dprintf("bmap empty\n");
401 d74bc79c Csaba Kiraly
      return 1;        // if the bmap seems empty, it needs the chunk
402
    }
403 742dfaec Csaba Kiraly
    missing = cb_size - chunkID_set_size(cset);
404 d74bc79c Csaba Kiraly
    missing = missing < 0 ? 0 : missing;
405 eb42de41 Csaba Kiraly
    min = chunkID_set_get_earliest(cset);
406 3cd33bb0 Csaba Kiraly
      //dprintf("%s ... cid(%d) >= min(%d) - missing(%d) ?\n",(cid >= min - missing)?"YES":"NO",cid, min, missing);
407 742dfaec Csaba Kiraly
    return (cid >= min - missing);
408 d74bc79c Csaba Kiraly
  }
409
410 3cd33bb0 Csaba Kiraly
  //dprintf("has it\n");
411 d74bc79c Csaba Kiraly
  return 0;
412 4367dafd Csaba Kiraly
}
413 d74bc79c Csaba Kiraly
414 7b86e7d9 Csaba Kiraly
double peerWeightReceivedfrom(struct peer **n){
415
  struct peer * p = *n;
416 1b7da906 Csaba Kiraly
  return timerisset(&p->bmap_timestamp) ? 1 : 0.1;
417 4367dafd Csaba Kiraly
}
418 eb4ecf1c Csaba Kiraly
419 8bb39b0b Csaba Kiraly
double peerWeightUniform(struct peer **n){
420 eb4ecf1c Csaba Kiraly
  return 1;
421
}
422
423 7b86e7d9 Csaba Kiraly
double peerWeightRtt(struct peer **n){
424 d4a680a0 Csaba Kiraly
#ifdef MONL
425 7b86e7d9 Csaba Kiraly
  double rtt = get_rtt(*n->id);
426 1b7da906 Csaba Kiraly
  //dprintf("RTT to %s: %f\n", node_addr(p->id), rtt);
427 3ffbcf2d Csaba Kiraly
  return finite(rtt) ? 1 / (rtt + 0.005) : 1 / 1;
428 d4a680a0 Csaba Kiraly
#else
429
  return 1;
430 5fc04950 Csaba Kiraly
#endif
431 d4a680a0 Csaba Kiraly
}
432 4c0ba13e Csaba Kiraly
433 ea084625 Csaba Kiraly
//ordering function for ELp peer selection, chunk ID based
434
//can't be used as weight
435 1b7da906 Csaba Kiraly
double peerScoreELpID(struct nodeID **n){
436 ea084625 Csaba Kiraly
  struct chunkID_set *bmap;
437
  int latest;
438 1b7da906 Csaba Kiraly
  struct peer * p = nodeid_to_peer(*n, 0);
439
  if (!p) return 0;
440 ea084625 Csaba Kiraly
441 1b7da906 Csaba Kiraly
  bmap = p->bmap;
442 ea084625 Csaba Kiraly
  if (!bmap) return 0;
443
  latest = chunkID_set_get_latest(bmap);
444
  if (latest == INT_MIN) return 0;
445
446
  return -latest;
447
}
448
449 97ab5c93 Csaba Kiraly
double chunkScoreChunkID(int *cid){
450
  return (double) *cid;
451
}
452
453 54da4e2b Csaba Kiraly
uint64_t get_chunk_deadline(int cid){
454 08f76adc Csaba Kiraly
  const struct chunk_attributes * ca;
455
  const struct chunk *c;
456 54da4e2b Csaba Kiraly
457
  c = cb_get_chunk(cb, cid);
458
  if (!c) return 0;
459
460
  if (!c->attributes || c->attributes_size != sizeof(struct chunk_attributes)) {
461
    fprintf(stderr,"Warning, chunk %d with strange attributes block\n", c->id);
462
    return 0;
463
  }
464
465
  ca = (struct chunk_attributes *) c->attributes;
466
  return ca->deadline;
467
}
468
469 e13e2d0e Csaba Kiraly
double chunkScoreDL(int *cid){
470 05a1dc0a Csaba Kiraly
  return - (double)get_chunk_deadline(*cid);
471 e13e2d0e Csaba Kiraly
}
472
473 74a5d4ae CsabaKiraly
double getChunkTimestamp(int *cid){
474 43dd1a10 Csaba Kiraly
  const struct chunk *c = cb_get_chunk(cb, *cid);
475 1b7da906 Csaba Kiraly
  if (!c) return 0;
476
477
  return (double) c->timestamp;
478 4367dafd Csaba Kiraly
}
479
480 8e750be6 Csaba Kiraly
void send_accepted_chunks(struct nodeID *toid, struct chunkID_set *cset_acc, int max_deliver, uint16_t trans_id){
481 8e85b948 Alessandro Russo
  int i, d, cset_acc_size, res;
482 b13977c0 Csaba Kiraly
  struct peer *to = nodeid_to_peer(toid, 0);
483 d447f71d Csaba Kiraly
484
  cset_acc_size = chunkID_set_size(cset_acc);
485 e2c563e7 Csaba Kiraly
  reg_offer_accept(cset_acc_size > 0 ? 1 : 0);        //this only works if accepts are sent back even if 0 is accepted
486 d447f71d Csaba Kiraly
  for (i = 0, d=0; i < cset_acc_size && d < max_deliver; i++) {
487 43dd1a10 Csaba Kiraly
    const struct chunk *c;
488 d447f71d Csaba Kiraly
    int chunkid = chunkID_set_get_chunk(cset_acc, i);
489
    c = cb_get_chunk(cb, chunkid);
490 f8286367 Csaba Kiraly
    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
491 f5d9663e Csaba Kiraly
      chunk_attributes_update_sending(c);
492 033319c7 Csaba Kiraly
      res = sendChunk(toid, c, trans_id);
493 d447f71d Csaba Kiraly
      if (res >= 0) {
494 f8286367 Csaba Kiraly
        if(to) chunkID_set_add_chunk(to->bmap, c->id); //don't send twice ... assuming that it will actually arrive
495 d447f71d Csaba Kiraly
        d++;
496 f740dafb Csaba Kiraly
        reg_chunk_send(c->id);
497 0adbb1ef Csaba Kiraly
        if(chunk_log){fprintf(stderr, "TEO: Sending chunk %d to peer: %s at: %"PRIu64" Result: %d\n", c->id, node_addr(toid), gettimeofday_in_us(), res);}
498 96b8d0a8 Csaba Kiraly
      } else {
499
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
500 d447f71d Csaba Kiraly
      }
501
    }
502
  }
503
}
504
505 dc87dca9 Csaba Kiraly
int offer_peer_count()
506
{
507
  return offer_per_tick;
508
}
509
510 74a5d4ae CsabaKiraly
int offer_max_deliver(struct nodeID *n)
511 960017bf Csaba Kiraly
{
512 10fd812c Csaba Kiraly
513
  if (!heuristics_distance_maxdeliver) return 1;
514
515 5fc04950 Csaba Kiraly
#ifdef MONL
516 1b7da906 Csaba Kiraly
  switch (get_hopcount(n)) {
517 960017bf Csaba Kiraly
    case 0: return 5;
518
    case 1: return 2;
519
    default: return 1;
520
  }
521 5fc04950 Csaba Kiraly
#else
522
  return 1;
523
#endif
524 960017bf Csaba Kiraly
}
525
526 fcb5c29b Csaba Kiraly
void send_offer()
527 43355360 Csaba Kiraly
{
528
  struct chunk *buff;
529 74a5d4ae CsabaKiraly
  int size, res, i, n;
530 43355360 Csaba Kiraly
  struct peer *neighbours;
531 fcb5c29b Csaba Kiraly
  struct peerset *pset;
532 43355360 Csaba Kiraly
533 fcb5c29b Csaba Kiraly
  pset = get_peers();
534 43355360 Csaba Kiraly
  n = peerset_size(pset);
535
  neighbours = peerset_get_peers(pset);
536
  dprintf("Send Offer: %d neighbours\n", n);
537
  if (n == 0) return;
538
  buff = cb_get_chunks(cb, &size);
539
  if (size == 0) return;
540
541
  {
542 dc87dca9 Csaba Kiraly
    size_t selectedpeers_len = offer_peer_count();
543 1b7da906 Csaba Kiraly
    int chunkids[size];
544 8bb39b0b Csaba Kiraly
    struct peer *nodeids[n];
545
    struct peer *selectedpeers[selectedpeers_len];
546 43355360 Csaba Kiraly
547 abbef5a5 Csaba Kiraly
    //reduce load a little bit if there are losses on the path from this guy
548 68330740 Csaba Kiraly
    double average_lossrate = get_average_lossrate_pset(pset);
549 abbef5a5 Csaba Kiraly
    average_lossrate = finite(average_lossrate) ? average_lossrate : 0;        //start agressively, assuming 0 loss
550 f76dc385 Csaba Kiraly
    if (rand()/((double)RAND_MAX + 1) < 10 * average_lossrate ) {
551 abbef5a5 Csaba Kiraly
      return;
552
    }
553
554 ffcc70ad CsabaKiraly
    for (i = 0;i < size; i++) chunkids[size - 1 - i] = (buff+i)->id;
555 8bb39b0b Csaba Kiraly
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
556 851fa962 Csaba Kiraly
    selectPeersForChunks(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpeers, &selectedpeers_len, SCHED_NEEDS, SCHED_PEER);
557 43355360 Csaba Kiraly
558
    for (i=0; i<selectedpeers_len ; i++){
559 8bb39b0b Csaba Kiraly
      int max_deliver = offer_max_deliver(selectedpeers[i]->id);
560 43355360 Csaba Kiraly
      struct chunkID_set *my_bmap = cb_to_bmap(cb);
561 8bb39b0b Csaba Kiraly
      dprintf("\t sending offer(%d) to %s, cb_size: %d\n", transid, node_addr(selectedpeers[i]->id), selectedpeers[i]->cb_size);
562
      res = offerChunks(selectedpeers[i]->id, my_bmap, max_deliver, transid++);
563 ddedf85f Csaba Kiraly
      chunkID_set_free(my_bmap);
564 43355360 Csaba Kiraly
    }
565
  }
566
}
567
568 4367dafd Csaba Kiraly
569 fcb5c29b Csaba Kiraly
void send_chunk()
570 89e893e2 Luca
{
571
  struct chunk *buff;
572 74a5d4ae CsabaKiraly
  int size, res, i, n;
573 924226c0 Luca Abeni
  struct peer *neighbours;
574 fcb5c29b Csaba Kiraly
  struct peerset *pset;
575 1cd06c26 CsabaKiraly
576 fcb5c29b Csaba Kiraly
  pset = get_peers();
577 0f35d029 Csaba Kiraly
  n = peerset_size(pset);
578
  neighbours = peerset_get_peers(pset);
579 e64fc7e5 Luca
  dprintf("Send Chunk: %d neighbours\n", n);
580 89e893e2 Luca
  if (n == 0) return;
581
  buff = cb_get_chunks(cb, &size);
582 e64fc7e5 Luca
  dprintf("\t %d chunks in buffer...\n", size);
583 89e893e2 Luca
  if (size == 0) return;
584
585
  /************ STUPID DUMB SCHEDULING ****************/
586 4367dafd Csaba Kiraly
  //target = n * (rand() / (RAND_MAX + 1.0)); /*0..n-1*/
587
  //c = size * (rand() / (RAND_MAX + 1.0)); /*0..size-1*/
588 89e893e2 Luca
  /************ /STUPID DUMB SCHEDULING ****************/
589
590 4367dafd Csaba Kiraly
  /************ USE SCHEDULER ****************/
591 924226c0 Luca Abeni
  {
592
    size_t selectedpairs_len = 1;
593 1b7da906 Csaba Kiraly
    int chunkids[size];
594 7b86e7d9 Csaba Kiraly
    struct peer *nodeids[n];
595 924226c0 Luca Abeni
    struct PeerChunk selectedpairs[1];
596
  
597 1b7da906 Csaba Kiraly
    for (i = 0;i < size; i++) chunkids[i] = (buff+i)->id;
598 7b86e7d9 Csaba Kiraly
    for (i = 0; i<n; i++) nodeids[i] = (neighbours+i);
599 0fec1310 Csaba Kiraly
    SCHED_TYPE(SCHED_WEIGHTING, nodeids, n, chunkids, size, selectedpairs, &selectedpairs_len, SCHED_NEEDS, SCHED_PEER, SCHED_CHUNK);
600 4367dafd Csaba Kiraly
  /************ /USE SCHEDULER ****************/
601
602 924226c0 Luca Abeni
    for (i=0; i<selectedpairs_len ; i++){
603 7b86e7d9 Csaba Kiraly
      struct peer *p = selectedpairs[i].peer;
604 74a5d4ae CsabaKiraly
      struct chunk *c = cb_get_chunk(cb, selectedpairs[i].chunk);
605 924226c0 Luca Abeni
      dprintf("\t sending chunk[%d] to ", c->id);
606
      dprintf("%s\n", node_addr(p->id));
607
608 b0225995 Csaba Kiraly
      send_bmap(p->id);
609 bc1ddc15 MatteoSammarco
610 f5d9663e Csaba Kiraly
      chunk_attributes_update_sending(c);
611 033319c7 Csaba Kiraly
      res = sendChunk(p->id, c, 0);        //we do not use transactions in pure push
612 84ff82ba CsabaKiraly
      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);}
613 13d85fc6 Csaba Kiraly
      dprintf("\tResult: %d\n", res);
614 924226c0 Luca Abeni
      if (res>=0) {
615
        chunkID_set_add_chunk(p->bmap,c->id); //don't send twice ... assuming that it will actually arrive
616 f740dafb Csaba Kiraly
        reg_chunk_send(c->id);
617 96b8d0a8 Csaba Kiraly
      } else {
618
        fprintf(stderr,"ERROR sending chunk %d\n",c->id);
619 924226c0 Luca Abeni
      }
620 0f35d029 Csaba Kiraly
    }
621 4367dafd Csaba Kiraly
  }
622 89e893e2 Luca
}