Revision 89b16ba8 src/Tests/cloud_helper_delegate_file.c

View differences:

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
}

Also available in: Unified diff