Statistics
| Branch: | Revision:

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

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 <stdbool.h>
11

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

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

    
17
#include "grapes_config.h"
18

    
19
#define CLOUD_HELPER_INITAIL_INSTANCES 2
20

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

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

    
30
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

    
37
static int add_context(const struct nodeID *local,
38
                       struct cloud_helper_context *ctx)
39
{
40
  int i;
41
  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

    
51
  /* 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

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

    
65
  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
}
75

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

    
83
  cfg_tags = grapes_config_parse(config);
84
  provider = grapes_config_value_str(cfg_tags, "provider");
85

    
86
  if (!provider) return NULL;
87

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

    
99
    return NULL;
100
  }
101

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

    
108
 if (add_context(local, ctx) != 0){
109
   //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
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){
119
  struct cloud_helper_context *ctx;
120
  struct ctx_map_entry *entry;
121
  int i;
122

    
123
  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
}
130

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

    
138
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
int put_on_cloud(struct cloud_helper_context *context, const char *key,
148
                 uint8_t *buffer_ptr, int buffer_size, int free_buffer)
149
{
150
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr,
151
                                   buffer_size, free_buffer);
152
}
153

    
154
struct nodeID* get_cloud_node(struct cloud_helper_context *context,
155
                              uint8_t variant)
156
{
157
  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
}
164

    
165
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
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
171
{
172
  return context->ch->wait4cloud(context->ch_context, tout);
173
}
174

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