Statistics
| Branch: | Revision:

streamers / topology.c @ 50e1a5a9

History | View | Annotate | Download (2.81 KB)

1
#include <stdint.h>
2
#include <stdio.h>
3
#include <sys/time.h>
4
#include <time.h>
5

    
6
#include <net_helper.h>
7
#include <peerset.h>
8
#include <peer.h>
9
#include <topmanager.h>
10

    
11
#include "topology.h"
12
#include "dbg.h"
13

    
14
#ifdef MONL
15
#include "measures.h"
16

    
17
#endif
18

    
19
#define NEIGHBORHOOD_TARGET_SIZE 0
20

    
21
static struct peerset *pset;
22
static struct timeval tout_bmap = {10, 0};
23

    
24

    
25
void add_peer(struct nodeID *id)
26
{
27
      dprintf("Adding %s to neighbourhood!\n", node_addr(id));
28
      peerset_add_peer(pset, id);
29
#ifdef MONL
30
      /* add measures here */
31
      add_measures(id);
32
#endif
33
}
34

    
35
void remove_peer(struct nodeID *id)
36
{
37
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
38
#ifdef MONL
39
      /* add measures here */
40
      delete_measures(id);
41
#endif
42
      peerset_remove_peer(pset, id);
43
}
44

    
45
// currently it just makes the peerset grow
46
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
47
{
48
  int n_ids, i;
49
  const struct nodeID **ids;
50
  struct peer *peers;
51
  struct timeval tnow, told;
52

    
53
  dprintf("Update peers: topo_msg:%d, ",len);
54
  if (from) {
55
    dprintf("from:%s, ",node_addr(from));
56
    if (peerset_check(pset, from) < 0) {
57
      topAddNeighbour(from, NULL, 0);        //@TODO: this is agressive
58
      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
59
        add_peer(from);
60
      }
61
    }
62
  }
63

    
64
  dprintf("before:%d, ",peerset_size(pset));
65
  topParseData(buff, len);
66
  ids = topGetNeighbourhood(&n_ids);
67
  for(i = 0; i < n_ids; i++) {
68
    if(peerset_check(pset, ids[i]) < 0) {
69
      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
70
        add_peer(ids[i]);
71
      }
72
    }
73
  }
74
  dprintf("after:%d, ",peerset_size(pset));
75

    
76
  gettimeofday(&tnow, NULL);
77
  timersub(&tnow, &tout_bmap, &told);
78
  peers = peerset_get_peers(pset);
79
  for (i = 0; i < peerset_size(pset); i++) {
80
    if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
81
         ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
82
      //if (peerset_size(pset) > 1) {        // avoid dropping our last link to the world
83
        topRemoveNeighbour(peers[i].id);
84
        remove_peer(peers[i--].id);
85
      //}
86
    }
87
  }
88

    
89
#ifdef MONL
90
  reg_neigh_size(peerset_size(pset));
91
#endif
92

    
93
  dprintf("after timer check:%d\n",peerset_size(pset));
94
}
95

    
96
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
97
{
98
  struct peer *p = peerset_get_peer(pset, id);
99
  if (!p) {
100
    fprintf(stderr,"warning: received message from unknown peer: %s!\n",node_addr(id));
101
    if (reg) {
102
      topAddNeighbour(id, NULL, 0);        //@TODO: this is agressive
103
      add_peer(id);
104
      p = peerset_get_peer(pset,id);
105
    }
106
  }
107

    
108
  return p;
109
}
110

    
111
int peers_init()
112
{
113
  fprintf(stderr,"peers_init\n");
114
  pset = peerset_init(0);
115
  return (int) pset;
116
}
117

    
118
struct peerset *get_peers()
119
{
120
  return pset;
121
}