Statistics
| Branch: | Revision:

grapes / src / PeerSampler / peersampler.c @ 10ddaca7

History | View | Annotate | Download (2.79 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 "grapes_config.h"
10

    
11
extern struct peersampler_iface ncast;
12
extern struct peersampler_iface ncastplus;
13
extern struct peersampler_iface cyclon;
14
extern struct peersampler_iface cloudcast;
15
extern struct peersampler_iface dummy;
16

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

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

    
28

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

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

    
62
int psample_change_metadata(struct psample_context *tc, const void *metadata, int metadata_size)
63
{
64
  return tc->ps->change_metadata(tc->ps_context, metadata, metadata_size);
65
}
66

    
67
int psample_add_peer(struct psample_context *tc, struct nodeID *neighbour, const void *metadata, int metadata_size)
68
{
69
  return tc->ps->add_neighbour(tc->ps_context, neighbour, metadata, metadata_size);
70
}
71

    
72
int psample_parse_data(struct psample_context *tc, const uint8_t *buff, int len)
73
{
74
  return tc->ps->parse_data(tc->ps_context, buff, len);
75
}
76

    
77
const struct nodeID *const *psample_get_cache(struct psample_context *tc, int *n)
78
{
79
  return tc->ps->get_neighbourhood(tc->ps_context, n);
80
}
81

    
82
const void *psample_get_metadata(struct psample_context *tc, int *metadata_size)
83
{
84
  return tc->ps->get_metadata(tc->ps_context, metadata_size);
85
}
86

    
87
int psample_grow_cache(struct psample_context *tc, int n)
88
{
89
  return tc->ps->grow_neighbourhood(tc->ps_context, n);
90
}
91

    
92
int psample_shrink_cache(struct psample_context *tc, int n)
93
{
94
  return tc->ps->shrink_neighbourhood(tc->ps_context, n);
95
}
96

    
97
int psample_remove_peer(struct psample_context *tc, const struct nodeID *neighbour)
98
{
99
  return tc->ps->remove_neighbour(tc->ps_context, neighbour);
100
}
101

    
102
void psample_destroy(struct psample_context **tc)
103
{
104
  if (tc && *tc)
105
  {
106
    (*tc)->ps->destroy(&((*tc)->ps_context));
107
    free(*tc);
108
    *tc = NULL;
109
  }
110
}