Statistics
| Branch: | Revision:

grapes / som / TopologyManager / ncast.c @ 8ab58ec7

History | View | Annotate | Download (2.45 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <sys/time.h>
8
#include <time.h>
9
#include <stdlib.h>
10
#include <stdint.h>
11
#include <stdio.h>
12
#include <string.h>
13

    
14
#include "net_helper.h"
15
#include "topmanager.h"
16
#include "nccache.h"
17
#include "ncast_proto.h"
18
#include "proto.h"
19
#include "msg_types.h"
20

    
21
#define MAX_PEERS 30
22

    
23
static uint64_t currtime;
24
static int cache_size = MAX_PEERS;
25
static struct cache_entry *local_cache;
26
static int period = 10000000;
27

    
28
static uint64_t gettime(void)
29
{
30
  struct timeval tv;
31

    
32
  gettimeofday(&tv, NULL);
33

    
34
  return tv.tv_usec + tv.tv_sec * 1000000ull;
35
}
36

    
37
static int time_to_send(void)
38
{
39
  if (gettime() - currtime > period) {
40
    currtime += period;
41

    
42
    return 1;
43
  }
44

    
45
  return 0;
46
}
47

    
48
/*
49
 * Public Functions!
50
 */
51
int topInit(struct nodeID *myID)
52
{
53
  local_cache = cache_init(cache_size);
54
  if (local_cache == NULL) {
55
    return -1;
56
  }
57
  ncast_proto_init(myID);
58
  currtime = gettime();
59

    
60
  return 1;
61
}
62

    
63
int topAddNeighbour(struct nodeID *neighbour)
64
{
65
  cache_add(local_cache, neighbour);
66
  return ncast_query_peer(local_cache, neighbour);
67
}
68

    
69
int topParseData(const uint8_t *buff, int len)
70
{
71
  if (len) {
72
    struct ncast_header *h = (struct ncast_header *)buff;
73
    struct cache_entry *new, *remote_cache;
74

    
75
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
76
      fprintf(stderr, "NCAST: Wrong protocol!\n");
77

    
78
      return -1;
79
    }
80

    
81
    if (h->type == NCAST_QUERY) {
82
      ncast_reply(buff + sizeof(struct ncast_header), len - sizeof(struct ncast_header), local_cache);
83
    }
84
    remote_cache = entries_undump(buff + sizeof(struct ncast_header), len - sizeof(struct ncast_header));
85
    new = merge_caches(local_cache, remote_cache, cache_size);
86
    cache_free(remote_cache);
87
    if (new != NULL) {
88
      cache_free(local_cache);
89
      local_cache = new;
90
    }
91
  }
92

    
93
  if (time_to_send()) {
94
    cache_update(local_cache);
95
    ncast_query(local_cache);
96
  }
97

    
98
  return 0;
99
}
100

    
101
const struct nodeID **topGetNeighbourhood(int *n)
102
{
103
  static struct nodeID *r[MAX_PEERS];
104

    
105
  for (*n = 0; nodeid(local_cache, *n); (*n)++) {
106
    r[*n] = nodeid(local_cache, *n);
107
    //fprintf(stderr, "Checking table[%d]\n", *n);
108
  }
109
  return (const struct nodeID **)r;
110
}
111

    
112
int topGrowNeighbourhood(int n)
113
{
114
  cache_size += n;
115

    
116
  return cache_size;
117
}
118

    
119
int topShrinkNeighbourhood(int n)
120
{
121
  if (cache_size < n) {
122
    return -1;
123
  }
124
  cache_size -= n;
125

    
126
  return cache_size;
127
}
128

    
129
int topRemoveNeighbour(struct nodeID *neighbour)
130
{
131
  return cache_del(local_cache, neighbour);
132
}
133