Statistics
| Branch: | Revision:

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

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

    
25
#include "peer.h"
26
#include "net_helper.h"
27
#include "tman.h"
28
#include "peersampler.h"
29
#include "net_helpers.h"
30

    
31
#include "topology.h"
32
#include "peer_util.h"
33

    
34
static const char *my_addr = "127.0.0.1";
35
static unsigned int port = 6666;
36
static int srv_port=0;
37
int srv_metadata =0; int my_metadata;
38
static const char *srv_ip;
39
tmanRankingFunction funct = NULL;
40

    
41
int testRanker (const void *tin, const void *p1in, const void *p2in) {
42
        const int *tt, *pp1, *pp2;
43

    
44
      tt = ((const int *)(tin));
45
      pp1 = ((const int *)(p1in));
46
      pp2 = ((const int *)(p2in));
47

    
48
        return (abs(*tt-*pp1) == abs(*tt-*pp2))?0:(abs(*tt-*pp1) < abs(*tt-*pp2))?1:2;
49
}
50

    
51
static void cmdline_parse(int argc, char *argv[])
52
{
53
  int o;
54

    
55
  while ((o = getopt(argc, argv, "p:i:P:I:")) != -1) {
56
    switch(o) {
57
      case 'p':
58
        srv_port = atoi(optarg);
59
        break;
60
      case 'i':
61
        srv_ip = strdup(optarg);
62
        break;
63
      case 'P':
64
        port =  atoi(optarg);
65
        break;
66
      case 'I':
67
        my_addr = iface_addr(optarg);
68
        break;
69
      default:
70
        fprintf(stderr, "Error: unknown option %c\n", o);
71

    
72
        exit(-1);
73
    }
74
  }
75
  if (srv_port) {
76
          srand(srv_port);
77
          srv_metadata = 1 + ((double)rand() / (double)RAND_MAX)*1000;
78
  }
79
  srand(port);
80
  my_metadata = 1 + ((double)rand() / (double)RAND_MAX)*1000;
81
  fprintf(stderr,"\tMy metadata = %d\n",my_metadata);
82
}
83

    
84
static void change_metadata (struct nodeID *id) {
85

    
86
        my_metadata = 1 + ((double)rand() / (double)RAND_MAX)*1000;
87
        fprintf(stderr,"\tMy metadata ARE CHANGED = %d\n",my_metadata);
88

    
89
        tmanChangeMetadata(&my_metadata, sizeof(my_metadata));
90
}
91

    
92
static struct nodeID *init()
93
{
94
  struct nodeID *myID;
95

    
96
  myID = net_helper_init(my_addr, port, "");
97
  if (myID == NULL) {
98
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
99

    
100
    return NULL;
101
  }
102
  context = psample_init(myID,&my_metadata, sizeof(my_metadata),NULL);
103
  tmanInit(myID,&my_metadata, sizeof(my_metadata),funct,NULL);
104

    
105
  return myID;
106
}
107

    
108
static void loop(struct nodeID *s)
109
{
110
  int done = 0, now = -1;
111
#define BUFFSIZE 1524
112
  static uint8_t buff[BUFFSIZE];
113
  int cnt = 0;
114
  
115
  topoParseData(NULL, 0);
116
  while (!done) {
117
    int len;
118
    int news;
119
    const struct timeval tout = {1, 0};
120
    struct timeval t1;
121

    
122
    t1 = tout;
123
    news = wait4data(s, &t1,NULL);
124
    if (news > 0) {
125
      struct nodeID *remote;
126
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
127
      topoParseData(buff, len);
128
      nodeid_free(remote);
129
    } else
130
      topoParseData(NULL, 0);
131
    if (++cnt % 1 == 0) {
132
        const uint8_t *mdata;
133
        const struct nodeID **neighbours;
134
        char addr[256];
135
        int n, i, msize;
136
        mdata = topoGetMetadata(&msize);
137
        neighbours = topoGetNeighbourhood(&n);
138
        fprintf(stderr, "\tMy metadata = %d\nIteration # %d -- Cache size now is : %d -- I have %d neighbours:\n",my_metadata,cnt,now,n);
139
        for (i = 0; i < n; i++) {
140
                const int *d;
141
                d = (const int*)((mdata+i*msize));
142
                node_addr(neighbours[i], addr, 256);
143
                fprintf(stderr, "\t%d: %s -- %d\n", i, addr, *d);
144
        }
145
    }
146
    if (cnt % 20 == 0) {
147
        change_metadata(s);
148
    }
149
//    if (cnt % 13 == 0) {
150
//      more = ((double)rand() / (double)RAND_MAX)*10;
151
//      now = topoGrowNeighbourhood(more);
152
//      printf("Growing : +%d -- Cache size now is : %d\n", more,now);
153
//    }
154
//    if (cnt % 10 == 0) {
155
//      less = ((double)rand() / (double)RAND_MAX)*10;
156
//      now = topoShrinkNeighbourhood(less);
157
//      printf("Shrinking : -%d -- Cache size now is : %d\n", less,now);
158
//    }
159
  }
160

    
161
}
162

    
163
int main(int argc, char *argv[])
164
{
165
  struct nodeID *my_sock;
166
  funct = testRanker;
167
  cmdline_parse(argc, argv);
168

    
169
  my_sock = init();
170
  if (my_sock == NULL) {
171
    return -1;
172
  }
173

    
174
  if (srv_port != 0) {
175
    struct nodeID *knownHost;
176

    
177
    knownHost = create_node(srv_ip, srv_port);
178
    if (knownHost == NULL) {
179
      fprintf(stderr, "Error creating knownHost socket (%s:%d)!\n", srv_ip, srv_port);
180

    
181
      return -1;
182
    }
183
    topoAddNeighbour(knownHost,NULL,0);
184
  }
185

    
186
  loop(my_sock);
187

    
188
  return 0;
189
}