Statistics
| Branch: | Revision:

streamers / topology.c @ 63ebb93d

History | View | Annotate | Download (6.66 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
#include <stdlib.h>
12

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

    
21
#include "compatibility/timer.h"
22

    
23
#include "topology.h"
24
#include "streaming.h"
25
#include "dbg.h"
26
#include "measures.h"
27

    
28
int NEIGHBORHOOD_TARGET_SIZE = 20;
29
double NEIGHBORHOOD_ROTATE_RATIO = 1.0;
30
#define TMAN_MAX_IDLE 10
31
#define TMAN_LOG_EVERY 1000
32

    
33
static struct peerset *pset;
34
static struct timeval tout_bmap = {0, 0};
35
static int counter = 0;
36
static int simpleRanker (const void *tin, const void *p1in, const void *p2in);
37
static tmanRankingFunction rankFunct = simpleRanker;
38
static double my_metadata;
39
static int cnt = 0;
40
static struct nodeID *me = NULL;
41
static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN};
42
static struct nodeID ** neighbors;
43

    
44
static void update_metadata(void) {
45

    
46
#ifndef MONL
47
        my_metadata = 1 + (((double)rand() / (double)RAND_MAX)*1000);
48
#endif
49
#ifdef MONL
50
        my_metadata = get_receive_delay();
51
#endif
52
}
53

    
54
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
55

    
56
        double t,p1,p2;
57
        t = *((const double *)tin);
58
        p1 = *((const double *)p1in);
59
        p2 = *((const double *)p2in);
60

    
61
        if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
62
        else if (isnan(p1)) return 2;
63
        else if (isnan(p2)) return 1;
64
        else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
65

    
66
}
67

    
68
int topologyInit(struct nodeID *myID, const char *config)
69
{
70
        int i;
71
        for (i=0;i<2;i++)
72
                bind_msg_type(mTypes[i]);
73
        update_metadata();
74
        me = myID;
75
        return (topInit(myID, &my_metadata, sizeof(my_metadata), config) && tmanInit(myID,&my_metadata, sizeof(my_metadata),rankFunct,0));
76
}
77

    
78
void topologyShutdown(void)
79
{
80
}
81

    
82
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
83
{
84
        // TODO: check this!! Just to use this function to bootstrap ncast...
85
        if (counter < TMAN_MAX_IDLE)
86
                return topAddNeighbour(neighbour,metadata,metadata_size);
87
        else return tmanAddNeighbour(neighbour,metadata,metadata_size);
88
}
89

    
90
static int topoParseData(const uint8_t *buff, int len)
91
{
92
        int res = -1,ncs = 0,msize;
93
        const struct nodeID **n; const void *m;
94
        if (!buff || buff[0] == MSG_TYPE_TOPOLOGY) {
95
                res = topParseData(buff,len);
96
//                if (counter <= TMAN_MAX_IDLE)
97
//                        counter++;
98
        }
99
        if (counter >= TMAN_MAX_IDLE && (!buff || buff[0] == MSG_TYPE_TMAN))
100
        {
101
                n = topGetNeighbourhood(&ncs);
102
                if (ncs) {
103
                m = topGetMetadata(&msize);
104
                res = tmanParseData(buff,len,n,ncs,m,msize);
105
                }
106
        }
107
  return res;
108
}
109

    
110
static const struct nodeID **topoGetNeighbourhood(int *n)
111
{
112
        int i; double d;
113
        if (counter > TMAN_MAX_IDLE) {
114
                uint8_t *mdata; int msize;
115
                *n = tmanGetNeighbourhoodSize();
116
                if (neighbors) free(neighbors);
117
                neighbors = calloc(*n,sizeof(struct nodeID *));
118
                tmanGetMetadata(&msize);
119
                mdata = calloc(*n,msize);
120
                tmanGivePeers(*n,neighbors,(void *)mdata);
121

    
122
                if (cnt % TMAN_LOG_EVERY == 0) {
123
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata);
124
                        for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
125
                                d = *((double *)(mdata+i*msize));
126
                                fprintf(stderr,"abouttopublish,%s,",node_addr(me));
127
                                fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
128
                        }
129
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
130
                }
131

    
132
                free(mdata);
133
                return (const struct nodeID **)neighbors;
134
        }
135
        else
136
                return topGetNeighbourhood(n);
137
}
138

    
139
static void topoAddToBL (struct nodeID *id)
140
{
141
        if (counter >= TMAN_MAX_IDLE)
142
                tmanAddToBlackList(id);
143
//        else
144
                topAddToBlackList(id);
145
}
146

    
147
void add_peer(struct nodeID *id)
148
{
149
      dprintf("Adding %s to neighbourhood!\n", node_addr(id));
150
      peerset_add_peer(pset, id);
151
      /* add measures here */
152
      add_measures(id);
153
      send_bmap(id);
154
}
155

    
156
void remove_peer(struct nodeID *id)
157
{
158
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
159
      /* add measures here */
160
      delete_measures(id);
161
      peerset_remove_peer(pset, id);
162
}
163

    
164
// currently it just makes the peerset grow
165
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
166
{
167
  int n_ids, i;
168
  static const struct nodeID **ids;
169
  struct peer *peers;
170
  struct timeval tnow, told;
171

    
172
//  dprintf("Update peers: topo_msg:%d, ",len);
173
//  if (from) {
174
//    dprintf("from:%s, ",node_addr(from));
175
//    if (peerset_check(pset, from) < 0) {
176
//      topAddNeighbour(from, NULL, 0);        //@TODO: this is agressive
177
//      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
178
//        add_peer(from);
179
//      }
180
//    }
181
//  }
182

    
183
  if (cnt++ % 10000 == 0) {
184
        update_metadata();
185
    if (counter > TMAN_MAX_IDLE) {
186
        tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
187
    }
188
  }
189

    
190
  topoParseData(buff, len);
191

    
192
  if (!buff) {
193
    reg_neigh_size(peerset_size(pset));
194
    return;
195
  }
196

    
197
  ids = topoGetNeighbourhood(&n_ids);
198
  for(i = 0; i < n_ids; i++) {
199
    if(peerset_check(pset, ids[i]) < 0) {
200
      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
201
        add_peer(ids[i]);
202
      } else {  //rotate neighbourhood
203
        if (rand()/((double)RAND_MAX + 1) < NEIGHBORHOOD_ROTATE_RATIO) {
204
          add_peer(ids[i]);
205
        }
206
      }
207
    }
208
  }
209

    
210
  if timerisset(&tout_bmap) {
211
    gettimeofday(&tnow, NULL);
212
    timersub(&tnow, &tout_bmap, &told);
213
    peers = peerset_get_peers(pset);
214
    for (i = 0; i < peerset_size(pset); i++) {
215
      if ( (!timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].creation_timestamp, &told, <) ) ||
216
           ( timerisset(&peers[i].bmap_timestamp) && timercmp(&peers[i].bmap_timestamp, &told, <)     )   ) {
217
        //if (peerset_size(pset) > 1) {        // avoid dropping our last link to the world
218
        topoAddToBL(peers[i].id);
219
        remove_peer(peers[i--].id);
220
        //}
221
      }
222
    }
223
  }
224

    
225
  while (NEIGHBORHOOD_TARGET_SIZE && peerset_size(pset) > NEIGHBORHOOD_TARGET_SIZE) { //reduce back neighbourhood to target size
226
    peers = peerset_get_peers(pset);
227
    remove_peer(peers[rand() % peerset_size(pset)].id);
228
  }
229

    
230
  reg_neigh_size(peerset_size(pset));
231
}
232

    
233
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
234
{
235
  struct peer *p = peerset_get_peer(pset, id);
236
  if (!p) {
237
    //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
238
    if (reg) {
239
      topoAddNeighbour(id, NULL, 0);
240
      add_peer(id);
241
      p = peerset_get_peer(pset,id);
242
    }
243
  }
244

    
245
  return p;
246
}
247

    
248
int peers_init(void)
249
{
250
  fprintf(stderr,"peers_init\n");
251
  pset = peerset_init(0);
252
  return pset ? 1 : 0;
253
}
254

    
255
struct peerset *get_peers(void)
256
{
257
  return pset;
258
}