Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ 913146ae

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

    
21
#include "peer.h"
22
#include "peerset.h"
23
#include "chunkidset.h"
24
#include "trade_sig_ha.h"
25
#include "trade_sig_la.h"
26
#include "msg_types.h"
27
#include "net_helper.h"
28

    
29
#include "chunk_signaling.h"
30

    
31
#include "streaming.h"
32
#include "topology.h"
33
#include "dbg.h"
34

    
35
static struct nodeID *localID;
36
/*
37

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

45
    meta = malloc(1024);
46

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

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

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

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

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

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

    
112
/// ==================== ///
113
///        RECEIVE       ///
114
/// ==================== ///
115

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

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

    
138
  if (from) {
139
    struct chunkID_set *cset_acc;
140
    int max_deliver2;
141

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

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

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

    
155
    chunkID_set_free(cset_acc);
156
  }
157
}
158

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

    
163
  if (from) {
164
    send_accepted_chunks(from, cset, max_deliver, trans_id);
165
  }
166
}
167

    
168

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

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

    
225
/// ==================== ///
226
///          INIT        ///
227
/// ==================== ///
228

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