Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.13 KB)

1
#include <stdint.h>
2
#include <stdlib.h>
3
#include <stdio.h>
4

    
5
#include "net_helper.h"
6
#include "nccache.h"
7
#include "proto.h"
8
#include "ncast_proto.h"
9
#include "msg_types.h"
10

    
11
static struct cache_entry *myEntry;
12

    
13
int ncast_payload_fill(uint8_t *payload, int size, struct cache_entry *c, struct nodeID *snot)
14
{
15
  int i;
16
  uint8_t *p = payload;
17

    
18
  p += entry_dump(p, myEntry, 0);
19
  for (i = 0; nodeid(c, i); i++) {
20
    if (!nodeid_equal(nodeid(c, i), snot)) {
21
      if (p - payload > size - 32 /* FIXME */) {
22
        fprintf(stderr, "too many entries!\n");
23
        return -1;
24
      }
25
      p += entry_dump(p, c, i);
26
    }
27
  }
28

    
29
  return p - payload;
30
}
31

    
32
int ncast_reply(const uint8_t *payload, int psize, struct cache_entry *local_cache)
33
{
34
  uint8_t pkt[1500];
35
  struct ncast_header *h = (struct ncast_header *)pkt;
36
  struct cache_entry *c = entries_undump(payload, psize);
37
  int len, res;
38
  struct nodeID *dst;
39

    
40
#if 0
41
  n = psize / sizeof(struct cache_entry);
42
  if (n * sizeof(struct cache_entry) != psize) {
43
    fprintf(stderr, "Wrong number of elems %d (%d / %d)!!!\n", n, psize, sizeof(struct cache_entry));
44
    return -1;
45
  }
46
#endif
47
  dst = nodeid(c, 0);
48
  h->protocol = MSG_TYPE_TOPOLOGY;
49
  h->type = NCAST_REPLY;
50
  len = ncast_payload_fill(pkt + sizeof(struct ncast_header), 1500 - sizeof(struct ncast_header), local_cache, dst);
51

    
52
  res = send_data(nodeid(myEntry, 0), dst, pkt, sizeof(struct ncast_header) + len);
53
  cache_free(c);
54
  return res;
55
}
56

    
57
int ncast_query_peer(struct cache_entry *local_cache, struct nodeID *dst)
58
{
59
  uint8_t pkt[1500];
60
  struct ncast_header *h = (struct ncast_header *)pkt;
61
  int len;
62

    
63
  h->protocol = MSG_TYPE_TOPOLOGY;
64
  h->type = NCAST_QUERY;
65
  len = ncast_payload_fill(pkt + sizeof(struct ncast_header), 1500 - sizeof(struct ncast_header), local_cache, dst);
66
  return send_data(nodeid(myEntry, 0), dst, pkt, sizeof(struct ncast_header) + len);
67
}
68

    
69
int ncast_query(struct cache_entry *local_cache)
70
{
71
  struct nodeID *dst;
72

    
73
  dst = rand_peer(local_cache);
74
  if (dst == NULL) {
75
    return 0;
76
  }
77
  return ncast_query_peer(local_cache, dst);
78
}
79

    
80
int ncast_proto_init(struct nodeID *s)
81
{
82
  myEntry = cache_init(2);
83
  cache_add(myEntry, s);
84

    
85
  return 0;
86
}