Statistics
| Branch: | Revision:

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

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 <semaphore.h>
11
#include <stdbool.h>
12 5033613a Andrea Zito
13
#include "cloud_helper.h"
14
#include "cloud_helper_iface.h"
15 6ebf2e47 Andrea Zito
16
#include "../Utils/fifo_queue.h"
17
18 5033613a Andrea Zito
#include "config.h"
19
20 6ebf2e47 Andrea Zito
#define CLOUD_HELPER_INITAIL_INSTANCES 2
21
22 0d550e4f Luca Abeni
#ifdef DELEGATE
23 5033613a Andrea Zito
extern struct cloud_helper_iface delegate;
24 0d550e4f Luca Abeni
#endif
25 5033613a Andrea Zito
26
struct cloud_helper_context {
27
  struct cloud_helper_iface *ch;
28
  struct cloud_helper_impl_context *ch_context;
29
};
30
31 6ebf2e47 Andrea Zito
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 5033613a Andrea Zito
38 b9e7dd7b Andrea Zito
static int add_context(const struct nodeID *local,
39
                       struct cloud_helper_context *ctx)
40 5033613a Andrea Zito
{
41
  int i;
42 6ebf2e47 Andrea Zito
  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 49b99aeb Andrea Zito
52 6ebf2e47 Andrea Zito
  /* 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 5033613a Andrea Zito
60 6ebf2e47 Andrea Zito
  /* Add the new entry to the ctx_map */
61
  entry = malloc(sizeof(struct ctx_map_entry));
62
  if (!entry) {
63
    return 1;
64
  }
65 49b99aeb Andrea Zito
66 6ebf2e47 Andrea Zito
  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 5033613a Andrea Zito
}
76
77 b9e7dd7b Andrea Zito
struct cloud_helper_context* cloud_helper_init(struct nodeID *local,
78
                                               const char *config)
79 5033613a Andrea Zito
{
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 0d550e4f Luca Abeni
  memset(ctx, 0, sizeof(struct cloud_helper_context));
92
#ifdef DELEGATE
93 5033613a Andrea Zito
  if (strcmp(provider, "delegate") == 0){
94
    ctx->ch = &delegate;
95
  }
96 0d550e4f Luca Abeni
#endif
97
  if (!ctx->ch) {
98
    free(ctx);
99
100
    return NULL;
101
  }
102 49b99aeb Andrea Zito
103 5033613a Andrea Zito
 ctx->ch_context = ctx->ch->cloud_helper_init(local, config);
104
 if(!ctx->ch_context){
105
   free(ctx);
106
   return NULL;
107
 }
108
109 6ebf2e47 Andrea Zito
 if (add_context(local, ctx) != 0){
110 5033613a Andrea Zito
   //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 b9e7dd7b Andrea Zito
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){
120 6ebf2e47 Andrea Zito
  struct cloud_helper_context *ctx;
121
  struct ctx_map_entry *entry;
122 5033613a Andrea Zito
  int i;
123 49b99aeb Andrea Zito
124 6ebf2e47 Andrea Zito
  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 5033613a Andrea Zito
}
131
132 b9e7dd7b Andrea Zito
int get_from_cloud(struct cloud_helper_context *context, const char *key,
133
                   uint8_t *header_ptr, int header_size, int free_header)
134 5033613a Andrea Zito
{
135 b9e7dd7b Andrea Zito
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr,
136
                                     header_size, free_header);
137 5033613a Andrea Zito
}
138
139 98b047dd Andrea Zito
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 b9e7dd7b Andrea Zito
int put_on_cloud(struct cloud_helper_context *context, const char *key,
149
                 uint8_t *buffer_ptr, int buffer_size, int free_buffer)
150 5033613a Andrea Zito
{
151 b9e7dd7b Andrea Zito
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr,
152
                                   buffer_size, free_buffer);
153 5033613a Andrea Zito
}
154
155 b9e7dd7b Andrea Zito
struct nodeID* get_cloud_node(struct cloud_helper_context *context,
156
                              uint8_t variant)
157 5033613a Andrea Zito
{
158 0e9bde83 Andrea Zito
  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 5033613a Andrea Zito
}
165
166 ef8002fd Andrea Zito
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 5033613a Andrea Zito
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
172
{
173
  return context->ch->wait4cloud(context->ch_context, tout);
174
}
175
176 b9e7dd7b Andrea Zito
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr,
177
                    int buffer_size)
178 5033613a Andrea Zito
{
179 b9e7dd7b Andrea Zito
  return context->ch->recv_from_cloud(context->ch_context, buffer_ptr,
180
                                      buffer_size);
181 5033613a Andrea Zito
}