Statistics
| Branch: | Revision:

grapes / src / Tests / cloudcast_topology_test.c @ 88f5c397

History | View | Annotate | Download (4.8 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *  Copyright (c) 2011 Andrea Zito
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 *
7
 *  This is a small test program for the gossip based TopologyManager (cloudcast protocol)
8
 *  To try the simple test: run it with
9
 *    ./cloudcast_topology_test -I <network interface> -P <port> -c <cloud_conf>
10
 *    -c parameters describe the configuration of the cloud which is
11
 *  used to bootstrap the topology
12
 *    For example, run
13
 *      ./topology_test -I eth0 -P 6666 -c "provider=delegate,delegate_lib=libfilecloud.so"
14
 *    to use the filecloud implementation with the default parameters
15
 */
16
#include <stdlib.h>
17
#include <stdint.h>
18
#include <stdio.h>
19
#include <string.h>
20
#include <getopt.h>
21

    
22
#include "net_helper.h"
23
#include "peersampler.h"
24
#include "net_helpers.h"
25
#include "cloud_helper.h"
26
#include "cloud_helper_utils.h"
27

    
28
struct context{
29
  struct psample_context *ps_context;
30
  struct cloud_helper_context *cloud_context;
31
  struct nodeID *net_context;
32
};
33

    
34
static const char *my_addr = "127.0.0.1";
35
static int port = 6666;
36
static const char *cloud_conf = NULL;
37
static char *fprefix = NULL;
38

    
39
static int cmdline_parse(int argc, char *argv[])
40
{
41
  int o;
42

    
43
  while ((o = getopt(argc, argv, "s:P:I:c:i:p:")) != -1) {
44
    printf("%c", o);
45
    if (optarg) printf(" -> %s\n", optarg);
46
    else printf("\n");
47
    switch(o) {
48
      case 'c':
49
        cloud_conf = strdup(optarg);
50
        break;
51
      case 'P':
52
        port =  atoi(optarg);
53
        break;
54
      case 'I':
55
        my_addr = iface_addr(optarg);
56
        break;
57
      case 's':
58
        fprefix = strdup(optarg);
59
        break;
60
      case 'i':
61
      case 'p':
62
        fprintf(stderr, "Ignoring option %c, just for compatibility\n", o);
63
        break;
64
      default:
65
        fprintf(stderr, "Error: unknown option %c\n", o);
66
        return 1;
67
    }
68
  }
69

    
70
  return 0;
71
}
72

    
73
static struct context *init(void)
74
{
75
  struct context *c;
76
  c = malloc(sizeof(struct context));
77
  c->net_context = net_helper_init(my_addr, port, "");
78
  if (c->net_context == NULL) {
79
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
80
    return NULL;
81
  }
82

    
83
  c->cloud_context = cloud_helper_init(c->net_context, cloud_conf);
84
  if (c->cloud_context == NULL) {
85
    fprintf(stderr, "Error initiating cloud_helper\n");
86
    return NULL;
87
  }
88

    
89
  c->ps_context = psample_init(c->net_context, NULL, 0, "protocol=cloudcast");
90
  if (c->ps_context == NULL) {
91
    fprintf(stderr, "Error initiating peer sampler\n");
92
    return NULL;
93
  }
94
  return c;
95
}
96

    
97

    
98

    
99
static void loop(struct context *con)
100
{
101
  int done = 0;
102
#define BUFFSIZE 1024
103
  static uint8_t buff[BUFFSIZE];
104
  int cnt = 0;
105

    
106
  psample_parse_data(con->ps_context, NULL, 0);
107
  while (!done) {
108
    int len;
109
    int news;
110
    int data_source;
111
    const struct timeval tout = {1, 0};
112
    struct timeval t1;
113

    
114
    t1 = tout;
115
    news = wait4any_threaded(con->net_context, con->cloud_context, &t1, NULL, &data_source);
116
    if (news > 0) {
117
      struct nodeID *remote = NULL;
118
      //      printf("Got data from: %d\n", data_source);
119
      if (data_source == DATA_SOURCE_NET)
120
        len = recv_from_peer(con->net_context, &remote, buff, BUFFSIZE);
121
      else if (data_source == DATA_SOURCE_CLOUD)
122
        len = recv_from_cloud(con->cloud_context, buff, BUFFSIZE);
123
      psample_parse_data(con->ps_context, buff, len);
124
      if (remote) nodeid_free(remote);
125
    } else {
126
      if (psample_parse_data(con->ps_context, NULL, 0) != 0){
127
        fprintf(stderr, "Error parsing data... quitting\n");
128
        exit(1);
129
      }
130
      if (cnt % 10 == 0) {
131
        const struct nodeID **neighbourhoods;
132
        int n, i;
133

    
134
        neighbourhoods = psample_get_cache(con->ps_context, &n);
135
        printf("I have %d neighbours:\n", n);
136
        for (i = 0; i < n; i++) {
137
          printf("\t%d: %s\n", i, node_addr(neighbourhoods[i]));
138
        }
139
        fflush(stdout);
140
        if (fprefix) {
141
          FILE *f;
142
          char fname[64];
143
          fprintf(stderr, "ci sono??? %s\n", fprefix);
144
          sprintf(fname, "%s-%d.txt", fprefix, port);
145
          f = fopen(fname, "w");
146
          if (f) fprintf(f, "#Cache size: %d\n", n);
147
          for (i = 0; i < n; i++) {
148
            if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, node_addr(neighbourhoods[i]));
149
          }
150
          fclose(f);
151
        }
152
      }
153
      cnt++;
154
    }
155
  }
156
}
157

    
158
int main(int argc, char *argv[])
159
{
160
  struct nodeID *cloudHost;
161
  struct context *con;
162

    
163
  if (cmdline_parse(argc, argv) != 0){
164
    fprintf(stderr, "Error parsing parameters!\n");
165
    return 1;
166
  }
167

    
168
  if (!cloud_conf){
169
    fprintf(stderr, "No cloud configuration provided!\n");
170
    return 1;
171
  }
172

    
173
  con = init();
174
  if (!con){
175
    fprintf(stderr, "Error initializing!\n");
176
    return 1;
177
  }
178

    
179
  cloudHost = get_cloud_node(con->cloud_context, 0);
180
  psample_add_peer(con->ps_context, cloudHost, NULL, 0);
181
  nodeid_free(cloudHost);
182
  loop(con);
183
  fprintf(stderr, "Quitting!\n");
184
  return 0;
185
}