Statistics
| Branch: | Revision:

grapes / som / PeerSet / peerset_ops.c @ d1de8494

History | View | Annotate | Download (2.38 KB)

1 8ab58ec7 Luca Abeni
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see lgpl-2.1.txt
6
 */
7
8 c139c4f2 CsabaKiraly
#include <stdlib.h>
9
#include <stdint.h>
10 9e6eebab Csaba Kiraly
#include <string.h>
11 7180e574 CsabaKiraly
#include <limits.h>
12 c139c4f2 CsabaKiraly
13 47f2affa CsabaKiraly
#include "peerset_private.h"
14
#include "peer.h"
15
#include "peerset.h"
16 fb76785c CsabaKiraly
#include "chunkidset.h"
17 a4f059a8 CsabaKiraly
#include "net_helper.h"
18 c139c4f2 CsabaKiraly
19
#define DEFAULT_SIZE_INCREMENT 32
20
21 47f2affa CsabaKiraly
struct nodeID;
22
23
struct peerset *peerset_init(int size)
24 c139c4f2 CsabaKiraly
{
25 47f2affa CsabaKiraly
  struct peerset *p;
26 c139c4f2 CsabaKiraly
27 47f2affa CsabaKiraly
  p = malloc(sizeof(struct peerset));
28 c139c4f2 CsabaKiraly
  if (p == NULL) {
29
    return NULL;
30
  }
31
  p->n_elements = 0;
32
  p->size = size;
33
  if (p->size) {
34 47f2affa CsabaKiraly
    p->elements = malloc(p->size * sizeof(struct peer));
35 c139c4f2 CsabaKiraly
  } else {
36
    p->elements = NULL;
37
  }
38
39
  return p;
40
}
41
42 d1de8494 Luca Abeni
int peerset_add_peer(struct peerset *h, struct nodeID *id)
43 c139c4f2 CsabaKiraly
{
44 fb76785c CsabaKiraly
  struct peer *e;
45 47f2affa CsabaKiraly
  if (peerset_check(h, id) >= 0) {
46 c139c4f2 CsabaKiraly
    return 0;
47
  }
48
49
  if (h->n_elements == h->size) {
50
    int *res;
51
52 47f2affa CsabaKiraly
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(struct peer));
53 c139c4f2 CsabaKiraly
    if (res == NULL) {
54
      return -1;
55
    }
56
    h->size += DEFAULT_SIZE_INCREMENT;
57 47f2affa CsabaKiraly
    h->elements = (struct peer*) res;
58 c139c4f2 CsabaKiraly
  }
59 fb76785c CsabaKiraly
  e = &(h->elements[h->n_elements++]);
60
  e->id = nodeid_dup(id);
61 73ed9608 Csaba Kiraly
  gettimeofday(&e->creation_timestamp,NULL);
62 fb76785c CsabaKiraly
  e->bmap = chunkID_set_init(0);
63 539f4a1a CsabaKiraly
  timerclear(&e->bmap_timestamp);
64 7180e574 CsabaKiraly
  e->cb_size = INT_MAX;
65 c139c4f2 CsabaKiraly
66
  return h->n_elements;
67
}
68
69 d1de8494 Luca Abeni
void peerset_add_peers(struct peerset *h, struct nodeID **ids, int n)
70 13c9731e CsabaKiraly
{
71
  int i;
72
73
  for (i = 0; i < n; i++) {
74 d1de8494 Luca Abeni
    peerset_add_peer(h, ids[i]);
75 13c9731e CsabaKiraly
  }
76
}
77
78 47f2affa CsabaKiraly
int peerset_size(const struct peerset *h)
79 c139c4f2 CsabaKiraly
{
80
  return h->n_elements;
81
}
82
83 47f2affa CsabaKiraly
struct peer* peerset_get_peers(const struct peerset *h)
84 c139c4f2 CsabaKiraly
{
85 47f2affa CsabaKiraly
  return h->elements;
86
}
87 c139c4f2 CsabaKiraly
88 060267cd Luca Abeni
struct peer *peerset_get_peer(const struct peerset *h, const struct nodeID *id)
89 47f2affa CsabaKiraly
{
90
  int i = peerset_check(h,id);
91 c642734a CsabaKiraly
  return (i<0) ? NULL : &(h->elements[i]);
92 c139c4f2 CsabaKiraly
}
93
94 9e6eebab Csaba Kiraly
int peerset_remove_peer(struct peerset *h, const struct nodeID *id){
95
  int i = peerset_check(h,id);
96
  if (i >= 0) {
97
    memmove(&h->elements[i], &h->elements[i+1], ((h->n_elements--) - (i+1)) * sizeof(struct peer));
98
    return i;
99
  }
100
  return -1;
101
}
102
103 c552e22d CsabaKiraly
int peerset_check(const struct peerset *h, const struct nodeID *id)
104 c139c4f2 CsabaKiraly
{
105
  int i;
106
107
  for (i = 0; i < h->n_elements; i++) {
108 a4f059a8 CsabaKiraly
    if (nodeid_equal(h->elements[i].id, id)) {
109 c139c4f2 CsabaKiraly
      return i;
110
    }
111
  }
112
113
  return -1;
114
}
115
116 47f2affa CsabaKiraly
void peerset_clear(struct peerset *h, int size)
117 c139c4f2 CsabaKiraly
{
118
  h->n_elements = 0;
119
  h->size = size;
120 47f2affa CsabaKiraly
  h->elements = realloc(h->elements, size * sizeof(struct peer));
121 c139c4f2 CsabaKiraly
  if (h->elements == NULL) {
122
    h->size = 0;
123
  }
124
}