Statistics
| Branch: | Revision:

grapes / som / TopologyManager / topocache.c @ df98cd3a

History | View | Annotate | Download (8.63 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <arpa/inet.h>
8
#include <stdint.h>
9
#include <stdlib.h>
10
#include <string.h>
11

    
12
#include <stdio.h>
13

    
14
#include "net_helper.h"
15
#include "topocache.h"
16

    
17
#define MAX_TIMESTAMP 5
18
struct cache_entry {
19
  struct nodeID *id;
20
  uint32_t timestamp;
21
};
22

    
23
struct peer_cache {
24
  struct cache_entry *entries;
25
  int cache_size;
26
  int current_size;
27
  int metadata_size;
28
  uint8_t *metadata; 
29
};
30

    
31
static inline void int_cpy(uint8_t *p, int v)
32
{
33
  int tmp;
34

    
35
  tmp = htonl(v);
36
  memcpy(p, &tmp, 4);
37
}
38

    
39
static inline int int_rcpy(const uint8_t *p)
40
{
41
  int tmp;
42
  
43
  memcpy(&tmp, p, 4);
44
  tmp = ntohl(tmp);
45

    
46
  return tmp;
47
}
48

    
49
struct nodeID *nodeid(const struct peer_cache *c, int i)
50
{
51
  if (i < c->current_size) {
52
    return c->entries[i].id;
53
  }
54

    
55
  return NULL;
56
}
57

    
58
const void *get_metadata(const struct peer_cache *c, int *size)
59
{
60
  *size = c->metadata_size;
61
  return c->metadata;
62
}
63

    
64
int cache_metadata_update(struct peer_cache *c, struct nodeID *p, const void *meta, int meta_size)
65
{
66
  int i;
67

    
68
  if (!meta_size || meta_size != c->metadata_size) {
69
    return -3;
70
  }
71
  for (i = 0; i < c->current_size; i++) {
72
    if (nodeid_equal(c->entries[i].id, p)) {
73
      memcpy(c->metadata + i * meta_size, meta, meta_size);
74
      return 1;
75
    }
76
  }
77

    
78
  return 0;
79
}
80

    
81
int cache_add_ranked(struct peer_cache *c, const struct nodeID *neighbour, const void *meta, int meta_size, ranking_function f, const void *tmeta)
82
{
83
  int i, pos = 0;
84

    
85
  if (meta_size && meta_size != c->metadata_size) {
86
    return -3;
87
  }
88
  for (i = 0; i < c->current_size; i++) {
89
    if (nodeid_equal(c->entries[i].id, neighbour)) {
90
      return -1;
91
    }
92
    if ((f != NULL) && f(tmeta, meta, c->metadata+(c->metadata_size * i)) == 2) {
93
      pos++;
94
    }
95
  }
96
  if (c->current_size == c->cache_size) {
97
    return -2;
98
  }
99
  if (meta_size) {
100
    memmove(c->metadata + (pos + 1) * meta_size, c->metadata + pos * meta_size, (c->current_size - pos) * meta_size);
101
    memcpy(c->metadata + pos * meta_size, meta, meta_size);
102
  }
103
  for (i = pos; i < c->current_size; i++) {
104
    c->entries[i + 1] = c->entries[i];
105
  }
106
  c->entries[pos].id = nodeid_dup(neighbour);
107
  c->entries[pos].timestamp = 1;
108
  c->current_size++;
109

    
110
  return c->current_size;
111
}
112

    
113
int cache_add(struct peer_cache *c, struct nodeID *neighbour, const void *meta, int meta_size)
114
{
115
  return cache_add_ranked(c, neighbour, meta, meta_size, NULL, NULL);
116
}
117

    
118
int cache_del(struct peer_cache *c, struct nodeID *neighbour)
119
{
120
  int i;
121
  int found = 0;
122

    
123
  for (i = 0; i < c->current_size; i++) {
124
    if (nodeid_equal(c->entries[i].id, neighbour)) {
125
      nodeid_free(c->entries[i].id);
126
      c->current_size--;
127
      found = 1;
128
      if (c->metadata_size && (i < c->current_size)) {
129
        memmove(c->metadata + c->metadata_size * i,
130
                c->metadata + c->metadata_size * (found + 1),
131
                c->metadata_size * (c->current_size - i));
132
      }
133
    }
134
    if (found && (i < c->current_size)) {
135
      c->entries[i] = c->entries[i + 1];
136
    }
137
  }
138

    
139
  return c->current_size;
140
}
141

    
142
void cache_update(struct peer_cache *c)
143
{
144
  int i;
145
  
146
  for (i = 0; i < c->current_size; i++) {
147
    if (c->entries[i].timestamp == MAX_TIMESTAMP) {
148
      c->current_size = i;        /* The cache is ordered by timestamp...
149
                                   all the other entries wiil be older than
150
                                   this one, so remove all of them
151
                                */
152
    } else {
153
      c->entries[i].timestamp++;
154
    }
155
  }
156
}
157

    
158
struct peer_cache *cache_init(int n, int metadata_size)
159
{
160
  struct peer_cache *res;
161

    
162
  res = malloc(sizeof(struct peer_cache));
163
  if (res == NULL) {
164
    return NULL;
165
  }
166
  res->cache_size = n;
167
  res->current_size = 0;
168
  res->entries = malloc(sizeof(struct cache_entry) * n);
169
  if (res->entries == NULL) {
170
    free(res);
171

    
172
    return NULL;
173
  }
174
  
175
  memset(res->entries, 0, sizeof(struct cache_entry) * n);
176
  if (metadata_size) {
177
    res->metadata = malloc(metadata_size * n);
178
  } else {
179
    res->metadata = NULL;
180
  }
181

    
182
  if (res->metadata) {
183
    res->metadata_size = metadata_size;
184
    memset(res->metadata, 0, metadata_size * n);
185
  } else {
186
    res->metadata_size = 0;
187
  }
188

    
189
  return res;
190
}
191

    
192
void cache_free(struct peer_cache *c)
193
{
194
  int i;
195

    
196
  for (i = 0; i < c->current_size; i++) {
197
    nodeid_free(c->entries[i].id);
198
  }
199
  free(c->entries);
200
  free(c->metadata);
201
  free(c);
202
}
203

    
204
static int in_cache(const struct peer_cache *c, const struct cache_entry *elem)
205
{
206
  int i;
207

    
208
  for (i = 0; i < c->current_size; i++) {
209
    if (nodeid_equal(c->entries[i].id, elem->id)) {
210
      return 1;
211
    }
212
  }
213

    
214
  return 0;
215
}
216

    
217
struct nodeID *rand_peer(struct peer_cache *c, void **meta)
218
{
219
  int j;
220

    
221
  if (c->current_size == 0) {
222
    return NULL;
223
  }
224
  j = ((double)rand() / (double)RAND_MAX) * c->current_size;
225

    
226
  if (*meta) {
227
    *meta = c->metadata + (j * c->metadata_size);
228
  }
229

    
230
  return c->entries[j].id;
231
}
232

    
233
struct peer_cache *entries_undump(const uint8_t *buff, int size)
234
{
235
  struct peer_cache *res;
236
  int i = 0;
237
  const uint8_t *p = buff;
238
  uint8_t *meta;
239
  int cache_size, metadata_size;
240

    
241
  cache_size = int_rcpy(buff);
242
  metadata_size = int_rcpy(buff + 4);
243
  p = buff + 8;
244
  res = cache_init(cache_size, metadata_size);
245
  meta = res->metadata;
246
  while (p - buff < size) {
247
    int len;
248

    
249
    res->entries[i].timestamp = int_rcpy(p);
250
    p += sizeof(uint32_t);
251
    res->entries[i++].id = nodeid_undump(p, &len);
252
    p += len;
253
    if (metadata_size) {
254
      memcpy(meta, p, metadata_size);
255
      p += metadata_size;
256
      meta += metadata_size;
257
    }
258
  }
259
  res->current_size = i;
260
if (p - buff != size) { fprintf(stderr, "Waz!! %d != %d\n", p - buff, size); exit(-1);}
261

    
262
  return res;
263
}
264

    
265
int cache_header_dump(uint8_t *b, const struct peer_cache *c)
266
{
267
  int_cpy(b, c->cache_size);
268
  int_cpy(b + 4, c->metadata_size);
269

    
270
  return 8;
271
}
272

    
273
int entry_dump(uint8_t *b, struct peer_cache *c, int i)
274
{
275
  int res;
276
  
277
  int_cpy(b, c->entries[i].timestamp);
278
  res = 4;
279
  res += nodeid_dump(b + res, c->entries[i].id);
280
  if (c->metadata_size) {
281
    memcpy(b + res, c->metadata + c->metadata_size * i, c->metadata_size);
282
    res += c->metadata_size;
283
  }
284

    
285
  return res;
286
}
287

    
288
struct peer_cache *merge_caches_ranked(struct peer_cache *c1, struct peer_cache *c2, int newsize, int *source, ranking_function rank, void *mymetadata)
289
{
290
  int n1, n2;
291
  struct peer_cache *new_cache;
292
  uint8_t *meta;
293

    
294
  new_cache = cache_init(newsize, c1->metadata_size);
295
  if (new_cache == NULL) {
296
    return NULL;
297
  }
298

    
299
  meta = new_cache->metadata;
300
  *source = 0;
301
  for (n1 = 0, n2 = 0; new_cache->current_size < new_cache->cache_size;) {
302
    if ((n1 == c1->current_size) && (n2 == c2->current_size)) {
303
      return new_cache;
304
    }
305
    if (n1 == c1->current_size) {
306
      if (!in_cache(new_cache, &c2->entries[n2])) {
307
        if (new_cache->metadata_size) {
308
          memcpy(meta, c2->metadata + n2 * c2->metadata_size, c2->metadata_size);
309
          meta += new_cache->metadata_size;
310
        }
311
        new_cache->entries[new_cache->current_size++] = c2->entries[n2];
312
        c2->entries[n2].id = NULL;
313
        *source |= 0x02;
314
      }
315
      n2++;
316
    } else if (n2 == c2->current_size) {
317
      if (!in_cache(new_cache, &c1->entries[n1])) {
318
        if (new_cache->metadata_size) {
319
          memcpy(meta, c1->metadata + n1 * c1->metadata_size, c1->metadata_size);
320
          meta += new_cache->metadata_size;
321
        }
322
        new_cache->entries[new_cache->current_size++] = c1->entries[n1];
323
        c1->entries[n1].id = NULL;
324
        *source |= 0x01;
325
      }
326
      n1++;
327
    } else {
328
      int nowFirst;
329

    
330
      nowFirst = 0;
331
      if (rank) {
332
        nowFirst = rank(mymetadata, c1->metadata + n1 * c1->metadata_size,
333
                        c2->metadata + n2 * c2->metadata_size);
334
      }
335
      if (nowFirst == 0) {
336
        nowFirst = c2->entries[n2].timestamp > c1->entries[n1].timestamp ? 1 : 2;
337
      }
338
      if (nowFirst == 1) {
339
        if (!in_cache(new_cache, &c1->entries[n1])) {
340
          if (new_cache->metadata_size) {
341
            memcpy(meta, c1->metadata + n1 * c1->metadata_size, c1->metadata_size);
342
            meta += new_cache->metadata_size;
343
          }
344
          new_cache->entries[new_cache->current_size++] = c1->entries[n1];
345
          c1->entries[n1].id = NULL;
346
          *source |= 0x01;
347
        }
348
        n1++;
349
      } else {
350
        if (!in_cache(new_cache, &c2->entries[n2])) {
351
          if (new_cache->metadata_size) {
352
            memcpy(meta, c2->metadata + n2 * c2->metadata_size, c2->metadata_size);
353
            meta += new_cache->metadata_size;
354
          }
355
          new_cache->entries[new_cache->current_size++] = c2->entries[n2];
356
          c2->entries[n2].id = NULL;
357
          *source |= 0x02;
358
        }
359
        n2++;
360
      }
361
    }
362
  }
363

    
364
  return new_cache;
365
}
366

    
367
struct peer_cache *merge_caches(struct peer_cache *c1, struct peer_cache *c2, int newsize, int *source)
368
{
369
  return merge_caches_ranked(c1, c2, newsize, source, NULL, NULL);
370
}