Statistics
| Branch: | Revision:

grapes / som / TopologyManager / topo_proto.c @ df98cd3a

History | View | Annotate | Download (3.13 KB)

1 418dafe6 Luca
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6
7
#include <stdint.h>
8
#include <stdlib.h>
9
#include <stdio.h>
10
11
#include "net_helper.h"
12
#include "topocache.h"
13
#include "proto.h"
14
#include "topo_proto.h"
15
#include "msg_types.h"
16
17
#define MAX_MSG_SIZE 1500
18
19
static struct peer_cache *myEntry;
20
21
static int ncast_payload_fill(uint8_t *payload, int size, struct peer_cache *c, struct nodeID *snot)
22
{
23
  int i;
24
  uint8_t *p = payload;
25
26
  p += cache_header_dump(p, c);
27
  p += entry_dump(p, myEntry, 0);
28
  for (i = 0; nodeid(c, i); i++) {
29
    if (!nodeid_equal(nodeid(c, i), snot)) {
30
      if (p - payload > size - 32 /* FIXME */) {
31
        fprintf(stderr, "too many entries!\n");
32
        return -1;
33
      }
34
      p += entry_dump(p, c, i);
35
    }
36
  }
37
38
  return p - payload;
39
}
40
41 df98cd3a Luca Abeni
static int topo_reply(const struct peer_cache *c, struct peer_cache *local_cache, int protocol, int type)
42 418dafe6 Luca
{
43
  uint8_t pkt[MAX_MSG_SIZE];
44
  struct topo_header *h = (struct topo_header *)pkt;
45
  int len, res;
46
  struct nodeID *dst;
47
48
#if 0
49
  n = psize / sizeof(struct cache_entry);
50
  if (n * sizeof(struct cache_entry) != psize) {
51
    fprintf(stderr, "Wrong number of elems %d (%d / %d)!!!\n", n, psize, sizeof(struct cache_entry));
52
    return -1;
53
  }
54
#endif
55
  dst = nodeid(c, 0);
56 df98cd3a Luca Abeni
  h->protocol = protocol;
57
  h->type = type;
58 418dafe6 Luca
  len = ncast_payload_fill(pkt + sizeof(struct topo_header), MAX_MSG_SIZE - sizeof(struct topo_header), local_cache, dst);
59
60
  res = len > 0 ? send_to_peer(nodeid(myEntry, 0), dst, pkt, sizeof(struct topo_header) + len) : len;
61
62
  return res;
63
}
64
65 df98cd3a Luca Abeni
static int topo_query_peer(struct peer_cache *local_cache, struct nodeID *dst, int protocol, int type)
66 418dafe6 Luca
{
67
  uint8_t pkt[MAX_MSG_SIZE];
68
  struct topo_header *h = (struct topo_header *)pkt;
69
  int len;
70
71 df98cd3a Luca Abeni
  h->protocol = protocol;
72
  h->type = type;
73 418dafe6 Luca
  len = ncast_payload_fill(pkt + sizeof(struct topo_header), MAX_MSG_SIZE - sizeof(struct topo_header), local_cache, dst);
74
  return len > 0  ? send_to_peer(nodeid(myEntry, 0), dst, pkt, sizeof(struct topo_header) + len) : len;
75
}
76
77 df98cd3a Luca Abeni
int ncast_reply(const struct peer_cache *c, struct peer_cache *local_cache)
78
{
79
  return topo_reply(c, local_cache, MSG_TYPE_TOPOLOGY, NCAST_REPLY);
80
}
81
82
int tman_reply(const struct peer_cache *c, struct peer_cache *local_cache)
83
{
84
  return topo_reply(c, local_cache, MSG_TYPE_TMAN, TMAN_REPLY);
85
}
86
87
int ncast_query_peer(struct peer_cache *local_cache, struct nodeID *dst)
88
{
89
  return topo_query_peer(local_cache, dst, MSG_TYPE_TOPOLOGY, NCAST_QUERY);
90
}
91
92
int tman_query_peer(struct peer_cache *local_cache, struct nodeID *dst)
93
{
94
  return topo_query_peer(local_cache, dst, MSG_TYPE_TMAN, TMAN_QUERY);
95
}
96
97
int ncast_query(struct peer_cache *local_cache)
98 418dafe6 Luca
{
99
  struct nodeID *dst;
100
101 df98cd3a Luca Abeni
  dst = rand_peer(local_cache, NULL);
102 418dafe6 Luca
  if (dst == NULL) {
103
    return 0;
104
  }
105 df98cd3a Luca Abeni
  return topo_query_peer(local_cache, dst, MSG_TYPE_TOPOLOGY, NCAST_QUERY);
106 418dafe6 Luca
}
107
108
int topo_proto_metadata_update(struct nodeID *peer, void *meta, int meta_size)
109
{
110
  if (cache_metadata_update(myEntry, peer, meta, meta_size) > 0) {
111
    return 1;
112
  }
113
114
  return -1;
115
}
116
117
int topo_proto_init(struct nodeID *s, void *meta, int meta_size)
118
{
119
  myEntry = cache_init(1, meta_size);
120
  cache_add(myEntry, s, meta, meta_size);
121
122
  return 0;
123
}