Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.82 KB)

1 88f5c397 Andrea Zito
/*
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
}