Revision 7f5321cb

View differences:

chunk_signaling.c
24 24
#include "chunk_signaling.h"
25 25
#include "msg_types.h"
26 26
#include "net_helper.h"
27
#include <trade_sig_ha.h>
27 28

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

  
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 33
void bmap_received(const struct nodeID *fromid, const struct nodeID *ownerid, struct chunkID_set *c_set, int cb_size, int trans_id) {
114 34
  struct peer *owner;
115 35
  if (nodeid_equal(fromid, ownerid)) {
......
171 91
 *
172 92
 * @param[in] buff buffer which contains the signaling message
173 93
 * @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 94
 * @return 0 on success, <0 on error
178 95
 */
179 96

  
180 97
int sigParseData(const struct nodeID *fromid, uint8_t *buff, int buff_len) {
181 98
    struct chunkID_set *c_set;
182
    void *meta;
183
    int meta_len;
184
    struct sig_nal *signal;
185
    int sig;
99
    struct nodeID *ownerid;
100
    enum signaling_type sig_type;
101
    int max_deliver = 0, trans_id = 0;
186 102
    int ret = 1;
187 103
    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");
104

  
105
    ret = parseSignaling(buff + 1, buff_len-1, &ownerid, &c_set, &max_deliver, &trans_id, &sig_type);
106
    if (ret < 0) {
107
      fprintf(stdout, "ERROR parsing signaling message\n");
192 108
      return -1;
193 109
    }
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);
110
    switch (sig_type) {
111
        case sig_send_buffermap:
112
          bmap_received(fromid, ownerid, c_set, trans_id); //FIXME: cb_size has gone from signaling
205 113
          break;
206
        }
207
        case MSG_SIG_OFF:
208
          offer_received(fromid, c_set, signal->max_deliver, signal->trans_id);
114
        case sig_offer:
115
          offer_received(fromid, c_set, max_deliver, trans_id);
209 116
          break;
210
        case MSG_SIG_ACC:
211
          accept_received(fromid, c_set, signal->max_deliver, signal->trans_id);
117
        case sig_accept:
118
          accept_received(fromid, c_set, chunkID_set_size(c_set), trans_id);
212 119
          break;
213 120
        default:
214 121
          ret = -1;
215 122
    }
216
    
217 123
    chunkID_set_free(c_set);
218
    free(meta);
219 124
    return ret;
220 125
}
221

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

  
226
int sigInit(struct nodeID *myID)
227
{
228
  localID = myID;
229
  return 1;
230
}
chunk_signaling.h
7 7
#ifndef CHUNK_SIGNALING_H
8 8
#define CHUNK_SIGNALING_H
9 9

  
10
#include "chunkidset.h"
11

  
12
//Type of signaling message
13
//Request a ChunkIDSet
14
#define MSG_SIG_REQ 0
15
//Diliver a ChunkIDSet (reply to a request)
16
#define MSG_SIG_DEL 1
17
//Offer a ChunkIDSet
18
#define MSG_SIG_OFF 2
19
//Accept a ChunkIDSet (reply to an offer)
20
#define MSG_SIG_ACC 3
21
//Receive the BufferMap
22
#define MSG_SIG_BMOFF 4
23
//Request the BufferMap
24
#define MSG_SIG_BMREQ 5
25

  
26

  
27
struct sig_nal {
28
    uint8_t type;//type of signal.
29
    uint8_t max_deliver;//Max number of chunks to deliver.
30
    uint16_t cb_size;
31
    uint16_t trans_id;//future use...
32
    uint8_t third_peer;//for buffer map exchange from other peers, just the first byte!
33
} __attribute__((packed));
34

  
35

  
36 10
int sigParseData(const struct nodeID *from_id, uint8_t *buff, int buff_len);
37 11

  
38
//int sendBufferMap(const struct nodeID *to_id, const struct nodeID *owner_id, ChunkIDSet *bmap, int cb_size, int trans_id);
39

  
40
//int sendMyBufferMap(const struct nodeID *to_id, ChunkIDSet *bmap, int cb_size, int trans_id);
41

  
42
//int offerChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id);
43

  
44
//int acceptChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id);
45

  
46
/**
47
  * Init the chunk signaling stuff...
48
  *
49
  *
50
  * @param myID address of this peer
51
  * @return >= 0 on success, <0 on error
52
  */
53
int sigInit(struct nodeID *myID);
54

  
55 12
#endif

Also available in: Unified diff