Statistics
| Branch: | Revision:

grapes / src / CloudSupport / cloud_helper_delegate.c @ b9e7dd7b

History | View | Annotate | Download (4.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 "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 (*put_on_cloud)(void *context, const char *key, uint8_t *buffer_ptr,
22
                      int buffer_size, int free_buffer);
23

    
24
  struct nodeID* (*get_cloud_node)(void *context, uint8_t variant);
25

    
26
  time_t (*timestamp_cloud)(void *context);
27

    
28
  int (*is_cloud_node)(void *context, struct nodeID* node);
29

    
30
  int (*wait4cloud)(void *context, struct timeval *tout);
31

    
32
  int (*recv_from_cloud)(void *context, uint8_t *buffer_ptr, int buffer_size);
33
};
34

    
35
struct cloud_helper_impl_context {
36
  struct delegate_iface *delegate;
37
  void *delegate_context;
38
};
39

    
40
static struct cloud_helper_impl_context*
41
delegate_cloud_init(struct nodeID *local, const char *config)
42
{
43
  struct cloud_helper_impl_context *ctx;
44
  struct tag *cfg_tags;
45
  const char *dlib_name;
46
  struct delegate_iface *delegate_impl;
47
  void *dlib;
48

    
49
  cfg_tags = config_parse(config);
50
  dlib_name = config_value_str(cfg_tags, "delegate_lib");
51
  dlib = dlopen(dlib_name, RTLD_NOW);
52
  if (dlib == NULL) {
53
    printf("error: %s", dlerror());
54
    return NULL;
55
  }
56

    
57

    
58
  delegate_impl = (struct delegate_iface *) dlsym(dlib, "delegate_impl");
59
  if (!delegate_impl) return NULL;
60

    
61
  ctx = malloc(sizeof(struct cloud_helper_impl_context));
62
  ctx->delegate = delegate_impl;
63

    
64
  ctx->delegate_context = ctx->delegate->cloud_helper_init(local, config);
65
  if(!ctx->delegate_context) {
66
    free(ctx);
67
    return NULL;
68
  }
69

    
70
  return ctx;
71
}
72

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

    
83
static int
84
delegate_cloud_put_on_cloud(struct cloud_helper_impl_context *context,
85
                            const char *key, uint8_t *buffer_ptr,
86
                            int buffer_size, int free_buffer)
87
{
88
  return context->delegate->put_on_cloud(context->delegate_context, key,
89
                                         buffer_ptr, buffer_size, free_buffer);
90
}
91
static struct nodeID*
92
delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context,
93
                              uint8_t variant)
94
{
95
  return context->delegate->get_cloud_node(context->delegate_context, variant);
96
}
97

    
98
static time_t
99
delegate_timestamp_cloud(struct cloud_helper_impl_context *context)
100
{
101
  return context->delegate->timestamp_cloud(context->delegate_context);
102
}
103

    
104
int delegate_is_cloud_node(struct cloud_helper_impl_context *context,
105
                           struct nodeID* node)
106
{
107
  return context->delegate->is_cloud_node(context->delegate_context, node);
108
}
109

    
110
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context,
111
                                     struct timeval *tout)
112
{
113
  return context->delegate->wait4cloud(context->delegate_context, tout);
114
}
115

    
116
static int
117
delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context,
118
                               uint8_t *buffer_ptr, int buffer_size)
119
{
120
  return context->delegate->recv_from_cloud(context->delegate_context,
121
                                            buffer_ptr, buffer_size);
122
}
123

    
124
struct cloud_helper_iface delegate = {
125
  .cloud_helper_init = delegate_cloud_init,
126
  .get_from_cloud = delegate_cloud_get_from_cloud,
127
  .put_on_cloud = delegate_cloud_put_on_cloud,
128
  .get_cloud_node = delegate_cloud_get_cloud_node,
129
  .timestamp_cloud = delegate_timestamp_cloud,
130
  .is_cloud_node = delegate_is_cloud_node,
131
  .wait4cloud = delegate_cloud_wait4cloud,
132
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
133
};