Statistics
| Branch: | Revision:

grapes / src / Tests / cloud_test.c @ fa752c57

History | View | Annotate | Download (4.43 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 | -n variant | -e ip:port]
9
 *
10
 *    -g  GET key from cloud
11
 *    -p  PUT key=value on cloud
12
 *    -n  print the cloud node for the specified variant
13
 *    -e  check if ip:port references the cloud
14
 *    -c  set the configuration of the cloud provider
15
 *
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
#include <time.h>
29

    
30
#include "cloud_helper.h"
31

    
32
#define GET 0
33
#define PUT 1
34
#define GET_CLOUD_NODE 2
35
#define EQ_CLOUD_NODE 3
36

    
37
static const char *config;
38
static int operation;
39
static int variant;
40
static char *key;
41
static char *value;
42

    
43
static uint8_t *HEADER = (const uint8_t *) "<-header->";
44

    
45
static void cmdline_parse(int argc, char *argv[])
46
{
47
  int o;
48
  char *temp;
49

    
50
  while ((o = getopt(argc, argv, "c:g:p:n:e:")) != -1) {
51
    switch(o) {
52
    case 'c':
53
      config = strdup(optarg);
54
      break;
55
    case 'p':
56
      temp = strdup(optarg);
57
      operation = PUT;
58

    
59
      key = strsep(&optarg, "=");
60
      value = optarg;
61

    
62
      if (!value || !key){
63
        printf("Expected key=value for option -p");
64
        free(temp);
65
        exit(-1);
66
      }
67
      break;
68
      case 'g':
69
        key =  strdup(optarg);
70
        break;
71
    case 'n':
72
      operation = GET_CLOUD_NODE;
73
      variant = atoi(optarg);
74
      break;
75
    case 'e':
76
      operation = EQ_CLOUD_NODE;
77
      temp = strdup(optarg);
78

    
79
      key = strsep(&optarg, ":");
80
      value = optarg;
81

    
82

    
83
      if (!value || !key){
84
        printf("Expected ip:port for option -e");
85
        free(temp);
86
        exit(-1);
87
      }
88
      break;
89
    default:
90
        fprintf(stderr, "Error: unknown option %c\n", o);
91

    
92
        exit(-1);
93
    }
94
  }
95
}
96

    
97
static struct cloud_helper_context *init(const char *config)
98
{
99
  struct nodeID *myID;
100
  struct cloud_helper_context *cloud_ctx;
101

    
102
  myID = net_helper_init("127.0.0.1", 1234, "");
103
  if (myID == NULL) {
104
    fprintf(stderr, "Error creating my socket\n");
105

    
106
    return NULL;
107
  }
108
  cloud_ctx = cloud_helper_init(myID, config);
109
  if (cloud_ctx == NULL) {
110
    fprintf(stderr, "Error initiating cloud_helper\n");
111
    return NULL;
112
  }
113

    
114
  return cloud_ctx;
115
}
116

    
117

    
118

    
119
int main(int argc, char *argv[])
120
{
121
  struct cloud_helper_context *cloud;
122
  char buffer[100];
123
  int err;
124
  struct nodeID *t;
125
  struct timeval tout = {10, 0};
126

    
127
  cmdline_parse(argc, argv);
128

    
129
  cloud = init(config);
130

    
131
  if (!cloud) return -1;
132

    
133
  switch(operation) {
134
  case PUT:
135
    printf("Putting on cloud value \"%s\" for key \"%s\"\n", value, key);
136
    err = put_on_cloud(cloud, key, value, strlen(value), 0);
137
    if (err) {
138
      printf("Error performing the operation");
139
      return 1;
140
    }
141
    break;
142
  case GET:
143
    printf("Getting from cloud value for key \"%s\": ", key);
144
    err = get_from_cloud(cloud, key, HEADER, strlen(HEADER), 0);
145
    if (err) {
146
      printf("Error performing the operation");
147
      return 1;
148
    }
149

    
150
    err = wait4cloud(cloud, &tout);
151
    if (err > 0) {
152
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
153
      if (err < 0) {
154
        printf("Erorr receiving cloud response\n");
155
        return 1;
156
      } else {
157
        time_t timestamp;
158
        int i;
159
        buffer[err] = '\0';
160
        printf("len=%d\n", err);
161
        for (i=0; i<err; i++)
162
          printf("%x(%c) ", buffer[i], buffer[i]);
163
        printf("\n");
164
        timestamp = timestamp_cloud(cloud);
165
        printf("Timestamp: %s\n", ctime(&timestamp));
166
      }
167
    } else if (err == 0){
168
      printf("No response from cloud\n");
169
      return 1;
170
    } else {
171
      err = recv_from_cloud(cloud, buffer, sizeof(buffer)-1);
172
      buffer[sizeof(buffer) - 1] = '\0';
173
      printf("No value for the specified key. Received: %s\n", buffer);
174
      return 1;
175
    }
176
    break;
177
  case GET_CLOUD_NODE:
178
    printf("Cloud node: %s\n", node_addr(get_cloud_node(cloud, variant)));
179
    break;
180
  case EQ_CLOUD_NODE:
181
    t = create_node(key, atoi(value));
182
    printf("Node %s references cloud? %d\n", node_addr(get_cloud_node(cloud, variant)),
183
           is_cloud_node(cloud, t));
184
    break;
185
  }
186

    
187

    
188
  return 0;
189
}