Statistics
| Branch: | Revision:

grapes / src / CloudSupport / cloud_helper.c @ 176b8de8

History | View | Annotate | Download (4.56 KB)

1 dd3024b0 Andrea Zito
/*
2
 *  Copyright (c) 2010 Andrea Zito
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6
7 6ebf2e47 Andrea Zito
#include <stdio.h>
8 5033613a Andrea Zito
#include <stdlib.h>
9
#include <string.h>
10 6ebf2e47 Andrea Zito
#include <stdbool.h>
11 5033613a Andrea Zito
12
#include "cloud_helper.h"
13
#include "cloud_helper_iface.h"
14 6ebf2e47 Andrea Zito
15
#include "../Utils/fifo_queue.h"
16
17 176b8de8 Luca Baldesi
#include "grapes_config.h"
18 5033613a Andrea Zito
19 6ebf2e47 Andrea Zito
#define CLOUD_HELPER_INITAIL_INSTANCES 2
20
21 0d550e4f Luca Abeni
#ifdef DELEGATE
22 5033613a Andrea Zito
extern struct cloud_helper_iface delegate;
23 0d550e4f Luca Abeni
#endif
24 5033613a Andrea Zito
25
struct cloud_helper_context {
26
  struct cloud_helper_iface *ch;
27
  struct cloud_helper_impl_context *ch_context;
28
};
29
30 6ebf2e47 Andrea Zito
struct ctx_map_entry {
31
  const struct nodeID *node;
32
  struct cloud_helper_context *cloud_ctx;
33
};
34
35
static struct fifo_queue *ctx_map = NULL;
36 5033613a Andrea Zito
37 b9e7dd7b Andrea Zito
static int add_context(const struct nodeID *local,
38
                       struct cloud_helper_context *ctx)
39 5033613a Andrea Zito
{
40
  int i;
41 6ebf2e47 Andrea Zito
  struct ctx_map_entry *entry;
42
43
  /* Checks whether the queue is already initialized */
44
  if (ctx_map == NULL) {
45
    ctx_map = fifo_queue_create(CLOUD_HELPER_INITAIL_INSTANCES);
46
    if (!ctx_map) {
47
      return 1;
48
    }
49
  }
50 49b99aeb Andrea Zito
51 6ebf2e47 Andrea Zito
  /* Check if the node is already present in the ctx_map */
52
  for (i=0; i<fifo_queue_size(ctx_map); i++) {
53
    entry = fifo_queue_get(ctx_map, i);
54
    if (nodeid_equal(entry->node, local)) {
55
      return 1;
56
    }
57
  }
58 5033613a Andrea Zito
59 6ebf2e47 Andrea Zito
  /* Add the new entry to the ctx_map */
60
  entry = malloc(sizeof(struct ctx_map_entry));
61
  if (!entry) {
62
    return 1;
63
  }
64 49b99aeb Andrea Zito
65 6ebf2e47 Andrea Zito
  entry->node = local;
66
  entry->cloud_ctx = ctx;
67
68
  if (fifo_queue_add(ctx_map, entry) != 0) {
69
    free (entry);
70
    return 1;
71
  }
72
73
  return 0;
74 5033613a Andrea Zito
}
75
76 b9e7dd7b Andrea Zito
struct cloud_helper_context* cloud_helper_init(struct nodeID *local,
77
                                               const char *config)
78 5033613a Andrea Zito
{
79
  struct cloud_helper_context *ctx;
80
  struct tag *cfg_tags;
81
  const char *provider;
82
83 176b8de8 Luca Baldesi
  cfg_tags = grapes_config_parse(config);
84
  provider = grapes_config_value_str(cfg_tags, "provider");
85 5033613a Andrea Zito
86
  if (!provider) return NULL;
87
88
  ctx = malloc(sizeof(struct cloud_helper_context));
89
  if (!ctx) return NULL;
90 0d550e4f Luca Abeni
  memset(ctx, 0, sizeof(struct cloud_helper_context));
91
#ifdef DELEGATE
92 5033613a Andrea Zito
  if (strcmp(provider, "delegate") == 0){
93
    ctx->ch = &delegate;
94
  }
95 0d550e4f Luca Abeni
#endif
96
  if (!ctx->ch) {
97
    free(ctx);
98
99
    return NULL;
100
  }
101 49b99aeb Andrea Zito
102 5033613a Andrea Zito
 ctx->ch_context = ctx->ch->cloud_helper_init(local, config);
103
 if(!ctx->ch_context){
104
   free(ctx);
105
   return NULL;
106
 }
107
108 6ebf2e47 Andrea Zito
 if (add_context(local, ctx) != 0){
109 5033613a Andrea Zito
   //TODO: a better deallocation process is needed
110
   free(ctx->ch_context);
111
   free(ctx);
112
   return NULL;
113
 }
114
115
 return ctx;
116
}
117
118 b9e7dd7b Andrea Zito
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){
119 6ebf2e47 Andrea Zito
  struct cloud_helper_context *ctx;
120
  struct ctx_map_entry *entry;
121 5033613a Andrea Zito
  int i;
122 49b99aeb Andrea Zito
123 6ebf2e47 Andrea Zito
  ctx = NULL;
124
  for (i=0; i<fifo_queue_size(ctx_map); i++) {
125
    entry = fifo_queue_get(ctx_map, i);
126
    if (nodeid_equal(entry->node, local)) ctx = entry->cloud_ctx;
127
  }
128
  return ctx;
129 5033613a Andrea Zito
}
130
131 b9e7dd7b Andrea Zito
int get_from_cloud(struct cloud_helper_context *context, const char *key,
132
                   uint8_t *header_ptr, int header_size, int free_header)
133 5033613a Andrea Zito
{
134 b9e7dd7b Andrea Zito
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr,
135
                                     header_size, free_header);
136 5033613a Andrea Zito
}
137
138 98b047dd Andrea Zito
int get_from_cloud_default(struct cloud_helper_context *context, const char *key,
139
                           uint8_t *header_ptr, int header_size, int free_header,
140
                           uint8_t *defval_ptr, int defval_size, int free_defval)
141
{
142
  return context->ch->get_from_cloud_default(context->ch_context, key, header_ptr,
143
                                             header_size, free_header, defval_ptr,
144
                                             defval_size, free_defval);
145
}
146
147 b9e7dd7b Andrea Zito
int put_on_cloud(struct cloud_helper_context *context, const char *key,
148
                 uint8_t *buffer_ptr, int buffer_size, int free_buffer)
149 5033613a Andrea Zito
{
150 b9e7dd7b Andrea Zito
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr,
151
                                   buffer_size, free_buffer);
152 5033613a Andrea Zito
}
153
154 b9e7dd7b Andrea Zito
struct nodeID* get_cloud_node(struct cloud_helper_context *context,
155
                              uint8_t variant)
156 5033613a Andrea Zito
{
157 0e9bde83 Andrea Zito
  return context->ch->get_cloud_node(context->ch_context, variant);
158
}
159
160
time_t timestamp_cloud(struct cloud_helper_context *context)
161
{
162
  return context->ch->timestamp_cloud(context->ch_context);
163 5033613a Andrea Zito
}
164
165 ef8002fd Andrea Zito
int is_cloud_node(struct cloud_helper_context *context, struct nodeID* node)
166
{
167
  return context->ch->is_cloud_node(context->ch_context, node);
168
}
169
170 5033613a Andrea Zito
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
171
{
172
  return context->ch->wait4cloud(context->ch_context, tout);
173
}
174
175 b9e7dd7b Andrea Zito
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr,
176
                    int buffer_size)
177 5033613a Andrea Zito
{
178 b9e7dd7b Andrea Zito
  return context->ch->recv_from_cloud(context->ch_context, buffer_ptr,
179
                                      buffer_size);
180 5033613a Andrea Zito
}