Statistics
| Branch: | Revision:

grapes / som / PeerSet / peerset_ops.c @ a4f059a8

History | View | Annotate | Download (1.79 KB)

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