Statistics
| Branch: | Revision:

streamers / topology.c @ c3d27400

History | View | Annotate | Download (2.57 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
static struct peerset *pset;
20
static struct timeval tout_bmap = {10, 0};
21

    
22

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

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

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

    
51
  dprintf("Update peers: topo_msg:%d, ",len);
52
  if (from) {
53
    dprintf("from:%s, ",node_addr(from));
54
    if (peerset_check(pset, from) < 0) {
55
      topAddNeighbour(from, NULL, 0);        //@TODO: this is agressive
56
      add_peer(from);
57
    }
58
  }
59

    
60
  dprintf("before:%d, ",peerset_size(pset));
61
  topParseData(buff, len);
62
  ids = topGetNeighbourhood(&n_ids);
63
  for(i = 0; i < n_ids; i++) {
64
    if(peerset_check(pset, ids[i]) < 0) {
65
      add_peer(ids[i]);
66
    }
67
  }
68
  dprintf("after:%d, ",peerset_size(pset));
69

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

    
83
  reg_neigh_size(peerset_size(pset));
84

    
85
  dprintf("after timer check:%d\n",peerset_size(pset));
86
}
87

    
88
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
89
{
90
  struct peer *p = peerset_get_peer(pset, id);
91
  if (!p) {
92
    fprintf(stderr,"warning: received message from unknown peer: %s!\n",node_addr(id));
93
    if (reg) {
94
      topAddNeighbour(id, NULL, 0);        //@TODO: this is agressive
95
      add_peer(id);
96
      p = peerset_get_peer(pset,id);
97
    }
98
  }
99

    
100
  return p;
101
}
102

    
103
int peers_init()
104
{
105
  fprintf(stderr,"peers_init\n");
106
  pset = peerset_init(0);
107
  return (int) pset;
108
}
109

    
110
struct peerset *get_peers()
111
{
112
  return pset;
113
}