Statistics
| Branch: | Revision:

grapes / src / Tests / cloud_test.c @ 89b16ba8

History | View | Annotate | Download (3.31 KB)

1
/*
2
 *  Copyright (c) 2009 Andrea Zito
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 *
6
 *  This is a small test program for the cloud interface
7
 *  To try the simple test: run it with
8
 *    ./cloud_test -c "provider=<cloud_provider>,<provider_opts>" [-g key | -p key=value]
9
 *
10
 *    the "-g" and "-p" parameters select the GET or PUT operation
11
 *
12
 *    For example, run
13
 *      ./cloud_test -c "provider=delegate,delegate_lib=libfilecloud.so" -p hello=world
14
 *      ./cloud_test -c "provider=delegate,delegate_lib=libfilecloud.so" -g hello
15
 *    
16
 *    to test the delegate cloud provider with delegate implementation provided by libfilecloud.so.
17
 */
18

    
19

    
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <getopt.h>
24

    
25
#include "cloud_helper.h"
26

    
27
#define GET 0
28
#define PUT 1
29

    
30
static const char *config;
31
static int operation;
32
static char *key;
33
static char *value;
34

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

    
37

    
38
static void cmdline_parse(int argc, char *argv[])
39
{
40
  int o;
41
  char *temp;
42
      
43
  while ((o = getopt(argc, argv, "c:g:p:")) != -1) {
44
    switch(o) {
45
    case 'c':
46
      config = strdup(optarg);
47
      break;
48
    case 'p':
49
      temp = strdup(optarg);
50
      operation = PUT;
51
      
52
      key = strsep(&optarg, "=");
53
      value = optarg;
54

    
55
      if (!value || !key){
56
        printf("Expected key=value for option -p");
57
        free(temp);
58
        exit(-1);
59
      }
60
      break;
61
      case 'g':
62
        key =  strdup(optarg);
63
        break;
64
      default:
65
        fprintf(stderr, "Error: unknown option %c\n", o);
66

    
67
        exit(-1);
68
    }
69
  }
70
}
71

    
72
static struct cloud_helper_context *init(const char *config)
73
{
74
  struct nodeID *myID;
75
  struct cloud_helper_context *cloud_ctx;
76

    
77
  myID = net_helper_init("127.0.0.1", 1234, "");
78
  if (myID == NULL) {
79
    fprintf(stderr, "Error creating my socket\n");
80

    
81
    return NULL;
82
  }
83
  cloud_ctx = cloud_helper_init(myID, config);
84
  if (cloud_ctx == NULL) {
85
    fprintf(stderr, "Error initiating cloud_helper\n");
86
    return NULL;
87
  }
88

    
89
  return cloud_ctx;
90
}
91

    
92

    
93

    
94
int main(int argc, char *argv[])
95
{
96
  struct cloud_helper_context *cloud;
97
  char buffer[100];
98
  int err;
99
  struct timeval tout = {10, 0};
100

    
101
  cmdline_parse(argc, argv);
102

    
103
  cloud = init(config);
104

    
105
  if (!cloud) return -1;
106

    
107
  switch(operation) {
108
  case PUT:
109
    printf("Putting on cloud value \"%s\" for key \"%s\"\n", value, key);
110
    err = put_on_cloud(cloud, key, value, strlen(value));
111
    if (err) {
112
      printf("Error performing the operation");
113
      return 1;
114
    }
115
    break;
116
  case GET:
117
    printf("Getting from cloud value for key \"%s\": ", key);
118
    err = get_from_cloud(cloud, key, HEADER, sizeof(HEADER));
119
    if (err) {
120
      printf("Error performing the operation");
121
      return 1;
122
    }
123

    
124
    err = wait4cloud(cloud, &tout);
125
    if (err > 0) {
126
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
127
      if (err < 0) {
128
        printf("Erorr receiving cloud response\n");
129
        return 1;
130
      } else if (err == 0) {
131
        printf("Key not present on the cloud\n");
132
      } else {
133
        buffer[sizeof(buffer) - 1] = '\0';
134
        printf("%s\n", buffer);
135
      }
136
    } else if (err == 0){
137
      printf("No response from cloud\n");
138
      return 1;
139
    } else {
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);
143
      return 1;      
144
    }
145
  }
146

    
147

    
148
  return 0;
149
}