Statistics
| Branch: | Revision:

grapes / src / Tests / topology_test.c @ 66516e5e

History | View | Annotate | Download (3.61 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 <sys/select.h>
20
#include <stdlib.h>
21
#include <stdint.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <getopt.h>
25

    
26
#include "net_helper.h"
27
#include "topmanager.h"
28
#include "net_helpers.h"
29

    
30
static const char *my_addr = "127.0.0.1";
31
static int port = 6666;
32
static int srv_port;
33
static const char *srv_ip;
34
static char *fprefix;
35

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

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

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

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

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

    
73
    return NULL;
74
  }
75
  topInit(myID, NULL, 0, "");
76

    
77
  return myID;
78
}
79

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

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

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

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

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

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

    
136
  cmdline_parse(argc, argv);
137

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

    
143
  if (srv_port != 0) {
144
    struct nodeID *knownHost;
145

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

    
150
      return -1;
151
    }
152
    topAddNeighbour(knownHost, NULL, 0);
153
  }
154

    
155
  loop(my_sock);
156

    
157
  return 0;
158
}