Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ 74a5d4ae

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

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

    
32
static struct nodeID *localID;
33
/*
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
/*
88
int sendBufferMap(const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *bmap, int cb_size, int trans_id) {
89
  return sendSignalling(MSG_SIG_BMOFF, to_id, owner_id, bmap, 0, cb_size, trans_id);
90
}
91

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

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

    
109
/// ==================== ///
110
///        RECEIVE       ///
111
/// ==================== ///
112

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

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

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

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

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

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

    
152
    chunkID_set_free(cset_acc);
153
  }
154
}
155

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

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

    
165

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

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

    
222
/// ==================== ///
223
///          INIT        ///
224
/// ==================== ///
225

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