Statistics
| Branch: | Revision:

grapes / src / CloudSupport / cloud_helper_delegate.c @ 4692000a

History | View | Annotate | Download (3.48 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
  int (*get_from_cloud)(void *context, char *key, uint8_t *header_ptr, int header_size);
18
  int (*put_on_cloud)(void *context, char *key, uint8_t *buffer_ptr, int buffer_size);
19
  struct nodeID* (*get_cloud_node)(void *context, uint8_t variant);
20
  time_t (*timestamp_cloud)(void *context);
21
  int (*is_cloud_node)(void *context, struct nodeID* node);
22
  int (*wait4cloud)(void *context, struct timeval *tout);
23
  int (*recv_from_cloud)(void *context, uint8_t *buffer_ptr, int buffer_size);
24
};
25

    
26
struct cloud_helper_impl_context {
27
  struct delegate_iface *delegate;
28
  void *delegate_context;
29
};
30

    
31
static struct cloud_helper_impl_context* delegate_cloud_init(struct nodeID *local, const char *config)
32
{
33
  struct cloud_helper_impl_context *ctx;
34
  struct tag *cfg_tags;
35
  const char *dlib_name;
36
  struct delegate_iface *delegate_impl;
37
  void *dlib;
38

    
39
  cfg_tags = config_parse(config);
40
  dlib_name = config_value_str(cfg_tags, "delegate_lib");
41
  dlib = dlopen(dlib_name, RTLD_NOW);
42
  if (dlib == NULL) {
43
    printf("error: %s", dlerror());
44
    return NULL;
45
  }
46

    
47

    
48
  delegate_impl = (struct delegate_iface *) dlsym(dlib, "delegate_impl");
49
  if (!delegate_impl) return NULL;
50

    
51
  ctx = malloc(sizeof(struct cloud_helper_impl_context));
52
  ctx->delegate = delegate_impl;
53

    
54
  ctx->delegate_context = ctx->delegate->cloud_helper_init(local, config);
55
  if(!ctx->delegate_context) {
56
    free(ctx);
57
    return NULL;
58
  }
59

    
60
  return ctx;
61
}
62

    
63
static int delegate_cloud_get_from_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *header_ptr, int header_size)
64
{
65
  return context->delegate->get_from_cloud(context->delegate_context, key, header_ptr, header_size);
66
}
67

    
68
static int delegate_cloud_put_on_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *buffer_ptr, int buffer_size)
69
{
70
  return context->delegate->put_on_cloud(context->delegate_context, key, buffer_ptr, buffer_size);
71
}
72

    
73
static struct nodeID* delegate_cloud_get_cloud_node(struct cloud_helper_impl_context *context, uint8_t variant)
74
{
75
  return context->delegate->get_cloud_node(context->delegate_context, variant);
76
}
77

    
78
static time_t delegate_timestamp_cloud(struct cloud_helper_impl_context *context)
79
{
80
  return context->delegate->timestamp_cloud(context->delegate_context);
81
}
82

    
83
int delegate_is_cloud_node(struct cloud_helper_impl_context *context, struct nodeID* node)
84
{
85
  return context->delegate->is_cloud_node(context->delegate_context, node);
86
}
87

    
88
static int delegate_cloud_wait4cloud(struct cloud_helper_impl_context *context, struct timeval *tout)
89
{
90
  return context->delegate->wait4cloud(context->delegate_context, tout);
91
}
92

    
93
static int delegate_cloud_recv_from_cloud(struct cloud_helper_impl_context *context, uint8_t *buffer_ptr, int buffer_size)
94
{
95
  return context->delegate->recv_from_cloud(context->delegate_context, buffer_ptr, buffer_size);
96
}
97

    
98
struct cloud_helper_iface delegate = {
99
  .cloud_helper_init = delegate_cloud_init,
100
  .get_from_cloud = delegate_cloud_get_from_cloud,
101
  .put_on_cloud = delegate_cloud_put_on_cloud,
102
  .get_cloud_node = delegate_cloud_get_cloud_node,
103
  .timestamp_cloud = delegate_timestamp_cloud,
104
  .is_cloud_node = delegate_is_cloud_node,
105
  .wait4cloud = delegate_cloud_wait4cloud,
106
  .recv_from_cloud = delegate_cloud_recv_from_cloud,
107
};