rms.cpp
Go to the documentation of this file.
00001 
00011 #include <rmscpp/rms.h>
00012 #include <sstream>
00013 #include <stdlib.h>
00014 #include <string.h>
00015 
00016 using namespace std;
00017 using namespace rms;
00018 
00019 rms_client::rms_client(std::string host, int port, PROTOCOL protocol)
00020 {
00021   // create the cURL handle
00022   curl = curl_easy_init();
00023   // set the pointer to the function which handles the responses
00024   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
00025 
00026   // build the start string
00027   switch (protocol)
00028   {
00029     case HTTPS_PROTOCOL:
00030       url_start = "https://";
00031       break;
00032     default:
00033       // default to HTTP
00034       url_start = "http://";
00035       break;
00036   }
00037   stringstream ss;
00038   ss << url_start << host << ":" << port << API;
00039   url_start = ss.str();
00040 }
00041 
00042 rms_client::~rms_client()
00043 {
00044   // cleanup anything left by cURL
00045   curl_easy_cleanup(curl);
00046 }
00047 
00048 void rms_client::set_authorization(string username, string password)
00049 {
00050   // set the username and password
00051   curl_easy_setopt(curl, CURLOPT_USERPWD, username.append(":").append(password).c_str());
00052 }
00053 
00054 vector<javascript_file> rms_client::get_javascript_files()
00055 {
00056   // build the string and make the request
00057   stringstream ss;
00058   ss << url_start << JAVASCRIPT_FILES_DEFAULT;
00059   json_object *json = make_request(ss.str());
00060 
00061   // check if we should throw an error
00062   if (extract_json_bool(json, "ok"))
00063   {
00064     vector<javascript_file> v = parse_javascript_files_array(json_object_object_get(json, "data"));
00065     json_object_put(json);
00066     return v;
00067   }
00068   else
00069   {
00070     string msg = extract_json_string(json, "msg");
00071     json_object_put(json);
00072     throw msg;
00073   }
00074 }
00075 
00076 javascript_file rms_client::get_javascript_file_by_id(int fileid)
00077 {
00078   // build the string and make the request
00079   stringstream ss;
00080   ss << url_start << JAVASCRIPT_FILES_DEFAULT << "?" << JAVASCRIPT_FILES_ID_FIELD << "=" << fileid;
00081   json_object *json = make_request(ss.str());
00082 
00083   // check if we should throw an error
00084   if (extract_json_bool(json, "ok"))
00085   {
00086     javascript_file jsf = parse_javascript_file(json_object_object_get(json, "data"));
00087     json_object_put(json);
00088     return jsf;
00089   }
00090   else
00091   {
00092     string msg = extract_json_string(json, "msg");
00093     json_object_put(json);
00094     throw msg;
00095   }
00096 }
00097 
00098 vector<article> rms_client::get_articles()
00099 {
00100   // build the string and make the request
00101   stringstream ss;
00102   ss << url_start << ARTICLES_DEFAULT;
00103   json_object *json = make_request(ss.str());
00104 
00105   // check if we should throw an error
00106   if (extract_json_bool(json, "ok"))
00107   {
00108     vector<article> v = parse_articles_array(json_object_object_get(json, "data"));
00109     json_object_put(json);
00110     return v;
00111   }
00112   else
00113   {
00114     string msg = extract_json_string(json, "msg");
00115     json_object_put(json);
00116     throw msg;
00117   }
00118 }
00119 
00120 article rms_client::get_article_by_id(int artid)
00121 {
00122   // build the string and make the request
00123   stringstream ss;
00124   ss << url_start << ARTICLES_DEFAULT << "?" << ARTICLES_ID_FIELD << "=" << artid;
00125   json_object *json = make_request(ss.str());
00126 
00127   // check if we should throw an error
00128   if (extract_json_bool(json, "ok"))
00129   {
00130     article u = parse_article(json_object_object_get(json, "data"));
00131     json_object_put(json);
00132     return u;
00133   }
00134   else
00135   {
00136     string msg = extract_json_string(json, "msg");
00137     json_object_put(json);
00138     throw msg;
00139   }
00140 }
00141 
00142 vector<article> rms_client::get_articles_by_pageid(int pageid)
00143 {
00144   // build the string and make the request
00145   stringstream ss;
00146   ss << url_start << ARTICLES_DEFAULT << "?" << ARTICLES_PAGES_ID_FIELD << "=" << pageid;
00147   json_object *json = make_request(ss.str());
00148 
00149   // check if we should throw an error
00150   if (extract_json_bool(json, "ok"))
00151   {
00152     vector<article> v = parse_articles_array(json_object_object_get(json, "data"));
00153     json_object_put(json);
00154     return v;
00155   }
00156   else
00157   {
00158     string msg = extract_json_string(json, "msg");
00159     json_object_put(json);
00160     throw msg;
00161   }
00162 }
00163 
00164 vector<content_page> rms_client::get_content_pages()
00165 {
00166   // build the string and make the request
00167   stringstream ss;
00168   ss << url_start << CONTENT_PAGES_DEFAULT;
00169   json_object *json = make_request(ss.str());
00170 
00171   // check if we should throw an error
00172   if (extract_json_bool(json, "ok"))
00173   {
00174     vector<content_page> v = parse_content_pages_array(json_object_object_get(json, "data"));
00175     json_object_put(json);
00176     return v;
00177   }
00178   else
00179   {
00180     string msg = extract_json_string(json, "msg");
00181     json_object_put(json);
00182     throw msg;
00183   }
00184 }
00185 
00186 content_page rms_client::get_content_page_by_id(int pageid)
00187 {
00188   // build the string and make the request
00189   stringstream ss;
00190   ss << url_start << CONTENT_PAGES_DEFAULT << "?" << CONTENT_PAGES_ID_FIELD << "=" << pageid;
00191   json_object *json = make_request(ss.str());
00192 
00193   // check if we should throw an error
00194   if (extract_json_bool(json, "ok"))
00195   {
00196     content_page cp = parse_content_page(json_object_object_get(json, "data"));
00197     json_object_put(json);
00198     return cp;
00199   }
00200   else
00201   {
00202     string msg = extract_json_string(json, "msg");
00203     json_object_put(json);
00204     throw msg;
00205   }
00206 }
00207 
00208 vector<slide> rms_client::get_slides()
00209 {
00210   // build the string and make the request
00211   stringstream ss;
00212   ss << url_start << SLIDES_DEFAULT;
00213   json_object *json = make_request(ss.str());
00214 
00215   // check if we should throw an error
00216   if (extract_json_bool(json, "ok"))
00217   {
00218     vector<slide> v = parse_slides_array(json_object_object_get(json, "data"));
00219     json_object_put(json);
00220     return v;
00221   }
00222   else
00223   {
00224     string msg = extract_json_string(json, "msg");
00225     json_object_put(json);
00226     throw msg;
00227   }
00228 }
00229 
00230 vector<condition> rms_client::get_conditions()
00231 {
00232   // build the string and make the request
00233   stringstream ss;
00234   ss << url_start << CONDITIONS_DEFAULT;
00235   json_object *json = make_request(ss.str());
00236 
00237   // check if we should throw an error
00238   if (extract_json_bool(json, "ok"))
00239   {
00240     vector<condition> v = parse_conditions_array(json_object_object_get(json, "data"));
00241     json_object_put(json);
00242     return v;
00243   }
00244   else
00245   {
00246     string msg = extract_json_string(json, "msg");
00247     json_object_put(json);
00248     throw msg;
00249   }
00250 }
00251 
00252 condition rms_client::get_condition_by_id(int condid)
00253 {
00254   // build the string and make the request
00255   stringstream ss;
00256   ss << url_start << CONDITIONS_DEFAULT << "?" << CONDITIONS_ID_FIELD << "=" << condid;
00257   json_object *json = make_request(ss.str());
00258 
00259   // check if we should throw an error
00260   if (extract_json_bool(json, "ok"))
00261   {
00262     condition c = parse_condition(json_object_object_get(json, "data"));
00263     json_object_put(json);
00264     return c;
00265   }
00266   else
00267   {
00268     string msg = extract_json_string(json, "msg");
00269     json_object_put(json);
00270     throw msg;
00271   }
00272 }
00273 
00274 vector<experiment> rms_client::get_experiments()
00275 {
00276   // build the string and make the request
00277   stringstream ss;
00278   ss << url_start << EXPERIMENTS_DEFAULT;
00279   json_object *json = make_request(ss.str());
00280 
00281   // check if we should throw an error
00282   if (extract_json_bool(json, "ok"))
00283   {
00284     vector<experiment> v = parse_experiments_array(json_object_object_get(json, "data"));
00285     json_object_put(json);
00286     return v;
00287   }
00288   else
00289   {
00290     string msg = extract_json_string(json, "msg");
00291     json_object_put(json);
00292     throw msg;
00293   }
00294 }
00295 
00296 experiment rms_client::get_experiment_by_id(int expid)
00297 {
00298   // build the string and make the request
00299   stringstream ss;
00300   ss << url_start << EXPERIMENTS_DEFAULT << "?" << EXPERIMENTS_ID_FIELD << "=" << expid;
00301   json_object *json = make_request(ss.str());
00302 
00303   // check if we should throw an error
00304   if (extract_json_bool(json, "ok"))
00305   {
00306     experiment c = parse_experiment(json_object_object_get(json, "data"));
00307     json_object_put(json);
00308     return c;
00309   }
00310   else
00311   {
00312     string msg = extract_json_string(json, "msg");
00313     json_object_put(json);
00314     throw msg;
00315   }
00316 }
00317 
00318 vector<study> rms_client::get_studies()
00319 {
00320   // build the string and make the request
00321   stringstream ss;
00322   ss << url_start << STUDIES_DEFAULT;
00323   json_object *json = make_request(ss.str());
00324 
00325   // check if we should throw an error
00326   if (extract_json_bool(json, "ok"))
00327   {
00328     vector<study> v = parse_studies_array(json_object_object_get(json, "data"));
00329     json_object_put(json);
00330     return v;
00331   }
00332   else
00333   {
00334     string msg = extract_json_string(json, "msg");
00335     json_object_put(json);
00336     throw msg;
00337   }
00338 }
00339 
00340 study rms_client::get_study_by_id(int studyid)
00341 {
00342   // build the string and make the request
00343   stringstream ss;
00344   ss << url_start << STUDIES_DEFAULT << "?" << STUDIES_ID_FIELD << "=" << studyid;
00345   json_object *json = make_request(ss.str());
00346 
00347   // check if we should throw an error
00348   if (extract_json_bool(json, "ok"))
00349   {
00350     study s = parse_study(json_object_object_get(json, "data"));
00351     json_object_put(json);
00352     return s;
00353   }
00354   else
00355   {
00356     string msg = extract_json_string(json, "msg");
00357     json_object_put(json);
00358     throw msg;
00359   }
00360 }
00361 
00362 vector<study_log> rms_client::get_study_logs()
00363 {
00364   // build the string and make the request
00365   stringstream ss;
00366   ss << url_start << STUDY_LOGS_DEFAULT;
00367   json_object *json = make_request(ss.str());
00368 
00369   // check if we should throw an error
00370   if (extract_json_bool(json, "ok"))
00371   {
00372     vector<study_log> v = parse_study_logs_array(json_object_object_get(json, "data"));
00373     json_object_put(json);
00374     return v;
00375   }
00376   else
00377   {
00378     string msg = extract_json_string(json, "msg");
00379     json_object_put(json);
00380     throw msg;
00381   }
00382 }
00383 
00384 study_log rms_client::get_study_log_by_id(int logid)
00385 {
00386   // build the string and make the request
00387   stringstream ss;
00388   ss << url_start << STUDY_LOGS_DEFAULT << "?" << STUDY_LOGS_ID_FIELD << "=" << logid;
00389   json_object *json = make_request(ss.str());
00390 
00391   // check if we should throw an error
00392   if (extract_json_bool(json, "ok"))
00393   {
00394     study_log sl = parse_study_log(json_object_object_get(json, "data"));
00395     json_object_put(json);
00396     return sl;
00397   }
00398   else
00399   {
00400     string msg = extract_json_string(json, "msg");
00401     json_object_put(json);
00402     throw msg;
00403   }
00404 }
00405 
00406 vector<study_log> rms_client::get_study_logs_by_expid(int expid)
00407 {
00408   // build the string and make the request
00409   stringstream ss;
00410   ss << url_start << STUDY_LOGS_DEFAULT << "?" << STUDY_LOGS_EXPERIMENTS_ID_FIELD << "=" << expid;
00411   json_object *json = make_request(ss.str());
00412 
00413   // check if we should throw an error
00414   if (extract_json_bool(json, "ok"))
00415   {
00416     vector<study_log> v = parse_study_logs_array(json_object_object_get(json, "data"));
00417     json_object_put(json);
00418     return v;
00419   }
00420   else
00421   {
00422     string msg = extract_json_string(json, "msg");
00423     json_object_put(json);
00424     throw msg;
00425   }
00426 }
00427 
00428 vector<user_account> rms_client::get_user_accounts()
00429 {
00430   // build the string and make the request
00431   stringstream ss;
00432   ss << url_start << USER_ACCOUNTS_DEFAULT;
00433   json_object *json = make_request(ss.str());
00434 
00435   // check if we should throw an error
00436   if (extract_json_bool(json, "ok"))
00437   {
00438     vector<user_account> v = parse_user_accounts_array(json_object_object_get(json, "data"));
00439     json_object_put(json);
00440     return v;
00441   }
00442   else
00443   {
00444     string msg = extract_json_string(json, "msg");
00445     json_object_put(json);
00446     throw msg;
00447   }
00448 }
00449 
00450 user_account rms_client::get_user_account_by_id(int userid)
00451 {
00452   // build the string and make the request
00453   stringstream ss;
00454   ss << url_start << USER_ACCOUNTS_DEFAULT << "?" << USER_ACCOUNTS_ID_FIELD << "=" << userid;
00455   json_object *json = make_request(ss.str());
00456 
00457   // check if we should throw an error
00458   if (extract_json_bool(json, "ok"))
00459   {
00460     user_account u = parse_user_account(json_object_object_get(json, "data"));
00461     json_object_put(json);
00462     return u;
00463   }
00464   else
00465   {
00466     string msg = extract_json_string(json, "msg");
00467     json_object_put(json);
00468     throw msg;
00469   }
00470 }
00471 
00472 vector<javascript_file> rms_client::parse_javascript_files_array(json_object *json_array)
00473 {
00474   vector<javascript_file> v;
00475 
00476   // go through each JSON object in the array
00477   int length = json_object_array_length(json_array);
00478   for (int i = 0; i < length; i++)
00479     v.push_back(parse_javascript_file(json_object_array_get_idx(json_array, i)));
00480 
00481   return v;
00482 }
00483 
00484 javascript_file rms_client::parse_javascript_file(json_object *json)
00485 {
00486   // pull out all of the info from the JSON object
00487   int fileid = extract_json_int(json, "fileid");
00488   string url = extract_json_string(json, "url");
00489   string path = extract_json_string(json, "path");
00490 
00491   return javascript_file(fileid, url, path);
00492 }
00493 
00494 vector<article> rms_client::parse_articles_array(json_object *json_array)
00495 {
00496   vector<article> v;
00497 
00498   // go through each JSON object in the array
00499   int length = json_object_array_length(json_array);
00500   for (int i = 0; i < length; i++)
00501     v.push_back(parse_article(json_object_array_get_idx(json_array, i)));
00502 
00503   return v;
00504 }
00505 
00506 article rms_client::parse_article(json_object *json)
00507 {
00508   // pull out all of the info from the JSON object
00509   int artid = extract_json_int(json, "artid");
00510   string title = extract_json_string(json, "title");
00511   string content = extract_json_string(json, "content");
00512   int pageid = extract_json_int(json, "pageid");
00513   int index = extract_json_int(json, "index");
00514 
00515   return article(artid, title, content, pageid, index);
00516 }
00517 
00518 vector<content_page> rms_client::parse_content_pages_array(json_object *json_array)
00519 {
00520   vector<content_page> v;
00521 
00522   // go through each JSON object in the array
00523   int length = json_object_array_length(json_array);
00524   for (int i = 0; i < length; i++)
00525     v.push_back(parse_content_page(json_object_array_get_idx(json_array, i)));
00526 
00527   return v;
00528 }
00529 
00530 content_page rms_client::parse_content_page(json_object *json)
00531 {
00532   // pull out all of the info from the JSON object
00533   int pageid = extract_json_int(json, "pageid");
00534   string title = extract_json_string(json, "title");
00535   string menu = extract_json_string(json, "menu");
00536   int index = extract_json_int(json, "index");
00537   string js = extract_json_string(json, "js");
00538 
00539   return content_page(pageid, title, menu, index, js);
00540 }
00541 
00542 vector<slide> rms_client::parse_slides_array(json_object *json_array)
00543 {
00544   vector<slide> v;
00545 
00546   // go through each JSON object in the array
00547   int length = json_object_array_length(json_array);
00548   for (int i = 0; i < length; i++)
00549     v.push_back(parse_slide(json_object_array_get_idx(json_array, i)));
00550 
00551   return v;
00552 }
00553 
00554 slide rms_client::parse_slide(json_object *json)
00555 {
00556   // pull out all of the info from the JSON object
00557   int slideid = extract_json_int(json, "slideid");
00558   string img = extract_json_string(json, "img");
00559   string caption = extract_json_string(json, "caption");
00560   int index = extract_json_int(json, "index");
00561 
00562   return slide(slideid, img, caption, index);
00563 }
00564 
00565 vector<condition> rms_client::parse_conditions_array(json_object *json_array)
00566 {
00567   vector<condition> v;
00568 
00569   // go through each JSON object in the array
00570   int length = json_object_array_length(json_array);
00571   for (int i = 0; i < length; i++)
00572     v.push_back(parse_condition(json_object_array_get_idx(json_array, i)));
00573 
00574   return v;
00575 }
00576 
00577 condition rms_client::parse_condition(json_object *json)
00578 {
00579   // pull out all of the info from the JSON object
00580   int condid = extract_json_int(json, "condid");
00581   int studyid = extract_json_int(json, "studyid");
00582   string name = extract_json_string(json, "name");
00583   int intid = extract_json_int(json, "intid");
00584 
00585   return condition(condid, studyid, name, intid);
00586 }
00587 
00588 vector<experiment> rms_client::parse_experiments_array(json_object *json_array)
00589 {
00590   vector<experiment> v;
00591 
00592   // go through each JSON object in the array
00593   int length = json_object_array_length(json_array);
00594   for (int i = 0; i < length; i++)
00595     v.push_back(parse_experiment(json_object_array_get_idx(json_array, i)));
00596 
00597   return v;
00598 }
00599 
00600 experiment rms_client::parse_experiment(json_object *json)
00601 {
00602   // pull out all of the info from the JSON object
00603   int expid = extract_json_int(json, "expid");
00604   int userid = extract_json_int(json, "userid");
00605   int condid = extract_json_int(json, "condid");
00606   int envid = extract_json_int(json, "envid");
00607   string start = extract_json_string(json, "start");
00608   string end = extract_json_string(json, "end");
00609 
00610   return experiment(expid, userid, condid, envid, start, end);
00611 }
00612 
00613 vector<study> rms_client::parse_studies_array(json_object *json_array)
00614 {
00615   vector<study> v;
00616 
00617   // go through each JSON object in the array
00618   int length = json_object_array_length(json_array);
00619   for (int i = 0; i < length; i++)
00620     v.push_back(parse_study(json_object_array_get_idx(json_array, i)));
00621 
00622   return v;
00623 }
00624 
00625 study rms_client::parse_study(json_object *json)
00626 {
00627   // pull out all of the info from the JSON object
00628   int studyid = extract_json_int(json, "studyid");
00629   string name = extract_json_string(json, "name");
00630   string description = extract_json_string(json, "description");
00631   string start = extract_json_string(json, "start");
00632   string end = extract_json_string(json, "end");
00633 
00634   return study(studyid, name, description, start, end);
00635 }
00636 
00637 vector<study_log> rms_client::parse_study_logs_array(json_object *json_array)
00638 {
00639   vector<study_log> v;
00640 
00641   // go through each JSON object in the array
00642   int length = json_object_array_length(json_array);
00643   for (int i = 0; i < length; i++)
00644     v.push_back(parse_study_log(json_object_array_get_idx(json_array, i)));
00645 
00646   return v;
00647 }
00648 
00649 study_log rms_client::parse_study_log(json_object *json)
00650 {
00651   // pull out all of the info from the JSON object
00652   int logid = extract_json_int(json, "logid");
00653   int expid = extract_json_int(json, "expid");
00654   string timestamp = extract_json_string(json, "timestamp");
00655   string entry = extract_json_string(json, "entry");
00656 
00657   return study_log(logid, expid, timestamp, entry);
00658 }
00659 
00660 vector<user_account> rms_client::parse_user_accounts_array(json_object *json_array)
00661 {
00662   vector<user_account> v;
00663 
00664   // go through each JSON object in the array
00665   int length = json_object_array_length(json_array);
00666   for (int i = 0; i < length; i++)
00667     v.push_back(parse_user_account(json_object_array_get_idx(json_array, i)));
00668 
00669   return v;
00670 }
00671 
00672 user_account rms_client::parse_user_account(json_object *json)
00673 {
00674   // pull out all of the info from the JSON object
00675   int userid = extract_json_int(json, "userid");
00676   string username = extract_json_string(json, "username");
00677   string firstname = extract_json_string(json, "firstname");
00678   string lastname = extract_json_string(json, "lastname");
00679   string email = extract_json_string(json, "email");
00680   string type_string = extract_json_string(json, "type");
00681   USER_ACCOUNT_TYPE type;
00682   if (type_string.compare("admin") == 0)
00683     type = ADMIN;
00684   else
00685     type = USER;
00686 
00687   return user_account(userid, username, firstname, lastname, email, type);
00688 }
00689 
00690 string rms_client::extract_json_string(json_object *json, string key)
00691 {
00692   json_object *obj = json_object_object_get(json, key.c_str());
00693 
00694   // check for a null value
00695   if (obj)
00696   {
00697     const char *src = json_object_get_string(obj);
00698     char *dst = (char *)malloc(strlen(src) + 1);
00699     string s = string(strcpy(dst, src));
00700 
00701     return s;
00702   }
00703   else
00704     return string("NULL");
00705 }
00706 
00707 bool rms_client::extract_json_bool(json_object *json, string key)
00708 {
00709   json_object *obj = json_object_object_get(json, key.c_str());
00710   bool b = json_object_get_boolean(obj);
00711 
00712   return b;
00713 }
00714 
00715 int rms_client::extract_json_int(json_object *json, string key)
00716 {
00717   json_object *obj = json_object_object_get(json, key.c_str());
00718   int i = json_object_get_int(obj);
00719 
00720   return i;
00721 }
00722 
00723 json_object *rms_client::make_request(string url)
00724 {
00725   // create the response
00726   rms_response *resp = new rms_response;
00727   resp->size = 0;
00728   resp->data = NULL;
00729   curl_easy_setopt(curl, CURLOPT_WRITEDATA, resp);
00730 
00731   //send the command to the server
00732   curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
00733   curl_easy_perform(curl);
00734 
00735   json_object *json = json_tokener_parse(resp->data);
00736 
00737   free_rms_response(resp);
00738   return json;
00739 }
00740 
00741 void rms_client::free_rms_response(rms::rms_response *buf)
00742 {
00743   // check if the response is null
00744   if (buf)
00745   {
00746     // free the data (if any)
00747     if (buf->data)
00748       free(buf->data);
00749     // free the struct
00750     free(buf);
00751   }
00752 }
00753 
00754 size_t rms::write_data(char *ptr, size_t size, size_t nmemb, rms::rms_response *buf)
00755 {
00756   // actual size of the new data
00757   size_t new_data = size * nmemb;
00758 
00759   // see if there is any data
00760   if (new_data > 0)
00761   {
00762     // check if the buffer already has data
00763     if (buf->data)
00764       // resize the buffer
00765       buf->data = (char *)realloc(buf->data, buf->size + new_data + 1);
00766     else
00767       // allocate the initial memory
00768       buf->data = (char *)malloc(new_data + 1);
00769 
00770     // add the data to the buffer
00771     memcpy(&(buf->data[buf->size]), ptr, new_data);
00772     //update the size
00773     buf->size += new_data;
00774     // null terminate
00775     buf->data[buf->size] = '\0';
00776   }
00777 
00778   return new_data;
00779 }


rmscpp
Author(s): Russell Toris
autogenerated on Sat Dec 28 2013 17:08:57