Statistics
| Branch: | Revision:

grapes / src / TopologyManager / ncast.c @ f3ab0d6d

History | View | Annotate | Download (5.39 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 "topocache.h"
18
#include "ncast_proto.h"
19
#include "proto.h"
20
#include "config.h"
21
#include "grapes_msg_types.h"
22

    
23
#define BOOTSTRAP_CYCLES 5
24
#define DEFAULT_CACHE_SIZE 10
25
#define DEFAULT_MAX_TIMESTAMP 5
26

    
27
struct peersampler_context{
28
  uint64_t currtime;
29
  int cache_size;
30
  struct peer_cache *local_cache;
31
  bool bootstrap;
32
  int bootstrap_period;
33
  int period;
34
  int counter;
35
  struct ncast_proto_context *tc;
36
};
37

    
38
static uint64_t gettime(void)
39
{
40
  struct timeval tv;
41

    
42
  gettimeofday(&tv, NULL);
43

    
44
  return tv.tv_usec + tv.tv_sec * 1000000ull;
45
}
46

    
47
static struct peersampler_context* ncast_context_init(void){
48
  struct peersampler_context* con;
49
  con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context));
50

    
51
  //Initialize context with default values
52
  con->bootstrap = true;
53
  con->bootstrap_period = 2000000;
54
  con->period = 10000000;
55
  con->currtime = gettime();
56

    
57
  return con;
58
}
59

    
60
static int time_to_send(struct peersampler_context *context)
61
{
62
  int p = context->bootstrap ? context->bootstrap_period : context->period;
63
  if (gettime() - context->currtime > p) {
64
    context->currtime += p;
65

    
66
    return 1;
67
  }
68

    
69
  return 0;
70
}
71

    
72
/*
73
 * Exported Functions!
74
 */
75
static struct peersampler_context* ncast_init(struct nodeID *myID, void *metadata, int metadata_size, const char *config)
76
{
77
  struct tag *cfg_tags;
78
  struct peersampler_context *context;
79
  int res, max_timestamp;
80

    
81
  context = ncast_context_init();
82
  if (!context) return NULL;
83

    
84
  cfg_tags = config_parse(config);
85
  res = config_value_int(cfg_tags, "cache_size", &context->cache_size);
86
  if (!res) {
87
    context->cache_size = DEFAULT_CACHE_SIZE;
88
  }
89
  res = config_value_int(cfg_tags, "max_timestamp", &max_timestamp);
90
  if (!res) {
91
    max_timestamp = DEFAULT_MAX_TIMESTAMP;
92
  }
93
  free(cfg_tags);
94
  
95
  context->local_cache = cache_init(context->cache_size, metadata_size, max_timestamp);
96
  if (context->local_cache == NULL) {
97
    free(context);
98
    return NULL;
99
  }
100

    
101
  context->tc = ncast_proto_init(myID, metadata, metadata_size);
102
  if (!context->tc){
103
    free(context->local_cache);
104
    free(context);
105
    return NULL;
106
  }
107

    
108
  return context;
109
}
110

    
111
static int ncast_change_metadata(struct peersampler_context *context, void *metadata, int metadata_size)
112
{
113
  if (ncast_proto_metadata_update(context->tc, metadata, metadata_size) <= 0) {
114
    return -1;
115
  }
116

    
117
  return 1;
118
}
119

    
120
static int ncast_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, void *metadata, int metadata_size)
121
{
122
  if (cache_add(context->local_cache, neighbour, metadata, metadata_size) < 0) {
123
    return -1;
124
  }
125
  return ncast_query_peer(context->tc, context->local_cache, neighbour);
126
}
127

    
128
static int ncast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
129
{
130
  int dummy;
131

    
132
  if (len) {
133
    const struct topo_header *h = (const struct topo_header *)buff;
134
    struct peer_cache *new, *remote_cache;
135

    
136
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
137
      fprintf(stderr, "NCAST: Wrong protocol!\n");
138

    
139
      return -1;
140
    }
141

    
142
    context->counter++;
143
    if (context->counter == BOOTSTRAP_CYCLES) context->bootstrap = false;
144

    
145
    remote_cache = entries_undump(buff + sizeof(struct topo_header), len - sizeof(struct topo_header));
146
    if (h->type == NCAST_QUERY) {
147
      ncast_reply(context->tc, remote_cache, context->local_cache);
148
    }
149
    new = merge_caches(context->local_cache, remote_cache, context->cache_size, &dummy);
150
    cache_free(remote_cache);
151
    if (new != NULL) {
152
      cache_free(context->local_cache);
153
      context->local_cache = new;
154
    }
155
  }
156

    
157
  if (time_to_send(context)) {
158
    cache_update(context->local_cache);
159
    ncast_query(context->tc, context->local_cache);
160
  }
161

    
162
  return 0;
163
}
164

    
165
static const struct nodeID **ncast_get_neighbourhood(struct peersampler_context *context, int *n)
166
{
167
  static struct nodeID **r;
168

    
169
  r = realloc(r, context->cache_size * sizeof(struct nodeID *));
170
  if (r == NULL) {
171
    return NULL;
172
  }
173

    
174
  for (*n = 0; nodeid(context->local_cache, *n) && (*n < context->cache_size); (*n)++) {
175
    r[*n] = nodeid(context->local_cache, *n);
176
    //fprintf(stderr, "Checking table[%d]\n", *n);
177
  }
178

    
179
  return (const struct nodeID **)r;
180
}
181

    
182
static const void *ncast_get_metadata(struct peersampler_context *context, int *metadata_size)
183
{
184
  return get_metadata(context->local_cache, metadata_size);
185
}
186

    
187
static int ncast_grow_neighbourhood(struct peersampler_context *context, int n)
188
{
189
  context->cache_size += n;
190

    
191
  return context->cache_size;
192
}
193

    
194
static int ncast_shrink_neighbourhood(struct peersampler_context *context, int n)
195
{
196
  if (context->cache_size < n) {
197
    return -1;
198
  }
199
  context->cache_size -= n;
200

    
201
  return context->cache_size;
202
}
203

    
204
static int ncast_remove_neighbour(struct peersampler_context *context, struct nodeID *neighbour)
205
{
206
  return cache_del(context->local_cache, neighbour);
207
}
208

    
209
struct peersampler_iface ncast = {
210
  .init = ncast_init,
211
  .change_metadata = ncast_change_metadata,
212
  .add_neighbour = ncast_add_neighbour,
213
  .parse_data = ncast_parse_data,
214
  .get_neighbourhood = ncast_get_neighbourhood,
215
  .get_metadata = ncast_get_metadata,
216
  .grow_neighbourhood = ncast_grow_neighbourhood,
217
  .shrink_neighbourhood = ncast_shrink_neighbourhood,
218
  .remove_neighbour = ncast_remove_neighbour,
219
};