grapes / src / CloudSupport / cloud_helper.c @ 4692000a
History | View | Annotate | Download (2.86 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2010 Andrea Zito
|
3 |
*
|
4 |
* This is free software; see lgpl-2.1.txt
|
5 |
*/
|
6 |
|
7 |
#include <stdlib.h> |
8 |
#include <string.h> |
9 |
|
10 |
#include "cloud_helper.h" |
11 |
#include "cloud_helper_iface.h" |
12 |
#include "config.h" |
13 |
|
14 |
extern struct cloud_helper_iface delegate; |
15 |
|
16 |
struct cloud_helper_context {
|
17 |
struct cloud_helper_iface *ch;
|
18 |
struct cloud_helper_impl_context *ch_context;
|
19 |
}; |
20 |
|
21 |
static int ctx_counter = 0; |
22 |
static struct nodeID* node_ids[CLOUD_HELPER_MAX_INSTANCES]; |
23 |
static struct cloud_helper_context* cloud_ctxs[CLOUD_HELPER_MAX_INSTANCES]; |
24 |
|
25 |
static int add_context(struct nodeID *local, struct cloud_helper_context *ctx) |
26 |
{ |
27 |
int i;
|
28 |
if (ctx_counter >= CLOUD_HELPER_MAX_INSTANCES) return 1; |
29 |
|
30 |
for (i=0; i<ctx_counter; i++) |
31 |
if (nodeid_equal(node_ids[i], local)) return 0; |
32 |
|
33 |
node_ids[ctx_counter] = local; |
34 |
cloud_ctxs[ctx_counter] = ctx; |
35 |
ctx_counter++; |
36 |
|
37 |
return 1; |
38 |
} |
39 |
|
40 |
struct cloud_helper_context* cloud_helper_init(struct nodeID *local, const char *config) |
41 |
{ |
42 |
struct cloud_helper_context *ctx;
|
43 |
struct tag *cfg_tags;
|
44 |
const char *provider; |
45 |
|
46 |
cfg_tags = config_parse(config); |
47 |
provider = config_value_str(cfg_tags, "provider");
|
48 |
|
49 |
if (!provider) return NULL; |
50 |
|
51 |
ctx = malloc(sizeof(struct cloud_helper_context)); |
52 |
if (!ctx) return NULL; |
53 |
if (strcmp(provider, "delegate") == 0){ |
54 |
ctx->ch = &delegate; |
55 |
} |
56 |
|
57 |
ctx->ch_context = ctx->ch->cloud_helper_init(local, config); |
58 |
if(!ctx->ch_context){
|
59 |
free(ctx); |
60 |
return NULL; |
61 |
} |
62 |
|
63 |
if (!add_context(local, ctx)){
|
64 |
//TODO: a better deallocation process is needed
|
65 |
free(ctx->ch_context); |
66 |
free(ctx); |
67 |
return NULL; |
68 |
} |
69 |
|
70 |
return ctx;
|
71 |
} |
72 |
|
73 |
struct cloud_helper_context* get_cloud_helper_for(struct nodeID *local){ |
74 |
int i;
|
75 |
for (i=0; i<ctx_counter; i++) |
76 |
if (node_ids[i] == local) return cloud_ctxs[i]; |
77 |
|
78 |
return NULL; |
79 |
} |
80 |
|
81 |
int get_from_cloud(struct cloud_helper_context *context, char *key, uint8_t *header_ptr, int header_size) |
82 |
{ |
83 |
return context->ch->get_from_cloud(context->ch_context, key, header_ptr, header_size);
|
84 |
} |
85 |
|
86 |
int put_on_cloud(struct cloud_helper_context *context, char *key, uint8_t *buffer_ptr, int buffer_size) |
87 |
{ |
88 |
return context->ch->put_on_cloud(context->ch_context, key, buffer_ptr, buffer_size);
|
89 |
} |
90 |
|
91 |
struct nodeID* get_cloud_node(struct cloud_helper_context *context, uint8_t variant) |
92 |
{ |
93 |
return context->ch->get_cloud_node(context->ch_context, variant);
|
94 |
} |
95 |
|
96 |
time_t timestamp_cloud(struct cloud_helper_context *context)
|
97 |
{ |
98 |
return context->ch->timestamp_cloud(context->ch_context);
|
99 |
} |
100 |
|
101 |
int is_cloud_node(struct cloud_helper_context *context, struct nodeID* node) |
102 |
{ |
103 |
return context->ch->is_cloud_node(context->ch_context, node);
|
104 |
} |
105 |
|
106 |
int wait4cloud(struct cloud_helper_context *context, struct timeval *tout) |
107 |
{ |
108 |
return context->ch->wait4cloud(context->ch_context, tout);
|
109 |
} |
110 |
|
111 |
int recv_from_cloud(struct cloud_helper_context *context, uint8_t *buffer_ptr, int buffer_size) |
112 |
{ |
113 |
return context->ch->recv_from_cloud(context->ch_context, buffer_ptr, buffer_size);
|
114 |
} |