Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.51 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

    
22
static struct peersampler_context* dummy_init(struct nodeID *myID, void *metadata, int metadata_size, const char *config)
23
{
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
      table[i++] = create_node(addr, port);
37
    }
38
  }
39
  table[i] = NULL;
40

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

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

    
52
static int dummy_add_neighbour(struct peersampler_context *context, struct nodeID *neighbour, void *metadata, int metadata_size)
53
{
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
static int dummy_parse_data(struct peersampler_context *context, const uint8_t *buff, int len)
64
{
65
  /* FAKE! */
66
  return 0;
67
}
68

    
69
static const struct nodeID **dummy_get_neighbourhood(struct peersampler_context *context, int *n)
70
{
71
  for (*n = 0; table[*n] != NULL; (*n)++) {
72
fprintf(stderr, "Checking table[%d]\n", *n);
73
  }
74
  return (const struct nodeID **)table;
75
}
76

    
77
static const void *dummy_get_metadata(struct peersampler_context *context, int *metadata_size)
78
{
79
  /* Metadata not supported: fail! */
80
  *metadata_size = -1;
81

    
82
  return NULL;
83
}
84

    
85
static int dummy_grow_neighbourhood(struct peersampler_context *context, int n)
86
{
87
  return -1;
88
}
89

    
90
static int dummy_shrink_neighbourhood(struct peersampler_context *context, int n)
91
{
92
  return -1;
93
}
94

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

    
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
};
112