Statistics
| Branch: | Revision:

grapes / src / Tests / topology.c @ eb607b63

History | View | Annotate | Download (2.45 KB)

1
/*
2
 *  Copyright (c) 2010 Marco Biazzini
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <sys/time.h>
8
#include <time.h>
9
#include <stdlib.h>
10
#include <stdint.h>
11
#include <stdio.h>
12
#include <string.h>
13

    
14
#include "grapes_msg_types.h"
15
#include "net_helper.h"
16
#include "peersampler.h"
17
#include "tman.h"
18

    
19
#define TMAN_MAX_IDLE 5
20

    
21
static int counter = 0;
22

    
23
struct psample_context *context;
24

    
25
int topoChangeMetadata(void *metadata, int metadata_size)
26
{
27
        int res = psample_change_metadata(context,metadata,metadata_size);
28
        if (counter >= TMAN_MAX_IDLE)
29
                return tmanChangeMetadata(metadata,metadata_size);
30
        else return res;
31
}
32

    
33
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
34
{
35
        // TODO: check this!! Just to use this function to bootstrap ncast...
36
        if (counter < TMAN_MAX_IDLE)
37
                return psample_add_peer(context,neighbour,metadata,metadata_size);
38
        else return tmanAddNeighbour(neighbour,metadata,metadata_size);
39
}
40

    
41
int topoParseData(const uint8_t *buff, int len)
42
{
43
        int res,ncs,msize;
44
        const struct nodeID **n; const void *m;
45
        if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) {
46
                res = psample_parse_data(context,buff,len);
47
                if (counter <= TMAN_MAX_IDLE)
48
                        counter++;
49
        }
50
        if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN))
51
        {
52
                n = psample_get_cache(context,&ncs);
53
                m = psample_get_metadata(context,&msize);
54
                res = tmanParseData(buff,len,n,ncs,m,msize);
55
        }
56
  return res;
57
}
58

    
59
const struct nodeID **topoGetNeighbourhood(int *n)
60
{
61
        struct nodeID ** neighbors; void *mdata; int msize;
62
        *n = tmanGetNeighbourhoodSize();
63
        if (*n) {
64
                neighbors = calloc(*n,sizeof(struct nodeID *));
65
                tmanGetMetadata(&msize);
66
                mdata = calloc(*n,msize);
67
                tmanGivePeers(*n,neighbors,mdata);
68
                free(mdata);
69
                return (const struct nodeID **)neighbors;
70
        }
71
        else
72
                return psample_get_cache(context,n);
73
}
74

    
75
const void *topoGetMetadata(int *metadata_size)
76
{
77
        int n = tmanGetNeighbourhoodSize();
78
        if (n)
79
                return tmanGetMetadata(metadata_size);
80
        else
81
                return psample_get_metadata(context,metadata_size);
82
}
83

    
84
int topoGrowNeighbourhood(int n)
85
{
86
        if (counter < TMAN_MAX_IDLE)
87
                return psample_grow_cache(context,n);
88
        else
89
          return tmanGrowNeighbourhood(n);
90
}
91

    
92
int topoShrinkNeighbourhood(int n)
93
{
94
        if (counter < TMAN_MAX_IDLE)
95
                return psample_shrink_cache(context,n);
96
        else
97
                return tmanShrinkNeighbourhood(n);
98
}
99

    
100
int topoRemoveNeighbour(struct nodeID *neighbour)
101
{
102
        if (counter < TMAN_MAX_IDLE)
103
                return psample_remove_peer(context,neighbour);
104
        else
105
                return tmanRemoveNeighbour(neighbour);
106
}