Statistics
| Branch: | Revision:

grapes / src / TopologyManager / dummy.c @ 5033613a

History | View | Annotate | Download (2.51 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 06113eb4 Andrea Zito
struct peersampler_context{};
21 496b4f3c Andrea Zito
22 06113eb4 Andrea Zito
static struct peersampler_context* dummy_init(struct nodeID *myID, void *metadata, int metadata_size, const char *config)
23 480921a6 Luca Abeni
{
24
  FILE *f;
25
  int i = 0;
26
27
  f = fopen("peers.txt", "r");
28
  while(!feof(f)) {
29
    int res;
30
    char addr[32];
31
    int port;
32
33
    res = fscanf(f, "%s %d\n", addr, &port);
34
    if ((res == 2) && (i < MAX_PEERS - 1)) {
35
fprintf(stderr, "Creating table[%d]\n", i);
36 e2b6ad48 Luca
      table[i++] = create_node(addr, port);
37 480921a6 Luca Abeni
    }
38
  }
39
  table[i] = NULL;
40
41 06113eb4 Andrea Zito
  //return i;
42
  //TODO: Returning the context may break some tests
43
  return NULL;
44 480921a6 Luca Abeni
}
45
46 06113eb4 Andrea Zito
static int dummy_change_metadata(struct peersampler_context *context, void *metadata, int metadata_size)
47 e2b6ad48 Luca
{
48
  /* Metadata not supported: fail! */
49
  return -1;
50
}
51
52 06113eb4 Andrea Zito
static int dummy_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, void *metadata, int metadata_size)
53 480921a6 Luca Abeni
{
54
  int i;
55
56
  for (i = 0; table[i] != NULL; i++);
57
  table[i++] = neighbour;
58
  table[i] = NULL;
59
60
  return i;
61
}
62
63 06113eb4 Andrea Zito
static int dummy_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
64 480921a6 Luca Abeni
{
65
  /* FAKE! */
66
  return 0;
67
}
68
69 06113eb4 Andrea Zito
static const struct nodeID **dummy_get_neighbourhood(struct peersampler_context *context, int *n)
70 480921a6 Luca Abeni
{
71
  for (*n = 0; table[*n] != NULL; (*n)++) {
72
fprintf(stderr, "Checking table[%d]\n", *n);
73
  }
74 e2b6ad48 Luca
  return (const struct nodeID **)table;
75
}
76
77 06113eb4 Andrea Zito
static const void *dummy_get_metadata(struct peersampler_context *context, int *metadata_size)
78 e2b6ad48 Luca
{
79
  /* Metadata not supported: fail! */
80
  *metadata_size = -1;
81
82
  return NULL;
83
}
84
85 06113eb4 Andrea Zito
static int dummy_grow_neighbourhood(struct peersampler_context *context, int n)
86 e2b6ad48 Luca
{
87
  return -1;
88
}
89
90 06113eb4 Andrea Zito
static int dummy_shrink_neighbourhood(struct peersampler_context *context, int n)
91 e2b6ad48 Luca
{
92
  return -1;
93
}
94
95 06113eb4 Andrea Zito
static int dummy_remove_neighbour(struct peersampler_context *context, struct nodeID *neighbour)
96 e2b6ad48 Luca
{
97
  return -1;
98 480921a6 Luca Abeni
}
99 e2b6ad48 Luca
100
101
struct peersampler_iface dummy = {
102
  .init = dummy_init,
103
  .change_metadata = dummy_change_metadata,
104
  .add_neighbour = dummy_add_neighbour,
105
  .parse_data = dummy_parse_data,
106
  .get_neighbourhood = dummy_get_neighbourhood,
107
  .get_metadata = dummy_get_metadata,
108
  .grow_neighbourhood = dummy_grow_neighbourhood,
109
  .shrink_neighbourhood = dummy_shrink_neighbourhood,
110
  .remove_neighbour = dummy_remove_neighbour,
111
};