grapes / src / PeerSampler / ncast.c @ f08e1d8c
History | View | Annotate | Download (6.27 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Luca Abeni
|
3 |
*
|
4 |
* This is free software; see lgpl-2.1.txt
|
5 |
*/
|
6 |
|
7 |
#include <sys/time.h> |
8 |
#include <time.h> |
9 |
#include <stdlib.h> |
10 |
#include <stdint.h> |
11 |
#include <stdio.h> |
12 |
#include <stdbool.h> |
13 |
#include <string.h> |
14 |
#include <limits.h> |
15 |
|
16 |
#include "net_helper.h" |
17 |
#include "peersampler_iface.h" |
18 |
#include "../Cache/topocache.h" |
19 |
#include "../Cache/ncast_proto.h" |
20 |
#include "../Cache/proto.h" |
21 |
#include "config.h" |
22 |
#include "grapes_msg_types.h" |
23 |
|
24 |
#define DEFAULT_CACHE_SIZE 10 |
25 |
#define DEFAULT_MAX_TIMESTAMP 5 |
26 |
#define DEFAULT_BOOTSTRAP_CYCLES 5 |
27 |
#define DEFAULT_BOOTSTRAP_PERIOD 2*1000*1000 |
28 |
#define DEFAULT_PERIOD 10*1000*1000 |
29 |
|
30 |
struct peersampler_context{
|
31 |
uint64_t currtime; |
32 |
int cache_size;
|
33 |
struct peer_cache *local_cache;
|
34 |
bool bootstrap;
|
35 |
int bootstrap_period;
|
36 |
int bootstrap_cycles;
|
37 |
int period;
|
38 |
int counter;
|
39 |
struct ncast_proto_context *tc;
|
40 |
const struct nodeID **r; |
41 |
int query_tokens;
|
42 |
}; |
43 |
|
44 |
static uint64_t gettime(void) |
45 |
{ |
46 |
struct timeval tv;
|
47 |
|
48 |
gettimeofday(&tv, NULL);
|
49 |
|
50 |
return tv.tv_usec + tv.tv_sec * 1000000ull; |
51 |
} |
52 |
|
53 |
static struct peersampler_context* ncast_context_init(void) |
54 |
{ |
55 |
struct peersampler_context* con;
|
56 |
con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context)); |
57 |
|
58 |
//Initialize context with default values
|
59 |
con->bootstrap = true;
|
60 |
con->currtime = gettime(); |
61 |
con->r = NULL;
|
62 |
|
63 |
return con;
|
64 |
} |
65 |
|
66 |
static int time_to_send(struct peersampler_context *context) |
67 |
{ |
68 |
int p = context->bootstrap ? context->bootstrap_period : context->period;
|
69 |
if (gettime() - context->currtime > p) {
|
70 |
context->currtime += p; |
71 |
|
72 |
return 1; |
73 |
} |
74 |
|
75 |
return 0; |
76 |
} |
77 |
|
78 |
/*
|
79 |
* Exported Functions!
|
80 |
*/
|
81 |
static struct peersampler_context* ncast_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config) |
82 |
{ |
83 |
struct tag *cfg_tags;
|
84 |
struct peersampler_context *context;
|
85 |
int res, max_timestamp;
|
86 |
|
87 |
context = ncast_context_init(); |
88 |
if (!context) return NULL; |
89 |
|
90 |
cfg_tags = config_parse(config); |
91 |
res = config_value_int(cfg_tags, "cache_size", &context->cache_size);
|
92 |
if (!res) {
|
93 |
context->cache_size = DEFAULT_CACHE_SIZE; |
94 |
} |
95 |
res = config_value_int(cfg_tags, "max_timestamp", &max_timestamp);
|
96 |
if (!res) {
|
97 |
max_timestamp = DEFAULT_MAX_TIMESTAMP; |
98 |
} |
99 |
res = config_value_int(cfg_tags, "period", &context->period);
|
100 |
if (!res) {
|
101 |
context->period = DEFAULT_PERIOD; |
102 |
} |
103 |
res = config_value_int(cfg_tags, "bootstrap_period", &context->bootstrap_period);
|
104 |
if (!res) {
|
105 |
context->bootstrap_period = DEFAULT_BOOTSTRAP_PERIOD; |
106 |
} |
107 |
res = config_value_int(cfg_tags, "bootstrap_cycles", &context->bootstrap_cycles);
|
108 |
if (!res) {
|
109 |
context->bootstrap_cycles = DEFAULT_BOOTSTRAP_CYCLES; |
110 |
} |
111 |
free(cfg_tags); |
112 |
|
113 |
context->local_cache = cache_init(context->cache_size, metadata_size, max_timestamp); |
114 |
if (context->local_cache == NULL) { |
115 |
free(context); |
116 |
return NULL; |
117 |
} |
118 |
|
119 |
context->tc = ncast_proto_init(myID, metadata, metadata_size); |
120 |
if (!context->tc){
|
121 |
free(context->local_cache); |
122 |
free(context); |
123 |
return NULL; |
124 |
} |
125 |
|
126 |
context->query_tokens = 0;
|
127 |
|
128 |
return context;
|
129 |
} |
130 |
|
131 |
static int ncast_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size) |
132 |
{ |
133 |
if (ncast_proto_metadata_update(context->tc, metadata, metadata_size) <= 0) { |
134 |
return -1; |
135 |
} |
136 |
|
137 |
return 1; |
138 |
} |
139 |
|
140 |
static int ncast_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size) |
141 |
{ |
142 |
if (cache_add(context->local_cache, neighbour, metadata, metadata_size) < 0) { |
143 |
return -1; |
144 |
} |
145 |
return ncast_query_peer(context->tc, context->local_cache, neighbour);
|
146 |
} |
147 |
|
148 |
static int ncast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len) |
149 |
{ |
150 |
int dummy;
|
151 |
|
152 |
if (len) {
|
153 |
const struct topo_header *h = (const struct topo_header *)buff; |
154 |
struct peer_cache *new, *remote_cache;
|
155 |
|
156 |
if (h->protocol != MSG_TYPE_TOPOLOGY) {
|
157 |
fprintf(stderr, "NCAST: Wrong protocol!\n");
|
158 |
|
159 |
return -1; |
160 |
} |
161 |
|
162 |
context->counter++; |
163 |
if (context->counter == context->bootstrap_cycles) context->bootstrap = false; |
164 |
|
165 |
remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header)); |
166 |
if (h->type == NCAST_QUERY) {
|
167 |
ncast_reply(context->tc, remote_cache, context->local_cache); |
168 |
} else {
|
169 |
context->query_tokens--; //a query was successful
|
170 |
} |
171 |
new = merge_caches(context->local_cache, remote_cache, context->cache_size, &dummy); |
172 |
cache_free(remote_cache); |
173 |
if (new != NULL) { |
174 |
cache_free(context->local_cache); |
175 |
context->local_cache = new; |
176 |
} |
177 |
} |
178 |
|
179 |
if (time_to_send(context)) {
|
180 |
int ret = INT_MIN;
|
181 |
int i;
|
182 |
|
183 |
context->query_tokens++; |
184 |
|
185 |
cache_update(context->local_cache); |
186 |
for (i = 0; i < context->query_tokens; i++) { |
187 |
int r;
|
188 |
|
189 |
r = ncast_query(context->tc, context->local_cache); |
190 |
r = r > ret ? r : ret; |
191 |
} |
192 |
} |
193 |
return 0; |
194 |
} |
195 |
|
196 |
static const struct nodeID *const*ncast_get_neighbourhood(struct peersampler_context *context, int *n) |
197 |
{ |
198 |
context->r = realloc(context->r, context->cache_size * sizeof(struct nodeID *)); |
199 |
if (context->r == NULL) { |
200 |
return NULL; |
201 |
} |
202 |
|
203 |
for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) { |
204 |
context->r[*n] = nodeid(context->local_cache, *n); |
205 |
//fprintf(stderr, "Checking table[%d]\n", *n);
|
206 |
} |
207 |
|
208 |
return context->r;
|
209 |
} |
210 |
|
211 |
static const void *ncast_get_metadata(struct peersampler_context *context, int *metadata_size) |
212 |
{ |
213 |
return get_metadata(context->local_cache, metadata_size);
|
214 |
} |
215 |
|
216 |
static int ncast_grow_neighbourhood(struct peersampler_context *context, int n) |
217 |
{ |
218 |
context->cache_size += n; |
219 |
|
220 |
return context->cache_size;
|
221 |
} |
222 |
|
223 |
static int ncast_shrink_neighbourhood(struct peersampler_context *context, int n) |
224 |
{ |
225 |
if (context->cache_size < n) {
|
226 |
return -1; |
227 |
} |
228 |
context->cache_size -= n; |
229 |
|
230 |
return context->cache_size;
|
231 |
} |
232 |
|
233 |
static int ncast_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour) |
234 |
{ |
235 |
return cache_del(context->local_cache, neighbour);
|
236 |
} |
237 |
|
238 |
struct peersampler_iface ncast = {
|
239 |
.init = ncast_init, |
240 |
.change_metadata = ncast_change_metadata, |
241 |
.add_neighbour = ncast_add_neighbour, |
242 |
.parse_data = ncast_parse_data, |
243 |
.get_neighbourhood = ncast_get_neighbourhood, |
244 |
.get_metadata = ncast_get_metadata, |
245 |
.grow_neighbourhood = ncast_grow_neighbourhood, |
246 |
.shrink_neighbourhood = ncast_shrink_neighbourhood, |
247 |
.remove_neighbour = ncast_remove_neighbour, |
248 |
}; |