Statistics
| Branch: | Revision:

grapes / src / TopologyManager / blist_proto.c @ 5033613a

History | View | Annotate | Download (3.6 KB)

1 33ea04ee Marco Biazzini
/*
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 "blist_cache.h"
13
#include "proto.h"
14
#include "blist_proto.h"
15
#include "grapes_msg_types.h"
16
17
#define MAX_MSG_SIZE 1500
18
19
static struct peer_cache *myEntry;
20
21
static int blist_payload_fill(uint8_t *payload, int size, struct peer_cache *c, struct nodeID *snot, int max_peers)
22
{
23
  int i;
24
  uint8_t *p = payload;
25
26
  if (!max_peers) max_peers = MAX_MSG_SIZE; // just to be sure to dump the whole cache...
27
  p += blist_cache_header_dump(p, c);
28
  p += blist_entry_dump(p, myEntry, 0, size - (p - payload));
29
  for (i = 0; blist_nodeid(c, i) && max_peers; i++) {
30
    if (!nodeid_equal(blist_nodeid(c, i), snot)) {
31
      int res;
32
      res = blist_entry_dump(p, c, i, size - (p - payload));
33
      if (res < 0) {
34
        fprintf(stderr, "too many entries!\n");
35
        return -1;
36
      }
37
      p += res;
38
      --max_peers;
39
    }
40
  }
41
42
  return p - payload;
43
}
44
45
static int blist_topo_reply(const struct peer_cache *c, struct peer_cache *local_cache, int protocol, int type, int max_peers)
46
{
47
  uint8_t pkt[MAX_MSG_SIZE];
48
  struct topo_header *h = (struct topo_header *)pkt;
49
  int len, res;
50
  struct nodeID *dst;
51
52
#if 0
53
  n = psize / sizeof(struct cache_entry);
54
  if (n * sizeof(struct cache_entry) != psize) {
55
    fprintf(stderr, "Wrong number of elems %d (%d / %d)!!!\n", n, psize, sizeof(struct cache_entry));
56
    return -1;
57
  }
58
#endif
59
  dst = blist_nodeid(c, 0);
60
  h->protocol = protocol;
61
  h->type = type;
62
  len = blist_payload_fill(pkt + sizeof(struct topo_header), MAX_MSG_SIZE - sizeof(struct topo_header), local_cache, dst, max_peers);
63
64
  res = len > 0 ? send_to_peer(blist_nodeid(myEntry, 0), dst, pkt, sizeof(struct topo_header) + len) : len;
65
66
  return res;
67
}
68
69
static int blist_topo_query_peer(struct peer_cache *local_cache, struct nodeID *dst, int protocol, int type, int max_peers)
70
{
71
  uint8_t pkt[MAX_MSG_SIZE];
72
  struct topo_header *h = (struct topo_header *)pkt;
73
  int len;
74
75
  h->protocol = protocol;
76
  h->type = type;
77
  len = blist_payload_fill(pkt + sizeof(struct topo_header), MAX_MSG_SIZE - sizeof(struct topo_header), local_cache, dst, max_peers);
78
  return len > 0  ? send_to_peer(blist_nodeid(myEntry, 0), dst, pkt, sizeof(struct topo_header) + len) : len;
79
}
80
81
int blist_ncast_reply(const struct peer_cache *c, struct peer_cache *local_cache)
82
{
83
  return blist_topo_reply(c, local_cache, MSG_TYPE_TOPOLOGY, NCAST_REPLY, 0);
84
}
85
86
int blist_tman_reply(const struct peer_cache *c, struct peer_cache *local_cache, int max_peers)
87
{
88
  return blist_topo_reply(c, local_cache, MSG_TYPE_TMAN, TMAN_REPLY, max_peers);
89
}
90
91
int blist_ncast_query_peer(struct peer_cache *local_cache, struct nodeID *dst)
92
{
93
  return blist_topo_query_peer(local_cache, dst, MSG_TYPE_TOPOLOGY, NCAST_QUERY, 0);
94
}
95
96
int blist_tman_query_peer(struct peer_cache *local_cache, struct nodeID *dst, int max_peers)
97
{
98
  return blist_topo_query_peer(local_cache, dst, MSG_TYPE_TMAN, TMAN_QUERY, max_peers);
99
}
100
101
int blist_ncast_query(struct peer_cache *local_cache)
102
{
103
  struct nodeID *dst;
104
105
  dst = blist_rand_peer(local_cache, NULL, 0);
106
  if (dst == NULL) {
107
    return 0;
108
  }
109
  return blist_topo_query_peer(local_cache, dst, MSG_TYPE_TOPOLOGY, NCAST_QUERY, 0);
110
}
111
112
int blist_proto_metadata_update(void *meta, int meta_size)
113
{
114
  if (blist_cache_metadata_update(myEntry, blist_nodeid(myEntry, 0), meta, meta_size) > 0) {
115
    return 1;
116
  }
117
118
  return -1;
119
}
120
121
int blist_proto_init(struct nodeID *s, void *meta, int meta_size)
122
{
123
  if (!myEntry) {
124
        myEntry = blist_cache_init(1, meta_size, 0);
125
        blist_cache_add(myEntry, s, meta, meta_size);
126
  }
127
  return 0;
128
}