Statistics
| Branch: | Revision:

grapes / som / ChunkTrading / chunk_signaling.c @ 2d4f54d6

History | View | Annotate | Download (9.06 KB)

1
/*
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 <arpa/inet.h>
19

    
20
#include "chunk.h"
21
#include "msg_types.h"
22
#include "trade_sig_la.h"
23
#include "trade_sig_ha.h"
24

    
25
//Type of signaling message
26
//Request a ChunkIDSet
27
#define MSG_SIG_REQ 1
28
//Diliver a ChunkIDSet (reply to a request)
29
#define MSG_SIG_DEL 2
30
//Offer a ChunkIDSet
31
#define MSG_SIG_OFF 4
32
//Accept a ChunkIDSet (reply to an offer)
33
#define MSG_SIG_ACC 6
34
//Receive the BufferMap
35
#define MSG_SIG_BMOFF 10
36
//Request the BufferMap
37
#define MSG_SIG_BMREQ 12
38

    
39
struct sig_nal {
40
    uint8_t type;//type of signal.
41
    uint8_t max_deliver;//Max number of chunks to deliver.
42
    //uint16_t cb_size;//size of the buffer map to send
43
    uint16_t trans_id;//future use...
44
    uint8_t third_peer;//for buffer map exchange from other peers, just the first byte!
45
} __attribute__((packed));
46

    
47
//set the local node ID
48
static struct nodeID *localID;
49

    
50

    
51
static inline void int_cpy(uint8_t *p, int v)
52
{
53
  int tmp;
54

    
55
  tmp = htonl(v);
56
  memcpy(p, &tmp, 4);
57
}
58

    
59
static inline int int_rcpy(const uint8_t *p)
60
{
61
  int tmp;
62

    
63
  memcpy(&tmp, p, 4);
64
  tmp = ntohl(tmp);
65

    
66
  return tmp;
67
}
68

    
69
/**
70
 * @brief Set current node identifier.
71
 *
72
 * Initialize the node identifier of the peer
73
 *
74
 * @param[in] current node indentifier.
75
 * @return 1 on success, <0 on error.
76
 */
77
int chunkSignalingInit(struct nodeID *myID) {
78
    if(!myID)
79
        return -1;
80
    localID = myID;
81
    return 1;
82
}
83

    
84
/**
85
 * @brief Parse an incoming signaling message, providing the signal type and the information of the signaling message.
86
 *
87
 * Parse an incoming signaling message provided in the buffer, giving the information of the message received.
88
 *
89
 * @param[in] buffer containing the incoming message.
90
 * @param[in] buff_len length of the buffer.
91
 * @param[out] owner_id identifier of the node on which refer the message just received.
92
 * @param[out] cset array of chunkIDs.
93
 * @param[out] max_deliver deliver at most this number of Chunks.
94
 * @param[out] trans_id transaction number associated with this message.
95
 * @param[out] sig_type Type of signaling message.
96
 * @return 1 on success, <0 on error.
97
 */
98
int parseSignaling(uint8_t *buff, int buff_len, const struct nodeID *owner_id, struct chunkID_set **cset,
99
        int *max_deliver, int *trans_id, int *sig_type) {
100
    struct sig_nal *signal;
101
    int ret, third_peer, meta_len;
102
    uint8_t *meta;
103
    third_peer = meta_len = 0;
104
    *cset = decodeChunkSignaling(&meta, &meta_len, buff, buff_len);
105
    if (meta_len) {
106
        signal = (struct sig_nal *) meta;
107
        switch (signal->type) {
108
            case MSG_SIG_OFF:
109
                *sig_type = sig_offer;
110
                break;
111
            case MSG_SIG_ACC:
112
                *sig_type = sig_accept;
113
                break;
114
            case MSG_SIG_REQ:
115
                *sig_type = sig_request;
116
                break;
117
            case MSG_SIG_DEL:
118
                *sig_type = sig_deliver;
119
                break;
120
            case MSG_SIG_BMOFF:
121
                *sig_type = sig_send_buffermap;
122
                break;
123
            case MSG_SIG_BMREQ:
124
                *sig_type = sig_request_buffermap;
125
                break;
126
            default:
127
                fprintf(stderr, "Error invalid signaling message\n");                
128
                return -1;
129
        }
130
        *max_deliver = signal->max_deliver;
131
        *trans_id = signal->trans_id;
132
        owner_id = (signal->third_peer ? nodeid_undump(&(signal->third_peer), &third_peer) : NULL);
133
        free(meta);
134
    }
135
    ret = (!*cset || !meta_len) ? -1 : 1;
136
    if (ret < 0) {
137
        fprintf(stderr, "Error parsing invalid signaling message\n");
138
    }
139
    return ret;
140
}
141

    
142

    
143
static int sendSignaling(int type, const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *cset, int max_deliver, int trans_id)
144
{
145
    int buff_len, meta_len, msg_len, ret;
146
    uint8_t *buff;
147
    struct sig_nal *sigmex;
148
    uint8_t *meta;
149
    ret = 1;
150
    meta = malloc(1024);
151
    if (!meta) {
152
        fprintf(stderr, "Error allocating meta-buffer\n");
153
        return -1;
154
    }
155
    sigmex = (struct sig_nal*) meta;
156
    sigmex->type = type;
157
    sigmex->max_deliver = max_deliver;    
158
    sigmex->trans_id = trans_id;
159
    sigmex->third_peer = 0;
160
    meta_len = sizeof(*sigmex) - 1;
161
    if (owner_id) {
162
        meta_len += nodeid_dump(&sigmex->third_peer, owner_id);
163
    }
164
    buff_len = 1 + (cset ? chunkID_set_size(cset):0) * 4 + 12 + meta_len; // this should be enough
165
    buff = malloc(buff_len);
166
    if (!buff) {
167
        fprintf(stderr, "Error allocating buffer\n");
168
        free(meta);
169
        return -1;
170
    }
171

    
172
    buff[0] = MSG_TYPE_SIGNALLING;
173
    msg_len = 1 + encodeChunkSignaling(cset, meta, meta_len, buff+1, buff_len-1);
174
    free(meta);
175
    if (msg_len <= 0) {
176
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
177
      ret = -1;
178
    } else {
179
      send_to_peer(localID, to_id, buff, msg_len);
180
    }    
181
    free(buff);
182
    return ret;
183
}
184

    
185
/**
186
 * @brief Request a set of chunks from a Peer.
187
 *
188
 * Request a set of Chunks towards a Peer, and specify the  maximum number of Chunks attempted to receive
189
 * (i.e., the number of chunks the destination peer would like to receive among those requested).
190
 *
191
 * @param[in] to target peer to request the ChunkIDs from.
192
 * @param[in] cset array of ChunkIDs.
193
 * @param[in] max_deliver deliver at most this number of Chunks.
194
 * @param[in] trans_id transaction number associated with this request.
195
 * @return 1 on success, <0 on error.
196
 */
197
int requestChunks(const struct nodeID *to, const ChunkIDSet *cset, int max_deliver, int trans_id) {
198
    return sendSignaling(MSG_SIG_REQ, to, NULL, cset, max_deliver, trans_id);
199
}
200

    
201

    
202
/**
203
 * @brief Deliver a set of Chunks to a Peer as a reply of its previous request of Chunks.
204
 *
205
 * Announce to the Peer which sent a request of a set of Chunks, that we have these chunks (sub)set available
206
 * among all those requested and willing to deliver them.
207
 *
208
 * @param[in] to target peer to which deliver the ChunkIDs.
209
 * @param[in] cset array of ChunkIDs.
210
 * @param[in] trans_id transaction number associated with this request.
211
 * @return 1 on success, <0 on error.
212
 */
213
int deliverChunks(const struct nodeID *to, ChunkIDSet *cset, int trans_id) {
214
    return sendSignaling(MSG_SIG_DEL, to, NULL, cset, 0, trans_id);
215
}
216

    
217
/**
218
 * @brief Offer a (sub)set of chunks to a Peer.
219
 *
220
 * Initiate a offer for a set of Chunks towards a Peer, and specify the  maximum number of Chunks attempted to deliver
221
 * (attempted to deliver: i.e., the sender peer should try to send at most this number of Chunks from the set).
222
 *
223
 * @param[in] to target peer to offer the ChunkIDs.
224
 * @param[in] cset array of ChunkIDs.
225
 * @param[in] max_deliver deliver at most this number of Chunks.
226
 * @param[in] trans_id transaction number associated with this request.
227
 * @return 1 on success, <0 on error.
228
 */
229
int offerChunks(const struct nodeID *to, struct chunkID_set *cset, int max_deliver, int trans_id){
230
  return sendSignaling(MSG_SIG_OFF, to, NULL, cset, max_deliver, trans_id);
231
}
232

    
233
/**
234
 * @brief Accept a (sub)set of chunks from a Peer.
235
 *
236
 * 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
237
 * (attempted to receive: i.e., the receiver peer would like to receive at most this number of Chunks from the set offered before).
238
 *
239
 * @param[in] to target peer to accept the ChunkIDs.
240
 * @param[in] cset array of ChunkIDs. 
241
 * @param[in] trans_id transaction number associated with this request.
242
 * @return 1 on success, <0 on error.
243
 */
244
int acceptChunks(const struct nodeID *to, struct chunkID_set *cset, int trans_id) {
245
  return sendSignaling(MSG_SIG_ACC, to, NULL, cset, 0, trans_id);
246
}
247

    
248
/**
249
 * @brief Send a BufferMap to a Peer.
250
 *
251
 * Send (our own or some other peer's) BufferMap to a third Peer.
252
 *
253
 * @param[in] to PeerID.
254
 * @param[in] owner Owner of the BufferMap to send.
255
 * @param[in] bmap the BufferMap to send.
256
 * @param[in] trans_id transaction number associated with this send.
257
 * @return 1 Success, <0 on error.
258
 */
259

    
260
int sendBufferMap(const struct nodeID *to, const struct nodeID *owner, struct chunkID_set *bmap, int trans_id) {
261
  return sendSignaling(MSG_SIG_BMOFF, to, (!owner?localID:owner), bmap, 0, trans_id);
262
}
263

    
264
/**
265
 * @brief Request a BufferMap to a Peer.
266
 *
267
 * Request (target peer or some other peer's) BufferMap to target Peer.
268
 *
269
 * @param[in] to PeerID.
270
 * @param[in] owner Owner of the BufferMap to request.
271
 * @param[in] trans_id transaction number associated with this request.
272
 * @return 1 Success, <0 on error.
273
 */
274

    
275
int requestBufferMap(const struct nodeID *to, const struct nodeID *owner, int trans_id) {
276
    return sendSignaling(MSG_SIG_BMREQ, to, (!owner?localID:owner), NULL, 0, trans_id);
277
}