Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ fdac07e1

History | View | Annotate | Download (4.26 KB)

1 fa655fab Csaba Kiraly
/*
2 7f591208 Csaba Kiraly
 *  Copyright (c) 2009 Alessandro Russo
3
 *  Copyright (c) 2009 Csaba Kiraly
4 fa655fab Csaba Kiraly
 *
5 7f591208 Csaba Kiraly
 *  This is free software; see gpl-3.0.txt
6 fa655fab Csaba Kiraly
 *
7
 * Chunk Signaling API - Higher Abstraction
8
 *
9
 * The Chunk Signaling HA provides a set of primitives for chunks signaling negotiation with other peers, in order to collect information for the effective chunk exchange with other peers. <br>
10
 * This is a part of the Data Exchange Protocol which provides high level abstraction for chunks' negotiations, like requesting and proposing chunks.
11
 *
12
 */
13 7d19f599 Csaba Kiraly
#include <stdint.h>
14
#include <stdlib.h>
15
#include <stdio.h>
16 c6575994 Csaba Kiraly
#include <sys/time.h>
17 fa655fab Csaba Kiraly
#include <errno.h>
18
#include <assert.h>
19 637f87e0 Csaba Kiraly
#include <string.h>
20 fa655fab Csaba Kiraly
#include "peer.h"
21 7d19f599 Csaba Kiraly
#include "peerset.h"
22 fa655fab Csaba Kiraly
#include "chunkidset.h"
23 7d19f599 Csaba Kiraly
#include "trade_sig_la.h"
24 74a5d4ae CsabaKiraly
#include "chunk_signaling.h"
25 7d19f599 Csaba Kiraly
#include "net_helper.h"
26 7f5321cb CsabaKiraly
#include <trade_sig_ha.h>
27 fa655fab Csaba Kiraly
28 2067b4af Csaba Kiraly
#include "streaming.h"
29 fcb5c29b Csaba Kiraly
#include "topology.h"
30 c1f8de1c Csaba Kiraly
#include "dbg.h"
31
32 b44ae8d2 CsabaKiraly
void bmap_received(const struct nodeID *fromid, const struct nodeID *ownerid, struct chunkID_set *c_set, int cb_size, int trans_id) {
33 242021e6 Csaba Kiraly
  struct peer *owner;
34
  if (nodeid_equal(fromid, ownerid)) {
35
    owner = nodeid_to_peer(ownerid,1);
36
  } else {
37
    dprintf("%s might be behind ",node_addr(ownerid));
38
    dprintf("NAT:%s\n",node_addr(fromid));
39
    owner = nodeid_to_peer(fromid,1);
40
  }
41
  
42 1bb397ce Csaba Kiraly
  if (owner) {        //now we have it almost sure
43 b44ae8d2 CsabaKiraly
    chunkID_set_clear(owner->bmap,cb_size+5);        //TODO: some better solution might be needed to keep info about chunks we sent in flight.
44 718a8f29 Csaba Kiraly
    chunkID_set_union(owner->bmap,c_set);
45 b44ae8d2 CsabaKiraly
    owner->cb_size = cb_size;
46 1bb397ce Csaba Kiraly
    gettimeofday(&owner->bmap_timestamp, NULL);
47
  }
48
}
49
50 2067b4af Csaba Kiraly
void offer_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, int trans_id) {
51
  struct peer *from = nodeid_to_peer(fromid,1);
52
  dprintf("The peer %s offers %d chunks, max deliver %d.\n", node_addr(fromid), chunkID_set_size(cset), max_deliver);
53
54
  if (from) {
55
    struct chunkID_set *cset_acc;
56
57 526c3729 Csaba Kiraly
    //register these chunks in the buffermap. Warning: this should be changed when offers become selective.
58
    chunkID_set_clear(from->bmap,0);        //TODO: some better solution might be needed to keep info about chunks we sent in flight.
59 2067b4af Csaba Kiraly
    chunkID_set_union(from->bmap,cset);
60
    gettimeofday(&from->bmap_timestamp, NULL);
61
62
    //decide what to accept
63 b5a5780a Csaba Kiraly
    cset_acc = get_chunks_to_accept(from, cset, max_deliver, trans_id);
64 2067b4af Csaba Kiraly
65
    //send accept message
66 652fb405 Csaba Kiraly
    dprintf("\t accept %d chunks from peer %s, trans_id %d\n", chunkID_set_size(cset_acc), node_addr(from->id), trans_id);
67 3e3bb788 CsabaKiraly
    acceptChunks(fromid, cset_acc, trans_id);
68 2067b4af Csaba Kiraly
69 ddedf85f Csaba Kiraly
    chunkID_set_free(cset_acc);
70 2067b4af Csaba Kiraly
  }
71
}
72
73
void accept_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, int trans_id) {
74
  struct peer *from = nodeid_to_peer(fromid,0);   //verify that we have really offered, 0 at least garantees that we've known the peer before
75
  dprintf("The peer %s accepted our offer for %d chunks, max deliver %d.\n", node_addr(fromid), chunkID_set_size(cset), max_deliver);
76
77
  if (from) {
78 b5a5780a Csaba Kiraly
    send_accepted_chunks(from, cset, max_deliver, trans_id);
79 2067b4af Csaba Kiraly
  }
80
}
81
82
83 fa655fab Csaba Kiraly
 /**
84
 * Dispatcher for signaling messages.
85
 *
86
 * This method decodes the signaling messages, retrieving the set of chunk and the signaling
87
 * message, invoking the corresponding method.
88
 *
89
 * @param[in] buff buffer which contains the signaling message
90
 * @param[in] buff_len length of the buffer
91
 * @return 0 on success, <0 on error
92
 */
93
94 1bb397ce Csaba Kiraly
int sigParseData(const struct nodeID *fromid, uint8_t *buff, int buff_len) {
95 fa655fab Csaba Kiraly
    struct chunkID_set *c_set;
96 7f5321cb CsabaKiraly
    struct nodeID *ownerid;
97
    enum signaling_type sig_type;
98
    int max_deliver = 0, trans_id = 0;
99 0c3f7376 Csaba Kiraly
    int ret = 1;
100 13d85fc6 Csaba Kiraly
    dprintf("Decoding signaling message...");
101 7f5321cb CsabaKiraly
102
    ret = parseSignaling(buff + 1, buff_len-1, &ownerid, &c_set, &max_deliver, &trans_id, &sig_type);
103
    if (ret < 0) {
104
      fprintf(stdout, "ERROR parsing signaling message\n");
105 c1f8de1c Csaba Kiraly
      return -1;
106
    }
107 7f5321cb CsabaKiraly
    switch (sig_type) {
108
        case sig_send_buffermap:
109 fdac07e1 CsabaKiraly
          bmap_received(fromid, ownerid, c_set, max_deliver, trans_id); //FIXME: cb_size has gone from signaling
110 b7374def Csaba Kiraly
          break;
111 7f5321cb CsabaKiraly
        case sig_offer:
112
          offer_received(fromid, c_set, max_deliver, trans_id);
113 2067b4af Csaba Kiraly
          break;
114 7f5321cb CsabaKiraly
        case sig_accept:
115
          accept_received(fromid, c_set, chunkID_set_size(c_set), trans_id);
116 2067b4af Csaba Kiraly
          break;
117 fa655fab Csaba Kiraly
        default:
118 0c3f7376 Csaba Kiraly
          ret = -1;
119 fa655fab Csaba Kiraly
    }
120 c8c4c779 Csaba Kiraly
    chunkID_set_free(c_set);
121 0c3f7376 Csaba Kiraly
    return ret;
122 fa655fab Csaba Kiraly
}