Statistics
| Branch: | Revision:

streamers / topology.c @ 913146ae

History | View | Annotate | Download (3.01 KB)

1
/*
2
 *  Copyright (c) 2010 Csaba Kiraly
3
 *  Copyright (c) 2010 Luca Abeni
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <stdint.h>
8
#include <stdio.h>
9
#include <sys/time.h>
10
#include <time.h>
11

    
12

    
13
#include <peerset.h>
14
#include <peer.h>
15
#include <topmanager.h>
16

    
17
#include <net_helper.h>
18
#include "topology.h"
19
#include "streaming.h"
20
#include "dbg.h"
21
#include "measures.h"
22

    
23
#define NEIGHBORHOOD_TARGET_SIZE 0
24

    
25
static struct peerset *pset;
26
static struct timeval tout_bmap = {10, 0};
27

    
28

    
29
void add_peer(struct nodeID *id)
30
{
31
      dprintf("Adding %s to neighbourhood!\n", node_addr(id));
32
      peerset_add_peer(pset, id);
33
      /* add measures here */
34
      add_measures(id);
35
      send_bmap(peerset_get_peer(pset,id));
36
}
37

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

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

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

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

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

    
90
  reg_neigh_size(peerset_size(pset));
91

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

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

    
107
  return p;
108
}
109

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

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