Statistics
| Branch: | Revision:

grapes / src / TopologyManager / peersampler.c @ 5033613a

History | View | Annotate | Download (2.21 KB)

1
#include <sys/time.h>
2
#include <stdint.h>
3
#include <stdlib.h>
4
#include <string.h>
5

    
6
#include "net_helper.h"
7
#include "peersampler.h"
8
#include "peersampler_iface.h"
9
#include "config.h"
10

    
11
extern struct peersampler_iface ncast;
12
extern struct peersampler_iface cyclon;
13
extern struct peersampler_iface dummy;
14

    
15
struct psample_context{
16
  struct peersampler_iface *ps;
17
  struct peersampler_context *ps_context;
18
};
19

    
20
struct psample_context* psample_init(struct nodeID *myID, void *metadata, int metadata_size, const char *config)
21
{
22
  struct psample_context *tc;
23
  struct tag *cfg_tags;
24
  const char *proto;
25

    
26

    
27
  tc = malloc(sizeof(struct psample_context));
28
  if (!tc) return NULL;
29

    
30
  tc->ps = &ncast;
31
  cfg_tags = config_parse(config);
32
  proto = config_value_str(cfg_tags, "protocol");
33
  if (proto) {
34
    if (strcmp(proto, "cyclon") == 0) {
35
      tc->ps = &cyclon;
36
    }
37
    if (strcmp(proto, "dummy") == 0) {
38
      tc->ps = &dummy;
39
    }
40
  }
41
  
42
  tc->ps_context = tc->ps->init(myID, metadata, metadata_size, config);
43
  if (!tc->ps_context){
44
    free(tc);
45
    return NULL;
46
  }
47
  
48
  return tc;
49
}
50

    
51
int psample_change_metadata(struct psample_context *tc, void *metadata, int metadata_size)
52
{
53
  return tc->ps->change_metadata(tc->ps_context, metadata, metadata_size);
54
}
55

    
56
int psample_add_peer(struct psample_context *tc, struct nodeID *neighbour, void *metadata, int metadata_size)
57
{
58
  return tc->ps->add_neighbour(tc->ps_context, neighbour, metadata, metadata_size);
59
}
60

    
61
int psample_parse_data(struct psample_context *tc, const uint8_t *buff, int len)
62
{
63
  return tc->ps->parse_data(tc->ps_context, buff, len);
64
}
65

    
66
const struct nodeID **psample_get_cache(struct psample_context *tc, int *n)
67
{
68
  return tc->ps->get_neighbourhood(tc->ps_context, n);
69
}
70

    
71
const void *psample_get_metadata(struct psample_context *tc, int *metadata_size)
72
{
73
  return tc->ps->get_metadata(tc->ps_context, metadata_size);
74
}
75

    
76
int psample_grow_cache(struct psample_context *tc, int n)
77
{
78
  return tc->ps->grow_neighbourhood(tc->ps_context, n);
79
}
80

    
81
int psample_shrink_cache(struct psample_context *tc, int n)
82
{
83
  return tc->ps->shrink_neighbourhood(tc->ps_context, n);
84
}
85

    
86
int psample_remove_peer(struct psample_context *tc, struct nodeID *neighbour)
87
{
88
  return tc->ps->remove_neighbour(tc->ps_context, neighbour);
89
}