Revision 1942bd59

View differences:

chunk_signaling.c
46 46
  return p;
47 47
}
48 48

  
49
int sendSignalling(int type, const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *cset, int max_deliver, int trans_id)
50
{
51
    int buff_len, id_len, msg_len, ret;
52
    uint8_t *buff;
53
    struct sig_nal sigmex;
54
    sigmex.type = type;
55
    sigmex.max_deliver = max_deliver;
56
    sigmex.trans_id = trans_id;
57
    if (owner_id) {
58
      id_len = nodeid_dump(&sigmex.third_peer, owner_id);
59
    } else {
60
      id_len = 1;
61
    }
62
    buff_len = 1 + chunkID_set_size(cset) * 4 + 12 + sizeof(sigmex)-1 + id_len; // this should be enough
63
    buff = malloc(buff_len);
64
    if (!buff) {
65
      return -1;
66
    }
67
    buff[0] = MSG_TYPE_SIGNALLING;
68
    msg_len = 1 + encodeChunkSignaling(cset, &sigmex, sizeof(sigmex)-1 + id_len, buff+1, buff_len-1);
69
    if (msg_len < 0) {
70
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
71
      ret = -1;
72
    } else {
73
      send_to_peer(localID, to_id, buff, msg_len);
74
    }
75
    ret = 1;
76
    free(buff);
77
    return ret;
78
}
79

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

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

  
100
int offerChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id) {
101
  return sendSignalling(MSG_SIG_OFF, to_id, NULL, cset, max_deliver, trans_id);
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, trans_id);
106
}
107

  
108

  
109
/// ==================== ///
110
///        RECEIVE       ///
111
/// ==================== ///
112

  
49 113
void bmap_received(const struct nodeID *fromid, const struct nodeID *ownerid, struct chunkID_set *c_set, int trans_id) {
50 114
  struct peer *owner = nodeid_to_peer(ownerid,1);
51 115
  if (owner) {	//now we have it almost sure
......
100 164
    return 1;
101 165
}
102 166

  
103
/**
104
 * Send a BufferMap to a Peer.
105
 *
106
 * Send (our own or some other peer's) BufferMap to a third Peer.
107
 *
108
 * @param[in] to PeerID.
109
 * @param[in] owner Owner of the BufferMap to send.
110
 * @param[in] bmap the BufferMap to send.
111
 * @param[in] trans_id transaction number associated with this send
112
 * @return 0 on success, <0 on error
113
 */
114
int sendBufferMap(const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *bmap, int trans_id) {
115
    int buff_len, id_len, msg_len, ret;
116
    uint8_t *buff;
117
    //msgtype = MSG_TYPE_SIGNALLING;
118
    struct sig_nal sigmex;
119
    sigmex.type = MSG_SIG_BMOFF;
120
    sigmex.trans_id = trans_id;
121
    id_len = nodeid_dump(&sigmex.third_peer, owner_id);
122
    buff_len = 1 + chunkID_set_size(bmap) * 4 + 12 + sizeof(sigmex)-1 + id_len; // this should be enough
123
    dprintf("SIG_HEADER: Type %d Tx %d PP %s\n",sigmex.type,sigmex.trans_id,node_addr(owner_id));
124
    buff = malloc(buff_len);
125
    if (!buff) {
126
      return -1;
127
    }
128
    buff[0] = MSG_TYPE_SIGNALLING;
129
    msg_len = 1 + encodeChunkSignaling(bmap, &sigmex, sizeof(sigmex)-1 + id_len, buff+1, buff_len-1);
130
    dprintf("SIG_HEADER: len: %d, of which meta: %d\n", msg_len, sizeof(sigmex)-1 + id_len);
131
    if (msg_len < 0) {
132
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
133
      ret = -1;
134
    } else {
135
      send_to_peer(localID, to_id, buff, msg_len);
136
    }
137
    ret = 1;
138
    free(buff);
139
    return ret;
140
}
141

  
142
int sendMyBufferMap(const struct nodeID *to_id, struct chunkID_set *bmap, int trans_id)
143
{
144
  return sendBufferMap(to_id, localID, bmap, trans_id);
145
}
167
/// ==================== ///
168
///          INIT        ///
169
/// ==================== ///
146 170

  
147 171
int sigInit(struct nodeID *myID, struct peerset *ps)
148 172
{
chunk_signaling.h
32 32

  
33 33
int sendMyBufferMap(const struct nodeID *to_id, ChunkIDSet *bmap, int trans_id);
34 34

  
35
int offerChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id);
36

  
37
int acceptChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id);
38

  
35 39
/**
36 40
  * Init the chunk signaling stuff...
37 41
  *

Also available in: Unified diff