Revision 88f5c397

View differences:

src/Tests/Makefile
16 16
        config_test \
17 17
        tman_test \
18 18
        topology_test_th \
19
	cloud_test
19
	cloud_test \
20
	cloudcast_topology_test \
21
	cloud_topology_monitor
20 22

  
21 23
DEPENDENCIES = libfilecloud.so
22 24

  
23 25
CPPFLAGS = -I$(BASE)/include
24 26

  
25
LDFLAGS += -L..
26
LDLIBS += -lgrapes
27
LDFLAGS += -L.. 
28
LDLIBS += -lgrapes -ldl
27 29
#LDFLAGS += -static
28 30

  
29 31
all: $(TESTS)
......
75 77

  
76 78
cloud_helper_delegate_file.o: CFLAGS += -fPIC
77 79

  
80
cloudcast_topology_test: cloudcast_topology_test.o net_helpers.o libfilecloud.so
81
cloudcast_topology_test: ../net_helper$(NH_INCARNATION).o
82
cloudcast_topology_test: LDFLAGS += -ldl
83

  
84
cloud_topology_monitor: cloud_topology_monitor.o net_helpers.o libfilecloud.so
85
cloud_topology_monitor: ../net_helper$(NH_INCARNATION).o
86
cloud_topology_monitor: LDFLAGS += -ldl
87
cloud_topology_monitor: CFLAGS += -I../TopologyManager
88

  
78 89
clean::
79 90
	rm -f $(TESTS)
80 91
	rm -f $(DEPENDENCIES)
src/Tests/cloud_topology_monitor.c
1
/*
2
 *  Copyright (c) 2011 Andrea Zito
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 *
6
 *  This is a small test program which monitors the topology partial seen by the cloud
7
 *  To try the simple test: run it with
8
 *    ./cloud_topology_monitor -c <cloud_conf>
9
 *    -c parameters describe the configuration of the cloud
10
 *    For example, run
11
 *      ./cloud_topology_monitor-c "provider=delegate,delegate_lib=libfilecloud.so"
12
 *    to use the filecloud implementation with the default parameters
13
 */
14
#include <stdlib.h>
15
#include <stdint.h>
16
#include <stdio.h>
17
#include <string.h>
18
#include <getopt.h>
19
#include <unistd.h>
20
#include <time.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
#define CLOUD_VIEW_KEY "view"
29

  
30
#include "topocache.h"
31

  
32
struct context{
33
  struct psample_context *ps_context;
34
  struct cloud_helper_context *cloud_context;
35
  struct nodeID *net_context;
36
};
37

  
38
static const char *my_addr = "127.0.0.1";
39
static int port = 9999;
40
static const char *cloud_conf = NULL;
41
static char *fprefix = NULL;
42

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

  
47
  while ((o = getopt(argc, argv, "s:c:")) != -1) {
48
    switch(o) {
49
      case 'c':
50
        cloud_conf = strdup(optarg);
51
        break;
52
      case 's':
53
        fprefix = strdup(optarg);
54
        break;
55
      default:
56
        fprintf(stderr, "Error: unknown option %c\n", o);
57
        exit(-1);
58
    }
59
  }
60
}
61

  
62
static struct context *init(void)
63
{
64
  struct context *c;
65
  c = malloc(sizeof(struct context));
66
  c->net_context = net_helper_init(my_addr, port, "");
67
  if (c->net_context == NULL) {
68
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
69
    return NULL;
70
  }
71

  
72
  c->cloud_context = cloud_helper_init(c->net_context, cloud_conf);
73
  if (c->cloud_context == NULL) {
74
    fprintf(stderr, "Error initiating cloud_helper\n");
75
    return NULL;
76
  }
77

  
78
  return c;
79
}
80

  
81
static void loop(struct context *con)
82
{
83
  int done = 0;
84
#define BUFFSIZE 1024
85
  static uint8_t buff[BUFFSIZE];
86
  int cnt = 0;
87

  
88
  printf("Initializing the cloud...\n");
89
  put_on_cloud(con->cloud_context, CLOUD_VIEW_KEY, NULL, 0);
90

  
91

  
92
  while (!done) {
93
    int len;
94
    int news;
95
    int err;
96
    int i;
97
    const struct timeval tout = {1, 0};
98
    struct timeval t1;
99
    struct peer_cache *remote_cache;
100
    time_t timestamp, now;
101
    char timestamp_str[26];
102

  
103

  
104
    t1 = tout;
105
    sleep(5);
106
    err = get_from_cloud(con->cloud_context, CLOUD_VIEW_KEY, NULL, 0);
107
    news = wait4cloud(con->cloud_context, &t1);
108
    if (news > 0) {
109
      len = recv_from_cloud(con->cloud_context, buff, BUFFSIZE);
110

  
111
      if (len == 0){
112
        printf("*** Empty view on cloud\n");
113
        continue;
114
      }
115
      timestamp = timestamp_cloud(con->cloud_context);
116
      remote_cache = entries_undump(buff, len);
117
      ctime_r(&timestamp, timestamp_str);
118
      timestamp_str[24] = '\0';
119
      now = time(NULL);
120
      printf("--------------------------------------\nCache (%s, last contact %d sec ago):\n", timestamp_str, (int)(now - timestamp));
121
      for (i=0; nodeid(remote_cache, i); i++)
122
        printf("\t%d: %s\n", i, node_addr(nodeid(remote_cache,i)));
123
      if (fprefix) {
124
        FILE *f;
125
        char fname[64];
126

  
127
        sprintf(fname, "%s.txt", fprefix);
128
        f = fopen(fname, "w");
129
        if (f) fprintf(f, "#Cache:");
130
        for (i=0; nodeid(remote_cache, i); i++)
131
          if (f) fprintf(f, "\t%d\t%s\n", i, node_addr(nodeid(remote_cache, i)));
132
        fclose(f);
133
      }
134
    } else if (news < 0) {
135
      printf("*** No view on cloud\n");
136
    } else {
137
      printf("*** !Cloud not responding!\n");
138
    }
139
    cnt++;
140
    fflush(stdout);
141
  }
142
}
143

  
144
int main(int argc, char *argv[])
145
{
146
  struct context *con;
147

  
148
  cmdline_parse(argc, argv);
149

  
150
  if (!cloud_conf){
151
    printf("No cloud configuration provided!\n");
152
    return 1;
153
  }
154

  
155
  con = init();
156
  if (!con) return 1;
157

  
158
  loop(con);
159

  
160
  return 0;
161
}
src/Tests/cloudcast_topology_test.c
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
}

Also available in: Unified diff