Statistics
| Branch: | Revision:

grapes / som / ChunkTrading / chunk_signalling.c @ d0f6aada

History | View | Annotate | Download (12.4 KB)

1 f3d61dbf Alessandro Russo
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see lgpl-2.1.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 <stdio.h>
15
#include <stdint.h>
16
#include <string.h>
17
#include <stdlib.h>
18
#include "chunk.h"
19
#include "chunkidset.h"
20
#include "msg_types.h"
21
#include "trade_sig_la.h"
22
#include "trade_sig_ha.h"
23
24 d0f6aada Alessandro Russo
//Type of signaling message
25
//Request a ChunkIDSet
26
#define MSG_SIG_REQ 0
27
//Diliver a ChunkIDSet (reply to a request)
28
#define MSG_SIG_DEL 1
29
//Offer a ChunkIDSet
30
#define MSG_SIG_OFF 2
31
//Accept a ChunkIDSet (reply to an offer)
32
#define MSG_SIG_ACC 3
33
//Receive the BufferMap
34
#define MSG_SIG_BMOFF 4
35
//Request the BufferMap
36
#define MSG_SIG_BMREQ 5
37
38
struct sig_nal {
39
    uint8_t type;//type of signal.
40
    uint8_t max_deliver;//Max number of chunks to deliver.
41
    uint16_t cb_size;
42
    uint16_t trans_id;//future use...
43
    uint8_t third_peer;//for buffer map exchange from other peers, just the first byte!
44
} __attribute__((packed));
45
46 f3d61dbf Alessandro Russo
//set the local node ID
47
static struct nodeID *localID;
48
49
int chunkSignalingInit(struct nodeID *myID) {
50
    if(!myID)
51
        return -1;
52
    localID = myID;
53
    return 1;
54
}
55
56 d0f6aada Alessandro Russo
int sendSignallingBIS(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)
57
{
58
    int buff_len, meta_len, msg_len, ret;
59
    uint8_t *buff;
60
    struct sig_nal *sigmex;
61
    uint8_t *meta;
62
    ret = 1;
63
    meta = malloc(1024);
64
65
    sigmex = (struct sig_nal*) meta;
66
    sigmex->type = type;
67
    sigmex->max_deliver = max_deliver;
68
    sigmex->cb_size = cb_size;
69
    sigmex->trans_id = trans_id;
70
    meta_len = sizeof(*sigmex)-1;
71
      sigmex->third_peer = 0;
72
    if (owner_id) {
73
      meta_len += nodeid_dump(&sigmex->third_peer, owner_id);
74
    }
75
76
    buff_len = 1 + chunkID_set_size(cset) * 4 + 16 + meta_len; // this should be enough
77
    buff = malloc(buff_len);
78
    if (!buff) {
79
      fprintf(stderr, "Error allocating buffer\n");
80
      return -1;
81
    }
82
83
    buff[0] = MSG_TYPE_SIGNALLING;
84
    msg_len = 1 + encodeChunkSignaling(cset, meta, meta_len, buff+1, buff_len-1);
85
    free(meta);
86
    if (msg_len <= 0) {
87
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
88
      ret = -1;
89
    } else {
90
      send_to_peer(localID, to_id, buff, msg_len);
91
    }    
92
    free(buff);
93
    return ret;
94
}
95
96 f3d61dbf Alessandro Russo
/**
97
 * Request a (sub)set of chunks from a Peer.
98
 *
99
 * Initiate a request for a set of Chunks towards a Peer, and specify the  maximum number of Chunks attempted to deliver
100
 * (attempted to deliver: i.e. the destination peer would like to receive at most this number of Chunks from the set)
101
 *
102
 * @param[in] to target peer to request the ChunkIDs from
103
 * @param[in] cset array of ChunkIDs
104
 * @param[in] cset_len length of the ChunkID set
105
 * @param[in] max_deliver deliver at most this number of Chunks
106
 * @param[in] trans_id transaction number associated with this request
107
 * @return 1 on success, 0 no chunks, <0 on error,
108
 *//*
109
int requestChunks(const struct nodeID *to, const ChunkIDSet *cset, int cset_len, int max_deliver, int trans_id) {
110
    int buff_len;
111
    int res;
112
    uint8_t *buff;
113
    struct sig_nal *sigmex;
114
    sigmex = malloc(sizeof(struct sig_nal));
115
    sigmex->type = MSG_SIG_REQ;
116
    sigmex->max_deliver = max_deliver;
117
    sigmex->trans_id = trans_id;
118
    fprintf(stdout, "SIG_HEADER: Type %d Tx %d PP %d\n", sigmex->type, sigmex->trans_id, sigmex->third_peer);
119
    buff_len = cset_len * 4 + 12 + sizeof(struct sig_nal); // Signaling type + set len
120
    buff = malloc(buff_len + 1);    
121
    res = encodeChunkSignaling(cset, sigmex, sizeof(struct sig_nal), buff+1, buff_len);
122
    if (res < 0) {
123
        fprintf(stderr, "Error in encoding chunk set for chunks request\n");
124
        return -1;
125
    } else {
126
        buff[0] = MSG_TYPE_SIGNALLING; //the message type is CHUNK
127
        res = send_to_peer(localID, to, buff, buff_len + 1); //actual send chunk
128
        free(buff); //free memory allocated for the buffer
129
        free(sigmex);
130
    }
131
    return (1);
132
}
133

134
*/
135
/**
136
 * Deliver a set of Chunks to a Peer as a reply of its previous request of Chunks
137
 *
138
 * Announce to the Peer which sent a request of a set of Chunks, that we have these chunks (sub)set available
139
 * among all those requested and willing to deliver them.
140
 *
141
 * @param[in] to target peer to which deliver the ChunkIDs
142
 * @param[in] cset array of ChunkIDs
143
 * @param[in] cset_len length of the ChunkID set
144
 * @param[in] max_deliver we are able to deliver at most this many from the set
145
 * @param[in] trans_id transaction number associated with this request
146
 * @return 1 on success, <0 on error
147
 *//*
148

149
int deliverChunks(const struct nodeID *to, ChunkIDSet *cset, int cset_len, int max_deliver, int trans_id) {
150
    int buff_len;
151
    int res;
152
    uint8_t *buff;
153
    struct sig_nal *sigmex;
154
    sigmex = malloc(sizeof(struct sig_nal));
155
    sigmex->type = MSG_SIG_DEL;
156
    sigmex->max_deliver = max_deliver;
157
    sigmex->trans_id = trans_id;
158
    fprintf(stdout, "SIG_HEADER: Type %d Tx %d PP %d\n", sigmex->type, sigmex->trans_id, sigmex->third_peer);
159
    buff_len = cset_len * 4 + 12 + sizeof(struct sig_nal); // Signaling type + set len
160
    buff = malloc(buff_len + 1);
161
    res = encodeChunkSignaling(cset, sigmex, sizeof(struct sig_nal), buff+1, buff_len);
162
    if (res < 0) {
163
        fprintf(stderr, "Error in encoding chunk set for chunks request\n");
164
        return -1;
165
    } else {
166
        buff[0] = MSG_TYPE_SIGNALLING; //the message type is CHUNK
167
        res = send_to_peer(localID, to, buff, buff_len + 1); //actual send chunk
168
        free(buff); //free memory allocated for the buffer
169
        free(sigmex);
170
    }
171
    return 1;
172
}
173
*/
174
175
/**
176
 * Offer a (sub)set of chunks to a Peer.
177
 *
178
 * Initiate a offer for a set of Chunks towards a Peer, and specify the  maximum number of Chunks attempted to deliver
179
 * (attempted to deliver: i.e. the sender peer should try to send at most this number of Chunks from the set)
180
 *
181
 * @param[in] to target peer to offer the ChunkIDs
182
 * @param[in] cset array of ChunkIDs
183
 * @param[in] cset_len length of the ChunkID set
184
 * @param[in] max_deliver deliver at most this number of Chunks
185
 * @param[in] trans_id transaction number associated with this request
186
 * @return 1 on success, <0 on error
187
 */
188 d0f6aada Alessandro Russo
189
int offerChunks(const struct nodeID *to, struct chunkID_set *cset, int max_deliver, int trans_id)
190
{
191
  return sendSignallingBIS(MSG_SIG_OFF, to, NULL, cset, max_deliver, -1, trans_id);
192
}
193 f3d61dbf Alessandro Russo
/*
194
int offerChunks(const struct nodeID *to, ChunkIDSet *cset, int cset_len, int max_deliver, int trans_id) {
195
    int buff_len;
196
    int res;
197
    uint8_t *buff;
198
    struct sig_nal *sigmex;
199
    sigmex = malloc(sizeof(struct sig_nal));
200
    sigmex->type = MSG_SIG_OFF;
201
    sigmex->max_deliver = max_deliver;
202
    sigmex->trans_id = trans_id;
203
    fprintf(stdout, "SIG_HEADER: Type %d Tx %d PP %d\n", sigmex->type, sigmex->trans_id, sigmex->third_peer);
204
    buff_len = cset_len * 4 + 12 + sizeof(struct sig_nal); // Signaling type + set len
205
    buff = malloc(buff_len + 1);
206
    res = encodeChunkSignaling(cset, sigmex, sizeof(struct sig_nal), buff+1, buff_len);
207
    if (res < 0) {
208
        fprintf(stderr, "Error in encoding chunk set for chunks request\n");
209
        return -1;
210
    } else {
211
        buff[0] = MSG_TYPE_SIGNALLING; //the message type is CHUNK
212
        res = send_to_peer(localID, to, buff, buff_len + 1); //actual send chunk
213
        free(buff); //free memory allocated for the buffer
214
        free(sigmex);
215
    }
216
    return 1;
217
}*/
218
219
220
/**
221
 * Accept a (sub)set of chunks from a Peer.
222
 *
223
 * Announce to accept a (sub)set of Chunks from a Peer which sent a offer before, and specify the  maximum number of Chunks attempted to receive
224
 * (attempted to receive: i.e. the receiver peer would like to receive at most this number of Chunks from the set offerd before)
225
 *
226
 * @param[in] to target peer to accept the ChunkIDs
227
 * @param[in] cset array of ChunkIDs
228
 * @param[in] cset_len length of the ChunkID set
229
 * @param[in] max_deliver accept at most this number of Chunks
230
 * @param[in] trans_id transaction number associated with this request
231
 * @return 1 on success, <0 on error
232
 */
233
/*
234
int acceptChunks(const struct nodeID *to, ChunkIDSet *cset, int cset_len, int max_deliver, int trans_id) {
235
    int buff_len;
236
    int res;
237
    uint8_t *buff;
238
    struct sig_nal *sigmex;
239
    sigmex = malloc(sizeof(struct sig_nal));
240
    sigmex->type = MSG_SIG_ACC;
241
    sigmex->max_deliver = max_deliver;
242
    sigmex->trans_id = trans_id;
243
    fprintf(stdout, "SIG_HEADER: Type %d Tx %d PP %d\n", sigmex->type, sigmex->trans_id, sigmex->third_peer);
244
    buff_len = cset_len * 4 + 12 + sizeof(struct sig_nal); // Signaling type + set len
245
    buff = malloc(buff_len + 1);
246
    res = encodeChunkSignaling(cset, sigmex, sizeof(struct sig_nal), buff+1, buff_len);
247
    if (res < 0) {
248
        fprintf(stderr, "Error in encoding chunk set for chunks request\n");
249
        return -1;
250
    } else {
251
        buff[0] = MSG_TYPE_SIGNALLING; //the message type is CHUNK
252
        res = send_to_peer(localID, to, buff, buff_len + 1); //actual send chunk
253
        free(buff); //free memory allocated for the buffer
254
        free(sigmex);
255
    }
256
    return 1;
257
}*/
258
259
/**
260
 * Send a BufferMap to a Peer.
261
 *
262
 * Send (our own or some other peer's) BufferMap to a third Peer.
263
 *
264
 * @param[in] to PeerID.
265
 * @param[in] owner Owner of the BufferMap to send.
266
 * @param[in] bmap the BufferMap to send.
267
 * @param[in] bmap_len length of the buffer map
268
 * @param[in] trans_id transaction number associated with this send
269
 * @return 1 on success, <0 on error
270
 *//*
271
int sendBufferMap(const struct nodeID *to, const struct nodeID *owner, ChunkIDSet *bmap, int bmap_len, int trans_id) {
272
    int buff_len;
273
    int res;
274
    uint8_t *buff;
275
    struct sig_nal *sigmex;
276
    sigmex = malloc(sizeof(struct sig_nal));
277
    sigmex->type = MSG_SIG_BMOFF;
278
    sigmex->trans_id = trans_id;
279

280
     //sigmex->third_peer->fd = owner->fd ;
281
    //sigmex->third_peer->refcnt = owner->refcnt ;
282
    //socklen_t raddr_size = sizeof(struct sockaddr_in);
283
    //memcpy(&sigmex->third_peer->addr,&owner->addr, raddr_size);
284

285
    fprintf(stdout, "SIG_HEADER: Type %d Tx %d\n", sigmex->type, sigmex->trans_id);
286
    buff_len = bmap_len * 4 + 12 + sizeof(struct sig_nal); // Signaling type + set len
287
    buff = malloc(buff_len + 1);
288
    res = encodeChunkSignaling(bmap, sigmex, sizeof(struct sig_nal), buff+1, buff_len);
289
    if (res < 0) {
290
        fprintf(stderr, "Error in encoding chunk set for chunks request\n");
291
        return -1;
292
    } else {
293
        buff[0] = MSG_TYPE_SIGNALLING; //the message type is CHUNK
294
        res = send_to_peer(localID, to, buff, buff_len + 1); //actual send chunk
295
        free(buff); //free memory allocated for the buffer
296
        free(sigmex);
297
    }
298
    return 1;
299
}
300
*/
301
302
/**
303
 * Request a BufferMap to a Peer.
304
 *
305
 * Request (target peer or some other peer's) BufferMap to target Peer.
306
 *
307
 * @param[in] to PeerID.
308
 * @param[in] owner Owner of the BufferMap to request.
309
 * @param[in] trans_id transaction number associated with this request
310
 * @return 1 on success, <0 on error
311
 */
312
//TODO Receive would require a buffer map from a given chunk, with a given length
313
/*
314
int requestBufferMap(const struct nodeID *to, const struct nodeID *owner, int trans_id) {
315
    int buff_len;
316
    int res;
317
    uint8_t *buff;
318
    struct sig_nal *sigmex;
319
    sigmex = malloc(sizeof(struct sig_nal));
320
    sigmex->type = MSG_SIG_BMREQ;
321
    sigmex->trans_id = trans_id;         
322
    //sigmex->third_peer->fd = owner->fd ;
323
    //sigmex->third_peer->refcnt = owner->refcnt ;
324
    //socklen_t raddr_size = sizeof(struct sockaddr_in);
325
    //memcpy(&sigmex->third_peer->addr,&owner->addr, raddr_size);
326
     
327
    fprintf(stdout, "SIG_HEADER: Type %d Tx %d\n", sigmex->type, sigmex->trans_id);
328
    buff_len = 12 + sizeof(struct sig_nal);
329
    buff = malloc(buff_len + 1);
330
    ChunkIDSet *bmap;
331
    bmap = chunkID_set_init(0);
332
    res = encodeChunkSignaling(bmap, sigmex, sizeof(struct sig_nal), buff, buff_len);
333
    if (res < 0) {
334
        fprintf(stderr, "Error in encoding chunk set for chunks request\n");
335
        return -1;
336
    } else {
337
        buff[0] = MSG_TYPE_SIGNALLING; //the message type is CHUNK
338
        res = send_to_peer(localID, to, buff, buff_len + 1); //actual send chunk
339
        free(buff); //free memory allocated for the buffer
340
        free(sigmex);
341
    }
342
    return 1;
343
}*/