Statistics
| Branch: | Revision:

grapes / src / PeerSampler / peersampler.c @ 169c8925

History | View | Annotate | Download (2.25 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, const 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
  free(cfg_tags);
42
  
43
  tc->ps_context = tc->ps->init(myID, metadata, metadata_size, config);
44
  if (!tc->ps_context){
45
    free(tc);
46
    return NULL;
47
  }
48
  
49
  return tc;
50
}
51

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

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

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

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

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

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

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

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