Statistics
| Branch: | Revision:

grapes / src / TopologyManager / cloud_helper.c @ ef8002fd

History | View | Annotate | Download (2.63 KB)

1 5033613a Andrea Zito
#include <stdlib.h>
2
#include <string.h>
3
4
#include "cloud_helper.h"
5
#include "cloud_helper_iface.h"
6
#include "config.h"
7
8
extern struct cloud_helper_iface delegate;
9
10
struct cloud_helper_context {
11
  struct cloud_helper_iface *ch;
12
  struct cloud_helper_impl_context *ch_context;
13
};
14
15
static int ctx_counter = 0;
16
static struct nodeID* node_ids[CLOUD_HELPER_MAX_INSTANCES];
17
static struct cloud_helper_context* cloud_ctxs[CLOUD_HELPER_MAX_INSTANCES];
18
19
static int add_context(struct nodeID *local, struct cloud_helper_context *ctx)
20
{
21
  int i;
22
  if (ctx_counter >= CLOUD_HELPER_MAX_INSTANCES) return 1;
23
  
24
  for (i=0; i<ctx_counter; i++)
25
    if (nodeid_equal(node_ids[i], local)) return 0;
26
27
  node_ids[ctx_counter] = local;
28
  cloud_ctxs[ctx_counter] = ctx;
29
  ctx_counter++;
30
  
31
  return 1;
32
}
33
34
struct cloud_helper_context* cloud_helper_init(struct nodeID *local, const char *config)
35
{
36
  struct cloud_helper_context *ctx;
37
  struct tag *cfg_tags;
38
  const char *provider;
39
40
  cfg_tags = config_parse(config);
41
  provider = config_value_str(cfg_tags, "provider");
42
43
  if (!provider) return NULL;
44
45
  ctx = malloc(sizeof(struct cloud_helper_context));
46
  if (!ctx) return NULL;
47
  if (strcmp(provider, "delegate") == 0){
48
    ctx->ch = &delegate;
49
  }
50
   
51
 ctx->ch_context = ctx->ch->cloud_helper_init(local, config);
52
 if(!ctx->ch_context){
53
   free(ctx);
54
   return NULL;
55
 }
56
57
 if (!add_context(local, ctx)){
58
   //TODO: a better deallocation process is needed
59
   free(ctx->ch_context);
60
   free(ctx);
61
   return NULL;
62
 }
63
64
 return ctx;
65
}
66
67
struct cloud_helper_context* get_cloud_helper_for(struct nodeID *local){
68
  int i;
69
  for (i=0; i<ctx_counter; i++)
70
    if (node_ids[i] == local) return cloud_ctxs[i];
71
  
72
  return NULL;
73
}
74
75 89b16ba8 Andrea Zito
int get_from_cloud(struct cloud_helper_context *context, char *key, uint8_t *header_ptr, int header_size)
76 5033613a Andrea Zito
{
77 89b16ba8 Andrea Zito
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr, header_size);
78 5033613a Andrea Zito
}
79
80
int put_on_cloud(struct cloud_helper_context *context, char *key, uint8_t *buffer_ptr, int buffer_size)
81
{
82
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr, buffer_size);
83
}
84
85
struct nodeID* get_cloud_node(struct cloud_helper_context *context)
86
{
87
  return context->ch->get_cloud_node(context->ch_context);
88
}
89
90 ef8002fd Andrea Zito
int is_cloud_node(struct cloud_helper_context *context, struct nodeID* node)
91
{
92
  return context->ch->is_cloud_node(context->ch_context, node);
93
}
94
95 5033613a Andrea Zito
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
96
{
97
  return context->ch->wait4cloud(context->ch_context, tout);
98
}
99
100
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr, int buffer_size)
101
{
102
  return context->ch->recv_from_cloud(context->ch_context, buffer_ptr, buffer_size);
103
}