Statistics
| Branch: | Revision:

grapes / src / CloudSupport / cloud_helper.c @ 98b047dd

History | View | Annotate | Download (4.41 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
extern struct cloud_helper_iface delegate;
23

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

    
29
struct ctx_map_entry {
30
  const struct nodeID *node;
31
  struct cloud_helper_context *cloud_ctx;
32
};
33

    
34
static struct fifo_queue *ctx_map = NULL;
35

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

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

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

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

    
64
  entry->node = local;
65
  entry->cloud_ctx = ctx;
66

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

    
72
  return 0;
73
}
74

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

    
82
  cfg_tags = config_parse(config);
83
  provider = config_value_str(cfg_tags, "provider");
84

    
85
  if (!provider) return NULL;
86

    
87
  ctx = malloc(sizeof(struct cloud_helper_context));
88
  if (!ctx) return NULL;
89
  if (strcmp(provider, "delegate") == 0){
90
    ctx->ch = &delegate;
91
  }
92

    
93
 ctx->ch_context = ctx->ch->cloud_helper_init(local, config);
94
 if(!ctx->ch_context){
95
   free(ctx);
96
   return NULL;
97
 }
98

    
99
 if (add_context(local, ctx) != 0){
100
   //TODO: a better deallocation process is needed
101
   free(ctx->ch_context);
102
   free(ctx);
103
   return NULL;
104
 }
105

    
106
 return ctx;
107
}
108

    
109
struct cloud_helper_context* get_cloud_helper_for(const struct nodeID *local){
110
  struct cloud_helper_context *ctx;
111
  struct ctx_map_entry *entry;
112
  int i;
113

    
114
  ctx = NULL;
115
  for (i=0; i<fifo_queue_size(ctx_map); i++) {
116
    entry = fifo_queue_get(ctx_map, i);
117
    if (nodeid_equal(entry->node, local)) ctx = entry->cloud_ctx;
118
  }
119
  return ctx;
120
}
121

    
122
int get_from_cloud(struct cloud_helper_context *context, const char *key,
123
                   uint8_t *header_ptr, int header_size, int free_header)
124
{
125
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr,
126
                                     header_size, free_header);
127
}
128

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

    
138
int put_on_cloud(struct cloud_helper_context *context, const char *key,
139
                 uint8_t *buffer_ptr, int buffer_size, int free_buffer)
140
{
141
  return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr,
142
                                   buffer_size, free_buffer);
143
}
144

    
145
struct nodeID* get_cloud_node(struct cloud_helper_context *context,
146
                              uint8_t variant)
147
{
148
  return context->ch->get_cloud_node(context->ch_context, variant);
149
}
150

    
151
time_t timestamp_cloud(struct cloud_helper_context *context)
152
{
153
  return context->ch->timestamp_cloud(context->ch_context);
154
}
155

    
156
int is_cloud_node(struct cloud_helper_context *context, struct nodeID* node)
157
{
158
  return context->ch->is_cloud_node(context->ch_context, node);
159
}
160

    
161
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout)
162
{
163
  return context->ch->wait4cloud(context->ch_context, tout);
164
}
165

    
166
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr,
167
                    int buffer_size)
168
{
169
  return context->ch->recv_from_cloud(context->ch_context, buffer_ptr,
170
                                      buffer_size);
171
}