Statistics
| Branch: | Revision:

grapes / src / Tests / chunk_signaling_test.c @ 7bcfc98d

History | View | Annotate | Download (7.8 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
#include <stdlib.h>
7
#include <stdint.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <getopt.h>
11

    
12
#include "net_helper.h"
13
#include "net_helpers.h"
14
#include "chunk.h"
15
#include "chunkidset.h"
16
#include "grapes_msg_types.h"
17
#include "trade_sig_ha.h"
18
#include "chunkid_set_h.h"
19

    
20
#define BUFFSIZE 1024
21

    
22
static const char *my_addr = "127.0.0.1";
23
static int port = 6666;
24
static int dst_port;
25
static const char *dst_ip;
26
static int random_bmap = 0;
27
enum sigz { offer, request, sendbmap, reqbmap, unknown
28
};
29
static enum sigz sig = unknown;
30

    
31
static struct nodeID *init(void)
32
{
33
  struct nodeID *myID;
34

    
35
  myID = net_helper_init(my_addr, port, "");
36
  if (myID == NULL) {
37
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
38

    
39
    return NULL;
40
  }  
41
  chunkSignalingInit(myID);
42
  return myID;
43
}
44

    
45
static void cmdline_parse(int argc, char *argv[])
46
{
47
  int o;
48

    
49
  while ((o = getopt(argc, argv, "p:i:P:I:ORBbN")) != -1) {
50
    switch(o) {
51
      case 'p':
52
        dst_port = atoi(optarg);
53
        break;
54
      case 'i':
55
        dst_ip = strdup(optarg);
56
        break;
57
      case 'P':
58
        port =  atoi(optarg);
59
        break;
60
      case 'I':
61
        my_addr = iface_addr(optarg);
62
        break;
63
      case 'O':
64
          sig = offer;
65
          break;
66
      case 'R':
67
          sig = request;
68
          break;
69
      case 'B':
70
          sig = sendbmap;
71
          break;
72
      case 'b':
73
          sig = reqbmap;
74
          break;
75
     case 'N':
76
          random_bmap = 1;
77
          break;
78
     default:
79
        fprintf(stderr, "Error: unknown option %c\n", o);
80

    
81
        exit(-1);
82
    }
83
  }
84
}
85

    
86
static enum signaling_type sig_receive(struct nodeID *my_sock, struct nodeID **r, int *id, struct chunkID_set **cset)
87
{
88
    static uint8_t buff[BUFFSIZE];
89
    int ret;
90
    struct nodeID *owner;
91
    struct nodeID *remote;
92
    enum signaling_type sig_type;
93
    int max_deliver = 0;
94
    uint16_t trans_id = 0;
95

    
96
    ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE);
97

    
98
    /* TODO: Error check! */
99
    if (buff[0] != MSG_TYPE_SIGNALLING) {
100
      fprintf(stderr, "Wrong message type!!!\n");
101

    
102
      return -1;
103
    }
104
    fprintf(stdout,"Received message of %d bytes\n",ret);
105
    ret = parseSignaling(buff + 1, ret - 1, &owner, cset, &max_deliver, &trans_id, &sig_type);
106
    fprintf(stdout,"Parsed %d bytes: trans_id %d, Max_deliver %d\n", ret, trans_id, max_deliver);
107
    switch (sig_type) {
108
        case sig_accept:
109
            fprintf(stdout, "Accept of %d chunks\n", chunkID_set_size(*cset));
110
            break;
111
        case sig_deliver:
112
            fprintf(stdout, "Deliver of %d chunks\n", chunkID_set_size(*cset));
113
            break;
114
        case sig_send_buffermap:
115
            fprintf(stdout, "I received a buffer of %d chunks\n", chunkID_set_size(*cset));
116
            break;
117
    }
118
    if(owner)nodeid_free(owner);
119
    if (r) {
120
      *r = remote;
121
    } else {
122
      nodeid_free(remote);
123
    }
124
    if (id) *id = trans_id;
125

    
126
    return sig_type;
127
}
128

    
129
int client_side(struct nodeID *my_sock)
130
{
131
    struct nodeID *dst;
132
    struct chunkID_set *cset = NULL;
133
    struct chunkID_set *rcset = NULL;
134
    int ret;
135
    enum signaling_type sig_type;
136

    
137
    dst = create_node(dst_ip, dst_port);
138
    fprintf(stdout,"Sending signal\n");
139
    switch (sig) {
140
        case offer:
141
            cset = chunkID_set_init("size=10");
142
            if (!cset) {
143
                fprintf(stderr,"Unable to allocate memory for cset\n");
144
                nodeid_free(dst);
145

    
146
                return -1;
147
            }
148
            fillChunkID_set(cset, random_bmap);
149
            fprintf(stdout, "\"OFFER\" ");
150
            ret = offerChunks(dst, cset, 8, 22);
151
            break;
152
        case request:
153
            cset = chunkID_set_init("size=10");
154
            if (!cset) {
155
                fprintf(stderr,"Unable to allocate memory for cset\n");
156
                nodeid_free(dst);
157

    
158
                return -1;
159
            }
160
            fprintf(stdout, "\"REQUEST\" ");
161
            fillChunkID_set(cset,random_bmap);
162
            printChunkID_set(cset);
163
            ret = requestChunks(dst, cset, 10, 44);
164
            break;
165
        case sendbmap:
166
            cset = chunkID_set_init("type=bitmap,size=10");
167
            if (!cset) {
168
                fprintf(stderr,"Unable to allocate memory for cset\n");
169
                nodeid_free(dst);
170

    
171
                return -1;
172
            }
173
            fillChunkID_set(cset, random_bmap);
174
            fprintf(stdout, "\"SEND BMAP\" ");
175
            ret = sendBufferMap(dst, dst, cset, 0, 88);
176
            break;
177
        case reqbmap:
178
            fprintf(stdout, "\"REQUEST BMAP\" ");
179
            ret = requestBufferMap(dst, NULL, 99);
180
            break;
181
        default:
182
            printf("Please select one operation (O)ffer, (R)equest, send (B)map, request (b)map\n");
183
            nodeid_free(dst);
184

    
185
            return -1;
186
    }
187
    fprintf(stdout, "done: %d\n", ret);
188
    nodeid_free(dst);
189

    
190
    sig_type = sig_receive(my_sock, NULL, NULL, &rcset);
191
    printChunkID_set(rcset);
192
    if (cset) chunkID_set_free(cset);
193
    if (rcset) chunkID_set_free(rcset);
194

    
195
    return 1;
196
}
197

    
198
int server_side(struct nodeID *my_sock)
199
{
200
    int trans_id = 0;
201
    int chunktosend;
202
    struct chunkID_set *cset = NULL;
203
    struct chunkID_set *rcset;
204
    struct nodeID *remote;
205
    enum signaling_type sig_type;
206

    
207
    sig_type = sig_receive(my_sock, &remote, &trans_id, &cset);
208
    switch(sig_type) {
209
        case sig_offer:
210
            fprintf(stdout, "1) Message OFFER: peer offers %d chunks\n", chunkID_set_size(cset));
211
            chunktosend = chunkID_set_get_latest(cset);
212
            printChunkID_set(cset);
213
            rcset = chunkID_set_init("size=1");
214
            fprintf(stdout, "2) Acceping only latest chunk #%d\n", chunktosend);
215
            chunkID_set_add_chunk(rcset, chunktosend);
216
            acceptChunks(remote, rcset, trans_id++);
217
            break;
218
        case sig_request:
219
            fprintf(stdout, "1) Message REQUEST: peer requests %d chunks\n", chunkID_set_size(cset));
220
            printChunkID_set(cset);
221
            chunktosend = chunkID_set_get_earliest(cset);
222
            rcset = chunkID_set_init("size=1");
223
            fprintf(stdout, "2) Deliver only earliest chunk #%d\n", chunktosend);
224
            chunkID_set_add_chunk(rcset, chunktosend);
225
            deliverChunks(remote, rcset, trans_id++);
226
            break;
227
        case sig_send_buffermap:
228
            fprintf(stdout, "1) Message SEND_BMAP: I received a buffer of %d chunks\n", chunkID_set_size(cset));
229
            printChunkID_set(cset);
230
            rcset = chunkID_set_init("size=15,type=bitmap");
231
            if (!rcset) {
232
                fprintf(stderr,"Unable to allocate memory for rcset\n");
233

    
234
                return -1;
235
            }
236
            fillChunkID_set(rcset, random_bmap);
237
            fprintf(stdout, "2) Message SEND_BMAP: I send my buffer of %d chunks\n", chunkID_set_size(rcset));
238
            printChunkID_set(rcset);
239
            sendBufferMap(remote, my_sock, rcset, 0, trans_id++);
240
            break;
241
        case sig_request_buffermap:
242
            fprintf(stdout, "1) Message REQUEST_BMAP: Someone requeste my buffer map [%d]\n", (cset == NULL));
243
            rcset = chunkID_set_init("size=15,type=bitmap");
244
            if (!rcset) {
245
                fprintf(stderr,"Unable to allocate memory for rcset\n");
246

    
247
                return -1;
248
            }
249
            fillChunkID_set(rcset, random_bmap);
250
            sendBufferMap(remote, my_sock, rcset, 0, trans_id++);
251
            break;
252
    }
253
    nodeid_free(remote);
254
    if (cset) chunkID_set_free(cset);
255
    if (rcset) chunkID_set_free(rcset);
256

    
257
    return 1;
258
}
259

    
260
int main(int argc, char *argv[])
261
{
262
    struct nodeID *my_sock;    
263
    int ret;
264

    
265
    cmdline_parse(argc, argv);
266
    my_sock = init();
267
    ret = 0;
268
    if (dst_port != 0) {
269
        ret = client_side(my_sock);
270
    } else {
271
        ret = server_side(my_sock);
272
    }
273
    nodeid_free(my_sock);
274

    
275
    return ret;
276
}