Statistics
| Branch: | Revision:

grapes / som / Tests / topology_test_attr.c @ 9450a425

History | View | Annotate | Download (4.73 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 struct peer_attributes {
36
  enum peer_state {sleep, awake, tired} state;
37
  char colour[20];
38
  char name[40];
39
} my_attr;
40

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

    
45
  my_attr.state = awake;
46
  sprintf(my_attr.colour, "red");
47
  while ((o = getopt(argc, argv, "p:i:P:I:c:ST")) != -1) {
48
    switch(o) {
49
      case 'p':
50
        srv_port = atoi(optarg);
51
        break;
52
      case 'i':
53
        srv_ip = strdup(optarg);
54
        break;
55
      case 'P':
56
        port =  atoi(optarg);
57
        break;
58
      case 'I':
59
        my_addr = iface_addr(optarg);
60
        break;
61
      case 'c':
62
        strcpy(my_attr.colour, optarg);
63
        break;
64
      case 'S':
65
        my_attr.state = sleep;
66
        break;
67
      case 'T':
68
        my_attr.state = tired;
69
        break;
70
      default:
71
        fprintf(stderr, "Error: unknown option %c\n", o);
72

    
73
        exit(-1);
74
    }
75
  }
76
}
77

    
78
static struct nodeID *init(void)
79
{
80
  struct nodeID *myID;
81

    
82
  myID = net_helper_init(my_addr, port);
83
  if (myID == NULL) {
84
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
85

    
86
    return NULL;
87
  }
88

    
89
  strcpy(my_attr.name, node_addr(myID));
90
  topInit(myID, &my_attr, sizeof(struct peer_attributes), "");
91

    
92
  return myID;
93
}
94

    
95
static const char *status_print(enum peer_state s)
96
{
97
  switch (s) {
98
    case sleep:
99
      return "sleeping";
100
    case awake:
101
      return "awake";
102
    case tired:
103
      return "tired";
104
    default:
105
      return "Boh?";
106
  }
107
}
108

    
109
static void status_update(void)
110
{
111
  struct nodeID *myself;
112

    
113
  switch (my_attr.state) {
114
    case sleep:
115
      my_attr.state = awake;
116
      break;
117
    case awake:
118
      my_attr.state = tired;
119
      break;
120
    case tired:
121
      my_attr.state = sleep;
122
      break;
123
  }
124
  printf("goin' %s\n", status_print(my_attr.state));
125
  myself = create_node(my_addr, port);
126
  topChangeMetadata(myself, &my_attr, sizeof(struct peer_attributes));
127
  nodeid_free(myself);
128
}
129

    
130
static void loop(struct nodeID *s)
131
{
132
  int done = 0;
133
#define BUFFSIZE 1024
134
  static uint8_t buff[BUFFSIZE];
135
  int cnt = 0;
136
  
137
  topParseData(NULL, 0);
138
  while (!done) {
139
    int len;
140
    int news;
141
    struct timeval tout = {1, 0};
142

    
143
    news = wait4data(s, &tout, NULL);
144
    if (news > 0) {
145
      struct nodeID *remote;
146

    
147
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
148
      topParseData(buff, len);
149
      nodeid_free(remote);
150
    } else {
151
      if (cnt % 30 == 0) {
152
        status_update();
153
      }
154
      topParseData(NULL, 0);
155
      if (cnt++ % 10 == 0) {
156
        const struct nodeID **neighbourhoods;
157
        int n, i, size;
158
        const struct peer_attributes *meta;
159

    
160
        neighbourhoods = topGetNeighbourhood(&n);
161
        meta = topGetMetadata(&size);
162
        if (meta == NULL) {
163
          printf("No MetaData!\n");
164
        } else {
165
          if (size != sizeof(struct peer_attributes)) {
166
            fprintf(stderr, "Bad MetaData!\n");
167
            exit(-1);
168
          }
169
        }
170
        printf("I have %d neighbourhoods:\n", n);
171
        for (i = 0; i < n; i++) {
172
          printf("\t%d: %s", i, node_addr(neighbourhoods[i]));
173
          if (meta) {
174
            printf("\tPeer %s is a %s peer and is %s", meta[i].name, meta[i].colour, status_print(meta[i].state));
175
          }
176
          printf("\n");
177
        }
178
      }
179
    }
180
  }
181
}
182

    
183
int main(int argc, char *argv[])
184
{
185
  struct nodeID *my_sock;
186

    
187
  cmdline_parse(argc, argv);
188

    
189
  my_sock = init();
190
  if (my_sock == NULL) {
191
    return -1;
192
  }
193

    
194
  if (srv_port != 0) {
195
    struct nodeID *knownHost;
196

    
197
    knownHost = create_node(srv_ip, srv_port);
198
    if (knownHost == NULL) {
199
      fprintf(stderr, "Error creating knownHost socket (%s:%d)!\n", srv_ip, srv_port);
200

    
201
      return -1;
202
    }
203
    topAddNeighbour(knownHost, NULL, 0);
204
  }
205

    
206
  loop(my_sock);
207

    
208
  return 0;
209
}