Revision f3ab0d6d src/TopologyManager/ncast.c

View differences:

src/TopologyManager/ncast.c
15 15
#include "net_helper.h"
16 16
#include "peersampler_iface.h"
17 17
#include "topocache.h"
18
#include "topo_proto.h"
19 18
#include "ncast_proto.h"
20 19
#include "proto.h"
21 20
#include "config.h"
......
25 24
#define DEFAULT_CACHE_SIZE 10
26 25
#define DEFAULT_MAX_TIMESTAMP 5
27 26

  
28
static uint64_t currtime;
29
static int cache_size;
30
static struct peer_cache *local_cache;
31
static bool bootstrap = true;
32
static int bootstrap_period = 2000000;
33
static int period = 10000000;
34
static int counter;
35

  
36
//TODO: context support not introduced
37
struct peersampler_context{};
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
};
38 37

  
39 38
static uint64_t gettime(void)
40 39
{
......
50 49
  con = (struct peersampler_context*) calloc(1,sizeof(struct peersampler_context));
51 50

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

  
55 57
  return con;
56 58
}
57 59

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

  
64 66
    return 1;
65 67
  }
......
73 75
static struct peersampler_context* ncast_init(struct nodeID *myID, void *metadata, int metadata_size, const char *config)
74 76
{
75 77
  struct tag *cfg_tags;
76
  struct peersampler_context *con;
78
  struct peersampler_context *context;
77 79
  int res, max_timestamp;
78 80

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

  
82 84
  cfg_tags = config_parse(config);
83
  res = config_value_int(cfg_tags, "cache_size", &cache_size);
85
  res = config_value_int(cfg_tags, "cache_size", &context->cache_size);
84 86
  if (!res) {
85
    cache_size = DEFAULT_CACHE_SIZE;
87
    context->cache_size = DEFAULT_CACHE_SIZE;
86 88
  }
87 89
  res = config_value_int(cfg_tags, "max_timestamp", &max_timestamp);
88 90
  if (!res) {
......
90 92
  }
91 93
  free(cfg_tags);
92 94
  
93
  local_cache = cache_init(cache_size, metadata_size, max_timestamp);
94
  if (local_cache == NULL) {
95
    free(con);
95
  context->local_cache = cache_init(context->cache_size, metadata_size, max_timestamp);
96
  if (context->local_cache == NULL) {
97
    free(context);
96 98
    return NULL;
97 99
  }
98
  topo_proto_init(myID, metadata, metadata_size);
99
  currtime = gettime();
100
  bootstrap = true;
101 100

  
102
  return con;
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;
103 109
}
104 110

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

  
......
113 119

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

  
122 128
static int ncast_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
......
133 139
      return -1;
134 140
    }
135 141

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

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

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

  
156 162
  return 0;
......
160 166
{
161 167
  static struct nodeID **r;
162 168

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

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

  
......
175 181

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

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

  
185
  return cache_size;
191
  return context->cache_size;
186 192
}
187 193

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

  
195
  return cache_size;
201
  return context->cache_size;
196 202
}
197 203

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

  
203 209
struct peersampler_iface ncast = {

Also available in: Unified diff