Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.04 KB)

1 b2cc9555 Andrea Zito
/*
2
 *  Copyright (c) 2011 Andrea Zito
3
 *
4
 *  This is free software; see lgpl-2.1.txt
5
 */
6
7 27079cd9 Andrea Zito
#include <stdlib.h>
8
#include <stdint.h>
9
#include <dlfcn.h>
10 0e9bde83 Andrea Zito
#include <stdio.h>
11 27079cd9 Andrea Zito
12
#include "cloud_helper_iface.h"
13 176b8de8 Luca Baldesi
#include "grapes_config.h"
14 27079cd9 Andrea Zito
15
struct delegate_iface {
16
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
17 b9e7dd7b Andrea Zito
18
  int (*get_from_cloud)(void *context, const char *key, uint8_t *header_ptr,
19
                        int header_size, int free_header);
20
21 98b047dd Andrea Zito
  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 b9e7dd7b Andrea Zito
  int (*put_on_cloud)(void *context, const char *key, uint8_t *buffer_ptr,
26
                      int buffer_size, int free_buffer);
27
28 0e9bde83 Andrea Zito
  struct nodeID* (*get_cloud_node)(void *context, uint8_t variant);
29 b9e7dd7b Andrea Zito
30 0e9bde83 Andrea Zito
  time_t (*timestamp_cloud)(void *context);
31 b9e7dd7b Andrea Zito
32 ef8002fd Andrea Zito
  int (*is_cloud_node)(void *context, struct nodeID* node);
33 b9e7dd7b Andrea Zito
34 27079cd9 Andrea Zito
  int (*wait4cloud)(void *context, struct timeval *tout);
35 b9e7dd7b Andrea Zito
36 27079cd9 Andrea Zito
  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 b9e7dd7b Andrea Zito
static struct cloud_helper_impl_context*
45
delegate_cloud_init(struct nodeID *local, const char *config)
46 27079cd9 Andrea Zito
{
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 176b8de8 Luca Baldesi
  cfg_tags = grapes_config_parse(config);
54
  dlib_name = grapes_config_value_str(cfg_tags, "delegate_lib");
55 27079cd9 Andrea Zito
  dlib = dlopen(dlib_name, RTLD_NOW);
56
  if (dlib == NULL) {
57
    printf("error: %s", dlerror());
58
    return NULL;
59
  }
60 49b99aeb Andrea Zito
61 27079cd9 Andrea Zito
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 49b99aeb Andrea Zito
68 27079cd9 Andrea Zito
  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 b9e7dd7b Andrea Zito
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 27079cd9 Andrea Zito
{
82 b9e7dd7b Andrea Zito
  return context->delegate->get_from_cloud(context->delegate_context, key,
83
                                           header_ptr, header_size,
84
                                           free_header);
85 27079cd9 Andrea Zito
}
86
87 b9e7dd7b Andrea Zito
static int
88 98b047dd Andrea Zito
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 b9e7dd7b Andrea Zito
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 27079cd9 Andrea Zito
{
105 b9e7dd7b Andrea Zito
  return context->delegate->put_on_cloud(context->delegate_context, key,
106
                                         buffer_ptr, buffer_size, free_buffer);
107 27079cd9 Andrea Zito
}
108 b9e7dd7b Andrea Zito
static struct nodeID*
109
delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context,
110
                              uint8_t variant)
111 27079cd9 Andrea Zito
{
112 0e9bde83 Andrea Zito
  return context->delegate->get_cloud_node(context->delegate_context, variant);
113
}
114
115 b9e7dd7b Andrea Zito
static time_t
116
delegate_timestamp_cloud(struct cloud_helper_impl_context *context)
117 0e9bde83 Andrea Zito
{
118
  return context->delegate->timestamp_cloud(context->delegate_context);
119 27079cd9 Andrea Zito
}
120
121 b9e7dd7b Andrea Zito
int delegate_is_cloud_node(struct cloud_helper_impl_context *context,
122
                           struct nodeID* node)
123 ef8002fd Andrea Zito
{
124
  return context->delegate->is_cloud_node(context->delegate_context, node);
125
}
126
127 b9e7dd7b Andrea Zito
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context,
128
                                     struct timeval *tout)
129 27079cd9 Andrea Zito
{
130
  return context->delegate->wait4cloud(context->delegate_context, tout);
131
}
132
133 b9e7dd7b Andrea Zito
static int
134
delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context,
135
                               uint8_t *buffer_ptr, int buffer_size)
136 27079cd9 Andrea Zito
{
137 b9e7dd7b Andrea Zito
  return context->delegate->recv_from_cloud(context->delegate_context,
138
                                            buffer_ptr, buffer_size);
139 27079cd9 Andrea Zito
}
140
141
struct cloud_helper_iface delegate = {
142
  .cloud_helper_init = delegate_cloud_init,
143
  .get_from_cloud = delegate_cloud_get_from_cloud,
144 98b047dd Andrea Zito
  .get_from_cloud_default = delegate_cloud_get_from_cloud_default,
145 27079cd9 Andrea Zito
  .put_on_cloud = delegate_cloud_put_on_cloud,
146
  .get_cloud_node = delegate_cloud_get_cloud_node,
147 0e9bde83 Andrea Zito
  .timestamp_cloud = delegate_timestamp_cloud,
148 ef8002fd Andrea Zito
  .is_cloud_node = delegate_is_cloud_node,
149 27079cd9 Andrea Zito
  .wait4cloud = delegate_cloud_wait4cloud,
150
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
151
};