Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.77 KB)

1 480921a6 Luca Abeni
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4 8ab58ec7 Luca Abeni
 *  This is free software; see gpl-3.0.txt
5 b576198c Luca Abeni
 *
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 480921a6 Luca Abeni
 */
19
#include <stdlib.h>
20
#include <stdint.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <getopt.h>
24
25 155319cd Luca Abeni
#include "net_helper.h"
26 efeaec84 Luca Abeni
#include "peersampler.h"
27 480921a6 Luca Abeni
#include "net_helpers.h"
28
29 5c447764 Andrea Zito
30 eb607b63 Luca
static struct psample_context *context;
31 480921a6 Luca Abeni
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 66516e5e Luca
static char *fprefix;
36 480921a6 Luca Abeni
37
static void cmdline_parse(int argc, char *argv[])
38
{
39
  int o;
40
41 66516e5e Luca
  while ((o = getopt(argc, argv, "s:p:i:P:I:")) != -1) {
42 480921a6 Luca Abeni
    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 66516e5e Luca
      case 's':
56
        fprefix = strdup(optarg);
57
        break;
58 480921a6 Luca Abeni
      default:
59
        fprintf(stderr, "Error: unknown option %c\n", o);
60
61
        exit(-1);
62
    }
63
  }
64
}
65
66 651ed37d Luca
static struct nodeID *init(void)
67 480921a6 Luca Abeni
{
68 651ed37d Luca
  struct nodeID *myID;
69 480921a6 Luca Abeni
70 c919f1bf Csaba Kiraly
  myID = net_helper_init(my_addr, port, "");
71 480921a6 Luca Abeni
  if (myID == NULL) {
72
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
73 b576198c Luca Abeni
74
    return NULL;
75 480921a6 Luca Abeni
  }
76 6d3a033f Luca
//  context = psample_init(myID, NULL, 0, "protocol=cyclon");
77
  context = psample_init(myID, NULL, 0, "");
78 651ed37d Luca
79
  return myID;
80 480921a6 Luca Abeni
}
81
82 651ed37d Luca
static void loop(struct nodeID *s)
83 480921a6 Luca Abeni
{
84
  int done = 0;
85
#define BUFFSIZE 1024
86
  static uint8_t buff[BUFFSIZE];
87
  int cnt = 0;
88
  
89 6d3a033f Luca
  psample_parse_data(context, NULL, 0);
90 480921a6 Luca Abeni
  while (!done) {
91
    int len;
92 b576198c Luca Abeni
    int news;
93 e02431fb Luca Abeni
    const struct timeval tout = {1, 0};
94
    struct timeval t1;
95 480921a6 Luca Abeni
96 e02431fb Luca Abeni
    t1 = tout;
97 ed23d319 Luca Abeni
    news = wait4data(s, &t1, NULL);
98 b576198c Luca Abeni
    if (news > 0) {
99 651ed37d Luca
      struct nodeID *remote;
100 480921a6 Luca Abeni
101 b576198c Luca Abeni
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
102 6d3a033f Luca
      psample_parse_data(context, buff, len);
103 678c612d Luca Abeni
      nodeid_free(remote);
104 480921a6 Luca Abeni
    } else {
105 6d3a033f Luca
      psample_parse_data(context, NULL, 0);
106 66516e5e Luca
      if (cnt % 10 == 0) {
107 651ed37d Luca
        const struct nodeID **neighbourhoods;
108 480921a6 Luca Abeni
        int n, i;
109
110 6d3a033f Luca
        neighbourhoods = psample_get_cache(context, &n);
111 66516e5e Luca
        printf("I have %d neighbours:\n", n);
112 480921a6 Luca Abeni
        for (i = 0; i < n; i++) {
113 651ed37d Luca
          printf("\t%d: %s\n", i, node_addr(neighbourhoods[i]));
114 480921a6 Luca Abeni
        }
115 66516e5e Luca
        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 480921a6 Luca Abeni
      }
129 66516e5e Luca
      cnt++;
130 480921a6 Luca Abeni
    }
131
  }
132
}
133
134
int main(int argc, char *argv[])
135
{
136 651ed37d Luca
  struct nodeID *my_sock;
137
138 480921a6 Luca Abeni
  cmdline_parse(argc, argv);
139
140 651ed37d Luca
  my_sock = init();
141 b576198c Luca Abeni
  if (my_sock == NULL) {
142
    return -1;
143
  }
144
145 480921a6 Luca Abeni
  if (srv_port != 0) {
146 b576198c Luca Abeni
    struct nodeID *knownHost;
147 480921a6 Luca Abeni
148 b576198c Luca Abeni
    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 6d3a033f Luca
    psample_add_peer(context, knownHost, NULL, 0);
155 480921a6 Luca Abeni
  }
156
157 651ed37d Luca
  loop(my_sock);
158 480921a6 Luca Abeni
159
  return 0;
160
}