Statistics
| Branch: | Revision:

grapes / som / PeerSet / peerset_ops.c @ 539f4a1a

History | View | Annotate | Download (2.19 KB)

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