Statistics
| Branch: | Revision:

grapes / src / TopologyManager / cloud_helper_delegate.c @ 0e9bde83

History | View | Annotate | Download (3.39 KB)

1
#include <stdlib.h>
2
#include <stdint.h>
3
#include <dlfcn.h>
4
#include <stdio.h>
5

    
6
#include "cloud_helper_iface.h"
7
#include "config.h"
8

    
9
struct delegate_iface {
10
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
11
  int (*get_from_cloud)(void *context, char *key, uint8_t *header_ptr, int header_size);
12
  int (*put_on_cloud)(void *context, char *key, uint8_t *buffer_ptr, int buffer_size);
13
  struct nodeID* (*get_cloud_node)(void *context, uint8_t variant);
14
  time_t (*timestamp_cloud)(void *context);
15
  int (*is_cloud_node)(void *context, struct nodeID* node);
16
  int (*wait4cloud)(void *context, struct timeval *tout);
17
  int (*recv_from_cloud)(void *context, uint8_t *buffer_ptr, int buffer_size);
18
};
19

    
20
struct cloud_helper_impl_context {
21
  struct delegate_iface *delegate;
22
  void *delegate_context;
23
};
24

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

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

    
42
  delegate_impl = (struct delegate_iface *) dlsym(dlib, "delegate_impl");
43
  if (!delegate_impl) return NULL;
44

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

    
54
  return ctx;
55
}
56

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

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

    
67
static struct nodeID* delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context, uint8_t variant)
68
{
69
  return context->delegate->get_cloud_node(context->delegate_context, variant);
70
}
71

    
72
static time_t delegate_timestamp_cloud(struct cloud_helper_impl_context *context)
73
{
74
  return context->delegate->timestamp_cloud(context->delegate_context);
75
}
76

    
77
int delegate_is_cloud_node(struct cloud_helper_impl_context *context, struct nodeID* node)
78
{
79
  return context->delegate->is_cloud_node(context->delegate_context, node);
80
}
81

    
82
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context, struct timeval *tout)
83
{
84
  return context->delegate->wait4cloud(context->delegate_context, tout);
85
}
86

    
87
static int delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context, uint8_t *buffer_ptr, int buffer_size)
88
{
89
  return context->delegate->recv_from_cloud(context->delegate_context, buffer_ptr, buffer_size);
90
}
91

    
92
struct cloud_helper_iface delegate = {
93
  .cloud_helper_init = delegate_cloud_init,
94
  .get_from_cloud = delegate_cloud_get_from_cloud,
95
  .put_on_cloud = delegate_cloud_put_on_cloud,
96
  .get_cloud_node = delegate_cloud_get_cloud_node,
97
  .timestamp_cloud = delegate_timestamp_cloud,
98
  .is_cloud_node = delegate_is_cloud_node,
99
  .wait4cloud = delegate_cloud_wait4cloud,
100
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
101
};