Revision d0f6aada som/ChunkTrading/chunk_signalling.c

View differences:

som/ChunkTrading/chunk_signalling.c
21 21
#include "trade_sig_la.h"
22 22
#include "trade_sig_ha.h"
23 23

  
24
//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

  
24 46
//set the local node ID
25 47
static struct nodeID *localID;
26 48

  
......
31 53
    return 1;
32 54
}
33 55

  
56
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

  
34 96
/**
35 97
 * Request a (sub)set of chunks from a Peer.
36 98
 *
......
123 185
 * @param[in] trans_id transaction number associated with this request
124 186
 * @return 1 on success, <0 on error
125 187
 */
188

  
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
}
126 193
/*
127 194
int offerChunks(const struct nodeID *to, ChunkIDSet *cset, int cset_len, int max_deliver, int trans_id) {
128 195
    int buff_len;

Also available in: Unified diff