source: geovis/trunk/IData.cpp @ 5852

Last change on this file since 5852 was 5852, checked in by ldelgass, 9 years ago

Add initial iData client support

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2015 HUBzero Foundation, LLC
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7#include <string>
8#include <iostream>
9#include <sstream>
10#include <cstring>
11#include <cstdlib>
12#include <cstddef>
13
14#include <curl/curl.h>
15#include <osgEarth/JsonUtils>
16
17#include "IData.h"
18
19static const char *commandNames[] = {
20    "list",
21    "get",
22    "contents",
23    "setglobalmeta",
24    "delete",
25    "upload",
26    "finishupload",
27    "checksum"
28};
29#if 0
30static const char *sharingValues[] = {
31    "no",
32    "read-only",
33    "read-write"
34};
35#endif
36static const char *serviceURL = "http://mygeohub.org/api/idata/collection/";
37static std::string g_iDataUsername;
38
39using namespace IData;
40using namespace osgEarth;
41
42void IData::iDataInit(const char *username, const char *hub)
43{
44    static bool first = true;
45    g_iDataUsername = username;
46    if (first) {
47        curl_global_init(CURL_GLOBAL_DEFAULT);
48        first = false;
49    }
50}
51
52void IData::iDataCleanup()
53{
54    curl_global_cleanup();
55}
56
57static std::string urlEncode(const std::string& str)
58{
59    std::string retStr;
60    CURL *curl = curl_easy_init();
61    if (curl) {
62        char *encoded = curl_easy_escape(curl, str.c_str(), str.length());
63        retStr = encoded;
64        curl_free(encoded);
65        curl_easy_cleanup(curl);
66    }
67    return retStr;
68}
69
70static std::string formatQuery(Command cmd, std::vector<std::string> &params)
71{
72    std::ostringstream oss;
73    oss << serviceURL << commandNames[cmd] << "?username=" << g_iDataUsername;
74    for (std::vector<std::string>::iterator itr = params.begin();
75         itr != params.end(); ++itr) {
76        oss << "&" << *itr << "=";
77        itr++;
78        oss << urlEncode(*itr);
79    }
80
81    return oss.str();
82}
83
84static size_t writeData(void *buffer, size_t size, size_t nmemb, void *userp)
85{
86    size_t numBytes = size * nmemb;
87    Buffer *out = static_cast<Buffer *>(userp);
88    out->data = realloc(out->data, out->size + numBytes);
89    memcpy((char *)out->data + out->size, buffer, numBytes);
90    out->size += numBytes;
91    return numBytes;
92}
93
94void IData::getCollectionList(std::vector<Collection>& list)
95{
96    std::vector<std::string> params;
97    std::string query = formatQuery(COMMAND_LIST, params);
98    CURL *curl = curl_easy_init();
99    if (curl) {
100        curl_easy_setopt(curl, CURLOPT_URL, query.c_str());
101        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
102        Buffer buff;
103        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff);
104        CURLcode ret = curl_easy_perform(curl);
105        if (ret == CURLE_OK) {
106            //std::cout.write((char *)buff.data, buff.size);
107            Json::Reader reader;
108            Json::Value result;
109            const char *begin = (const char *)buff.data;
110            const char *end = begin + buff.size;
111            reader.parse(begin, end, result, false);
112            for (size_t i = 0; i < result["collections"].size(); i++) {
113                Collection collection;
114                Json::Value& val = result["collections"][i];
115                collection.name = val["name"].asString();
116                collection.shared = val["shared"].asString();
117                collection.id = atoi(val["id"].asCString());
118                list.push_back(collection);
119            }
120        }
121        curl_easy_cleanup(curl);
122    }
123}
124
125void IData::getContents(int collectionID, const char *path,
126                        std::vector<DirectoryItem>& contents)
127{
128    std::vector<std::string> params;
129    {
130        std::ostringstream oss;
131        oss << collectionID;
132        params.push_back("id");
133        params.push_back(oss.str());
134    }
135    if (path) {
136        params.push_back("path");
137        params.push_back(path);
138    }
139    std::string query = formatQuery(COMMAND_CONTENTS, params);
140    CURL *curl = curl_easy_init();
141    if (curl) {
142        curl_easy_setopt(curl, CURLOPT_URL, query.c_str());
143        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
144        Buffer buff;
145        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff);
146        CURLcode ret = curl_easy_perform(curl);
147        if (ret == CURLE_OK) {
148            std::cout.write((char *)buff.data, buff.size);
149            Json::Reader reader;
150            Json::Value result;
151            const char *begin = (const char *)buff.data;
152            const char *end = begin + buff.size;
153            reader.parse(begin, end, result, false);
154            for (size_t i = 0; i < result["contents"].size(); i++) {
155                DirectoryItem dir;
156                Json::Value& val = result["contents"][i];
157                dir.name = val["name"].asString();
158                dir.isDir = (val["dir-or-file"].asInt() == TYPE_DIRECTORY);
159                if (!dir.isDir) {
160                    dir.type = val["type"].asString();
161                    dir.size = atol(val["size"].asCString());
162                    dir.ctime = val["ctime"].asString();
163                    dir.doi = val["doi"].asString();
164                }
165                contents.push_back(dir);
166            }
167        }
168        curl_easy_cleanup(curl);
169    }
170}
171
172void IData::getFile(int collectionID, const char *doi, Buffer *buff)
173{
174    std::vector<std::string> params;
175    {
176        std::ostringstream oss;
177        oss << collectionID;
178        params.push_back("id");
179        params.push_back(oss.str());
180    }
181    if (doi) {
182        params.push_back("doi");
183        params.push_back(doi);
184    }
185    std::string query = formatQuery(COMMAND_GET, params);
186    CURL *curl = curl_easy_init();
187    if (curl) {
188        curl_easy_setopt(curl, CURLOPT_URL, query.c_str());
189        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
190        curl_easy_setopt(curl, CURLOPT_WRITEDATA, buff);
191        CURLcode ret = curl_easy_perform(curl);
192        if (ret == CURLE_OK) {
193            //std::cout.write((char *)buff->data, buff->size);
194        }
195        curl_easy_cleanup(curl);
196    }
197}
198
199void setFileMetadata(int collectionID, const char *doi, const char *name, const char *value)
200{
201    std::vector<std::string> params;
202    {
203        std::ostringstream oss;
204        oss << collectionID;
205        params.push_back("id");
206        params.push_back(oss.str());
207    }
208    if (doi) {
209        params.push_back("doi");
210        params.push_back(doi);
211    }
212    if (name) {
213        params.push_back(name);
214        params.push_back(value);
215    }
216    std::string query = formatQuery(COMMAND_SETGLOBALMETA, params);
217    CURL *curl = curl_easy_init();
218    if (curl) {
219        curl_easy_setopt(curl, CURLOPT_URL, query.c_str());
220        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
221        Buffer buff;
222        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff);
223        CURLcode ret = curl_easy_perform(curl);
224        if (ret == CURLE_OK) {
225            //std::cout.write((char *)buff->data, buff->size);
226        }
227        curl_easy_cleanup(curl);
228    }
229}
230
231void deleteFile(int collectionID, const char *path, const char *filename)
232{
233    std::vector<std::string> params;
234    {
235        std::ostringstream oss;
236        oss << collectionID;
237        params.push_back("id");
238        params.push_back(oss.str());
239    }
240    if (path) {
241        params.push_back("path");
242        params.push_back(path);
243    }
244    if (filename) {
245        params.push_back("filename");
246        params.push_back(filename);
247    }
248    std::string query = formatQuery(COMMAND_DELETE, params);
249    CURL *curl = curl_easy_init();
250    if (curl) {
251        curl_easy_setopt(curl, CURLOPT_URL, query.c_str());
252        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
253        Buffer buff;
254        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff);
255        CURLcode ret = curl_easy_perform(curl);
256        if (ret == CURLE_OK) {
257            //std::cout.write((char *)buff->data, buff->size);
258        }
259        curl_easy_cleanup(curl);
260    }
261}
Note: See TracBrowser for help on using the repository browser.