Statistics
| Branch: | Revision:

grapes / som / TopologyManager / ncast.c @ 005954ae

History | View | Annotate | Download (2.89 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, void *metadata, int metadata_size)
52
{
53
  if (metadata_size) {
54
    fprintf(stderr, "Metadata are not implemented yet!\n");
55

    
56
    return -1;
57
  }
58

    
59
  local_cache = cache_init(cache_size);
60
  if (local_cache == NULL) {
61
    return -1;
62
  }
63
  ncast_proto_init(myID);
64
  currtime = gettime();
65

    
66
  return 1;
67
}
68

    
69
int topAddNeighbour(struct nodeID *neighbour, void *metadata, int metadata_size)
70
{
71
  if (metadata_size) {
72
    fprintf(stderr, "Metadata are not implemented yet!\n");
73

    
74
    return -1;
75
  }
76

    
77
  if (cache_add(local_cache, neighbour) < 0) {
78
    return -1;
79
  }
80
  return ncast_query_peer(local_cache, neighbour);
81
}
82

    
83
int topParseData(const uint8_t *buff, int len)
84
{
85
  if (len) {
86
    const struct ncast_header *h = (const struct ncast_header *)buff;
87
    struct cache_entry *new, *remote_cache;
88

    
89
    if (h->protocol != MSG_TYPE_TOPOLOGY) {
90
      fprintf(stderr, "NCAST: Wrong protocol!\n");
91

    
92
      return -1;
93
    }
94

    
95
    if (h->type == NCAST_QUERY) {
96
      ncast_reply(buff + sizeof(struct ncast_header), len - sizeof(struct ncast_header), local_cache);
97
    }
98
    remote_cache = entries_undump(buff + sizeof(struct ncast_header), len - sizeof(struct ncast_header));
99
    new = merge_caches(local_cache, remote_cache, cache_size);
100
    cache_free(remote_cache);
101
    if (new != NULL) {
102
      cache_free(local_cache);
103
      local_cache = new;
104
    }
105
  }
106

    
107
  if (time_to_send()) {
108
    cache_update(local_cache);
109
    ncast_query(local_cache);
110
  }
111

    
112
  return 0;
113
}
114

    
115
const struct nodeID **topGetNeighbourhood(int *n)
116
{
117
  static struct nodeID *r[MAX_PEERS];
118

    
119
  for (*n = 0; nodeid(local_cache, *n); (*n)++) {
120
    r[*n] = nodeid(local_cache, *n);
121
    //fprintf(stderr, "Checking table[%d]\n", *n);
122
  }
123
  return (const struct nodeID **)r;
124
}
125

    
126
const void **topGetMetadata(int *metadata_size)
127
{
128
  fprintf(stderr, "MetaData are not implemented yet!!!\n");
129

    
130
  return NULL;
131
}
132

    
133
int topGrowNeighbourhood(int n)
134
{
135
  cache_size += n;
136

    
137
  return cache_size;
138
}
139

    
140
int topShrinkNeighbourhood(int n)
141
{
142
  if (cache_size < n) {
143
    return -1;
144
  }
145
  cache_size -= n;
146

    
147
  return cache_size;
148
}
149

    
150
int topRemoveNeighbour(struct nodeID *neighbour)
151
{
152
  return cache_del(local_cache, neighbour);
153
}
154