Statistics
| Branch: | Revision:

grapes / src / PeerSampler / ncast.c @ fb49d822

History | View | Annotate | Download (6.4 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

    
15
#include "net_helper.h"
16
#include "peersampler_iface.h"
17
#include "../Cache/topocache.h"
18
#include "../Cache/ncast_proto.h"
19
#include "../Cache/proto.h"
20
#include "config.h"
21
#include "grapes_msg_types.h"
22

    
23
#define DEFAULT_CACHE_SIZE 10
24
#define DEFAULT_MAX_TIMESTAMP 5
25
#define DEFAULT_BOOTSTRAP_CYCLES 5
26
#define DEFAULT_BOOTSTRAP_PERIOD 2*1000*1000
27
#define DEFAULT_PERIOD 10*1000*1000
28

    
29
struct peersampler_context{
30
  uint64_t currtime;
31
  int cache_size;
32
  struct peer_cache *local_cache;
33
  bool bootstrap;
34
  int bootstrap_period;
35
  int bootstrap_cycles;
36
  int period;
37
  int counter;
38
  struct ncast_proto_context *tc;
39
  const struct nodeID **r;
40
  int first_ts;
41
};
42

    
43
static uint64_t gettime(void)
44
{
45
  struct timeval tv;
46

    
47
  gettimeofday(&tv, NULL);
48

    
49
  return tv.tv_usec + tv.tv_sec * 1000000ull;
50
}
51

    
52
static struct peersampler_context* ncast_context_init(void)
53
{
54
  struct peersampler_context* con;
55
  con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context));
56

    
57
  //Initialize context with default values
58
  con->bootstrap = true;
59
  con->currtime = gettime();
60
  con->r = NULL;
61

    
62
  return con;
63
}
64

    
65
static int time_to_send(struct peersampler_context *context)
66
{
67
  int p = context->bootstrap ? context->bootstrap_period : context->period;
68
  if (gettime() - context->currtime > p) {
69
    context->currtime += p;
70

    
71
    return 1;
72
  }
73

    
74
  return 0;
75
}
76

    
77
/*
78
 * Exported Functions!
79
 */
80
static struct peersampler_context* ncast_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config)
81
{
82
  struct tag *cfg_tags;
83
  struct peersampler_context *context;
84
  int res, max_timestamp;
85

    
86
  context = ncast_context_init();
87
  if (!context) return NULL;
88

    
89
  cfg_tags = config_parse(config);
90
  res = config_value_int(cfg_tags, "cache_size", &context->cache_size);
91
  if (!res) {
92
    context->cache_size = DEFAULT_CACHE_SIZE;
93
  }
94
  res = config_value_int(cfg_tags, "max_timestamp", &max_timestamp);
95
  if (!res) {
96
    max_timestamp = DEFAULT_MAX_TIMESTAMP;
97
  }
98
  res = config_value_int(cfg_tags, "period", &context->period);
99
  if (!res) {
100
    context->period = DEFAULT_PERIOD;
101
  }
102
  res = config_value_int(cfg_tags, "bootstrap_period", &context->bootstrap_period);
103
  if (!res) {
104
    context->bootstrap_period = DEFAULT_BOOTSTRAP_PERIOD;
105
  }
106
  res = config_value_int(cfg_tags, "bootstrap_cycles", &context->bootstrap_cycles);
107
  if (!res) {
108
    context->bootstrap_cycles = DEFAULT_BOOTSTRAP_CYCLES;
109
  }
110
  free(cfg_tags);
111
  
112
  context->local_cache = cache_init(context->cache_size, metadata_size, max_timestamp);
113
  if (context->local_cache == NULL) {
114
    free(context);
115
    return NULL;
116
  }
117

    
118
  context->tc = ncast_proto_init(myID, metadata, metadata_size);
119
  if (!context->tc){
120
    free(context->local_cache);
121
    free(context);
122
    return NULL;
123
  }
124

    
125
  context->first_ts = (max_timestamp + 1) / 2;
126
  // increase timestamp for initial message, since that is out of the normal cycle of the bootstrap peer
127
  ncast_proto_myentry_update(context->tc, NULL, context->first_ts, NULL, 0);
128

    
129
  return context;
130
}
131

    
132
static int ncast_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size)
133
{
134
  if (ncast_proto_metadata_update(context->tc, metadata, metadata_size) <= 0) {
135
    return -1;
136
  }
137

    
138
  return 1;
139
}
140

    
141
static int ncast_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size)
142
{
143
  if (cache_add(context->local_cache, neighbour, metadata, metadata_size) < 0) {
144
    return -1;
145
  }
146
  return ncast_query_peer(context->tc, context->local_cache, neighbour);
147
}
148

    
149
static int ncast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
150
{
151
  int dummy;
152

    
153
  if (len) {
154
    const struct topo_header *h = (const struct topo_header *)buff;
155
    struct peer_cache *new, *remote_cache;
156

    
157
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
158
      fprintf(stderr, "NCAST: Wrong protocol!\n");
159

    
160
      return -1;
161
    }
162

    
163
    context->counter++;
164
    if (context->counter == context->bootstrap_cycles) {
165
      context->bootstrap = false;
166
      ncast_proto_myentry_update(context->tc, NULL , - context->first_ts, NULL, 0);  // reset the timestamp of our own ID, we are in normal cycle, we will not disturb the algorithm
167
    }
168

    
169
    remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header));
170
    if (h->type == NCAST_QUERY) {
171
      ncast_reply(context->tc, remote_cache, context->local_cache);
172
    }
173
    new = merge_caches(context->local_cache, remote_cache, context->cache_size, &dummy);
174
    cache_free(remote_cache);
175
    if (new != NULL) {
176
      cache_free(context->local_cache);
177
      context->local_cache = new;
178
    }
179
  }
180

    
181
  if (time_to_send(context)) {
182
    cache_update(context->local_cache);
183
    return ncast_query(context->tc, context->local_cache);
184
  }
185

    
186
  return 0;
187
}
188

    
189
static const struct nodeID **ncast_get_neighbourhood(struct peersampler_context *context, int *n)
190
{
191
  context->r = realloc(context->r, context->cache_size * sizeof(struct nodeID *));
192
  if (context->r == NULL) {
193
    return NULL;
194
  }
195

    
196
  for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) {
197
    context->r[*n] = nodeid(context->local_cache, *n);
198
    //fprintf(stderr, "Checking table[%d]\n", *n);
199
  }
200

    
201
  return context->r;
202
}
203

    
204
static const void *ncast_get_metadata(struct peersampler_context *context, int *metadata_size)
205
{
206
  return get_metadata(context->local_cache, metadata_size);
207
}
208

    
209
static int ncast_grow_neighbourhood(struct peersampler_context *context, int n)
210
{
211
  context->cache_size += n;
212

    
213
  return context->cache_size;
214
}
215

    
216
static int ncast_shrink_neighbourhood(struct peersampler_context *context, int n)
217
{
218
  if (context->cache_size < n) {
219
    return -1;
220
  }
221
  context->cache_size -= n;
222

    
223
  return context->cache_size;
224
}
225

    
226
static int ncast_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour)
227
{
228
  return cache_del(context->local_cache, neighbour);
229
}
230

    
231
struct peersampler_iface ncast = {
232
  .init = ncast_init,
233
  .change_metadata = ncast_change_metadata,
234
  .add_neighbour = ncast_add_neighbour,
235
  .parse_data = ncast_parse_data,
236
  .get_neighbourhood = ncast_get_neighbourhood,
237
  .get_metadata = ncast_get_metadata,
238
  .grow_neighbourhood = ncast_grow_neighbourhood,
239
  .shrink_neighbourhood = ncast_shrink_neighbourhood,
240
  .remove_neighbour = ncast_remove_neighbour,
241
};