Statistics
| Branch: | Revision:

grapes / src / TopologyManager / cloud_helper_delegate.c @ 89b16ba8

History | View | Annotate | Download (2.82 KB)

1
#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
  int (*get_from_cloud)(void *context, char *key, uint8_t *header_ptr, int header_size);
11
  int (*put_on_cloud)(void *context, char *key, uint8_t *buffer_ptr, int buffer_size);
12
  struct nodeID* (*get_cloud_node)(void *context);
13
  int (*wait4cloud)(void *context, struct timeval *tout);
14
  int (*recv_from_cloud)(void *context, uint8_t *buffer_ptr, int buffer_size);
15
};
16

    
17
struct cloud_helper_impl_context {
18
  struct delegate_iface *delegate;
19
  void *delegate_context;
20
};
21

    
22
static struct cloud_helper_impl_context* delegate_cloud_init(struct nodeID *local, const char *config)
23
{
24
  struct cloud_helper_impl_context *ctx;
25
  struct tag *cfg_tags;
26
  const char *dlib_name;
27
  struct delegate_iface *delegate_impl;
28
  void *dlib;
29

    
30
  cfg_tags = config_parse(config);
31
  dlib_name = config_value_str(cfg_tags, "delegate_lib");
32
  dlib = dlopen(dlib_name, RTLD_NOW);
33
  if (dlib == NULL) {
34
    printf("error: %s", dlerror());
35
    return NULL;
36
  }
37
  
38

    
39
  delegate_impl = (struct delegate_iface *) dlsym(dlib, "delegate_impl");
40
  if (!delegate_impl) return NULL;
41

    
42
  ctx = malloc(sizeof(struct cloud_helper_impl_context));
43
  ctx->delegate = delegate_impl;
44
 
45
  ctx->delegate_context = ctx->delegate->cloud_helper_init(local, config);
46
  if(!ctx->delegate_context) {
47
    free(ctx);
48
    return NULL;
49
  }
50

    
51
  return ctx;
52
}
53

    
54
static int delegate_cloud_get_from_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *header_ptr, int header_size)
55
{
56
  return context->delegate->get_from_cloud(context->delegate_context, key, header_ptr, header_size);
57
}
58

    
59
static int delegate_cloud_put_on_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *buffer_ptr, int buffer_size)
60
{
61
  return context->delegate->put_on_cloud(context->delegate_context, key, buffer_ptr, buffer_size);
62
}
63

    
64
static struct nodeID* delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context)
65
{
66
  return context->delegate->get_cloud_node(context->delegate_context);
67
}
68

    
69
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context, struct timeval *tout)
70
{
71
  return context->delegate->wait4cloud(context->delegate_context, tout);
72
}
73

    
74
static int delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context, uint8_t *buffer_ptr, int buffer_size)
75
{
76
  return context->delegate->recv_from_cloud(context->delegate_context, buffer_ptr, buffer_size);
77
}
78

    
79
struct cloud_helper_iface delegate = {
80
  .cloud_helper_init = delegate_cloud_init,
81
  .get_from_cloud = delegate_cloud_get_from_cloud,
82
  .put_on_cloud = delegate_cloud_put_on_cloud,
83
  .get_cloud_node = delegate_cloud_get_cloud_node,
84
  .wait4cloud = delegate_cloud_wait4cloud,
85
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
86
};