Statistics
| Branch: | Revision:

grapes / src / PeerSet / peerset_ops.c @ 67700aad

History | View | Annotate | Download (4.8 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 <stdio.h>
10
#include <stdint.h>
11
#include <string.h>
12
#include <limits.h>
13

    
14
#include "peerset_private.h"
15
#include "peer.h"
16
#include "peerset.h"
17
#include "chunkidset.h"
18
#include "net_helper.h"
19
#include "grapes_config.h"
20

    
21
#define DEFAULT_SIZE_INCREMENT 32
22

    
23
static int nodeid_peer_cmp(const void *id, const void *p)
24
{
25
  const struct peer *peer = *(struct peer *const *)p;
26

    
27
  if(id && p)
28
    return nodeid_cmp( (const struct nodeID *) id, peer->id);
29
  else
30
  {
31
    //fprintf(stderr,"[DEBUG] wrong peer or id\n");
32
    return 0;
33
  }
34
}
35

    
36
static int peerset_check_insert_pos(const struct peerset *h, const struct nodeID *id)
37
{
38
  int a, b, c, r;
39

    
40
  if (! h->n_elements) {
41
    return 0;
42
  }
43

    
44
  a = 0;
45
  b = c = h->n_elements - 1;
46

    
47
  while ((r = nodeid_peer_cmp(id, &h->elements[b])) != 0) {
48
    if (r > 0) {
49
      if (b == c) {
50
        return b + 1;
51
      } else {
52
        a = b + 1;
53
      }
54
    } else {
55
      if (b == a) {
56
        return b;
57
      } else {
58
        c = b;
59
      }
60
    }
61
    b = (a + c) / 2;
62
  }
63

    
64
  return -1;
65
}
66

    
67

    
68
struct peerset *peerset_init(const char *config)
69
{
70
  struct peerset *p;
71
  struct tag *cfg_tags;
72
  int res;
73

    
74
  p = malloc(sizeof(struct peerset));
75
  if (p == NULL) {
76
    return NULL;
77
  }
78
  p->n_elements = 0;
79
  cfg_tags = grapes_config_parse(config);
80
  if (!cfg_tags) {
81
    free(p);
82
    return NULL;
83
  }
84
  res = grapes_config_value_int(cfg_tags, "size", &p->size);
85
  if (!res) {
86
    p->size = 0;
87
  }
88
  free(cfg_tags);
89
  if (p->size) {
90
    p->elements = malloc(p->size * sizeof(struct peer *));
91
  } else {
92
    p->elements = NULL;
93
  }
94

    
95
  return p;
96
}
97

    
98
void peerset_destroy(struct peerset **h)
99
{
100
        peerset_clear(*h,0);
101
        free(*h);
102
        *h = NULL;
103
}
104

    
105
int peerset_push_peer(struct peerset *h,const  struct peer *e)
106
{
107
  int pos;
108

    
109
  pos = peerset_check_insert_pos(h, e->id);
110
  if (pos < 0){
111
    return 0;
112
  }
113

    
114
  if (h->n_elements == h->size) {
115
    struct peer **res;
116

    
117
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(struct peer *));
118
    if (res == NULL) {
119
      return -1;
120
    }
121
    h->size += DEFAULT_SIZE_INCREMENT;
122
    h->elements = res;
123
  }
124

    
125
  memmove(&h->elements[pos + 1], &h->elements[pos] , ((h->n_elements++) - pos) * sizeof(struct peer *));
126

    
127
  h->elements[pos] = e;;
128

    
129
  return h->n_elements;
130
}
131

    
132
int peerset_add_peer(struct peerset *h,const  struct nodeID *id)
133
{
134
  struct peer *e;
135
  int pos;
136

    
137
  pos = peerset_check_insert_pos(h, id);
138
  if (pos < 0){
139
    return 0;
140
  }
141

    
142
  if (h->n_elements == h->size) {
143
    struct peer **res;
144

    
145
    res = realloc(h->elements, (h->size + DEFAULT_SIZE_INCREMENT) * sizeof(struct peer *));
146
    if (res == NULL) {
147
      return -1;
148
    }
149
    h->size += DEFAULT_SIZE_INCREMENT;
150
    h->elements = res;
151
  }
152

    
153
  memmove(&h->elements[pos + 1], &h->elements[pos] , ((h->n_elements++) - pos) * sizeof(struct peer *));
154

    
155
  e = malloc(sizeof(struct peer));
156
  h->elements[pos] = e;;
157
  e->id = nodeid_dup(id);
158
  gettimeofday(&e->creation_timestamp,NULL);
159
  e->bmap = chunkID_set_init("type=bitmap");
160
  timerclear(&e->bmap_timestamp);
161
  e->cb_size = 0;
162
  e->capacity = 0;
163

    
164
  return h->n_elements;
165
}
166

    
167
void peerset_add_peers(struct peerset *h, struct nodeID **ids, int n)
168
{
169
  int i;
170

    
171
  for (i = 0; i < n; i++) {
172
    peerset_add_peer(h, ids[i]);
173
  }
174
}
175

    
176
int peerset_size(const struct peerset *h)
177
{
178
  return h->n_elements;
179
}
180

    
181
struct peer **peerset_get_peers(const struct peerset *h)
182
{
183
  return h->elements;
184
}
185

    
186
struct peer *peerset_get_peer(const struct peerset *h, const struct nodeID *id)
187
{
188
  int i = peerset_check(h,id);
189
  return (i<0) ? NULL : h->elements[i];
190
}
191

    
192
struct peer *peerset_pop_peer(struct peerset *h, const struct nodeID *id){
193
  int i = peerset_check(h,id);
194
  if (i >= 0) {
195
    struct peer *e = h->elements[i];
196
    memmove(&h->elements[i], &h->elements[i+1], ((h->n_elements--) - (i+1)) * sizeof(struct peer *));
197

    
198
    return e;
199
  }
200
  return NULL;
201
}
202

    
203
int peerset_remove_peer(struct peerset *h, const struct nodeID *id){
204
  int i = peerset_check(h,id);
205
  if (i >= 0) {
206
    struct peer *e = h->elements[i];
207
    nodeid_free(e->id);
208
    chunkID_set_free(e->bmap);
209
    memmove(&h->elements[i], &h->elements[i+1], ((h->n_elements--) - (i+1)) * sizeof(struct peer *));
210
    free(e);
211

    
212
    return i;
213
  }
214
  return -1;
215
}
216

    
217
int peerset_check(const struct peerset *h, const struct nodeID *id)
218
{
219
  struct peer **p;
220

    
221
  p = bsearch(id, h->elements, (size_t) h->n_elements, sizeof(h->elements[0]), nodeid_peer_cmp);
222

    
223
  return p ? p - h->elements : -1;
224
}
225

    
226
void peerset_clear(struct peerset *h, int size)
227
{
228
  int i;
229

    
230
  for (i = 0; i < h->n_elements; i++) {
231
    struct peer *e = h->elements[i];
232
    nodeid_free(e->id);
233
    chunkID_set_free(e->bmap);
234
    free(e);
235
  }
236

    
237
  h->n_elements = 0;
238
  h->size = size;
239
  h->elements = realloc(h->elements, size * sizeof(struct peer *));
240
  if (h->elements == NULL) {
241
    h->size = 0;
242
  }
243
}