Statistics
| Branch: | Revision:

grapes / src / PeerSampler / dummy.c @ 7b60aa43

History | View | Annotate | Download (2.72 KB)

1 8ab58ec7 Luca Abeni
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6
7 e2b6ad48 Luca
#include <sys/time.h>
8 480921a6 Luca Abeni
#include <stdint.h>
9 e2b6ad48 Luca
#include <stdlib.h>
10 480921a6 Luca Abeni
#include <stdio.h>
11
12 e2b6ad48 Luca
13 155319cd Luca Abeni
#include "net_helper.h"
14 e2b6ad48 Luca
#include "peersampler_iface.h"
15 480921a6 Luca Abeni
16
#define MAX_PEERS 5000
17 e2b6ad48 Luca
static struct nodeID *table[MAX_PEERS];
18 480921a6 Luca Abeni
19 496b4f3c Andrea Zito
//TODO: context support not implemented
20 46878222 Luca Abeni
struct peersampler_context{
21
  const struct nodeID **r;
22
};
23 496b4f3c Andrea Zito
24 15d5934d Luca Abeni
static struct peersampler_context* dummy_init(struct nodeID *myID, const void *metadata, int metadata_size, const char *config)
25 480921a6 Luca Abeni
{
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 e2b6ad48 Luca
      table[i++] = create_node(addr, port);
39 480921a6 Luca Abeni
    }
40
  }
41
  table[i] = NULL;
42
43 06113eb4 Andrea Zito
  //return i;
44
  //TODO: Returning the context may break some tests
45
  return NULL;
46 480921a6 Luca Abeni
}
47
48 15d5934d Luca Abeni
static int dummy_change_metadata(struct peersampler_context *context, const void *metadata, int metadata_size)
49 e2b6ad48 Luca
{
50
  /* Metadata not supported: fail! */
51
  return -1;
52
}
53
54 15d5934d Luca Abeni
static int dummy_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, const void *metadata, int metadata_size)
55 480921a6 Luca Abeni
{
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 06113eb4 Andrea Zito
static int dummy_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
66 480921a6 Luca Abeni
{
67
  /* FAKE! */
68
  return 0;
69
}
70
71 06113eb4 Andrea Zito
static const struct nodeID **dummy_get_neighbourhood(struct peersampler_context *context, int *n)
72 480921a6 Luca Abeni
{
73 46878222 Luca Abeni
  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 480921a6 Luca Abeni
  for (*n = 0; table[*n] != NULL; (*n)++) {
81 46878222 Luca Abeni
    context->r[*n] = table[*n];
82 480921a6 Luca Abeni
fprintf(stderr, "Checking table[%d]\n", *n);
83
  }
84 46878222 Luca Abeni
  return context->r;
85 e2b6ad48 Luca
}
86
87 06113eb4 Andrea Zito
static const void *dummy_get_metadata(struct peersampler_context *context, int *metadata_size)
88 e2b6ad48 Luca
{
89
  /* Metadata not supported: fail! */
90
  *metadata_size = -1;
91
92
  return NULL;
93
}
94
95 06113eb4 Andrea Zito
static int dummy_grow_neighbourhood(struct peersampler_context *context, int n)
96 e2b6ad48 Luca
{
97
  return -1;
98
}
99
100 06113eb4 Andrea Zito
static int dummy_shrink_neighbourhood(struct peersampler_context *context, int n)
101 e2b6ad48 Luca
{
102
  return -1;
103
}
104
105 15d5934d Luca Abeni
static int dummy_remove_neighbour(struct peersampler_context *context, const struct nodeID *neighbour)
106 e2b6ad48 Luca
{
107
  return -1;
108 480921a6 Luca Abeni
}
109 e2b6ad48 Luca
110
111
struct peersampler_iface dummy = {
112
  .init = dummy_init,
113
  .change_metadata = dummy_change_metadata,
114
  .add_neighbour = dummy_add_neighbour,
115
  .parse_data = dummy_parse_data,
116
  .get_neighbourhood = dummy_get_neighbourhood,
117
  .get_metadata = dummy_get_metadata,
118
  .grow_neighbourhood = dummy_grow_neighbourhood,
119
  .shrink_neighbourhood = dummy_shrink_neighbourhood,
120
  .remove_neighbour = dummy_remove_neighbour,
121
};