Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ 7f5321cb

History | View | Annotate | Download (4.35 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 "msg_types.h"
26
#include "net_helper.h"
27
#include <trade_sig_ha.h>
28

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

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

    
51
void offer_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, int trans_id) {
52
  struct peer *from = nodeid_to_peer(fromid,1);
53
  dprintf("The peer %s offers %d chunks, max deliver %d.\n", node_addr(fromid), chunkID_set_size(cset), max_deliver);
54

    
55
  if (from) {
56
    struct chunkID_set *cset_acc;
57
    int max_deliver2;
58

    
59
    //register these chunks in the buffermap. Warning: this should be changed when offers become selective.
60
    chunkID_set_clear(from->bmap,0);        //TODO: some better solution might be needed to keep info about chunks we sent in flight.
61
    chunkID_set_union(from->bmap,cset);
62
    gettimeofday(&from->bmap_timestamp, NULL);
63

    
64
    //decide what to accept
65
    cset_acc = get_chunks_to_accept(from, cset, max_deliver, trans_id);
66

    
67
    //send accept message
68
    dprintf("\t accept %d chunks from peer %s, trans_id %d\n", chunkID_set_size(cset_acc), node_addr(from->id), trans_id);
69
    max_deliver2 = chunkID_set_size(cset_acc);
70
    acceptChunks(fromid, cset_acc, max_deliver2, trans_id);
71

    
72
    chunkID_set_free(cset_acc);
73
  }
74
}
75

    
76
void accept_received(const struct nodeID *fromid, struct chunkID_set *cset, int max_deliver, int trans_id) {
77
  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
78
  dprintf("The peer %s accepted our offer for %d chunks, max deliver %d.\n", node_addr(fromid), chunkID_set_size(cset), max_deliver);
79

    
80
  if (from) {
81
    send_accepted_chunks(from, cset, max_deliver, trans_id);
82
  }
83
}
84

    
85

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

    
97
int sigParseData(const struct nodeID *fromid, uint8_t *buff, int buff_len) {
98
    struct chunkID_set *c_set;
99
    struct nodeID *ownerid;
100
    enum signaling_type sig_type;
101
    int max_deliver = 0, trans_id = 0;
102
    int ret = 1;
103
    dprintf("Decoding signaling message...");
104

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