Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.21 KB)

1 a515fd33 Luca
#include <sys/time.h>
2
#include <stdint.h>
3
#include <stdlib.h>
4 6c27d53c Luca
#include <string.h>
5 a515fd33 Luca
6
#include "net_helper.h"
7 efeaec84 Luca Abeni
#include "peersampler.h"
8 a515fd33 Luca
#include "peersampler_iface.h"
9 6c27d53c Luca
#include "config.h"
10 a515fd33 Luca
11
extern struct peersampler_iface ncast;
12 194ec532 Luca Abeni
extern struct peersampler_iface cyclon;
13 e2b6ad48 Luca
extern struct peersampler_iface dummy;
14 a515fd33 Luca
15 eb607b63 Luca
struct psample_context{
16 a5978256 Andrea Zito
  struct peersampler_iface *ps;
17 d18f543e Andrea Zito
  struct peersampler_context *ps_context;
18 a5978256 Andrea Zito
};
19
20 eb607b63 Luca
struct psample_context* psample_init(struct nodeID *myID, void *metadata, int metadata_size, const char *config)
21 a515fd33 Luca
{
22 eb607b63 Luca
  struct psample_context *tc;
23 6c27d53c Luca
  struct tag *cfg_tags;
24
  const char *proto;
25
26 a5024326 Andrea Zito
27 eb607b63 Luca
  tc = malloc(sizeof(struct psample_context));
28 06113eb4 Andrea Zito
  if (!tc) return NULL;
29 a5024326 Andrea Zito
30
  tc->ps = &ncast;
31 6c27d53c Luca
  cfg_tags = config_parse(config);
32
  proto = config_value_str(cfg_tags, "protocol");
33
  if (proto) {
34
    if (strcmp(proto, "cyclon") == 0) {
35 a5024326 Andrea Zito
      tc->ps = &cyclon;
36 6c27d53c Luca
    }
37
    if (strcmp(proto, "dummy") == 0) {
38 a5024326 Andrea Zito
      tc->ps = &dummy;
39 6c27d53c Luca
    }
40
  }
41 06113eb4 Andrea Zito
  
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 a515fd33 Luca
}
50
51 eb607b63 Luca
int psample_change_metadata(struct psample_context *tc, void *metadata, int metadata_size)
52 a515fd33 Luca
{
53 a5978256 Andrea Zito
  return tc->ps->change_metadata(tc->ps_context, metadata, metadata_size);
54 a515fd33 Luca
}
55
56 eb607b63 Luca
int psample_add_peer(struct psample_context *tc, struct nodeID *neighbour, void *metadata, int metadata_size)
57 a515fd33 Luca
{
58 a5978256 Andrea Zito
  return tc->ps->add_neighbour(tc->ps_context, neighbour, metadata, metadata_size);
59 a515fd33 Luca
}
60
61 eb607b63 Luca
int psample_parse_data(struct psample_context *tc, const uint8_t *buff, int len)
62 a515fd33 Luca
{
63 a5978256 Andrea Zito
  return tc->ps->parse_data(tc->ps_context, buff, len);
64 a515fd33 Luca
}
65
66 eb607b63 Luca
const struct nodeID **psample_get_cache(struct psample_context *tc, int *n)
67 a515fd33 Luca
{
68 a5978256 Andrea Zito
  return tc->ps->get_neighbourhood(tc->ps_context, n);
69 a515fd33 Luca
}
70
71 eb607b63 Luca
const void *psample_get_metadata(struct psample_context *tc, int *metadata_size)
72 a515fd33 Luca
{
73 a5978256 Andrea Zito
  return tc->ps->get_metadata(tc->ps_context, metadata_size);
74 a515fd33 Luca
}
75
76 eb607b63 Luca
int psample_grow_cache(struct psample_context *tc, int n)
77 a515fd33 Luca
{
78 a5978256 Andrea Zito
  return tc->ps->grow_neighbourhood(tc->ps_context, n);
79 a515fd33 Luca
}
80
81 eb607b63 Luca
int psample_shrink_cache(struct psample_context *tc, int n)
82 a515fd33 Luca
{
83 a5978256 Andrea Zito
  return tc->ps->shrink_neighbourhood(tc->ps_context, n);
84 a515fd33 Luca
}
85
86 eb607b63 Luca
int psample_remove_peer(struct psample_context *tc, struct nodeID *neighbour)
87 a515fd33 Luca
{
88 a5978256 Andrea Zito
  return tc->ps->remove_neighbour(tc->ps_context, neighbour);
89 a515fd33 Luca
}