Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ 0ebdcf82

History | View | Annotate | Download (4.94 KB)

1
/*
2
 *  Copyright (c) 2009 Alessandro Russo
3
 *  Copyright (c) 2009 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 *
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
#include <stdint.h>
14
#include <stdlib.h>
15
#include <stdio.h>
16
#include <sys/time.h>
17
#include <errno.h>
18
#include <assert.h>
19
#include <string.h>
20
#include "peer.h"
21
#include "peerset.h"
22
#include "chunkidset.h"
23
#include "trade_sig_la.h"
24
#include "chunk_signaling.h"
25
#include "net_helper.h"
26
#include <trade_sig_ha.h>
27

    
28
#include "streaming.h"
29
#include "topology.h"
30
#include "dbg.h"
31

    
32
static bool neigh_on_sign_recv = false;
33

    
34
void ack_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, uint16_t trans_id) {
35
  struct peer *from = nodeid_to_peer(fromid,0);   //verify that we have really sent, 0 at least garantees that we've known the peer before
36
  dprintf("The peer %s acked our chunk %d chunks, max deliver %d, trans_id %d.\n", node_addr(fromid), chunkID_set_get_latest(cset), max_deliver, trans_id);
37
}
38

    
39
void bmap_received(const struct nodeID *fromid, const struct nodeID *ownerid, struct chunkID_set *c_set, int cb_size, uint16_t trans_id) {
40
  struct peer *owner;
41
  if (nodeid_equal(fromid, ownerid)) {
42
    owner = nodeid_to_peer(ownerid, neigh_on_sign_recv);
43
  } else {
44
    dprintf("%s might be behind ",node_addr(ownerid));
45
    dprintf("NAT:%s\n",node_addr(fromid));
46
    owner = nodeid_to_peer(fromid, neigh_on_sign_recv);
47
  }
48
  
49
  if (owner) {        //now we have it almost sure
50
    chunkID_set_clear(owner->bmap,cb_size+5);        //TODO: some better solution might be needed to keep info about chunks we sent in flight.
51
    chunkID_set_union(owner->bmap,c_set);
52
    owner->cb_size = cb_size;
53
    gettimeofday(&owner->bmap_timestamp, NULL);
54
  }
55
}
56

    
57
void offer_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, uint16_t trans_id) {
58
  struct chunkID_set *cset_acc;
59

    
60
  struct peer *from = nodeid_to_peer(fromid, neigh_on_sign_recv);
61
  dprintf("The peer %s offers %d chunks, max deliver %d.\n", node_addr(fromid), chunkID_set_size(cset), max_deliver);
62

    
63
  if (from) {
64
    //register these chunks in the buffermap. Warning: this should be changed when offers become selective.
65
    chunkID_set_clear(from->bmap,0);        //TODO: some better solution might be needed to keep info about chunks we sent in flight.
66
    chunkID_set_union(from->bmap,cset);
67
    gettimeofday(&from->bmap_timestamp, NULL);
68
  }
69

    
70
    //decide what to accept
71
    cset_acc = get_chunks_to_accept(fromid, cset, max_deliver, trans_id);
72

    
73
    //send accept message
74
    dprintf("\t accept %d chunks from peer %s, trans_id %d\n", chunkID_set_size(cset_acc), node_addr(fromid), trans_id);
75
    acceptChunks(fromid, cset_acc, trans_id);
76

    
77
    chunkID_set_free(cset_acc);
78
}
79

    
80
void accept_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, uint16_t trans_id) {
81
  //TODO: verify if such a verification is needed
82
  //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
83

    
84
  dprintf("The peer %s accepted our offer for %d chunks, max deliver %d.\n", node_addr(fromid), chunkID_set_size(cset), max_deliver);
85

    
86
  send_accepted_chunks(fromid, cset, max_deliver, trans_id);
87
}
88

    
89

    
90
 /**
91
 * Dispatcher for signaling messages.
92
 *
93
 * This method decodes the signaling messages, retrieving the set of chunk and the signaling
94
 * message, invoking the corresponding method.
95
 *
96
 * @param[in] buff buffer which contains the signaling message
97
 * @param[in] buff_len length of the buffer
98
 * @return 0 on success, <0 on error
99
 */
100

    
101
int sigParseData(const struct nodeID *fromid, uint8_t *buff, int buff_len) {
102
    struct chunkID_set *c_set;
103
    struct nodeID *ownerid;
104
    enum signaling_type sig_type;
105
    int max_deliver = 0;
106
    uint16_t trans_id = 0;
107
    int ret = 1;
108
    dprintf("Decoding signaling message...\n");
109

    
110
    ret = parseSignaling(buff + 1, buff_len-1, &ownerid, &c_set, &max_deliver, &trans_id, &sig_type);
111
    if (ret < 0) {
112
      fprintf(stdout, "ERROR parsing signaling message\n");
113
      return -1;
114
    }
115
    switch (sig_type) {
116
        case sig_send_buffermap:
117
          bmap_received(fromid, ownerid, c_set, max_deliver, trans_id); //FIXME: cb_size has gone from signaling
118
          break;
119
        case sig_offer:
120
          offer_received(fromid, c_set, max_deliver, trans_id);
121
          break;
122
        case sig_accept:
123
          accept_received(fromid, c_set, chunkID_set_size(c_set), trans_id);
124
          break;
125
            case sig_ack:
126
              ack_received(fromid, c_set, chunkID_set_size(c_set), trans_id);
127
          break;
128
        default:
129
          ret = -1;
130
    }
131
    chunkID_set_free(c_set);
132
    nodeid_free(ownerid);
133
    return ret;
134
}