Revision 211a0bc9

View differences:

som/Tests/Makefile
14 14

  
15 15
BASE = ../..
16 16
TESTS = topology_test \
17
        topology_test_attr \
17 18
        chunk_encoding_test \
18 19
        chunk_sending_test \
19 20
        chunkidset_test \
......
27 28

  
28 29
all: $(TESTS)
29 30

  
31
topology_test_attr: topology_test_attr.o net_helpers.o
32
topology_test_attr: ../net_helper.o
33
topology_test_attr: LDLIBS += -ltopman
34

  
30 35
topology_test: topology_test.o net_helpers.o
31 36
topology_test: ../net_helper.o
32 37
topology_test: LDLIBS += -ltopman
som/Tests/topology_test_attr.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 <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
struct peer_attributes {
36
  enum peer_state {sleep, awake, tired} state;
37
  char colour[20];
38
  char name[40];
39
};
40

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

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

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

  
67
static struct nodeID *init(void)
68
{
69
  struct nodeID *myID;
70
  struct peer_attributes my_attr;
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
  memset(&my_attr, 0, sizeof(struct peer_attributes));
79
  my_attr.state = awake;
80
  sprintf(my_attr.colour, "red");
81
  sprintf(my_attr.name, node_addr(myID));
82

  
83
  topInit(myID, &my_attr, sizeof(struct peer_attributes));
84

  
85
  return myID;
86
}
87

  
88
static void loop(struct nodeID *s)
89
{
90
  int done = 0;
91
#define BUFFSIZE 1024
92
  static uint8_t buff[BUFFSIZE];
93
  int cnt = 0;
94
  
95
  topParseData(NULL, 0);
96
  while (!done) {
97
    int len;
98
    int news;
99
    struct timeval tout = {1, 0};
100

  
101
    news = wait4data(s, tout);
102
    if (news > 0) {
103
      struct nodeID *remote;
104

  
105
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
106
      topParseData(buff, len);
107
      nodeid_free(remote);
108
    } else {
109
      topParseData(NULL, 0);
110
      if (cnt++ % 10 == 0) {
111
        const struct nodeID **neighbourhoods;
112
        int n, i, size;
113
        const struct peer_attributes *meta;
114

  
115
        neighbourhoods = topGetNeighbourhood(&n);
116
        meta = topGetMetadata(&size);
117
        if (meta == NULL) {
118
          printf("No MetaData!\n");
119
        } else {
120
          if (size != sizeof(struct peer_attributes)) {
121
            fprintf(stderr, "Bad MetaData!\n");
122
            exit(-1);
123
          }
124
        }
125
        printf("I have %d neighbourhoods:\n", n);
126
        for (i = 0; i < n; i++) {
127
          printf("\t%d: %s", i, node_addr(neighbourhoods[i]));
128
          if (meta) {
129
            printf("\tPeer %s is a %s peer and is %d\n", meta[i].name, meta[i].colour, meta[i].state);
130
          }
131
          printf("\n");
132
        }
133
      }
134
    }
135
  }
136
}
137

  
138
int main(int argc, char *argv[])
139
{
140
  struct nodeID *my_sock;
141

  
142
  cmdline_parse(argc, argv);
143

  
144
  my_sock = init();
145
  if (my_sock == NULL) {
146
    return -1;
147
  }
148

  
149
  if (srv_port != 0) {
150
    struct nodeID *knownHost;
151

  
152
    knownHost = create_node(srv_ip, srv_port);
153
    if (knownHost == NULL) {
154
      fprintf(stderr, "Error creating knownHost socket (%s:%d)!\n", srv_ip, srv_port);
155

  
156
      return -1;
157
    }
158
    topAddNeighbour(knownHost, NULL, 0);
159
  }
160

  
161
  loop(my_sock);
162

  
163
  return 0;
164
}

Also available in: Unified diff