Statistics
| Branch: | Revision:

grapes / src / Tests / topology.c @ 5941d7a1

History | View | Annotate | Download (2.17 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 "topmanager.h"
17
#include "tman.h"
18

    
19
#define TMAN_MAX_IDLE 5
20

    
21
static int counter = 0;
22

    
23

    
24
int topoChangeMetadata(void *metadata, int metadata_size)
25
{
26
        // this because if my own metadata are to be changed, it shouldn't be done twice!
27
         if (counter < TMAN_MAX_IDLE)
28
                 return topChangeMetadata(metadata,metadata_size);
29
    return tmanChangeMetadata(metadata,metadata_size);
30
}
31

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

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

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

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

    
83
int topoGrowNeighbourhood(int n)
84
{
85
        return tmanGrowNeighbourhood(n);
86
}
87

    
88
int topoShrinkNeighbourhood(int n)
89
{
90
  return tmanShrinkNeighbourhood(n);
91
}
92

    
93
int topoRemoveNeighbour(struct nodeID *neighbour)
94
{
95
  return topRemoveNeighbour(neighbour);
96
}
97

    
98