Statistics
| Branch: | Revision:

streamers / topology.c @ cc0cd459

History | View | Annotate | Download (6.97 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
struct metadata {
39
  double value;
40
};
41
static struct metadata my_metadata;
42
static int cnt = 0;
43
static struct nodeID *me = NULL;
44
static unsigned char mTypes[] = {MSG_TYPE_TOPOLOGY,MSG_TYPE_TMAN};
45
static struct nodeID ** neighbors;
46

    
47
static void update_metadata(void) {
48

    
49
#ifndef MONL
50
        my_metadata.value = 1 + (((double)rand() / (double)RAND_MAX)*1000);
51
#endif
52
#ifdef MONL
53
        my_metadata.value = get_receive_delay();
54
#endif
55
}
56

    
57
static int simpleRanker (const void *tin, const void *p1in, const void *p2in) {
58

    
59
        double t,p1,p2;
60
        t = *((const double *)tin);
61
        p1 = *((const double *)p1in);
62
        p2 = *((const double *)p2in);
63

    
64
        if (isnan(t) || (isnan(p1) && isnan(p2))) return 0;
65
        else if (isnan(p1)) return 2;
66
        else if (isnan(p2)) return 1;
67
        else return (fabs(t-p1) == fabs(t-p2))?0:(fabs(t-p1) < fabs(t-p2))?1:2;
68

    
69
}
70

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

    
81
void topologyShutdown(void)
82
{
83
}
84

    
85
int topoAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
86
{
87
        // TODO: check this!! Just to use this function to bootstrap ncast...
88
        struct metadata m = {0};        //TODO: check what metadata option should mean
89

    
90
        if (counter < TMAN_MAX_IDLE)
91
                return topAddNeighbour(neighbour,&m,sizeof(m));
92
        else return tmanAddNeighbour(neighbour,&m,sizeof(m));
93
}
94

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

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

    
127
                if (cnt % TMAN_LOG_EVERY == 0) {
128
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_chunk_delay,%f\n",node_addr(me),node_addr(me),my_metadata.value);
129
                        for (i=0;i<(*n) && i<NEIGHBORHOOD_TARGET_SIZE;i++) {
130
                                d = *((double *)(mdata+i*msize));
131
                                fprintf(stderr,"abouttopublish,%s,",node_addr(me));
132
                                fprintf(stderr,"%s,,Tman_chunk_delay,%f\n",node_addr(neighbors[i]),d);
133
                        }
134
                        fprintf(stderr,"abouttopublish,%s,%s,,Tman_neighborhood_size,%d\n\n",node_addr(me),node_addr(me),*n);
135
                }
136

    
137
                free(mdata);
138
                return (const struct nodeID **)neighbors;
139
        }
140
        else
141
                return topGetNeighbourhood(n);
142
}
143

    
144
static void topoAddToBL (struct nodeID *id)
145
{
146
        if (counter >= TMAN_MAX_IDLE)
147
                tmanAddToBlackList(id);
148
//        else
149
                topAddToBlackList(id);
150
}
151

    
152
void add_peer(const struct nodeID *id, const struct metadata *m)
153
{
154
      dprintf("Adding %s to neighbourhood!\n", node_addr(id));
155
      peerset_add_peer(pset, id);
156
      /* add measures here */
157
      add_measures(id);
158
      send_bmap(id);
159
}
160

    
161
void remove_peer(struct nodeID *id)
162
{
163
      dprintf("Removing %s from neighbourhood!\n", node_addr(id));
164
      /* add measures here */
165
      delete_measures(id);
166
      peerset_remove_peer(pset, id);
167
}
168

    
169
// currently it just makes the peerset grow
170
void update_peers(struct nodeID *from, const uint8_t *buff, int len)
171
{
172
  int n_ids, metasize, i;
173
  static const struct nodeID **ids;
174
  static const struct metadata *metas;
175
  struct peer *peers;
176
  struct timeval tnow, told;
177

    
178
//  dprintf("Update peers: topo_msg:%d, ",len);
179
//  if (from) {
180
//    dprintf("from:%s, ",node_addr(from));
181
//    if (peerset_check(pset, from) < 0) {
182
//      topAddNeighbour(from, NULL, 0);        //@TODO: this is agressive
183
//      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
184
//        add_peer(from);
185
//      }
186
//    }
187
//  }
188

    
189
  if (cnt++ % 100 == 0) {
190
        update_metadata();
191
    if (counter > TMAN_MAX_IDLE) {
192
        tmanChangeMetadata(&my_metadata,sizeof(my_metadata));
193
    }
194
  }
195

    
196
  topoParseData(buff, len);
197

    
198
  if (!buff) {
199
    reg_neigh_size(peerset_size(pset));
200
    return;
201
  }
202

    
203
  ids = topoGetNeighbourhood(&n_ids);        //TODO handle both tman and topo
204
  metas = topGetMetadata(&metasize);        //TODO: check metasize
205
  for(i = 0; i < n_ids; i++) {
206
    if(peerset_check(pset, ids[i]) < 0) {
207
      if (!NEIGHBORHOOD_TARGET_SIZE || peerset_size(pset) < NEIGHBORHOOD_TARGET_SIZE) {
208
        add_peer(ids[i],&metas[i]);
209
      } else {  //rotate neighbourhood
210
        if (rand()/((double)RAND_MAX + 1) < NEIGHBORHOOD_ROTATE_RATIO) {
211
          add_peer(ids[i],&metas[i]);
212
        }
213
      }
214
    }
215
  }
216

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

    
232
  while (NEIGHBORHOOD_TARGET_SIZE && peerset_size(pset) > NEIGHBORHOOD_TARGET_SIZE) { //reduce back neighbourhood to target size
233
    peers = peerset_get_peers(pset);
234
    remove_peer(peers[rand() % peerset_size(pset)].id);
235
  }
236

    
237
  reg_neigh_size(peerset_size(pset));
238
}
239

    
240
struct peer *nodeid_to_peer(const struct nodeID* id, int reg)
241
{
242
  struct peer *p = peerset_get_peer(pset, id);
243
  if (!p) {
244
    //fprintf(stderr,"warning: received message from unknown peer: %s!%s\n",node_addr(id), reg ? " Adding it to pset." : "");
245
    if (reg) {
246
      topoAddNeighbour(id, NULL, 0);
247
      add_peer(id,NULL);
248
      p = peerset_get_peer(pset,id);
249
    }
250
  }
251

    
252
  return p;
253
}
254

    
255
int peers_init(void)
256
{
257
  fprintf(stderr,"peers_init\n");
258
  pset = peerset_init(0);
259
  return pset ? 1 : 0;
260
}
261

    
262
struct peerset *get_peers(void)
263
{
264
  return pset;
265
}