Statistics
| Branch: | Revision:

grapes / src / Tests / topology_test.c @ a445564c

History | View | Annotate | Download (3.83 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 *
6
 *  This is a small test program for the gossip based TopologyManager
7
 *  To try the simple test: run it with
8
 *    ./topology_test -I <network interface> -P <port> [-i <remote IP> -p <remote port>]
9
 *    the "-i" and "-p" parameters can be used to add an initial neighbour
10
 *    (otherwise, the peer risks to stay out of the overlay).
11
 *    For example, run
12
 *      ./topology_test -I eth0 -P 6666
13
 *    on a computer, and then
14
 *      ./topology_test -I eth0 -P 2222 -i <ip_address> -p 6666
15
 *  on another one ... Of course, one of the two peers has to use -i... -p...
16
 *  (in general, to be part of the overlay a peer must either use
17
 *  "-i<known peer IP> -p<known peer port>" or be referenced by another peer).
18
 */
19
#include <stdlib.h>
20
#include <stdint.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <getopt.h>
24

    
25
#include "net_helper.h"
26
#include "peersampler.h"
27
#include "net_helpers.h"
28

    
29
static const char *psample_config;
30
static struct psample_context *context;
31
static const char *my_addr = "127.0.0.1";
32
static int port = 6666;
33
static int srv_port;
34
static const char *srv_ip;
35
static char *fprefix;
36

    
37
static void cmdline_parse(int argc, char *argv[])
38
{
39
  int o;
40

    
41
  while ((o = getopt(argc, argv, "s:p:i:P:I:c")) != -1) {
42
    switch(o) {
43
      case 'p':
44
        srv_port = atoi(optarg);
45
        break;
46
      case 'i':
47
        srv_ip = strdup(optarg);
48
        break;
49
      case 'P':
50
        port =  atoi(optarg);
51
        break;
52
      case 'I':
53
        my_addr = iface_addr(optarg);
54
        break;
55
      case 's':
56
        fprefix = strdup(optarg);
57
        break;
58
      case 'c':
59
        psample_config = "protocol=cyclon";
60
        break;
61
      default:
62
        fprintf(stderr, "Error: unknown option %c\n", o);
63

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

    
69
static struct nodeID *init(void)
70
{
71
  struct nodeID *myID;
72

    
73
  myID = net_helper_init(my_addr, port, "");
74
  if (myID == NULL) {
75
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
76

    
77
    return NULL;
78
  }
79
  context = psample_init(myID, NULL, 0, psample_config);
80

    
81
  return myID;
82
}
83

    
84
static void loop(struct nodeID *s)
85
{
86
  int done = 0;
87
#define BUFFSIZE 1024
88
  static uint8_t buff[BUFFSIZE];
89
  int cnt = 0;
90
  
91
  psample_parse_data(context, NULL, 0);
92
  while (!done) {
93
    int len;
94
    int news;
95
    const struct timeval tout = {1, 0};
96
    struct timeval t1;
97

    
98
    t1 = tout;
99
    news = wait4data(s, &t1, NULL);
100
    if (news > 0) {
101
      struct nodeID *remote;
102

    
103
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
104
      psample_parse_data(context, buff, len);
105
      nodeid_free(remote);
106
    } else {
107
      psample_parse_data(context, NULL, 0);
108
      if (cnt % 10 == 0) {
109
        const struct nodeID **neighbourhoods;
110
        int n, i;
111

    
112
        neighbourhoods = psample_get_cache(context, &n);
113
        printf("I have %d neighbours:\n", n);
114
        for (i = 0; i < n; i++) {
115
          printf("\t%d: %s\n", i, node_addr(neighbourhoods[i]));
116
        }
117
        fflush(stdout);
118
        if (fprefix) {
119
          FILE *f;
120
          char fname[64];
121

    
122
          sprintf(fname, "%s-%d.txt", fprefix, port);
123
          f = fopen(fname, "w");
124
          if (f) fprintf(f, "#Cache size: %d\n", n);
125
          for (i = 0; i < n; i++) {
126
            if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, node_addr(neighbourhoods[i]));
127
          }
128
          fclose(f);
129
        }
130
      }
131
      cnt++;
132
    }
133
  }
134
}
135

    
136
int main(int argc, char *argv[])
137
{
138
  struct nodeID *my_sock;
139

    
140
  cmdline_parse(argc, argv);
141

    
142
  my_sock = init();
143
  if (my_sock == NULL) {
144
    return -1;
145
  }
146

    
147
  if (srv_port != 0) {
148
    struct nodeID *knownHost;
149

    
150
    knownHost = create_node(srv_ip, srv_port);
151
    if (knownHost == NULL) {
152
      fprintf(stderr, "Error creating knownHost socket (%s:%d)!\n", srv_ip, srv_port);
153

    
154
      return -1;
155
    }
156
    psample_add_peer(context, knownHost, NULL, 0);
157
  }
158

    
159
  loop(my_sock);
160

    
161
  return 0;
162
}