source: geovis/trunk/IData.cpp @ 6313

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

add tracing to iDataInit

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