Statistics
| Branch: | Revision:

grapes / src / CloudSupport / cloud_helper.c @ b9e7dd7b

History | View | Annotate | Download (3.21 KB)

1
/*
2
 *  Copyright (c) 2010 Andrea Zito
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <stdlib.h>
8
#include <string.h>
9

    
10
#include "cloud_helper.h"
11
#include "cloud_helper_iface.h"
12
#include "config.h"
13

    
14
extern struct cloud_helper_iface delegate;
15

    
16
struct cloud_helper_context {
17
  struct cloud_helper_iface *ch;
18
  struct cloud_helper_impl_context *ch_context;
19
};
20

    
21
static int ctx_counter = 0;
22
static const struct nodeID* node_ids[CLOUD_HELPER_MAX_INSTANCES];
23
static struct cloud_helper_context* cloud_ctxs[CLOUD_HELPER_MAX_INSTANCES];
24

    
25
static int add_context(const struct nodeID *local,
26
                       struct cloud_helper_context *ctx)
27
{
28
  int i;
29
  if (ctx_counter >= CLOUD_HELPER_MAX_INSTANCES) return 1;
30

    
31
  for (i=0; i<ctx_counter; i++)
32
    if (nodeid_equal(node_ids[i], local)) return 0;
33

    
34
  node_ids[ctx_counter] = local;
35
  cloud_ctxs[ctx_counter] = ctx;
36
  ctx_counter++;
37

    
38
  return 1;
39
}
40

    
41
struct cloud_helper_context* cloud_helper_init(struct nodeID *local,
42
                                               const char *config)
43
{
44
  struct cloud_helper_context *ctx;
45
  struct tag *cfg_tags;
46
  const char *provider;
47

    
48
  cfg_tags = config_parse(config);
49
  provider = config_value_str(cfg_tags, "provider");
50

    
51
  if (!provider) return NULL;
52

    
53
  ctx = malloc(sizeof(struct cloud_helper_context));
54
  if (!ctx) return NULL;
55
  if (strcmp(provider, "delegate") == 0){
56
    ctx->ch = &delegate;
57
  }
58

    
59
 ctx->ch_context = ctx->ch->cloud_helper_init(local, config);
60
 if(!ctx->ch_context){
61
   free(ctx);
62
   return NULL;
63
 }
64

    
65
 if (!add_context(local, ctx)){
66
   //TODO: a better deallocation process is needed
67
   free(ctx->ch_context);
68
   free(ctx);
69
   return NULL;
70
 }
71

    
72
 return ctx;
73
}
74

    
75
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){
76
  int i;
77
  for (i=0; i<ctx_counter; i++)
78
    if (node_ids[i] == local) return cloud_ctxs[i];
79

    
80
  return NULL;
81
}
82

    
83
int get_from_cloud(struct cloud_helper_context *context, const char *key,
84
                   uint8_t *header_ptr, int header_size, int free_header)
85
{
86
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr,
87
                                     header_size, free_header);
88
}
89

    
90
int put_on_cloud(struct cloud_helper_context *context, const char *key,
91
                 uint8_t *buffer_ptr, int buffer_size, int free_buffer)
92
{
93
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr,
94
                                   buffer_size, free_buffer);
95
}
96

    
97
struct nodeID* get_cloud_node(struct cloud_helper_context *context,
98
                              uint8_t variant)
99
{
100
  return context->ch->get_cloud_node(context->ch_context, variant);
101
}
102

    
103
time_t timestamp_cloud(struct cloud_helper_context *context)
104
{
105
  return context->ch->timestamp_cloud(context->ch_context);
106
}
107

    
108
int is_cloud_node(struct cloud_helper_context *context, struct nodeID* node)
109
{
110
  return context->ch->is_cloud_node(context->ch_context, node);
111
}
112

    
113
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
114
{
115
  return context->ch->wait4cloud(context->ch_context, tout);
116
}
117

    
118
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr,
119
                    int buffer_size)
120
{
121
  return context->ch->recv_from_cloud(context->ch_context, buffer_ptr,
122
                                      buffer_size);
123
}