Revision 40923828 som/Tests/chunk_signaling_test.c

View differences:

som/Tests/chunk_signaling_test.c
20 20
#include "trade_sig_ha.h"
21 21
#include "chunkid_set_h.h"
22 22

  
23
#define BUFFSIZE 1024
24

  
23 25
static const char *my_addr = "127.0.0.1";
24 26
static int port = 6666;
25 27
static int dst_port;
......
28 30
enum sigz { offer, request, sendbmap, reqbmap, unknown
29 31
};
30 32
static enum sigz sig = unknown;
31
#define BUFFSIZE 1024
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

  
32 41

  
33 42
static struct nodeID *init(void)
34 43
{
......
86 95
}
87 96

  
88 97

  
89
int main(int argc, char *argv[]) {
90
    struct nodeID *my_sock;
91
    struct chunkID_set *cset = NULL;
92
    struct chunkID_set *rcset = NULL;
93
    struct nodeID *remote;   
94
    static uint8_t buff[BUFFSIZE];    
95
    int max_deliver, trans_id, ret;
96
    enum signaling_type sig_type;
97
    struct nodeID *owner;
98
    max_deliver = trans_id = ret = 0;
99
    cmdline_parse(argc, argv);
100
    my_sock = init();    
101
    if (dst_port != 0) {//sender
98
int client_side(struct nodeID *my_sock){//sender
102 99
    struct nodeID *dst;
100
    max_deliver = trans_id = ret = 0;    
103 101
    dst = create_node(dst_ip, dst_port);    
104 102
    fprintf(stdout,"Sending signal ");
105 103
    switch(sig){
......
165 163
    }
166 164
    printChunkID_set(rcset);
167 165
    fprintf(stdout, "Trans_id = %d; Max_del = %d\n", trans_id,max_deliver);
168
    nodeid_free(remote);
169
  } else {//receiver side
170
    int chunktosend;
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;    
171 175
    ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE);
172
    
176

  
173 177
    /* TODO: Error check! */
174 178
    if (buff[0] != MSG_TYPE_SIGNALLING) {
175 179
      fprintf(stderr, "Wrong message type!!!\n");
......
186 190
            fprintf(stdout, "2) Acceping only latest chunk #%d\n", chunktosend);
187 191
            chunkID_set_add_chunk(rcset,chunktosend);
188 192
            acceptChunks(remote,rcset,trans_id++);
189
            break;        
193
            break;
190 194
        case sig_request:
191 195
            fprintf(stdout, "1) Message REQUEST: peer requests %d chunks\n", chunkID_set_size(cset));
192 196
            printChunkID_set(cset);
......
195 199
            fprintf(stdout, "2) Deliver only earliest chunk #%d\n", chunktosend);
196 200
            chunkID_set_add_chunk(rcset,chunktosend);
197 201
            deliverChunks(remote,rcset,trans_id++);
198
            break;        
202
            break;
199 203
        case sig_send_buffermap:
200 204
            fprintf(stdout, "1) Message SEND_BMAP: I received a buffer of %d chunks\n", chunkID_set_size(cset));
201 205
            printChunkID_set(cset);
......
221 225
            break;
222 226
    }
223 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;
224 232
  }
225
  if (cset) chunkID_set_free(cset);
226
  if (rcset) chunkID_set_free(rcset);
227
  nodeid_free(my_sock);
228
  return 0;
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;
229 246
}

Also available in: Unified diff