Statistics
| Branch: | Revision:

grapes / src / TopologyManager / peersampler.c @ 36dc16e0

History | View | Annotate | Download (2.38 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 cloudcast;
14
extern struct peersampler_iface dummy;
15

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

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

    
27

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

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

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

    
61
int psample_add_peer(struct psample_context *tc, struct nodeID *neighbour, void *metadata, int metadata_size)
62
{
63
  return tc->ps->add_neighbour(tc->ps_context, neighbour, metadata, metadata_size);
64
}
65

    
66
int psample_parse_data(struct psample_context *tc, const uint8_t *buff, int len)
67
{
68
  return tc->ps->parse_data(tc->ps_context, buff, len);
69
}
70

    
71
const struct nodeID **psample_get_cache(struct psample_context *tc, int *n)
72
{
73
  return tc->ps->get_neighbourhood(tc->ps_context, n);
74
}
75

    
76
const void *psample_get_metadata(struct psample_context *tc, int *metadata_size)
77
{
78
  return tc->ps->get_metadata(tc->ps_context, metadata_size);
79
}
80

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

    
86
int psample_shrink_cache(struct psample_context *tc, int n)
87
{
88
  return tc->ps->shrink_neighbourhood(tc->ps_context, n);
89
}
90

    
91
int psample_remove_peer(struct psample_context *tc, struct nodeID *neighbour)
92
{
93
  return tc->ps->remove_neighbour(tc->ps_context, neighbour);
94
}