Statistics
| Branch: | Revision:

grapes / src / Tests / topology_test.c @ 0c461e28

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

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

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

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

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

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

    
138
  cmdline_parse(argc, argv);
139

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

    
145
  if (srv_port != 0) {
146
    struct nodeID *knownHost;
147

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

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

    
157
  loop(my_sock);
158

    
159
  return 0;
160
}