Statistics
| Branch: | Revision:

grapes / som / Tests / chunk_signaling_test.c @ 8268c97a

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

    
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

    
27
#define BUFFSIZE 1024
28

    
29
static struct nodeID *init(void)
30
{
31
  struct nodeID *myID;
32

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

    
37
    return NULL;
38
  }  
39
  chunkSignalingInit(myID);
40
  return myID;
41
}
42

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

    
47
  while ((o = getopt(argc, argv, "p:i:P:I:")) != -1) {
48
    switch(o) {
49
      case 'p':
50
        dst_port = atoi(optarg);
51
        break;
52
      case 'i':
53
        dst_ip = strdup(optarg);
54
        break;
55
      case 'P':
56
        port =  atoi(optarg);
57
        break;
58
      case 'I':
59
        my_addr = iface_addr(optarg);
60
        break;
61
      default:
62
        fprintf(stderr, "Error: unknown option %c\n", o);
63

    
64
        exit(-1);
65
    }
66
  }
67
}
68

    
69
int main(int argc, char *argv[])
70
{
71
  struct nodeID *my_sock;
72
  struct chunkID_set *cset;
73
  cmdline_parse(argc, argv);
74
  my_sock = init();
75
  if (dst_port != 0) {//sender
76
    struct nodeID *dst;        
77
    int res;
78
    cset = chunkID_set_init("type=1,size=10");
79
    res = chunkID_set_add_chunk(cset, 3);
80
    fprintf(stdout,"Adding chunk %d\n",3);
81
    res = chunkID_set_add_chunk(cset, 4);
82
    fprintf(stdout,"Adding chunk %d\n",4);
83
    res = chunkID_set_add_chunk(cset, 5);
84
    fprintf(stdout,"Adding chunk %d\n",5);
85
    res = chunkID_set_add_chunk(cset, 8);
86
    fprintf(stdout,"Adding chunk %d\n",8);
87
    res = chunkID_set_add_chunk(cset, 9);
88
    fprintf(stdout,"Adding chunk %d\n",9);
89
    res = chunkID_set_size(cset);
90
    fprintf(stdout,"Creating c_set with %d chunks\n",res);
91
    fprintf(stdout,"Sending signal ");
92
        
93
    dst = create_node(dst_ip, dst_port);
94
    //res = requestChunks(dst,cset,10,44);
95
    res = deliverChunks(dst,cset,33);
96
    //res = offerChunks(dst,cset,8,22);
97
    //res = acceptChunks(dst,cset,11);
98
    //res = sendBufferMap(dst,dst,cset,88);
99
    //res = requestBufferMap(dst,NULL,99);
100
    fprintf(stdout,"done: %d\n",res);
101
    free(cset);
102
    nodeid_free(dst);    
103
  } else {
104
    /* Receive a chunk and print it! */        
105
    struct nodeID *owner;
106
    int max_deliver, trans_id;
107
    enum signaling_type sig_type;
108
    int ret;
109
    struct nodeID *remote;    
110
    static uint8_t buff[BUFFSIZE];
111

    
112
    max_deliver = trans_id = ret = 0;
113
    ret = recv_from_peer(my_sock, &remote, buff, BUFFSIZE);   
114
    /* TODO: Error check! */
115
    if (buff[0] != MSG_TYPE_SIGNALLING) {
116
      fprintf(stderr, "Wrong message type!!!\n");
117
      return -1;
118
    }    
119
    fprintf(stdout,"Received message of %d bytes\n",ret);
120
    ret = parseSignaling(buff+1,ret-1, &owner, &cset, &max_deliver, &trans_id, &sig_type);
121
    if(cset){
122
    fprintf(stdout, "Chunk %d= %d\n",3, chunkID_set_check(cset,3));
123
    fprintf(stdout, "Chunk %d= %d\n",4, chunkID_set_check(cset,4));
124
    fprintf(stdout, "Chunk %d= %d\n",5, chunkID_set_check(cset,5));
125
    fprintf(stdout, "Chunk %d= %d\n",8, chunkID_set_check(cset,8));
126
    fprintf(stdout, "Chunk %d= %d\n",9, chunkID_set_check(cset,9));
127
    }    
128
    switch(sig_type){
129
        case sig_offer:
130
            fprintf(stdout, "Offer of %d chunks\n", chunkID_set_size(cset));
131
            break;
132
        case sig_accept:
133
            fprintf(stdout, "Accept of %d chunks\n", chunkID_set_size(cset));
134
            break;
135
        case sig_request:
136
            fprintf(stdout, "Request of %d chunks\n", chunkID_set_size(cset));
137
            break;
138
        case sig_deliver:
139
            fprintf(stdout, "Deliver of %d chunks\n", chunkID_set_size(cset));
140
            break;
141
        case sig_send_buffermap:
142
            fprintf(stdout, "Send buffer map of %d chunks\n", chunkID_set_size(cset));
143
            break;
144
        case sig_request_buffermap:
145
            fprintf(stdout, "Request buffer map %d\n", (cset == NULL));
146
            break;
147
    }
148
    fprintf(stdout, "Trans_id = %d; Max_del = %d\n", trans_id,max_deliver);
149
    nodeid_free(remote);
150
    chunkID_set_free(cset);
151
  }
152
  nodeid_free(my_sock);
153
  
154
  return 0;
155
}
156