Statistics
| Branch: | Revision:

grapes / som / Tests / chunk_signaling_test.c @ 40923828

History | View | Annotate | Download (7.7 KB)

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

    
13
#include "net_helper.h"
14
#include "net_helpers.h"
15
#include "chunk.h"
16
#include "trade_msg_la.h"
17
#include "trade_msg_ha.h"
18
#include "chunkidset.h"
19
#include "msg_types.h"
20
#include "trade_sig_ha.h"
21
#include "chunkid_set_h.h"
22

    
23
#define BUFFSIZE 1024
24

    
25
static const char *my_addr = "127.0.0.1";
26
static int port = 6666;
27
static int dst_port;
28
static const char *dst_ip;
29
static int random_bmap = 0;
30
enum sigz { offer, request, sendbmap, reqbmap, unknown
31
};
32
static enum sigz sig = unknown;
33
static struct chunkID_set *cset = NULL;
34
static struct chunkID_set *rcset = NULL;
35
static struct nodeID *remote;
36
static uint8_t buff[BUFFSIZE];
37
static int max_deliver, trans_id, ret, chunktosend;
38
static enum signaling_type sig_type;
39
static struct nodeID *owner;
40

    
41

    
42
static struct nodeID *init(void)
43
{
44
  struct nodeID *myID;
45

    
46
  myID = net_helper_init(my_addr, port);
47
  if (myID == NULL) {
48
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
49

    
50
    return NULL;
51
  }  
52
  chunkSignalingInit(myID);
53
  return myID;
54
}
55

    
56
static void cmdline_parse(int argc, char *argv[])
57
{
58
  int o;
59

    
60
  while ((o = getopt(argc, argv, "p:i:P:I:ORBbN")) != -1) {
61
    switch(o) {
62
      case 'p':
63
        dst_port = atoi(optarg);
64
        break;
65
      case 'i':
66
        dst_ip = strdup(optarg);
67
        break;
68
      case 'P':
69
        port =  atoi(optarg);
70
        break;
71
      case 'I':
72
        my_addr = iface_addr(optarg);
73
        break;
74
      case 'O':
75
          sig = offer;
76
          break;
77
      case 'R':
78
          sig = request;
79
          break;
80
      case 'B':
81
          sig = sendbmap;
82
          break;
83
      case 'b':
84
          sig = reqbmap;
85
          break;
86
     case 'N':
87
          random_bmap = 1;
88
          break;
89
     default:
90
        fprintf(stderr, "Error: unknown option %c\n", o);
91

    
92
        exit(-1);
93
    }
94
  }
95
}
96

    
97

    
98
int client_side(struct nodeID *my_sock){//sender
99
    struct nodeID *dst;
100
    max_deliver = trans_id = ret = 0;    
101
    dst = create_node(dst_ip, dst_port);    
102
    fprintf(stdout,"Sending signal ");
103
    switch(sig){
104
        case offer:
105
            cset = chunkID_set_init("size=10");
106
            if(!cset){
107
                fprintf(stderr,"Unable to allocate memory for cset\n");
108
                return -1;
109
            }
110
            fillChunkID_set(cset,random_bmap);
111
            fprintf(stdout,"\"OFFER\" ");
112
            ret = offerChunks(dst,cset,8,22);
113
            break;
114
        case request:
115
            cset = chunkID_set_init("size=10");
116
            if(!cset){
117
                fprintf(stderr,"Unable to allocate memory for cset\n");
118
                return -1;
119
            }
120
            fprintf(stdout,"\"REQUEST\" ");
121
            fillChunkID_set(cset,random_bmap);
122
            printChunkID_set(cset);
123
            ret = requestChunks(dst,cset,10,44);
124
            break;
125
        case sendbmap:
126
            cset = chunkID_set_init("type=bitmap,size=10");
127
            if(!cset){
128
                fprintf(stderr,"Unable to allocate memory for cset\n");
129
                return -1;
130
            }
131
            fillChunkID_set(cset,random_bmap);
132
            fprintf(stdout,"\"SEND BMAP\" ");
133
            ret = sendBufferMap(dst,dst,cset,88);
134
            break;
135
        case reqbmap:
136
            fprintf(stdout,"\"REQUEST BMAP\" ");
137
            ret = requestBufferMap(dst,NULL,99);
138
            break;
139
        default:
140
            printf("Please select one operation (O)ffer, (R)equest, send (B)map, request (b)map\n");
141
            return -1;
142
    }   
143
    fprintf(stdout,"done: %d\n",ret);
144
    nodeid_free(dst);
145
    ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE);
146
    if (buff[0] != MSG_TYPE_SIGNALLING) {
147
      fprintf(stderr, "Wrong message type!!!\n");
148
      return -1;
149
    }
150
    fprintf(stdout,"Received message of %d bytes\n",ret);
151
    ret = parseSignaling(buff+1,ret-1, &owner, &rcset, &max_deliver, &trans_id, &sig_type);    
152
    fprintf(stdout, "Trans_id = %d; Max_del = %d\n", trans_id,max_deliver);
153
    switch(sig_type){        
154
        case sig_accept:
155
            fprintf(stdout, "Accept of %d chunks\n", chunkID_set_size(rcset));            
156
            break;
157
        case sig_deliver:
158
            fprintf(stdout, "Deliver of %d chunks\n", chunkID_set_size(rcset));            
159
            break;
160
        case sig_send_buffermap:
161
            fprintf(stdout, "I received a buffer of %d chunks\n", chunkID_set_size(rcset));
162
            break;
163
    }
164
    printChunkID_set(rcset);
165
    fprintf(stdout, "Trans_id = %d; Max_del = %d\n", trans_id,max_deliver);
166
    if (cset) chunkID_set_free(cset);
167
    if (rcset) chunkID_set_free(rcset);
168
    if(owner)nodeid_free(owner);
169
    nodeid_free(remote);    
170
    return 1;
171
  }
172

    
173
int server_side(struct nodeID *my_sock){
174
    max_deliver = trans_id = ret = 0;    
175
    ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE);
176

    
177
    /* TODO: Error check! */
178
    if (buff[0] != MSG_TYPE_SIGNALLING) {
179
      fprintf(stderr, "Wrong message type!!!\n");
180
      return -1;
181
    }
182
    fprintf(stdout,"Received message of %d bytes: trans_id %d, Max_deliver %d\n",ret,trans_id,max_deliver);
183
    ret = parseSignaling(buff+1,ret-1, &owner, &cset, &max_deliver, &trans_id, &sig_type);
184
    switch(sig_type){
185
        case sig_offer:
186
            fprintf(stdout, "1) Message OFFER: peer offers %d chunks\n", chunkID_set_size(cset));
187
            chunktosend = chunkID_set_get_latest(cset);
188
            printChunkID_set(cset);
189
            rcset = chunkID_set_init("size=1");
190
            fprintf(stdout, "2) Acceping only latest chunk #%d\n", chunktosend);
191
            chunkID_set_add_chunk(rcset,chunktosend);
192
            acceptChunks(remote,rcset,trans_id++);
193
            break;
194
        case sig_request:
195
            fprintf(stdout, "1) Message REQUEST: peer requests %d chunks\n", chunkID_set_size(cset));
196
            printChunkID_set(cset);
197
            chunktosend = chunkID_set_get_earliest(cset);
198
            rcset = chunkID_set_init("size=1");
199
            fprintf(stdout, "2) Deliver only earliest chunk #%d\n", chunktosend);
200
            chunkID_set_add_chunk(rcset,chunktosend);
201
            deliverChunks(remote,rcset,trans_id++);
202
            break;
203
        case sig_send_buffermap:
204
            fprintf(stdout, "1) Message SEND_BMAP: I received a buffer of %d chunks\n", chunkID_set_size(cset));
205
            printChunkID_set(cset);
206
            rcset = chunkID_set_init("size=15,type=bitmap");
207
            if(!rcset){
208
                fprintf(stderr,"Unable to allocate memory for rcset\n");
209
                return -1;
210
            }
211
            fillChunkID_set(rcset,random_bmap);
212
            fprintf(stdout, "2) Message SEND_BMAP: I send my buffer of %d chunks\n", chunkID_set_size(rcset));
213
            printChunkID_set(rcset);
214
            sendBufferMap(remote,my_sock,rcset,trans_id++);
215
            break;
216
        case sig_request_buffermap:
217
            fprintf(stdout, "1) Message REQUEST_BMAP: Someone requeste my buffer map [%d]\n", (cset == NULL));
218
            rcset = chunkID_set_init("size=15,type=bitmap");
219
            if(!rcset){
220
                fprintf(stderr,"Unable to allocate memory for rcset\n");
221
                return -1;
222
            }
223
            fillChunkID_set(rcset,random_bmap);
224
            sendBufferMap(remote,my_sock,rcset,trans_id++);
225
            break;
226
    }
227
    nodeid_free(remote);
228
    if(owner)nodeid_free(owner);
229
    if (cset) chunkID_set_free(cset);
230
    if (rcset) chunkID_set_free(rcset);
231
    return 1;
232
  }
233

    
234
int main(int argc, char *argv[]) {
235
    struct nodeID *my_sock;    
236
    int ret;
237
    cmdline_parse(argc, argv);
238
    my_sock = init();
239
    ret = 0;
240
    if(dst_port!=0)
241
        ret = client_side(my_sock);
242
     else
243
        ret = server_side(my_sock);//receiver side
244
    nodeid_free(my_sock);
245
    return ret;
246
}