Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.04 KB)

1
/*
2
 *  Copyright (c) 2011 Andrea Zito
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6

    
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <dlfcn.h>
10
#include <stdio.h>
11

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

    
15
struct delegate_iface {
16
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
17

    
18
  int (*get_from_cloud)(void *context, const char *key, uint8_t *header_ptr,
19
                        int header_size, int free_header);
20

    
21
  int (*get_from_cloud_default)(void *context, const char *key,
22
                                uint8_t *header_ptr, int header_size, int free_header,
23
                                uint8_t *defval_ptr, int defval_size, int free_defval);
24

    
25
  int (*put_on_cloud)(void *context, const char *key, uint8_t *buffer_ptr,
26
                      int buffer_size, int free_buffer);
27

    
28
  struct nodeID* (*get_cloud_node)(void *context, uint8_t variant);
29

    
30
  time_t (*timestamp_cloud)(void *context);
31

    
32
  int (*is_cloud_node)(void *context, struct nodeID* node);
33

    
34
  int (*wait4cloud)(void *context, struct timeval *tout);
35

    
36
  int (*recv_from_cloud)(void *context, uint8_t *buffer_ptr, int buffer_size);
37
};
38

    
39
struct cloud_helper_impl_context {
40
  struct delegate_iface *delegate;
41
  void *delegate_context;
42
};
43

    
44
static struct cloud_helper_impl_context*
45
delegate_cloud_init(struct nodeID *local, const char *config)
46
{
47
  struct cloud_helper_impl_context *ctx;
48
  struct tag *cfg_tags;
49
  const char *dlib_name;
50
  struct delegate_iface *delegate_impl;
51
  void *dlib;
52

    
53
  cfg_tags = grapes_config_parse(config);
54
  dlib_name = grapes_config_value_str(cfg_tags, "delegate_lib");
55
  dlib = dlopen(dlib_name, RTLD_NOW);
56
  if (dlib == NULL) {
57
    printf("error: %s", dlerror());
58
    return NULL;
59
  }
60

    
61

    
62
  delegate_impl = (struct delegate_iface *) dlsym(dlib, "delegate_impl");
63
  if (!delegate_impl) return NULL;
64

    
65
  ctx = malloc(sizeof(struct cloud_helper_impl_context));
66
  ctx->delegate = delegate_impl;
67

    
68
  ctx->delegate_context = ctx->delegate->cloud_helper_init(local, config);
69
  if(!ctx->delegate_context) {
70
    free(ctx);
71
    return NULL;
72
  }
73

    
74
  return ctx;
75
}
76

    
77
static int
78
delegate_cloud_get_from_cloud(struct cloud_helper_impl_context *context,
79
                              const char *key, uint8_t *header_ptr,
80
                              int header_size, int free_header)
81
{
82
  return context->delegate->get_from_cloud(context->delegate_context, key,
83
                                           header_ptr, header_size,
84
                                           free_header);
85
}
86

    
87
static int
88
delegate_cloud_get_from_cloud_default(struct cloud_helper_impl_context *context,
89
                                      const char *key, uint8_t *header_ptr,
90
                                      int header_size, int free_header,
91
                                      uint8_t *defval_ptr, int defval_size,
92
                                      int free_defval)
93
{
94
  return context->delegate->get_from_cloud_default(context->delegate_context,
95
                                                   key, header_ptr, header_size,
96
                                                   free_header, defval_ptr,
97
                                                   defval_size, free_defval);
98
}
99

    
100
static int
101
delegate_cloud_put_on_cloud(struct cloud_helper_impl_context *context,
102
                            const char *key, uint8_t *buffer_ptr,
103
                            int buffer_size, int free_buffer)
104
{
105
  return context->delegate->put_on_cloud(context->delegate_context, key,
106
                                         buffer_ptr, buffer_size, free_buffer);
107
}
108
static struct nodeID*
109
delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context,
110
                              uint8_t variant)
111
{
112
  return context->delegate->get_cloud_node(context->delegate_context, variant);
113
}
114

    
115
static time_t
116
delegate_timestamp_cloud(struct cloud_helper_impl_context *context)
117
{
118
  return context->delegate->timestamp_cloud(context->delegate_context);
119
}
120

    
121
int delegate_is_cloud_node(struct cloud_helper_impl_context *context,
122
                           struct nodeID* node)
123
{
124
  return context->delegate->is_cloud_node(context->delegate_context, node);
125
}
126

    
127
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context,
128
                                     struct timeval *tout)
129
{
130
  return context->delegate->wait4cloud(context->delegate_context, tout);
131
}
132

    
133
static int
134
delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context,
135
                               uint8_t *buffer_ptr, int buffer_size)
136
{
137
  return context->delegate->recv_from_cloud(context->delegate_context,
138
                                            buffer_ptr, buffer_size);
139
}
140

    
141
struct cloud_helper_iface delegate = {
142
  .cloud_helper_init = delegate_cloud_init,
143
  .get_from_cloud = delegate_cloud_get_from_cloud,
144
  .get_from_cloud_default = delegate_cloud_get_from_cloud_default,
145
  .put_on_cloud = delegate_cloud_put_on_cloud,
146
  .get_cloud_node = delegate_cloud_get_cloud_node,
147
  .timestamp_cloud = delegate_timestamp_cloud,
148
  .is_cloud_node = delegate_is_cloud_node,
149
  .wait4cloud = delegate_cloud_wait4cloud,
150
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
151
};