Statistics
| Branch: | Revision:

grapes / TopologyManager / ncast_proto.c @ e1630aa8

History | View | Annotate | Download (2.17 KB)

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

    
5
#include "msglayer.h"
6
#include "nccache.h"
7
#include "proto.h"
8
#include "ncast_proto.h"
9

    
10
static struct cache_entry *myEntry;
11

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

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

    
28
  return p - payload;
29
}
30

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

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

    
52
  return send_data(conn, pkt, sizeof(struct ncast_header) + len);
53
}
54

    
55
int ncast_query_peer(struct cache_entry *local_cache, struct socketID *dst)
56
{
57
  uint8_t pkt[1500];
58
  struct ncast_header *h = (struct ncast_header *)pkt;
59
  struct connectionID *conn;
60
  int len;
61

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

    
69
int ncast_query(struct cache_entry *local_cache)
70
{
71
  struct socketID *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 socketID *s)
81
{
82
  myEntry = cache_init(2);
83
  cache_add(myEntry, s);
84

    
85
  return 0;
86
}