Revision 7e39164f topology.c

View differences:

topology.c
8 8
#include <stdio.h>
9 9
#include <sys/time.h>
10 10
#include <time.h>
11
#include <stdlib.h>
11 12

  
13
#include <math.h>
12 14
#include <net_helper.h>
13 15
#include <peerset.h>
14 16
#include <peer.h>
17
#include <msg_types.h>
15 18
#include <topmanager.h>
19
#include <tman.h>
16 20

  
17 21
#include "topology.h"
18 22
#include "streaming.h"
......
20 24
#include "measures.h"
21 25

  
22 26
#define NEIGHBORHOOD_TARGET_SIZE 0
27
#define TMAN_MAX_IDLE 5
23 28

  
24 29
static struct peerset *pset;
25 30
static struct timeval tout_bmap = {10, 0};
31
static int counter = 0;
32
static int simpleRanker (const void *tin, const void *p1in, const void *p2in);
33
static tmanRankingFunction rankFunct = simpleRanker;
34
static double my_metadata;
35
static int cnt = 0;
36
static struct nodeID *me = NULL;
37

  
38
static void update_metadata(void) {
39

  
40
#ifndef MONL
41
	my_metadata = 1 + ceil(((double)rand() / (double)RAND_MAX)*1000);
42
#endif
43
#ifdef MONL
44
	my_metadata = get_receive_delay();
45
#endif
46
}
47

  
48
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
49

  
50
	double t,p1,p2;
51
	t = *((const double *)tin);
52
	p1 = *((const double *)p1in);
53
	p2 = *((const double *)p2in);
54

  
55
	if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
56
	else if (isnan(p1)) return 2;
57
	else if (isnan(p2)) return 1;
58
	else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
59

  
60
}
61

  
62
int topologyInit(struct nodeID *myID, const char *config)
63
{
64
	update_metadata();
65
	me = myID;
66
	return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0));
67
}
68

  
69
void topologyShutdown(void)
70
{
71
}
72

  
73
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
74
{
75
	// TODO: check this!! Just to use this function to bootstrap ncast...
76
	if (counter < TMAN_MAX_IDLE)
77
		return topAddNeighbour(neighbour,metadata,metadata_size);
78
	else return tmanAddNeighbour(neighbour,metadata,metadata_size);
79
}
80

  
81
static int topoParseData(const uint8_t *buff, int len)
82
{
83
	int res = -1,ncs = 0,msize;
84
	const struct nodeID **n; const void *m;
85
	if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) {
86
		res = topParseData(buff,len);
87
		if (counter <= TMAN_MAX_IDLE)
88
			counter++;
89
	}
90
	if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN))
91
	{
92
		n = topGetNeighbourhood(&ncs);
93
		if (ncs) {
94
		m = topGetMetadata(&msize);
95
		res = tmanParseData(buff,len,n,ncs,m,msize);
96
		}
97
	}
98
  return res;
99
}
100

  
101
static const struct nodeID **topoGetNeighbourhood(int *n)
102
{
103
	int i; double d;
104
	if (counter > TMAN_MAX_IDLE) {
105
		struct nodeID ** neighbors; uint8_t *mdata; int msize;
106
		*n = tmanGetNeighbourhoodSize();
107
		neighbors = calloc(*n,sizeof(struct nodeID *));
108
		tmanGetMetadata(&msize);
109
		mdata = calloc(*n,msize);
110
		tmanGivePeers(*n,neighbors,(void *)mdata);
111

  
112
		if (cnt % 5 == 0) {
113
			fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata);
114
			for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
115
				d = *((double *)(mdata+i*msize));
116
				fprintf(stderr,"abouttopublish,%s,",node_addr(me));
117
				fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
118
			}
119
			fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
120
		}
121

  
122
		free(mdata);
123
		return (const struct nodeID **)neighbors;
124
	}
125
	else
126
		return topGetNeighbourhood(n);
127
}
26 128

  
27 129

  
28 130
void add_peer(struct nodeID *id)
......
50 152
  struct peer *peers;
51 153
  struct timeval tnow, told;
52 154

  
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
  }
155
//  dprintf("Update peers: topo_msg:%d, ",len);
156
//  if (from) {
157
//    dprintf("from:%s, ",node_addr(from));
158
//    if (peerset_check(pset, from) < 0) {
159
//      topAddNeighbour(from, NULL, 0);	//@TODO: this is agressive
160
//      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
161
//        add_peer(from);
162
//      }
163
//    }
164
//  }
63 165

  
64 166
  dprintf("before:%d, ",peerset_size(pset));
65
  topParseData(buff, len);
66
  ids = topGetNeighbourhood(&n_ids);
167

  
168
  if (cnt++ % 100 == 0) {
169
	update_metadata();
170
	tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
171
  }
172

  
173
  topoParseData(buff, len);
174
  ids = topoGetNeighbourhood(&n_ids);
67 175
  for(i = 0; i < n_ids; i++) {
68 176
    if(peerset_check(pset, ids[i]) < 0) {
69 177
      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
......
80 188
    if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
81 189
         ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
82 190
      //if (peerset_size(pset) > 1) {	// avoid dropping our last link to the world
83
        topRemoveNeighbour(peers[i].id);
191
//        topRemoveNeighbour(peers[i].id);
84 192
        remove_peer(peers[i--].id);
85 193
      //}
86 194
    }
......
97 205
  if (!p) {
98 206
    fprintf(stderr,"warning: received message from unknown peer: %s!\n",node_addr(id));
99 207
    if (reg) {
100
      topAddNeighbour(id, NULL, 0);	//@TODO: this is agressive
208
      topoAddNeighbour(id, NULL, 0);
101 209
      add_peer(id);
102 210
      p = peerset_get_peer(pset,id);
103 211
    }
......
106 214
  return p;
107 215
}
108 216

  
109
int peers_init()
217
int peers_init(void)
110 218
{
111 219
  fprintf(stderr,"peers_init\n");
112 220
  pset = peerset_init(0);
113 221
  return pset ? 1 : 0;
114 222
}
115 223

  
116
struct peerset *get_peers()
224
struct peerset *get_peers(void)
117 225
{
118 226
  return pset;
119 227
}

Also available in: Unified diff