Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (7.8 KB)

1 c9e5a919 Alessandro Russo
/*
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 5941d7a1 Csaba Kiraly
#include "grapes_msg_types.h"
17 c9e5a919 Alessandro Russo
#include "trade_sig_ha.h"
18 d1d96863 Alessandro Russo
#include "chunkid_set_h.h"
19 c9e5a919 Alessandro Russo
20 40923828 Alessandro Russo
#define BUFFSIZE 1024
21
22 c9e5a919 Alessandro Russo
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 d1d96863 Alessandro Russo
static int random_bmap = 0;
27
enum sigz { offer, request, sendbmap, reqbmap, unknown
28
};
29
static enum sigz sig = unknown;
30 c9e5a919 Alessandro Russo
31
static struct nodeID *init(void)
32
{
33
  struct nodeID *myID;
34
35 c919f1bf Csaba Kiraly
  myID = net_helper_init(my_addr, port, "");
36 c9e5a919 Alessandro Russo
  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 d1d96863 Alessandro Russo
  while ((o = getopt(argc, argv, "p:i:P:I:ORBbN")) != -1) {
50 c9e5a919 Alessandro Russo
    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 d1d96863 Alessandro Russo
      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 c9e5a919 Alessandro Russo
        fprintf(stderr, "Error: unknown option %c\n", o);
80
81
        exit(-1);
82
    }
83
  }
84
}
85
86 2054cc6f Luca Abeni
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 7bcfc98d Luca Abeni
    int max_deliver = 0;
94
    uint16_t trans_id = 0;
95 2054cc6f Luca Abeni
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 8268c97a Luca Abeni
129 c3b1c35e Luca Abeni
int client_side(struct nodeID *my_sock)
130
{
131 d1d96863 Alessandro Russo
    struct nodeID *dst;
132 d3d350d1 Alessandro Russo
    struct chunkID_set *cset = NULL;
133 2054cc6f Luca Abeni
    struct chunkID_set *rcset = NULL;
134
    int ret;
135
    enum signaling_type sig_type;
136 c3b1c35e Luca Abeni
137
    dst = create_node(dst_ip, dst_port);
138 50e97f56 Alessandro Russo
    fprintf(stdout,"Sending signal\n");
139 c3b1c35e Luca Abeni
    switch (sig) {
140 d1d96863 Alessandro Russo
        case offer:
141
            cset = chunkID_set_init("size=10");
142 c3b1c35e Luca Abeni
            if (!cset) {
143 d1d96863 Alessandro Russo
                fprintf(stderr,"Unable to allocate memory for cset\n");
144 7985f506 Luca Abeni
                nodeid_free(dst);
145 c3b1c35e Luca Abeni
146 d1d96863 Alessandro Russo
                return -1;
147
            }
148 c3b1c35e Luca Abeni
            fillChunkID_set(cset, random_bmap);
149
            fprintf(stdout, "\"OFFER\" ");
150
            ret = offerChunks(dst, cset, 8, 22);
151 d1d96863 Alessandro Russo
            break;
152
        case request:
153
            cset = chunkID_set_init("size=10");
154 c3b1c35e Luca Abeni
            if (!cset) {
155 d1d96863 Alessandro Russo
                fprintf(stderr,"Unable to allocate memory for cset\n");
156 7985f506 Luca Abeni
                nodeid_free(dst);
157 c3b1c35e Luca Abeni
158 d1d96863 Alessandro Russo
                return -1;
159
            }
160 c3b1c35e Luca Abeni
            fprintf(stdout, "\"REQUEST\" ");
161 d1d96863 Alessandro Russo
            fillChunkID_set(cset,random_bmap);
162
            printChunkID_set(cset);
163 c3b1c35e Luca Abeni
            ret = requestChunks(dst, cset, 10, 44);
164 d1d96863 Alessandro Russo
            break;
165
        case sendbmap:
166
            cset = chunkID_set_init("type=bitmap,size=10");
167 c3b1c35e Luca Abeni
            if (!cset) {
168 d1d96863 Alessandro Russo
                fprintf(stderr,"Unable to allocate memory for cset\n");
169 7985f506 Luca Abeni
                nodeid_free(dst);
170 c3b1c35e Luca Abeni
171 d1d96863 Alessandro Russo
                return -1;
172
            }
173 c3b1c35e Luca Abeni
            fillChunkID_set(cset, random_bmap);
174
            fprintf(stdout, "\"SEND BMAP\" ");
175 dd3cc823 Csaba Kiraly
            ret = sendBufferMap(dst, dst, cset, 0, 88);
176 d1d96863 Alessandro Russo
            break;
177
        case reqbmap:
178 c3b1c35e Luca Abeni
            fprintf(stdout, "\"REQUEST BMAP\" ");
179
            ret = requestBufferMap(dst, NULL, 99);
180 d1d96863 Alessandro Russo
            break;
181
        default:
182
            printf("Please select one operation (O)ffer, (R)equest, send (B)map, request (b)map\n");
183 7985f506 Luca Abeni
            nodeid_free(dst);
184 c3b1c35e Luca Abeni
185 d1d96863 Alessandro Russo
            return -1;
186 c3b1c35e Luca Abeni
    }
187
    fprintf(stdout, "done: %d\n", ret);
188 d1d96863 Alessandro Russo
    nodeid_free(dst);
189 c3b1c35e Luca Abeni
190 2054cc6f Luca Abeni
    sig_type = sig_receive(my_sock, NULL, NULL, &rcset);
191 d1d96863 Alessandro Russo
    printChunkID_set(rcset);
192 40923828 Alessandro Russo
    if (cset) chunkID_set_free(cset);
193
    if (rcset) chunkID_set_free(rcset);
194 c3b1c35e Luca Abeni
195 40923828 Alessandro Russo
    return 1;
196 c3b1c35e Luca Abeni
}
197 40923828 Alessandro Russo
198 c3b1c35e Luca Abeni
int server_side(struct nodeID *my_sock)
199
{
200 2054cc6f Luca Abeni
    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 c3b1c35e Luca Abeni
207 2054cc6f Luca Abeni
    sig_type = sig_receive(my_sock, &remote, &trans_id, &cset);
208
    switch(sig_type) {
209 d1d96863 Alessandro Russo
        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 c3b1c35e Luca Abeni
            chunkID_set_add_chunk(rcset, chunktosend);
216
            acceptChunks(remote, rcset, trans_id++);
217 40923828 Alessandro Russo
            break;
218 d1d96863 Alessandro Russo
        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 c3b1c35e Luca Abeni
            chunkID_set_add_chunk(rcset, chunktosend);
225
            deliverChunks(remote, rcset, trans_id++);
226 40923828 Alessandro Russo
            break;
227 d1d96863 Alessandro Russo
        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 c3b1c35e Luca Abeni
            if (!rcset) {
232 d1d96863 Alessandro Russo
                fprintf(stderr,"Unable to allocate memory for rcset\n");
233 c3b1c35e Luca Abeni
234 d1d96863 Alessandro Russo
                return -1;
235
            }
236 c3b1c35e Luca Abeni
            fillChunkID_set(rcset, random_bmap);
237 d1d96863 Alessandro Russo
            fprintf(stdout, "2) Message SEND_BMAP: I send my buffer of %d chunks\n", chunkID_set_size(rcset));
238
            printChunkID_set(rcset);
239 dd3cc823 Csaba Kiraly
            sendBufferMap(remote, my_sock, rcset, 0, trans_id++);
240 c9e5a919 Alessandro Russo
            break;
241
        case sig_request_buffermap:
242 d1d96863 Alessandro Russo
            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 c3b1c35e Luca Abeni
            if (!rcset) {
245 d1d96863 Alessandro Russo
                fprintf(stderr,"Unable to allocate memory for rcset\n");
246 c3b1c35e Luca Abeni
247 d1d96863 Alessandro Russo
                return -1;
248
            }
249 c3b1c35e Luca Abeni
            fillChunkID_set(rcset, random_bmap);
250 dd3cc823 Csaba Kiraly
            sendBufferMap(remote, my_sock, rcset, 0, trans_id++);
251 c9e5a919 Alessandro Russo
            break;
252
    }
253
    nodeid_free(remote);
254 40923828 Alessandro Russo
    if (cset) chunkID_set_free(cset);
255
    if (rcset) chunkID_set_free(rcset);
256 c3b1c35e Luca Abeni
257 40923828 Alessandro Russo
    return 1;
258 c3b1c35e Luca Abeni
}
259 40923828 Alessandro Russo
260 c3b1c35e Luca Abeni
int main(int argc, char *argv[])
261
{
262 40923828 Alessandro Russo
    struct nodeID *my_sock;    
263
    int ret;
264 c3b1c35e Luca Abeni
265 40923828 Alessandro Russo
    cmdline_parse(argc, argv);
266
    my_sock = init();
267
    ret = 0;
268 2054cc6f Luca Abeni
    if (dst_port != 0) {
269 40923828 Alessandro Russo
        ret = client_side(my_sock);
270 2054cc6f Luca Abeni
    } else {
271
        ret = server_side(my_sock);
272
    }
273 40923828 Alessandro Russo
    nodeid_free(my_sock);
274 c3b1c35e Luca Abeni
275 40923828 Alessandro Russo
    return ret;
276 c3b1c35e Luca Abeni
}