Revision 89b16ba8

View differences:

include/cloud_helper.h
43 43
 * Use the wait4cloud to listen for the answer and revc_from_cloud to read the response.
44 44
 * @param[in] context The contex representing the desired cloud_helper instance.
45 45
 * @param[in] key Key to retrieve.
46
 * @param[in] header_ptr A pointer to the header which will be added to the retrieved data. May be NULL
47
 * @param[in] header_size The length of the header.
46 48
 * @return 0 if the request was successfully sent, 1 0therwise
47 49
 */
48
int get_from_cloud(struct cloud_helper_context *context, char *key);
50
int get_from_cloud(struct cloud_helper_context *context, char *key, uint8_t *header_ptr, int header_size);
49 51

  
50 52
/**
51 53
 * @brief Put on the cloud the value for a specified key.
src/Tests/cloud_helper_delegate_file.c
11 11

  
12 12
struct delegate_iface {
13 13
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
14
  int (*get_from_cloud)(void *context, char *key);
14
  int (*get_from_cloud)(void *context, char *key, uint8_t *header_ptr, int header_size);
15 15
  int (*put_on_cloud)(void *context, char *key, uint8_t *buffer_ptr, int buffer_size);
16 16
  struct nodeID* (*get_cloud_node)(void *context);
17 17
  int (*wait4cloud)(void *context, struct timeval *tout);
......
38 38
  char *key;
39 39
  uint8_t *value;
40 40
  int value_len;
41
  uint8_t *header_ptr;
42
  int header_size;
41 43
};
42 44

  
43 45
struct entry {
......
52 54
  struct entry e;
53 55
  struct gpThreadContext *ctx;
54 56
  FILE *fd;
57
  uint8_t *buffer_ptr;
58
  struct mem_offset *len;
55 59
  ctx = (struct gpThreadContext *) context;
56 60

  
57 61
  fd = fopen(ctx->cloud->path, "r");
......
62 66
  ctx->cloud->key_error = -1;
63 67
  sem_post(&ctx->cloud->sem);
64 68

  
69
  ctx->cloud->out_cnt++;
70
  ctx->cloud->out_buffer[ctx->cloud->out_cnt] = calloc(100, sizeof(uint8_t));
71
  len = malloc(sizeof(struct mem_offset));
72
  len->start = 0;
73
  len->end = ctx->header_size;
74

  
75
  buffer_ptr = ctx->cloud->out_buffer[ctx->cloud->out_cnt];
76
  memcpy(buffer_ptr, ctx->header_ptr, ctx->header_size);
77

  
78
  buffer_ptr += ctx->header_size;
79

  
65 80
  while (fread(&e, sizeof(e), 1, fd) != 0){
66 81
    if (strcmp(e.key, ctx->key) == 0){
67
      struct mem_offset *len = malloc(sizeof(struct mem_offset));
82

  
68 83
      sem_wait(&ctx->cloud->sem);
69
      ctx->cloud->out_cnt++;
70
      ctx->cloud->out_buffer[ctx->cloud->out_cnt] = calloc(100, sizeof(uint8_t));
71
      memcpy(ctx->cloud->out_buffer[ctx->cloud->out_cnt], e.value, e.value_len);
72
      
73
      len->start = 0;
74
      len->end = e.value_len;
75
      ctx->cloud->out_len[ctx->cloud->out_cnt] = *len;
84
      memcpy(buffer_ptr, e.value, e.value_len);
85
      len->end += e.value_len;      
86

  
76 87
      ctx->cloud->key_error = 0;
77 88
      sem_post(&ctx->cloud->sem);
78 89
      break;
......
80 91
  }
81 92

  
82 93
  sem_wait(&ctx->cloud->sem);
94
  ctx->cloud->out_len[ctx->cloud->out_cnt] = *len;
83 95
  if (ctx->cloud->key_error == -1)
84 96
    ctx->cloud->key_error = 1;
85 97
  sem_post(&ctx->cloud->sem);
......
160 172
  return ctx;
161 173
}
162 174

  
163
static int file_cloud_get_from_cloud(void *context, char *key)
175
static int file_cloud_get_from_cloud(void *context, char *key, uint8_t *header_ptr, int header_size)
164 176
{
165 177
  struct file_cloud_context *ctx;
166 178
  int err;
......
172 184
  tc = malloc(sizeof(struct gpThreadContext));
173 185
  tc->cloud = ctx;
174 186
  tc->key = strdup(key);
187
  tc->header_ptr = header_ptr;
188
  tc->header_size = header_size;
175 189

  
176 190
  err = pthread_create(thread, NULL, getValueForKey, (void *)tc);
177 191
  if (err) return 1;
......
219 233

  
220 234
  ctx = (struct file_cloud_context *)context;
221 235
  if (ctx->key_error == 1) return -1;
222
  if (ctx->out_cnt >= 0) return 1;
236
  if (ctx->out_cnt > 0) return 1;
237
  if (ctx->out_cnt == 0 && ctx->key_error >= 0) return 1;
223 238

  
224 239
  while (timeout > 0){
225 240
    usleep(100);
226 241
    timeout -= 100;
227
    if (ctx->out_cnt >= 0) return 1;
228 242
    if (ctx->key_error == 1) return -1;
243
    if (ctx->out_cnt > 0) return 1;
244
    if (ctx->out_cnt == 0 && ctx->key_error >= 0) return 1;
229 245
  }
230 246
  return 0;
231 247
}
src/Tests/cloud_test.c
32 32
static char *key;
33 33
static char *value;
34 34

  
35
static const uint8_t *HEADER = (const uint8_t *) "<header>";
36

  
37

  
35 38
static void cmdline_parse(int argc, char *argv[])
36 39
{
37 40
  int o;
......
112 115
    break;
113 116
  case GET:
114 117
    printf("Getting from cloud value for key \"%s\": ", key);
115
    err = get_from_cloud(cloud, key);
118
    err = get_from_cloud(cloud, key, HEADER, sizeof(HEADER));
116 119
    if (err) {
117 120
      printf("Error performing the operation");
118 121
      return 1;
......
134 137
      printf("No response from cloud\n");
135 138
      return 1;
136 139
    } else {
137
      printf("No value for the specified key\n");
140
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
141
      buffer[sizeof(buffer) - 1] = '\0';
142
      printf("No value for the specified key. Received: %s\n", buffer);
138 143
      return 1;      
139 144
    }
140 145
  }
src/TopologyManager/cloud_helper.c
72 72
  return NULL;
73 73
}
74 74

  
75
int get_from_cloud(struct cloud_helper_context *context, char *key)
75
int get_from_cloud(struct cloud_helper_context *context, char *key, uint8_t *header_ptr, int header_size)
76 76
{
77
  return context->ch->get_from_cloud(context->ch_context, key);
77
  return context->ch->get_from_cloud(context->ch_context, key, header_ptr, header_size);
78 78
}
79 79

  
80 80
int put_on_cloud(struct cloud_helper_context *context, char *key, uint8_t *buffer_ptr, int buffer_size)
src/TopologyManager/cloud_helper_delegate.c
7 7

  
8 8
struct delegate_iface {
9 9
  void* (*cloud_helper_init)(struct nodeID *local, const char *config);
10
  int (*get_from_cloud)(void *context, char *key);
10
  int (*get_from_cloud)(void *context, char *key, uint8_t *header_ptr, int header_size);
11 11
  int (*put_on_cloud)(void *context, char *key, uint8_t *buffer_ptr, int buffer_size);
12 12
  struct nodeID* (*get_cloud_node)(void *context);
13 13
  int (*wait4cloud)(void *context, struct timeval *tout);
......
51 51
  return ctx;
52 52
}
53 53

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

  
59 59
static int delegate_cloud_put_on_cloud(struct cloud_helper_impl_context *context, char *key, uint8_t *buffer_ptr, int buffer_size)
src/TopologyManager/cloud_helper_iface.h
7 7

  
8 8
struct cloud_helper_iface {
9 9
  struct cloud_helper_impl_context* (*cloud_helper_init)(struct nodeID *local, const char *config);
10
  int (*get_from_cloud)(struct cloud_helper_impl_context *context, char *key);
10
  int (*get_from_cloud)(struct cloud_helper_impl_context *context, char *key, uint8_t *header_ptr, int header_size);
11 11
  int (*put_on_cloud)(struct cloud_helper_impl_context *context, char *key, uint8_t *buffer_ptr, int buffer_size);
12 12
  struct nodeID* (*get_cloud_node)(struct cloud_helper_impl_context *context);
13 13
  int (*wait4cloud)(struct cloud_helper_impl_context *context, struct timeval *tout);

Also available in: Unified diff