Statistics
| Branch: | Revision:

grapes / som / PeerSet / peerset_ops.c @ 8ab58ec7

History | View | Annotate | Download (2.35 KB)

1
/*
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
#include <stdlib.h>
9
#include <stdint.h>
10
#include <string.h>
11
#include <limits.h>
12

    
13
#include "peerset_private.h"
14
#include "peer.h"
15
#include "peerset.h"
16
#include "chunkidset.h"
17
#include "net_helper.h"
18

    
19
#define DEFAULT_SIZE_INCREMENT 32
20

    
21
struct nodeID;
22

    
23
struct peerset *peerset_init(int size)
24
{
25
  struct peerset *p;
26

    
27
  p = malloc(sizeof(struct peerset));
28
  if (p == NULL) {
29
    return NULL;
30
  }
31
  p->n_elements = 0;
32
  p->size = size;
33
  if (p->size) {
34
    p->elements = malloc(p->size * sizeof(struct peer));
35
  } else {
36
    p->elements = NULL;
37
  }
38

    
39
  return p;
40
}
41

    
42
int peerset_add_peer(struct peerset *h, const struct nodeID *id)
43
{
44
  struct peer *e;
45
  if (peerset_check(h, id) >= 0) {
46
    return 0;
47
  }
48

    
49
  if (h->n_elements == h->size) {
50
    int *res;
51

    
52
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(struct peer));
53
    if (res == NULL) {
54
      return -1;
55
    }
56
    h->size += DEFAULT_SIZE_INCREMENT;
57
    h->elements = (struct peer*) res;
58
  }
59
  e = &(h->elements[h->n_elements++]);
60
  e->id = nodeid_dup(id);
61
  e->bmap = chunkID_set_init(0);
62
  timerclear(&e->bmap_timestamp);
63
  e->cb_size = INT_MAX;
64

    
65
  return h->n_elements;
66
}
67

    
68
void peerset_add_peers(struct peerset *h, const struct nodeID **ids, int n)
69
{
70
  int i;
71

    
72
  for (i = 0; i < n; i++) {
73
    peerset_add_peer(h,ids[i]);
74
  }
75
}
76

    
77
int peerset_size(const struct peerset *h)
78
{
79
  return h->n_elements;
80
}
81

    
82
struct peer* peerset_get_peers(const struct peerset *h)
83
{
84
  return h->elements;
85
}
86

    
87
struct peer *peerset_get_peer(const struct peerset *h, const struct nodeID *id)
88
{
89
  int i = peerset_check(h,id);
90
  return (i<0) ? NULL : &(h->elements[i]);
91
}
92

    
93
int peerset_remove_peer(struct peerset *h, const struct nodeID *id){
94
  int i = peerset_check(h,id);
95
  if (i >= 0) {
96
    memmove(&h->elements[i], &h->elements[i+1], ((h->n_elements--) - (i+1)) * sizeof(struct peer));
97
    return i;
98
  }
99
  return -1;
100
}
101

    
102
int peerset_check(const struct peerset *h, const struct nodeID *id)
103
{
104
  int i;
105

    
106
  for (i = 0; i < h->n_elements; i++) {
107
    if (nodeid_equal(h->elements[i].id, id)) {
108
      return i;
109
    }
110
  }
111

    
112
  return -1;
113
}
114

    
115
void peerset_clear(struct peerset *h, int size)
116
{
117
  h->n_elements = 0;
118
  h->size = size;
119
  h->elements = realloc(h->elements, size * sizeof(struct peer));
120
  if (h->elements == NULL) {
121
    h->size = 0;
122
  }
123
}