Statistics
| Branch: | Revision:

grapes / src / Tests / cloud_test.c @ ef8002fd

History | View | Annotate | Download (4.11 KB)

1 7342a625 Andrea Zito
/*
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 ef8002fd Andrea Zito
 *    ./cloud_test -c "provider=<cloud_provider>,<provider_opts>" [-g key | -p key=value | -n | -e ip:port]
9 7342a625 Andrea Zito
 *
10 ef8002fd Andrea Zito
 *    -g  GET key from cloud
11
 *    -p  PUT key=value on cloud
12
 *    -n  print the cloud node
13
 *    -e  check if ip:port references the cloud
14
 *    -c  set the configuration of the cloud provider
15 7342a625 Andrea Zito
 *
16
 *    For example, run
17
 *      ./cloud_test -c "provider=delegate,delegate_lib=libfilecloud.so" -p hello=world
18
 *      ./cloud_test -c "provider=delegate,delegate_lib=libfilecloud.so" -g hello
19
 *    
20
 *    to test the delegate cloud provider with delegate implementation provided by libfilecloud.so.
21
 */
22
23
24
#include <stdlib.h>
25
#include <stdio.h>
26
#include <string.h>
27
#include <getopt.h>
28
29
#include "cloud_helper.h"
30
31
#define GET 0
32
#define PUT 1
33 ef8002fd Andrea Zito
#define GET_CLOUD_NODE 2
34
#define EQ_CLOUD_NODE 3
35 7342a625 Andrea Zito
36
static const char *config;
37
static int operation;
38
static char *key;
39
static char *value;
40
41 89b16ba8 Andrea Zito
static const uint8_t *HEADER = (const uint8_t *) "<header>";
42
43
44 7342a625 Andrea Zito
static void cmdline_parse(int argc, char *argv[])
45
{
46
  int o;
47
  char *temp;
48
      
49 ef8002fd Andrea Zito
  while ((o = getopt(argc, argv, "c:g:p:ne:")) != -1) {
50 7342a625 Andrea Zito
    switch(o) {
51
    case 'c':
52
      config = strdup(optarg);
53
      break;
54
    case 'p':
55
      temp = strdup(optarg);
56
      operation = PUT;
57
      
58
      key = strsep(&optarg, "=");
59
      value = optarg;
60
61
      if (!value || !key){
62
        printf("Expected key=value for option -p");
63
        free(temp);
64
        exit(-1);
65
      }
66
      break;
67
      case 'g':
68
        key =  strdup(optarg);
69
        break;
70 ef8002fd Andrea Zito
    case 'n':
71
      operation = GET_CLOUD_NODE;
72
      break;
73
    case 'e':
74
      operation = EQ_CLOUD_NODE;
75
      temp = strdup(optarg);
76
      
77
      key = strsep(&optarg, ":");
78
      value = optarg;
79
80
81
      if (!value || !key){
82
        printf("Expected ip:port for option -e");
83
        free(temp);
84
        exit(-1);
85
      }  
86
      break;
87
    default:
88 7342a625 Andrea Zito
        fprintf(stderr, "Error: unknown option %c\n", o);
89
90
        exit(-1);
91
    }
92
  }
93
}
94
95
static struct cloud_helper_context *init(const char *config)
96
{
97
  struct nodeID *myID;
98
  struct cloud_helper_context *cloud_ctx;
99
100
  myID = net_helper_init("127.0.0.1", 1234, "");
101
  if (myID == NULL) {
102
    fprintf(stderr, "Error creating my socket\n");
103
104
    return NULL;
105
  }
106
  cloud_ctx = cloud_helper_init(myID, config);
107
  if (cloud_ctx == NULL) {
108
    fprintf(stderr, "Error initiating cloud_helper\n");
109
    return NULL;
110
  }
111
112
  return cloud_ctx;
113
}
114
115
116
117
int main(int argc, char *argv[])
118
{
119
  struct cloud_helper_context *cloud;
120
  char buffer[100];
121
  int err;
122 ef8002fd Andrea Zito
  struct nodeID *t;
123 7342a625 Andrea Zito
  struct timeval tout = {10, 0};
124
125
  cmdline_parse(argc, argv);
126
127
  cloud = init(config);
128
129
  if (!cloud) return -1;
130
131
  switch(operation) {
132
  case PUT:
133
    printf("Putting on cloud value \"%s\" for key \"%s\"\n", value, key);
134
    err = put_on_cloud(cloud, key, value, strlen(value));
135
    if (err) {
136
      printf("Error performing the operation");
137
      return 1;
138
    }
139
    break;
140
  case GET:
141
    printf("Getting from cloud value for key \"%s\": ", key);
142 89b16ba8 Andrea Zito
    err = get_from_cloud(cloud, key, HEADER, sizeof(HEADER));
143 7342a625 Andrea Zito
    if (err) {
144
      printf("Error performing the operation");
145
      return 1;
146
    }
147
148
    err = wait4cloud(cloud, &tout);
149
    if (err > 0) {
150
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
151
      if (err < 0) {
152
        printf("Erorr receiving cloud response\n");
153
        return 1;
154
      } else if (err == 0) {
155
        printf("Key not present on the cloud\n");
156
      } else {
157
        buffer[sizeof(buffer) - 1] = '\0';
158
        printf("%s\n", buffer);
159
      }
160
    } else if (err == 0){
161
      printf("No response from cloud\n");
162
      return 1;
163
    } else {
164 89b16ba8 Andrea Zito
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
165
      buffer[sizeof(buffer) - 1] = '\0';
166
      printf("No value for the specified key. Received: %s\n", buffer);
167 7342a625 Andrea Zito
      return 1;      
168
    }
169 ef8002fd Andrea Zito
    break;
170
  case GET_CLOUD_NODE:
171
    printf("Cloud node: %s\n", node_addr(get_cloud_node(cloud)));
172
    break;
173
  case EQ_CLOUD_NODE:
174
    t = create_node(key, atoi(value));
175
    printf("Node %s references cloud? %d\n", node_addr(get_cloud_node(cloud)),
176
           is_cloud_node(cloud, t));
177
    break;
178 7342a625 Andrea Zito
  }
179
180
181
  return 0;
182
}