Statistics
| Branch: | Revision:

grapes / src / CloudSupport / cloud_helper.c @ 0d550e4f

History | View | Annotate | Download (4.56 KB)

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

    
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <string.h>
10
#include <semaphore.h>
11
#include <stdbool.h>
12

    
13
#include "cloud_helper.h"
14
#include "cloud_helper_iface.h"
15

    
16
#include "../Utils/fifo_queue.h"
17

    
18
#include "config.h"
19

    
20
#define CLOUD_HELPER_INITAIL_INSTANCES 2
21

    
22
#ifdef DELEGATE
23
extern struct cloud_helper_iface delegate;
24
#endif
25

    
26
struct cloud_helper_context {
27
  struct cloud_helper_iface *ch;
28
  struct cloud_helper_impl_context *ch_context;
29
};
30

    
31
struct ctx_map_entry {
32
  const struct nodeID *node;
33
  struct cloud_helper_context *cloud_ctx;
34
};
35

    
36
static struct fifo_queue *ctx_map = NULL;
37

    
38
static int add_context(const struct nodeID *local,
39
                       struct cloud_helper_context *ctx)
40
{
41
  int i;
42
  struct ctx_map_entry *entry;
43

    
44
  /* Checks whether the queue is already initialized */
45
  if (ctx_map == NULL) {
46
    ctx_map = fifo_queue_create(CLOUD_HELPER_INITAIL_INSTANCES);
47
    if (!ctx_map) {
48
      return 1;
49
    }
50
  }
51

    
52
  /* Check if the node is already present in the ctx_map */
53
  for (i=0; i<fifo_queue_size(ctx_map); i++) {
54
    entry = fifo_queue_get(ctx_map, i);
55
    if (nodeid_equal(entry->node, local)) {
56
      return 1;
57
    }
58
  }
59

    
60
  /* Add the new entry to the ctx_map */
61
  entry = malloc(sizeof(struct ctx_map_entry));
62
  if (!entry) {
63
    return 1;
64
  }
65

    
66
  entry->node = local;
67
  entry->cloud_ctx = ctx;
68

    
69
  if (fifo_queue_add(ctx_map, entry) != 0) {
70
    free (entry);
71
    return 1;
72
  }
73

    
74
  return 0;
75
}
76

    
77
struct cloud_helper_context* cloud_helper_init(struct nodeID *local,
78
                                               const char *config)
79
{
80
  struct cloud_helper_context *ctx;
81
  struct tag *cfg_tags;
82
  const char *provider;
83

    
84
  cfg_tags = config_parse(config);
85
  provider = config_value_str(cfg_tags, "provider");
86

    
87
  if (!provider) return NULL;
88

    
89
  ctx = malloc(sizeof(struct cloud_helper_context));
90
  if (!ctx) return NULL;
91
  memset(ctx, 0, sizeof(struct cloud_helper_context));
92
#ifdef DELEGATE
93
  if (strcmp(provider, "delegate") == 0){
94
    ctx->ch = &delegate;
95
  }
96
#endif
97
  if (!ctx->ch) {
98
    free(ctx);
99

    
100
    return NULL;
101
  }
102

    
103
 ctx->ch_context = ctx->ch->cloud_helper_init(local, config);
104
 if(!ctx->ch_context){
105
   free(ctx);
106
   return NULL;
107
 }
108

    
109
 if (add_context(local, ctx) != 0){
110
   //TODO: a better deallocation process is needed
111
   free(ctx->ch_context);
112
   free(ctx);
113
   return NULL;
114
 }
115

    
116
 return ctx;
117
}
118

    
119
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){
120
  struct cloud_helper_context *ctx;
121
  struct ctx_map_entry *entry;
122
  int i;
123

    
124
  ctx = NULL;
125
  for (i=0; i<fifo_queue_size(ctx_map); i++) {
126
    entry = fifo_queue_get(ctx_map, i);
127
    if (nodeid_equal(entry->node, local)) ctx = entry->cloud_ctx;
128
  }
129
  return ctx;
130
}
131

    
132
int get_from_cloud(struct cloud_helper_context *context, const char *key,
133
                   uint8_t *header_ptr, int header_size, int free_header)
134
{
135
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr,
136
                                     header_size, free_header);
137
}
138

    
139
int get_from_cloud_default(struct cloud_helper_context *context, const char *key,
140
                           uint8_t *header_ptr, int header_size, int free_header,
141
                           uint8_t *defval_ptr, int defval_size, int free_defval)
142
{
143
  return context->ch->get_from_cloud_default(context->ch_context, key, header_ptr,
144
                                             header_size, free_header, defval_ptr,
145
                                             defval_size, free_defval);
146
}
147

    
148
int put_on_cloud(struct cloud_helper_context *context, const char *key,
149
                 uint8_t *buffer_ptr, int buffer_size, int free_buffer)
150
{
151
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr,
152
                                   buffer_size, free_buffer);
153
}
154

    
155
struct nodeID* get_cloud_node(struct cloud_helper_context *context,
156
                              uint8_t variant)
157
{
158
  return context->ch->get_cloud_node(context->ch_context, variant);
159
}
160

    
161
time_t timestamp_cloud(struct cloud_helper_context *context)
162
{
163
  return context->ch->timestamp_cloud(context->ch_context);
164
}
165

    
166
int is_cloud_node(struct cloud_helper_context *context, struct nodeID* node)
167
{
168
  return context->ch->is_cloud_node(context->ch_context, node);
169
}
170

    
171
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
172
{
173
  return context->ch->wait4cloud(context->ch_context, tout);
174
}
175

    
176
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr,
177
                    int buffer_size)
178
{
179
  return context->ch->recv_from_cloud(context->ch_context, buffer_ptr,
180
                                      buffer_size);
181
}