Statistics
| Branch: | Revision:

grapes / som / Tests / topology_test.c @ 8ab58ec7

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

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

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

    
56
        exit(-1);
57
    }
58
  }
59
}
60

    
61
static struct nodeID *init(void)
62
{
63
  struct nodeID *myID;
64

    
65
  myID = net_helper_init(my_addr, port);
66
  if (myID == NULL) {
67
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
68

    
69
    return NULL;
70
  }
71
  topInit(myID);
72

    
73
  return myID;
74
}
75

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

    
89
    news = wait4data(s, tout);
90
    if (news > 0) {
91
      struct nodeID *remote;
92

    
93
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
94
      topParseData(buff, len);
95
      free(remote);
96
    } else {
97
      topParseData(NULL, 0);
98
      if (cnt++ % 10 == 0) {
99
        const struct nodeID **neighbourhoods;
100
        int n, i;
101

    
102
        neighbourhoods = topGetNeighbourhood(&n);
103
        printf("I have %d neighbourhoods:\n", n);
104
        for (i = 0; i < n; i++) {
105
          printf("\t%d: %s\n", i, node_addr(neighbourhoods[i]));
106
        }
107
      }
108
    }
109
  }
110
}
111

    
112
int main(int argc, char *argv[])
113
{
114
  struct nodeID *my_sock;
115

    
116
  cmdline_parse(argc, argv);
117

    
118
  my_sock = init();
119
  if (my_sock == NULL) {
120
    return -1;
121
  }
122

    
123
  if (srv_port != 0) {
124
    struct nodeID *knownHost;
125

    
126
    knownHost = create_node(srv_ip, srv_port);
127
    if (knownHost == NULL) {
128
      fprintf(stderr, "Error creating knownHost socket (%s:%d)!\n", srv_ip, srv_port);
129

    
130
      return -1;
131
    }
132
    topAddNeighbour(knownHost);
133
  }
134

    
135
  loop(my_sock);
136

    
137
  return 0;
138
}