Revision ccf0be64

View differences:

src/Tests/Makefile
15 15
BASE = ../..
16 16
TESTS = topology_test \
17 17
        topology_test_attr \
18
        topology_test_th \
18 19
        chunk_encoding_test \
19 20
        chunk_sending_test \
20 21
        chunk_signaling_test \
......
38 39
topology_test: topology_test.o net_helpers.o
39 40
topology_test: ../net_helper.o
40 41

  
42
topology_test_th: topology_test_th.o net_helpers.o
43
topology_test_th: ../net_helper.o
44
topology_test_th: CFLAGS += -pthread
45
topology_test_th: LDFLAGS += -pthread
46

  
41 47
chunk_encoding_test: chunk_encoding_test.o
42 48

  
43 49
cb_test: cb_test.o
src/Tests/topology_test_th.c
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 <unistd.h>
21
#include <stdlib.h>
22
#include <stdint.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <getopt.h>
26
#include <pthread.h>
27

  
28
#include "net_helper.h"
29
#include "topmanager.h"
30
#include "net_helpers.h"
31

  
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
  topInit(myID, NULL, 0, "protocol=cyclon");
79
//  topInit(myID, NULL, 0, "");
80

  
81
  return myID;
82
}
83

  
84
static void *cycle_loop(void *p)
85
{
86
  int done = 0;
87
  int cnt = 0;
88
  
89
  while (!done) {
90
    const int tout = 1;
91

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

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

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

  
124
  return NULL;
125
}
126

  
127
static void *recv_loop(void *p)
128
{
129
  struct nodeID *s = p;
130
  int done = 0;
131
#define BUFFSIZE 1024
132
  static uint8_t buff[BUFFSIZE];
133
  
134
  while (!done) {
135
    int len;
136
    struct nodeID *remote;
137

  
138
    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
139
    pthread_mutex_lock(&neigh_lock);
140
    topParseData(buff, len);
141
    pthread_mutex_unlock(&neigh_lock);
142
    nodeid_free(remote);
143
  }
144

  
145
  return NULL;
146
}
147

  
148
int main(int argc, char *argv[])
149
{
150
  struct nodeID *my_sock;
151
  pthread_t cycle_id, recv_id;
152

  
153
  cmdline_parse(argc, argv);
154

  
155
  my_sock = init();
156
  if (my_sock == NULL) {
157
    return -1;
158
  }
159

  
160
  if (srv_port != 0) {
161
    struct nodeID *knownHost;
162

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

  
167
      return -1;
168
    }
169
    topAddNeighbour(knownHost, NULL, 0);
170
  }
171
  pthread_mutex_init(&neigh_lock, NULL);
172
  pthread_create(&recv_id, NULL, recv_loop, my_sock);
173
  pthread_create(&cycle_id, NULL, cycle_loop, my_sock);
174
  pthread_join(recv_id, NULL);
175
  pthread_join(cycle_id, NULL);
176

  
177
  return 0;
178
}

Also available in: Unified diff