Statistics
| Branch: | Revision:

grapes / src / Tests / topology_test.c @ 534e126a

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

    
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:")) != -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
      default:
59
        fprintf(stderr, "Error: unknown option %c\n", o);
60

    
61
        exit(-1);
62
    }
63
  }
64
}
65

    
66
static struct nodeID *init(void)
67
{
68
  struct nodeID *myID;
69

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

    
74
    return NULL;
75
  }
76
//  context = psample_init(myID, NULL, 0, "protocol=cyclon");
77
  context = psample_init(myID, NULL, 0, "");
78

    
79
  return myID;
80
}
81

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

    
89
  psample_parse_data(context, NULL, 0);
90
  while (!done) {
91
    int len;
92
    int news;
93
    const struct timeval tout = {1, 0};
94
    struct timeval t1;
95
    char addr[256];
96

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

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

    
111
        neighbourhoods = psample_get_cache(context, &n);
112
        printf("I have %d neighbours:\n", n);
113
        for (i = 0; i < n; i++) {
114
          node_addr(neighbourhoods[i], addr, 256);
115
          printf("\t%d: %s\n", i, addr);
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
            node_addr(neighbourhoods[i], addr, 256);
127
            if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, addr);
128
          }
129
          fclose(f);
130
        }
131
      }
132
      cnt++;
133
    }
134
  }
135
}
136

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

    
141
  cmdline_parse(argc, argv);
142

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

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

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

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

    
160
  loop(my_sock);
161

    
162
  return 0;
163
}