Statistics
| Branch: | Revision:

grapes / som / TopologyManager / topocache.c @ 661d190d

History | View | Annotate | Download (8.75 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, struct nodeID *neighbour, const void *meta, int meta_size, ranking_function f, struct peer_cache *target)
82
{
83
  int i, msize, 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(get_metadata(target, &msize), 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
int fill_cache_entry(struct cache_entry *c, const struct nodeID *s)
205
{
206
  c->id = nodeid_dup(s);
207
  c->timestamp = 1;
208
#warning Timestamps are probably wrong...
209
  return 1;
210
}
211

    
212
int in_cache(const struct peer_cache *c, const struct cache_entry *elem)
213
{
214
  int i;
215

    
216
  for (i = 0; i < c->current_size; i++) {
217
    if (nodeid_equal(c->entries[i].id, elem->id)) {
218
      return 1;
219
    }
220
  }
221

    
222
  return 0;
223
}
224

    
225
struct nodeID *rand_peer(struct peer_cache *c)
226
{
227
  int j;
228

    
229
  if (c->current_size == 0) {
230
    return NULL;
231
  }
232
  j = ((double)rand() / (double)RAND_MAX) * c->current_size;
233

    
234
  return c->entries[j].id;
235
}
236

    
237
struct peer_cache *entries_undump(const uint8_t *buff, int size)
238
{
239
  struct peer_cache *res;
240
  int i = 0;
241
  const uint8_t *p = buff;
242
  uint8_t *meta;
243
  int cache_size, metadata_size;
244

    
245
  cache_size = int_rcpy(buff);
246
  metadata_size = int_rcpy(buff + 4);
247
  p = buff + 8;
248
  res = cache_init(cache_size, metadata_size);
249
  meta = res->metadata;
250
  while (p - buff < size) {
251
    int len;
252

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

    
266
  return res;
267
}
268

    
269
int cache_header_dump(uint8_t *b, const struct peer_cache *c)
270
{
271
  int_cpy(b, c->cache_size);
272
  int_cpy(b + 4, c->metadata_size);
273

    
274
  return 8;
275
}
276

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

    
289
  return res;
290
}
291

    
292
struct peer_cache *merge_caches_ranked(struct peer_cache *c1, struct peer_cache *c2, int newsize, int *source, ranking_function rank, struct peer_cache *me)
293
{
294
  int n1, n2;
295
  struct peer_cache *new_cache;
296
  uint8_t *meta;
297

    
298
  new_cache = cache_init(newsize, c1->metadata_size);
299
  if (new_cache == NULL) {
300
    return NULL;
301
  }
302

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

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

    
368
  return new_cache;
369
}
370

    
371
struct peer_cache *merge_caches(struct peer_cache *c1, struct peer_cache *c2, int newsize, int *source)
372
{
373
  return merge_caches_ranked(c1, c2, newsize, source, NULL, NULL);
374
}