Statistics
| Branch: | Revision:

streamers / topology.c @ 74a5d4ae

History | View | Annotate | Download (2.92 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
#include <net_helper.h>
13
#include <peerset.h>
14
#include <peer.h>
15
#include <topmanager.h>
16

    
17
#include "topology.h"
18
#include "streaming.h"
19
#include "dbg.h"
20
#include "measures.h"
21

    
22
#define NEIGHBORHOOD_TARGET_SIZE 0
23

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

    
27

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

    
37
void remove_peer(struct nodeID *id)
38
{
39
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
40
      /* add measures here */
41
      delete_measures(id);
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
  reg_neigh_size(peerset_size(pset));
90

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

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

    
106
  return p;
107
}
108

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

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