Statistics
| Branch: | Revision:

streamers / chunk_signaling.c @ 0c3f7376

History | View | Annotate | Download (5.56 KB)

1
/*
2
 *  Copyright (c) 2009 Alessandro Russo.
3
 *
4
 *  This is free software;
5
 *  see GPL.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 <stdint.h>
15
#include <stdlib.h>
16
#include <stdio.h>
17
#include <sys/time.h>
18
#include <errno.h>
19
#include <assert.h>
20
#include <string.h>
21
#include "peer.h"
22
#include "peerset.h"
23
#include "chunkidset.h"
24
#include "trade_sig_la.h"
25
#include "chunk_signaling.h"
26
#include "msg_types.h"
27
#include "net_helper.h"
28

    
29
#include "dbg.h"
30

    
31
static struct nodeID *localID;
32
static struct peerset *pset;
33

    
34

    
35
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
36
{
37
  struct peer *p = peerset_get_peer(pset, id);
38
  if (!p) {
39
    fprintf(stderr,"warning: received message from unknown peer: %s!\n",node_addr(id));
40
    if (reg) {
41
      fprintf(stderr,"Adding %s to neighbourhood!\n", node_addr(id));
42
      peerset_add_peer(pset,id);
43
      p = peerset_get_peer(pset,id);
44
    }
45
  }
46

    
47
  return p;
48
}
49

    
50
int sendSignalling(int type, const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *cset, int max_deliver, int trans_id)
51
{
52
    int buff_len, meta_len, msg_len, ret;
53
    uint8_t *buff;
54
    struct sig_nal *sigmex;
55
    uint8_t *meta;
56

    
57
    meta = malloc(1024);
58

    
59
    sigmex = (struct sig_nal*) meta;
60
    sigmex->type = type;
61
    sigmex->max_deliver = max_deliver;
62
    sigmex->trans_id = trans_id;
63
    meta_len = sizeof(*sigmex)-1;
64
      sigmex->third_peer = 0;
65
    if (owner_id) {
66
      meta_len += nodeid_dump(&sigmex->third_peer, owner_id);
67
    }
68

    
69
    buff_len = 1 + chunkID_set_size(cset) * 4 + 12 + meta_len; // this should be enough
70
    buff = malloc(buff_len);
71
    if (!buff) {
72
      fprintf(stderr, "Error allocating buffer\n");
73
      return -1;
74
    }
75

    
76
    buff[0] = MSG_TYPE_SIGNALLING;
77
    msg_len = 1 + encodeChunkSignaling(cset, meta, meta_len, buff+1, buff_len-1);
78
    free(meta);
79
    if (msg_len < 0) {
80
      fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
81
      ret = -1;
82
    } else {
83
      send_to_peer(localID, to_id, buff, msg_len);
84
    }
85
    ret = 1;
86
    free(buff);
87
    return ret;
88
}
89

    
90
/**
91
 * Send a BufferMap to a Peer.
92
 *
93
 * Send (our own or some other peer's) BufferMap to a third Peer.
94
 *
95
 * @param[in] to PeerID.
96
 * @param[in] owner Owner of the BufferMap to send.
97
 * @param[in] bmap the BufferMap to send.
98
 * @param[in] trans_id transaction number associated with this send
99
 * @return 0 on success, <0 on error
100
 */
101
int sendBufferMap(const struct nodeID *to_id, const struct nodeID *owner_id, struct chunkID_set *bmap, int trans_id) {
102
  return sendSignalling(MSG_SIG_BMOFF, to_id, owner_id, bmap, 0, trans_id);
103
}
104

    
105
int sendMyBufferMap(const struct nodeID *to_id, struct chunkID_set *bmap, int trans_id)
106
{
107
  return sendBufferMap(to_id, localID, bmap, trans_id);
108
}
109

    
110
int offerChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id) {
111
  return sendSignalling(MSG_SIG_OFF, to_id, NULL, cset, max_deliver, trans_id);
112
}
113

    
114
int acceptChunks(const struct nodeID *to_id, struct chunkID_set *cset, int max_deliver, int trans_id) {
115
  return sendSignalling(MSG_SIG_ACC, to_id, NULL, cset, max_deliver, trans_id);
116
}
117

    
118

    
119
/// ==================== ///
120
///        RECEIVE       ///
121
/// ==================== ///
122

    
123
void bmap_received(const struct nodeID *fromid, const struct nodeID *ownerid, struct chunkID_set *c_set, int trans_id) {
124
  struct peer *owner = nodeid_to_peer(ownerid,1);
125
  if (owner) {        //now we have it almost sure
126
    chunkID_set_union(owner->bmap,c_set);        //don't send it back
127
    gettimeofday(&owner->bmap_timestamp, NULL);
128
  }
129
}
130

    
131
 /**
132
 * Dispatcher for signaling messages.
133
 *
134
 * This method decodes the signaling messages, retrieving the set of chunk and the signaling
135
 * message, invoking the corresponding method.
136
 *
137
 * @param[in] buff buffer which contains the signaling message
138
 * @param[in] buff_len length of the buffer
139
 * @param[in] msgtype type of message in the buffer
140
 * @param[in] max_deliver deliver at most this number of Chunks
141
 * @param[in] arg parameters associated to the signaling message
142
 * @return 0 on success, <0 on error
143
 */
144

    
145
int sigParseData(const struct nodeID *fromid, uint8_t *buff, int buff_len) {
146
    struct chunkID_set *c_set;
147
    void *meta;
148
    int meta_len;
149
    struct sig_nal *signal;
150
    int sig;
151
    int ret = 1;
152
    dprintf("\tDecoding signaling message...");
153
    c_set = decodeChunkSignaling(&meta, &meta_len, buff+1, buff_len-1);
154
    dprintf("SIG_HEADER: len: %d, of which meta: %d\n", buff_len, meta_len);
155
    if (!c_set) {
156
      fprintf(stdout, "ERROR decoding signaling message\n");
157
      return -1;
158
    }
159
    dprintf("done\n");
160
    signal = (struct sig_nal *) meta;
161
    sig = (int) (signal->type);
162
    dprintf("\tSignaling Type %d\n", sig);
163
    //MaxDelivery  and Trans_Id to be defined
164
    switch (sig) {
165
        case MSG_SIG_BMOFF:
166
        {
167
          int dummy;
168
          struct nodeID *ownerid = nodeid_undump(&(signal->third_peer),&dummy);
169
          bmap_received(fromid, ownerid, c_set, signal->trans_id);
170
          free(ownerid);
171
          break;
172
        }
173
        default:
174
          ret = -1;
175
    }
176
    
177
    chunkID_set_clear(c_set,0);
178
    free(c_set);
179
    free(meta);
180
    return ret;
181
}
182

    
183
/// ==================== ///
184
///          INIT        ///
185
/// ==================== ///
186

    
187
int sigInit(struct nodeID *myID, struct peerset *ps)
188
{
189
  localID = myID;
190
  pset = ps;
191
  return 1;
192
}