Statistics
| Branch: | Revision:

grapes / src / ChunkTrading / chunk_signaling.c @ c5922d21

History | View | Annotate | Download (5.38 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

    
19
#include "chunk.h"
20
#include "grapes_msg_types.h"
21
#include "chunkidset.h"
22
#include "trade_sig_la.h"
23
#include "trade_sig_ha.h"
24
#include "int_coding.h"
25

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

    
42
#define SIG_META_LEN 1024
43
#define SIG_BUF_LEN 2048
44

    
45
struct sig_nal {
46
  uint8_t type;//type of signal.
47
  uint8_t max_deliver;//Max number of chunks to deliver.
48
  //uint16_t cb_size;//size of the buffer map to send
49
  uint16_t trans_id;//future use...
50
  uint8_t third_peer;//for buffer map exchange from other peers, just the first byte!
51
} __attribute__((packed));
52

    
53
//set the local node ID
54
static struct nodeID *localID;
55

    
56
int chunkSignalingInit(struct nodeID *myID)
57
{
58
  if(!myID)
59
      return -1;
60
  localID = myID;
61

    
62
  return 1;
63
}
64

    
65
int parseSignaling(const uint8_t *buff, int buff_len, struct nodeID **owner_id,
66
                   struct chunkID_set **cset, int *max_deliver, uint16_t *trans_id,
67
                   enum signaling_type *sig_type)
68
{
69
  int meta_len = 0;
70
  void *meta;
71

    
72
  *cset = decodeChunkSignaling(&meta, &meta_len, buff, buff_len);
73
  if (meta_len) {
74
    struct sig_nal *signal = meta;
75
    int dummy;
76

    
77
    switch (signal->type) {
78
      case MSG_SIG_OFF:
79
        *sig_type = sig_offer;
80
        break;
81
      case MSG_SIG_ACC:
82
        *sig_type = sig_accept;
83
        break;
84
      case MSG_SIG_REQ:
85
        *sig_type = sig_request;
86
        break;
87
      case MSG_SIG_DEL:
88
        *sig_type = sig_deliver;
89
        break;
90
      case MSG_SIG_BMOFF:
91
        *sig_type = sig_send_buffermap;
92
        break;
93
      case MSG_SIG_ACK:
94
        *sig_type = sig_ack;
95
        break;
96
      case MSG_SIG_BMREQ:
97
        *sig_type = sig_request_buffermap;
98
        break;
99
      default:
100
        fprintf(stderr, "Error invalid signaling message: type %d\n", signal->type);
101
        return -1;
102
    }
103
    *max_deliver = signal->max_deliver;
104
    *trans_id = signal->trans_id;
105
    *owner_id = (meta_len > sizeof(struct sig_nal) - 1 ? nodeid_undump(&(signal->third_peer), &dummy) : NULL);
106
    free(meta);
107
  } else {
108
    return -1;
109
  }
110

    
111
  return 1;
112
}
113

    
114
static int sendSignaling(int type, const struct nodeID *to_id,
115
                         const struct nodeID *owner_id,
116
                         const struct chunkID_set *cset, int max_deliver,
117
                         uint16_t trans_id)
118
{
119
  int meta_len, msg_len;
120
  uint8_t *buff;
121
  struct sig_nal *sigmex;
122

    
123
  sigmex = malloc(SIG_META_LEN);
124
  if (!sigmex) {
125
    fprintf(stderr, "Error allocating meta-buffer\n");
126

    
127
    return -1;
128
  }
129
  sigmex->type = type;
130
  sigmex->max_deliver = max_deliver;    
131
  sigmex->trans_id = trans_id;
132
  sigmex->third_peer = 0;
133
  meta_len = sizeof(*sigmex) - 1;
134
  if (owner_id) {
135
    meta_len += nodeid_dump(&sigmex->third_peer, owner_id, SIG_META_LEN - meta_len);
136
  }
137
  buff = malloc(SIG_BUF_LEN);
138
  if (!buff) {
139
    fprintf(stderr, "Error allocating buffer\n");
140
    free(sigmex);
141

    
142
    return -1;
143
  }
144

    
145
  buff[0] = MSG_TYPE_SIGNALLING;
146
  msg_len = 1 + encodeChunkSignaling(cset, sigmex, meta_len, buff+1, SIG_BUF_LEN-1);
147
  free(sigmex);
148
  if (msg_len <= 0) {
149
    fprintf(stderr, "Error in encoding chunk set for sending a buffermap\n");
150
    free(buff);
151

    
152
    return -1;
153
  } else {
154
    send_to_peer(localID, to_id, buff, msg_len);
155
  }    
156
  free(buff);
157

    
158
  return 1;
159
}
160

    
161
int requestChunks(const struct nodeID *to, const ChunkIDSet *cset,
162
                  int max_deliver, uint16_t trans_id)
163
{
164
  return sendSignaling(MSG_SIG_REQ, to, NULL, cset, max_deliver, trans_id);
165
}
166

    
167
int deliverChunks(const struct nodeID *to, ChunkIDSet *cset, uint16_t trans_id)
168
{
169
  return sendSignaling(MSG_SIG_DEL, to, NULL, cset, 0, trans_id);
170
}
171

    
172
int offerChunks(const struct nodeID *to, struct chunkID_set *cset,
173
                int max_deliver, uint16_t trans_id)
174
{
175
  return sendSignaling(MSG_SIG_OFF, to, NULL, cset, max_deliver, trans_id);
176
}
177

    
178
int acceptChunks(const struct nodeID *to, struct chunkID_set *cset, uint16_t trans_id)
179
{
180
  return sendSignaling(MSG_SIG_ACC, to, NULL, cset, 0, trans_id);
181
}
182

    
183
int sendBufferMap(const struct nodeID *to, const struct nodeID *owner,
184
                  struct chunkID_set *bmap, int cb_size, uint16_t trans_id)
185
{
186
  return sendSignaling(MSG_SIG_BMOFF, to, (!owner ? localID : owner), bmap,
187
                       cb_size, trans_id);
188
}
189

    
190
int sendAck(const struct nodeID *to, struct chunkID_set *cset, uint16_t trans_id)
191
{
192
    return sendSignaling(MSG_SIG_ACK, to, NULL, cset, 0, trans_id);
193
}
194

    
195
int requestBufferMap(const struct nodeID *to, const struct nodeID *owner,
196
                     uint16_t trans_id)
197
{
198
  return sendSignaling(MSG_SIG_BMREQ, to, (!owner?localID:owner), NULL,
199
                       0, trans_id);
200
}