Statistics
| Branch: | Revision:

grapes / som / TopologyManager / ncast.c @ 026a7e5d

History | View | Annotate | Download (2.36 KB)

1
#include <sys/time.h>
2
#include <time.h>
3
#include <stdlib.h>
4
#include <stdint.h>
5
#include <stdio.h>
6
#include <string.h>
7

    
8
#include "net_helper.h"
9
#include "topmanager.h"
10
#include "nccache.h"
11
#include "ncast_proto.h"
12
#include "proto.h"
13
#include "msg_types.h"
14

    
15
#define MAX_PEERS 30
16

    
17
static uint64_t currtime;
18
static int cache_size = MAX_PEERS;
19
static struct cache_entry *local_cache;
20
static int period = 10000000;
21

    
22
static uint64_t gettime(void)
23
{
24
  struct timeval tv;
25

    
26
  gettimeofday(&tv, NULL);
27

    
28
  return tv.tv_usec + tv.tv_sec * 1000000ull;
29
}
30

    
31
static int time_to_send(void)
32
{
33
  if (gettime() - currtime > period) {
34
    currtime += period;
35

    
36
    return 1;
37
  }
38

    
39
  return 0;
40
}
41

    
42
/*
43
 * Public Functions!
44
 */
45
int topInit(struct nodeID *myID)
46
{
47
  local_cache = cache_init(cache_size);
48
  if (local_cache == NULL) {
49
    return -1;
50
  }
51
  ncast_proto_init(myID);
52
  currtime = gettime();
53

    
54
  return 1;
55
}
56

    
57
int topAddNeighbour(struct nodeID *neighbour)
58
{
59
  cache_add(local_cache, neighbour);
60
  return ncast_query_peer(local_cache, neighbour);
61
}
62

    
63
int topParseData(const uint8_t *buff, int len)
64
{
65
  if (len) {
66
    struct ncast_header *h = (struct ncast_header *)buff;
67
    struct cache_entry *new, *remote_cache;
68

    
69
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
70
      fprintf(stderr, "NCAST: Wrong protocol!\n");
71

    
72
      return -1;
73
    }
74

    
75
    if (h->type == NCAST_QUERY) {
76
      ncast_reply(buff + sizeof(struct ncast_header), len - sizeof(struct ncast_header), local_cache);
77
    }
78
    remote_cache = entries_undump(buff + sizeof(struct ncast_header), len - sizeof(struct ncast_header));
79
    new = merge_caches(local_cache, remote_cache, cache_size);
80
    cache_free(remote_cache);
81
    if (new != NULL) {
82
      cache_free(local_cache);
83
      local_cache = new;
84
    }
85
  }
86

    
87
  if (time_to_send()) {
88
    cache_update(local_cache);
89
    ncast_query(local_cache);
90
  }
91

    
92
  return 0;
93
}
94

    
95
const struct nodeID **topGetNeighbourhood(int *n)
96
{
97
  static struct nodeID *r[MAX_PEERS];
98

    
99
  for (*n = 0; nodeid(local_cache, *n); (*n)++) {
100
    r[*n] = nodeid(local_cache, *n);
101
    //fprintf(stderr, "Checking table[%d]\n", *n);
102
  }
103
  return (const struct nodeID **)r;
104
}
105

    
106
int topGrowNeighbourhood(int n)
107
{
108
  cache_size += n;
109

    
110
  return cache_size;
111
}
112

    
113
int topShrinkNeighbourhood(int n)
114
{
115
  if (cache_size < n) {
116
    return -1;
117
  }
118
  cache_size -= n;
119

    
120
  return cache_size;
121
}
122

    
123
int topRemoveNeighbour(struct nodeID *neighbour)
124
{
125
  return cache_del(local_cache, neighbour);
126
}
127