Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ b865a917

History | View | Annotate | Download (7.35 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

36
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)
37
{
38
    int buff_len, meta_len, msg_len, ret;
39
    uint8_t *buff;
40
    struct sig_nal *sigmex;
41
    uint8_t *meta;
42

43
    meta = malloc(1024);
44

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

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

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

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

93
int sendMyBufferMap(const struct nodeID *to_id, struct chunkID_set *bmap, int cb_size, int trans_id)
94
{
95
  return sendBufferMap(to_id, localID, bmap, cb_size, trans_id);
96
}
97
*/
98

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

    
110
/// ==================== ///
111
///        RECEIVE       ///
112
/// ==================== ///
113

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

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

    
136
  if (from) {
137
    struct chunkID_set *cset_acc;
138
    int max_deliver2;
139

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

    
145
    //decide what to accept
146
    cset_acc = get_chunks_to_accept(from, cset, max_deliver, trans_id);
147

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

    
153
    chunkID_set_free(cset_acc);
154
  }
155
}
156

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

    
161
  if (from) {
162
    send_accepted_chunks(from, cset, max_deliver, trans_id);
163
  }
164
}
165

    
166

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

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

    
223
/// ==================== ///
224
///          INIT        ///
225
/// ==================== ///
226

    
227
int sigInit(struct nodeID *myID)
228
{
229
  localID = myID;
230
  return 1;
231
}