Statistics
| Branch: | Revision:

grapes / src / Cache / ncast_proto.c @ 10ddaca7

History | View | Annotate | Download (2.16 KB)

1
/*
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 "ncast_proto.h"
16
#include "grapes_msg_types.h"
17

    
18
struct ncast_proto_context {
19
  struct topo_context *context;
20
};
21

    
22
struct ncast_proto_context* ncast_proto_init(struct nodeID *s, const void *meta, int meta_size)
23
{
24
  struct ncast_proto_context *con;
25
  con = malloc(sizeof(struct ncast_proto_context));
26

    
27
  if (!con) return NULL;
28

    
29
  con->context = topo_proto_init(s, meta, meta_size);
30
  if (!con->context){
31
    free(con);
32
    return NULL;
33
  }
34

    
35
  return con;
36
}
37

    
38
int ncast_reply(struct ncast_proto_context *context, const struct peer_cache *c, const struct peer_cache *local_cache)
39
{
40
  int ret;
41
  struct peer_cache *send_cache;
42

    
43
  send_cache = cache_copy(local_cache);
44
  cache_update(send_cache);
45
  ret = topo_reply(context->context, c, send_cache, MSG_TYPE_TOPOLOGY, NCAST_REPLY, 0, 1);
46
  cache_free(send_cache);
47

    
48
  return ret;
49
}
50

    
51
int ncast_query_peer(struct ncast_proto_context *context, const struct peer_cache *local_cache, struct nodeID *dst)
52
{
53
  return topo_query_peer(context->context, local_cache, dst, MSG_TYPE_TOPOLOGY, NCAST_QUERY, 0);
54
}
55

    
56
int ncast_query(struct ncast_proto_context *context, const struct peer_cache *local_cache)
57
{
58
  struct nodeID *dst;
59

    
60
  dst = rand_peer(local_cache, NULL, 0);
61
  if (dst == NULL) {
62
    //fprintf(stderr,"[DEBUG] Megaerror, no peer selected for TOPO\n");
63
    return 0;
64
  }
65
  return topo_query_peer(context->context, local_cache, dst, MSG_TYPE_TOPOLOGY, NCAST_QUERY, 0);
66
}
67

    
68
int ncast_proto_metadata_update(struct ncast_proto_context *context, const void *meta, int meta_size){
69
  return topo_proto_metadata_update(context->context, meta, meta_size);
70
}
71

    
72
int ncast_proto_myentry_update(struct ncast_proto_context *context, struct nodeID *s, int dts, const void *meta, int meta_size) {
73
  return topo_proto_myentry_update(context->context, s, dts, meta, meta_size);
74
}
75

    
76
void ncast_proto_destroy(struct ncast_proto_context ** c)
77
{
78
        if (c && *c)
79
        {
80
                if ((*c)->context)
81
                        topo_proto_destroy(&((*c)->context));
82
                free(*c);
83
                *c = NULL;
84
        }
85
}