Statistics
| Branch: | Revision:

grapes / src / TopologyManager / cloud_helper_delegate.c @ ef8002fd

History | View | Annotate | Download (3.09 KB)

1 27079cd9 Andrea Zito
#include <stdlib.h>
2
#include <stdint.h>
3
#include <dlfcn.h>
4
5
#include "cloud_helper_iface.h"
6
#include "config.h"
7
8
struct delegate_iface {
9
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
10 89b16ba8 Andrea Zito
  int (*get_from_cloud)(void *context, char *key, uint8_t *header_ptr, int header_size);
11 27079cd9 Andrea Zito
  int (*put_on_cloud)(void *context, char *key, uint8_t *buffer_ptr, int buffer_size);
12
  struct nodeID* (*get_cloud_node)(void *context);
13 ef8002fd Andrea Zito
  int (*is_cloud_node)(void *context, struct nodeID* node);
14 27079cd9 Andrea Zito
  int (*wait4cloud)(void *context, struct timeval *tout);
15
  int (*recv_from_cloud)(void *context, uint8_t *buffer_ptr, int buffer_size);
16
};
17
18
struct cloud_helper_impl_context {
19
  struct delegate_iface *delegate;
20
  void *delegate_context;
21
};
22
23
static struct cloud_helper_impl_context* delegate_cloud_init(struct nodeID *local, const char *config)
24
{
25
  struct cloud_helper_impl_context *ctx;
26
  struct tag *cfg_tags;
27
  const char *dlib_name;
28
  struct delegate_iface *delegate_impl;
29
  void *dlib;
30
31
  cfg_tags = config_parse(config);
32
  dlib_name = config_value_str(cfg_tags, "delegate_lib");
33
  dlib = dlopen(dlib_name, RTLD_NOW);
34
  if (dlib == NULL) {
35
    printf("error: %s", dlerror());
36
    return NULL;
37
  }
38
  
39
40
  delegate_impl = (struct delegate_iface *) dlsym(dlib, "delegate_impl");
41
  if (!delegate_impl) return NULL;
42
43
  ctx = malloc(sizeof(struct cloud_helper_impl_context));
44
  ctx->delegate = delegate_impl;
45
 
46
  ctx->delegate_context = ctx->delegate->cloud_helper_init(local, config);
47
  if(!ctx->delegate_context) {
48
    free(ctx);
49
    return NULL;
50
  }
51
52
  return ctx;
53
}
54
55 89b16ba8 Andrea Zito
static int delegate_cloud_get_from_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *header_ptr, int header_size)
56 27079cd9 Andrea Zito
{
57 89b16ba8 Andrea Zito
  return context->delegate->get_from_cloud(context->delegate_context, key, header_ptr, header_size);
58 27079cd9 Andrea Zito
}
59
60
static int delegate_cloud_put_on_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *buffer_ptr, int buffer_size)
61
{
62
  return context->delegate->put_on_cloud(context->delegate_context, key, buffer_ptr, buffer_size);
63
}
64
65
static struct nodeID* delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context)
66
{
67
  return context->delegate->get_cloud_node(context->delegate_context);
68
}
69
70 ef8002fd Andrea Zito
int delegate_is_cloud_node(struct cloud_helper_impl_context *context, struct nodeID* node)
71
{
72
  return context->delegate->is_cloud_node(context->delegate_context, node);
73
}
74
75 27079cd9 Andrea Zito
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context, struct timeval *tout)
76
{
77
  return context->delegate->wait4cloud(context->delegate_context, tout);
78
}
79
80
static int delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context, uint8_t *buffer_ptr, int buffer_size)
81
{
82
  return context->delegate->recv_from_cloud(context->delegate_context, buffer_ptr, buffer_size);
83
}
84
85
struct cloud_helper_iface delegate = {
86
  .cloud_helper_init = delegate_cloud_init,
87
  .get_from_cloud = delegate_cloud_get_from_cloud,
88
  .put_on_cloud = delegate_cloud_put_on_cloud,
89
  .get_cloud_node = delegate_cloud_get_cloud_node,
90 ef8002fd Andrea Zito
  .is_cloud_node = delegate_is_cloud_node,
91 27079cd9 Andrea Zito
  .wait4cloud = delegate_cloud_wait4cloud,
92
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
93
};