Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.25 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 <unistd.h>
20
#include <stdlib.h>
21
#include <stdint.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <getopt.h>
25
#include <pthread.h>
26

    
27
#include "net_helper.h"
28
#include "peersampler.h"
29
#include "net_helpers.h"
30

    
31
static struct psample_context *context;
32
static const char *my_addr = "127.0.0.1";
33
static int port = 6666;
34
static int srv_port;
35
static const char *srv_ip;
36
static char *fprefix;
37
static pthread_mutex_t neigh_lock;
38

    
39
static void cmdline_parse(int argc, char *argv[])
40
{
41
  int o;
42

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

    
63
        exit(-1);
64
    }
65
  }
66
}
67

    
68
static struct nodeID *init(void)
69
{
70
  struct nodeID *myID;
71

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

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

    
81
  return myID;
82
}
83

    
84
static void *cycle_loop(void *p)
85
{
86
  char addr[256];
87
  int done = 0;
88
  int cnt = 0;
89

    
90
  while (!done) {
91
    const int tout = 1;
92

    
93
    pthread_mutex_lock(&neigh_lock);
94
    psample_parse_data(context, NULL, 0);
95
    pthread_mutex_unlock(&neigh_lock);
96
    if (cnt % 10 == 0) {
97
      const struct nodeID **neighbours;
98
      int n, i;
99

    
100
      pthread_mutex_lock(&neigh_lock);
101
      neighbours = psample_get_cache(context, &n);
102
      printf("I have %d neighbours:\n", n);
103
      for (i = 0; i < n; i++) {
104
        node_addr(neighbours[i], addr, 256);
105
        printf("\t%d: %s\n", i, addr);
106
      }
107
      fflush(stdout);
108
      if (fprefix) {
109
        FILE *f;
110
        char fname[64];
111

    
112
        sprintf(fname, "%s-%d.txt", fprefix, port);
113
        f = fopen(fname, "w");
114
        if (f) fprintf(f, "#Cache size: %d\n", n);
115
        for (i = 0; i < n; i++) {
116
          node_addr(neighbours[i], addr, 256);
117
          if (f) fprintf(f, "%d\t\t%d\t%s\n", port, i, addr);
118
        }
119
        fclose(f);
120
      }
121
      pthread_mutex_unlock(&neigh_lock);
122
    }
123
    cnt++;
124
    sleep(tout);
125
  }
126

    
127
  return NULL;
128
}
129

    
130
static void *recv_loop(void *p)
131
{
132
  struct nodeID *s = p;
133
  int done = 0;
134
#define BUFFSIZE 1024
135
  static uint8_t buff[BUFFSIZE];
136

    
137
  while (!done) {
138
    int len;
139
    struct nodeID *remote;
140

    
141
    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
142
    pthread_mutex_lock(&neigh_lock);
143
    psample_parse_data(context, buff, len);
144
    pthread_mutex_unlock(&neigh_lock);
145
    nodeid_free(remote);
146
  }
147

    
148
  return NULL;
149
}
150

    
151
int main(int argc, char *argv[])
152
{
153
  struct nodeID *my_sock;
154
  pthread_t cycle_id, recv_id;
155

    
156
  cmdline_parse(argc, argv);
157

    
158
  my_sock = init();
159
  if (my_sock == NULL) {
160
    return -1;
161
  }
162

    
163
  if (srv_port != 0) {
164
    struct nodeID *knownHost;
165

    
166
    knownHost = create_node(srv_ip, srv_port);
167
    if (knownHost == NULL) {
168
      fprintf(stderr, "Error creating knownHost socket (%s:%d)!\n", srv_ip, srv_port);
169

    
170
      return -1;
171
    }
172
    psample_add_peer(context, knownHost, NULL, 0);
173
  }
174
  pthread_mutex_init(&neigh_lock, NULL);
175
  pthread_create(&recv_id, NULL, recv_loop, my_sock);
176
  pthread_create(&cycle_id, NULL, cycle_loop, my_sock);
177
  pthread_join(recv_id, NULL);
178
  pthread_join(cycle_id, NULL);
179

    
180
  return 0;
181
}