Statistics
| Branch: | Revision:

grapes / som / Tests / tman_test.c @ 9c84b6d3

History | View | Annotate | Download (5.24 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

    
20
#include <sys/select.h>
21
#include <stdlib.h>
22
#include <stdint.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <getopt.h>
26

    
27
#include "peer.h"
28
#include "net_helper.h"
29
#include "tman.h"
30
#include "topmanager.h"
31
#include "net_helpers.h"
32

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

    
39
int testRanker (const void *tin, const void *p1in, const void *p2in) {
40
        struct nodeID *n; int tt, pp1, pp2, s;
41
        const uint8_t *t = tin, *p1 = p1in, *p2 = p2in;
42

    
43
        n = nodeid_undump(t,&s); tt = *((int *)(t+s)); nodeid_free(n);
44
        n = nodeid_undump(p1,&s);  pp1 = *((int *)(p1+s)); nodeid_free(n);
45
        n = nodeid_undump(p2,&s);  pp2 = *((int *)(p2+s)); nodeid_free(n);
46

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

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

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

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

    
83
static void *create_metadata (struct nodeID *id, int metadata, int *size) {
84

    
85
        uint8_t *buf = malloc(32);
86
        struct peer *p = peerCreate(id,size);
87
        *size = peerDump(p, &buf);
88
        peerDelete(p);
89
        memcpy(buf+(*size)-sizeof(int),&metadata,sizeof(int));
90
        return buf;
91
}
92

    
93
static struct nodeID *init()
94
{
95
  struct nodeID *myID;
96
  void* metadata; int metadata_size;
97

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

    
102
    return NULL;
103
  }
104
  metadata = create_metadata(myID,my_metadata,&metadata_size);
105
  topInit(myID,metadata,metadata_size,NULL);
106
  tmanInit(myID,metadata,metadata_size,funct,0);
107
//  free(metadata); tman save this metadata...
108

    
109
  return myID;
110
}
111

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

    
126
    t1 = tout;
127
    news = wait4data(s, &t1,NULL);
128
    if (news > 0) {
129
      struct nodeID *remote;
130
      len = recv_from_peer(s, &remote, buff, BUFFSIZE);
131
      topoParseData(buff, len, funct);
132
      nodeid_free(remote);
133
    } else
134
      topoParseData(NULL, 0, funct);
135
    if (++cnt % 1 == 0) {
136
            const struct nodeID **neighbours; struct nodeID *neigh; uint8_t *mdata;
137
            int n, i,*d, msize,si;
138
            mdata = topoGetMetadata(&msize);
139
            neighbours = topoGetNeighbourhood(&n);
140
            fprintf(stderr, "\tMy metadata = %d\nIteration # %d -- Cache size now is : %d -- I have %d neighbours:\n",my_metadata,cnt,now,n);
141
            for (i = 0; i < n; i++) {
142
                    neigh = nodeid_undump(mdata+i*msize,&si);
143
                    d = (int*)((mdata+i*msize)+si);
144
                    fprintf(stderr, "\t%d: %s -- %d\n", i, node_addr(neighbours[i]), //node_addr(neigh),
145
                                *d);
146
                    nodeid_free(neigh);
147
            }
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; int msize; void *mdata;
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
    mdata = create_metadata(knownHost,srv_metadata,&msize);
184
    topoAddNeighbour(knownHost,mdata,msize,funct);
185
    free(mdata);
186
  }
187

    
188
  loop(my_sock);
189

    
190
  return 0;
191
}