Statistics
| Branch: | Revision:

grapes / src / PeerSet / peerset_ops.c @ 82438fe3

History | View | Annotate | Download (2.89 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
#include "config.h"
19

    
20
#define DEFAULT_SIZE_INCREMENT 32
21

    
22
struct nodeID;
23

    
24
struct peerset *peerset_init(const char *config)
25
{
26
  struct peerset *p;
27
  struct tag *cfg_tags;
28
  int res;
29

    
30
  p = malloc(sizeof(struct peerset));
31
  if (p == NULL) {
32
    return NULL;
33
  }
34
  p->n_elements = 0;
35
  cfg_tags = config_parse(config);
36
  if (!cfg_tags) {
37
    free(p);
38
    return NULL;
39
  }
40
  res = config_value_int(cfg_tags, "size", &p->size);
41
  if (!res) {
42
    p->size = 0;
43
  }
44
  free(cfg_tags);
45
  if (p->size) {
46
    p->elements = malloc(p->size * sizeof(struct peer *));
47
  } else {
48
    p->elements = NULL;
49
  }
50

    
51
  return p;
52
}
53

    
54
int peerset_add_peer(struct peerset *h, struct nodeID *id)
55
{
56
  struct peer *e;
57
  if (peerset_check(h, id) >= 0) {
58
    return 0;
59
  }
60

    
61
  if (h->n_elements == h->size) {
62
    struct peer **res;
63

    
64
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(struct peer *));
65
    if (res == NULL) {
66
      return -1;
67
    }
68
    h->size += DEFAULT_SIZE_INCREMENT;
69
    h->elements = res;
70
  }
71
  e = malloc(sizeof(struct peer));
72
  h->elements[h->n_elements++] = e;
73
  e->id = nodeid_dup(id);
74
  gettimeofday(&e->creation_timestamp,NULL);
75
  e->bmap = chunkID_set_init("type=bitmap");
76
  timerclear(&e->bmap_timestamp);
77
  e->cb_size = 0;
78

    
79
  return h->n_elements;
80
}
81

    
82
void peerset_add_peers(struct peerset *h, struct nodeID **ids, int n)
83
{
84
  int i;
85

    
86
  for (i = 0; i < n; i++) {
87
    peerset_add_peer(h, ids[i]);
88
  }
89
}
90

    
91
int peerset_size(const struct peerset *h)
92
{
93
  return h->n_elements;
94
}
95

    
96
struct peer **peerset_get_peers(const struct peerset *h)
97
{
98
  return h->elements;
99
}
100

    
101
struct peer *peerset_get_peer(const struct peerset *h, const struct nodeID *id)
102
{
103
  int i = peerset_check(h,id);
104
  return (i<0) ? NULL : h->elements[i];
105
}
106

    
107
int peerset_remove_peer(struct peerset *h, const struct nodeID *id){
108
  int i = peerset_check(h,id);
109
  if (i >= 0) {
110
    struct peer *e = h->elements[i];
111
    nodeid_free(e->id);
112
    chunkID_set_free(e->bmap);
113
    memmove(e, e + 1, ((h->n_elements--) - (i+1)) * sizeof(struct peer *));
114
    free(e);
115

    
116
    return i;
117
  }
118
  return -1;
119
}
120

    
121
int peerset_check(const struct peerset *h, const struct nodeID *id)
122
{
123
  int i;
124

    
125
  for (i = 0; i < h->n_elements; i++) {
126
    if (nodeid_equal(h->elements[i]->id, id)) {
127
      return i;
128
    }
129
  }
130

    
131
  return -1;
132
}
133

    
134
void peerset_clear(struct peerset *h, int size)
135
{
136
  int i;
137

    
138
  for (i = 0; i < h->n_elements; i++) {
139
    struct peer *e = h->elements[i];
140
    nodeid_free(e->id);
141
    chunkID_set_free(e->bmap);
142
    free(e);
143
  }
144

    
145
  h->n_elements = 0;
146
  h->size = size;
147
  h->elements = realloc(h->elements, size * sizeof(struct peer *));
148
  if (h->elements == NULL) {
149
    h->size = 0;
150
  }
151
}