Statistics
| Branch: | Revision:

grapes / src / PeerSampler / dummy.c @ 10ddaca7

History | View | Annotate | Download (2.93 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <sys/time.h>
8
#include <stdint.h>
9
#include <stdlib.h>
10
#include <stdio.h>
11

    
12

    
13
#include "net_helper.h"
14
#include "peersampler_iface.h"
15

    
16
#define MAX_PEERS 5000
17
static struct nodeID *table[MAX_PEERS];
18

    
19
//TODO: context support not implemented
20
struct peersampler_context{
21
  const struct nodeID **r;
22
};
23

    
24
static struct peersampler_context* dummy_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config)
25
{
26
  FILE *f;
27
  int i = 0;
28

    
29
  f = fopen("peers.txt", "r");
30
  while(!feof(f)) {
31
    int res;
32
    char addr[32];
33
    int port;
34

    
35
    res = fscanf(f, "%s %d\n", addr, &port);
36
    if ((res == 2) && (i < MAX_PEERS - 1)) {
37
fprintf(stderr, "Creating table[%d]\n", i);
38
      table[i++] = create_node(addr, port);
39
    }
40
  }
41
  table[i] = NULL;
42

    
43
  //return i;
44
  //TODO: Returning the context may break some tests
45
  return NULL;
46
}
47

    
48
static int dummy_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size)
49
{
50
  /* Metadata not supported: fail! */
51
  return -1;
52
}
53

    
54
static int dummy_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size)
55
{
56
  int i;
57

    
58
  for (i = 0; table[i] != NULL; i++);
59
  table[i++] = neighbour;
60
  table[i] = NULL;
61

    
62
  return i;
63
}
64

    
65
static int dummy_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
66
{
67
  /* FAKE! */
68
  return 0;
69
}
70

    
71
static const struct nodeID *const *dummy_get_neighbourhood(struct peersampler_context *context, int *n)
72
{
73
  if (context->r == NULL) {
74
    context->r = malloc(MAX_PEERS * sizeof(struct nodeID *));
75
    if (context->r == NULL) {
76
      return NULL;
77
    }
78
  }
79

    
80
  for (*n = 0; table[*n] != NULL; (*n)++) {
81
    context->r[*n] = table[*n];
82
fprintf(stderr, "Checking table[%d]\n", *n);
83
  }
84
  return context->r;
85
}
86

    
87
static const void *dummy_get_metadata(struct peersampler_context *context, int *metadata_size)
88
{
89
  /* Metadata not supported: fail! */
90
  *metadata_size = -1;
91

    
92
  return NULL;
93
}
94

    
95
static int dummy_grow_neighbourhood(struct peersampler_context *context, int n)
96
{
97
  return -1;
98
}
99

    
100
static int dummy_shrink_neighbourhood(struct peersampler_context *context, int n)
101
{
102
  return -1;
103
}
104

    
105
static int dummy_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour)
106
{
107
  return -1;
108
}
109

    
110
void dummy_destroy(struct peersampler_context **context)
111
{
112
        if (context && *context)
113
        {
114
                if((*context)->r)
115
                        free((*context)->r);
116
                free(*context);
117
                *context = NULL;
118
        }
119
}
120

    
121

    
122
struct peersampler_iface dummy = {
123
  .init = dummy_init,
124
  .destroy = dummy_destroy,
125
  .change_metadata = dummy_change_metadata,
126
  .add_neighbour = dummy_add_neighbour,
127
  .parse_data = dummy_parse_data,
128
  .get_neighbourhood = dummy_get_neighbourhood,
129
  .get_metadata = dummy_get_metadata,
130
  .grow_neighbourhood = dummy_grow_neighbourhood,
131
  .shrink_neighbourhood = dummy_shrink_neighbourhood,
132
  .remove_neighbour = dummy_remove_neighbour,
133
};
134