Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ d029edd1

History | View | Annotate | Download (7.34 KB)

1
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see GPL.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

    
14
#include <stdint.h>
15
#include <stdlib.h>
16
#include <stdio.h>
17
#include <sys/time.h>
18
#include <errno.h>
19
#include <assert.h>
20
#include <string.h>
21
#include "peer.h"
22
#include "peerset.h"
23
#include "chunkidset.h"
24
#include "trade_sig_la.h"
25
#include "chunk_signaling.h"
26
#include "msg_types.h"
27
#include "net_helper.h"
28

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

    
33
static struct nodeID *localID;
34

    
35
int sendSignalling(int type, const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *cset, int max_deliver, int cb_size, int trans_id)
36
{
37
    int buff_len, meta_len, msg_len, ret;
38
    uint8_t *buff;
39
    struct sig_nal *sigmex;
40
    uint8_t *meta;
41

    
42
    meta = malloc(1024);
43

    
44
    sigmex = (struct sig_nal*) meta;
45
    sigmex->type = type;
46
    sigmex->max_deliver = max_deliver;
47
    sigmex->cb_size = cb_size;
48
    sigmex->trans_id = trans_id;
49
    meta_len = sizeof(*sigmex)-1;
50
      sigmex->third_peer = 0;
51
    if (owner_id) {
52
      meta_len += nodeid_dump(&sigmex->third_peer, owner_id);
53
    }
54

    
55
    buff_len = 1 + chunkID_set_size(cset) * 4 + 16 + meta_len; // this should be enough
56
    buff = malloc(buff_len);
57
    if (!buff) {
58
      fprintf(stderr, "Error allocating buffer\n");
59
      return -1;
60
    }
61

    
62
    buff[0] = MSG_TYPE_SIGNALLING;
63
    msg_len = 1 + encodeChunkSignaling(cset, meta, meta_len, buff+1, buff_len-1);
64
    free(meta);
65
    if (msg_len <= 0) {
66
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
67
      ret = -1;
68
    } else {
69
      send_to_peer(localID, to_id, buff, msg_len);
70
    }
71
    ret = 1;
72
    free(buff);
73
    return ret;
74
}
75

    
76
/**
77
 * Send a BufferMap to a Peer.
78
 *
79
 * Send (our own or some other peer's) BufferMap to a third Peer.
80
 *
81
 * @param[in] to PeerID.
82
 * @param[in] owner Owner of the BufferMap to send.
83
 * @param[in] bmap the BufferMap to send.
84
 * @param[in] trans_id transaction number associated with this send
85
 * @return 0 on success, <0 on error
86
 */
87
int sendBufferMap(const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *bmap, int cb_size, int trans_id) {
88
  return sendSignalling(MSG_SIG_BMOFF, to_id, owner_id, bmap, 0, cb_size, trans_id);
89
}
90

    
91
int sendMyBufferMap(const struct nodeID *to_id, struct chunkID_set *bmap, int cb_size, int trans_id)
92
{
93
  return sendBufferMap(to_id, localID, bmap, cb_size, trans_id);
94
}
95

    
96
/*
97
int offerChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id) {
98
  return sendSignalling(MSG_SIG_OFF, to_id, NULL, cset, max_deliver, -1, trans_id);
99
}
100
*/
101
/*
102
int acceptChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id) {
103
  return sendSignalling(MSG_SIG_ACC, to_id, NULL, cset, max_deliver, -1, trans_id);
104
}
105
*/
106

    
107
/// ==================== ///
108
///        RECEIVE       ///
109
/// ==================== ///
110

    
111
void bmap_received(const struct nodeID *fromid, const struct nodeID *ownerid, struct chunkID_set *c_set, int cb_size, int trans_id) {
112
  struct peer *owner;
113
  if (nodeid_equal(fromid, ownerid)) {
114
    owner = nodeid_to_peer(ownerid,1);
115
  } else {
116
    dprintf("%s might be behind ",node_addr(ownerid));
117
    dprintf("NAT:%s\n",node_addr(fromid));
118
    owner = nodeid_to_peer(fromid,1);
119
  }
120
  
121
  if (owner) {        //now we have it almost sure
122
    chunkID_set_clear(owner->bmap,cb_size+5);        //TODO: some better solution might be needed to keep info about chunks we sent in flight.
123
    chunkID_set_union(owner->bmap,c_set);
124
    owner->cb_size = cb_size;
125
    gettimeofday(&owner->bmap_timestamp, NULL);
126
  }
127
}
128

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

    
133
  if (from) {
134
    struct chunkID_set *cset_acc;
135
    int max_deliver2;
136

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

    
142
    //decide what to accept
143
    cset_acc = get_chunks_to_accept(from, cset, max_deliver, trans_id);
144

    
145
    //send accept message
146
    dprintf("\t accept %d chunks from peer %s, trans_id %d\n", chunkID_set_size(cset_acc), node_addr(from->id), trans_id);
147
    max_deliver2 = chunkID_set_size(cset_acc);
148
    acceptChunks(fromid, cset_acc, max_deliver2, trans_id);
149

    
150
    chunkID_set_free(cset_acc);
151
  }
152
}
153

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

    
158
  if (from) {
159
    send_accepted_chunks(from, cset, max_deliver, trans_id);
160
  }
161
}
162

    
163

    
164
 /**
165
 * Dispatcher for signaling messages.
166
 *
167
 * This method decodes the signaling messages, retrieving the set of chunk and the signaling
168
 * message, invoking the corresponding method.
169
 *
170
 * @param[in] buff buffer which contains the signaling message
171
 * @param[in] buff_len length of the buffer
172
 * @param[in] msgtype type of message in the buffer
173
 * @param[in] max_deliver deliver at most this number of Chunks
174
 * @param[in] arg parameters associated to the signaling message
175
 * @return 0 on success, <0 on error
176
 */
177

    
178
int sigParseData(const struct nodeID *fromid, uint8_t *buff, int buff_len) {
179
    struct chunkID_set *c_set;
180
    void *meta;
181
    int meta_len;
182
    struct sig_nal *signal;
183
    int sig;
184
    int ret = 1;
185
    dprintf("Decoding signaling message...");
186
    c_set = decodeChunkSignaling(&meta, &meta_len, buff+1, buff_len-1);
187
    dprintf(" SIG_HEADER: len: %d, of which meta: %d\n", buff_len, meta_len);
188
    if (!c_set) {
189
      fprintf(stdout, "ERROR decoding signaling message\n");
190
      return -1;
191
    }
192
    signal = (struct sig_nal *) meta;
193
    sig = (int) (signal->type);
194
    dprintf("\tSignaling Type %d\n", sig);
195
    //MaxDelivery  and Trans_Id to be defined
196
    switch (sig) {
197
        case MSG_SIG_BMOFF:
198
        {
199
          int dummy;
200
          struct nodeID *ownerid = nodeid_undump(&(signal->third_peer),&dummy);
201
          bmap_received(fromid, ownerid, c_set, signal->cb_size, signal->trans_id);
202
          nodeid_free(ownerid);
203
          break;
204
        }
205
        case MSG_SIG_OFF:
206
          offer_received(fromid, c_set, signal->max_deliver, signal->trans_id);
207
          break;
208
        case MSG_SIG_ACC:
209
          accept_received(fromid, c_set, signal->max_deliver, signal->trans_id);
210
          break;
211
        default:
212
          ret = -1;
213
    }
214
    
215
    chunkID_set_free(c_set);
216
    free(meta);
217
    return ret;
218
}
219

    
220
/// ==================== ///
221
///          INIT        ///
222
/// ==================== ///
223

    
224
int sigInit(struct nodeID *myID)
225
{
226
  localID = myID;
227
  return 1;
228
}