Bridges-C++  3.2.0
Bridges(C++API)
Bridges.h
Go to the documentation of this file.
1 #ifndef BRIDGES_H
2 #define BRIDGES_H
3 
4 #include <iostream>
5 #include <algorithm>
6 using namespace std;
7 
8 #include "DataStructure.h" //string, using std
9 #include "ServerComm.h" //vector
10 
11 #include <JSONutil.h>
12 #include <alltypes.h>
13 #include <chrono>
14 
15 
16 
17 namespace bridges {
18  using namespace bridges::datastructure;
19 
20  namespace game {
21  class SocketConnection;
22  }
39  class Bridges {
40  private:
41  static bool profile() {
42  return false;
43  }
44 
45 
46  static string getDefaultServerURL() {
47  return "http://bridges-cs.herokuapp.com";
48  }
49 
50  bool jsonFlag = false; // if JSON is to be printed
51 
52  bool post_visualization_link = true; // post flag of visualization url
53 
54  string user_name = string(),
55  api_key = string(); // user credentials
56 
57  string description = string(); // visualization description
58 
59  string title = string(); // title of visualization
60 
61  unsigned int assn_num = 0; // assignment id
62 
63  DataStructure* ds_handle = nullptr; // data structure handle
64 
65  string server_url = "http://bridges-cs.herokuapp.com";
66 
67  string BASE_URL = server_url + "/assignments/";
68 
69  // map overlay options
70  string map_overlay_options[3] = {"cartesian", "albersusa", "equirectangular"};
71  bool map_overlay = false;
72  string coord_system_type = "cartesian";
73 
74  // world coordinate window
75  vector<double> wc_window;
76 
77  unsigned int lastAssignNum = 0, subAssignNum = 0;
78 
79 
80  public:
81  Bridges() {
82  Bridges (0, "", "");
83  }
96  Bridges (const string& name, const string& key) {
97  Bridges (0, name, key);
98  }
116  Bridges (unsigned int num, const string& name, const string& key) {
117  setAssignment(num);
118  setUserName(name);
119  setApiKey(key);
120  }
121 
127  bool getVisualizeJSONFlag() const {
128  return jsonFlag;
129  }
130 
140  void postVisualizationLink(bool link_url) {
141  post_visualization_link = link_url;
142  }
143 
149  void setVisualizeJSONFlag(bool flag) {
150  jsonFlag = flag;
151  }
152 
160  const string& getUserName() const {
161  return user_name;
162  }
172  void setUserName(string name) {
173  char* force = getenv("FORCE_BRIDGES_USERNAME");
174  if (force != nullptr) {
175  name = force;
176  }
177 
178  user_name = name;
179  }
185  const string& getApiKey() const {
186  return api_key;
187  }
198  void setApiKey(string key) {
199  char* force = getenv("FORCE_BRIDGES_APIKEY");
200  if (force != nullptr) {
201  key = force;
202  }
203  api_key = key;
204  }
211  unsigned int getAssignment() const {
212  return assn_num;
213  }
223  void setAssignment(unsigned int num) {
224  char* force = getenv("FORCE_BRIDGES_ASSIGNMENT");
225  if (force != nullptr) {
226  num = std::atoi(force);
227  }
228 
229  assn_num = num;
230 
231  if (assn_num != lastAssignNum) { // reset if a new assignment
232  lastAssignNum = assn_num;
233  subAssignNum = 0;
234  }
235  }
243  const string& getTitle() const {
244  return title;
245  }
246 
254  void setTitle(const string& t) {
255  title = t;
256  }
262  const string& getDescription() const {
263  return description;
264  }
269  void setDescription(const string& descr) {
270  description = descr;
271  }
272 
281  ds_handle = ds;
282  }
283 
292  setDataStructure(&ds);
293  }
294 
295 
302  return ds_handle;
303  }
304 
322  void initialize(unsigned int num, const string& username, const string& apikey) {
323  setAssignment (num);
324  setUserName(username);
325  setApiKey(apikey);
326  }
335  void setServer(const string& server_type) {
336  if (server_type == "live")
337  server_url = "http://bridges-cs.herokuapp.com";
338  else if (server_type == "clone")
339  server_url = "http://bridges-clone.herokuapp.com";
340  else if (server_type == "games")
341  server_url = "http://bridges-games.herokuapp.com";
342  else if (server_type == "local")
343  server_url = "http://127.0.0.1:3000";
344 
345  BASE_URL = server_url + "/assignments/";
346  }
347 
355  void setMapOverlay (bool overlay_flag) {
356  map_overlay = overlay_flag;
357  }
358 
368  void setCoordSystemType (string coord) {
369  std::transform(coord.begin(), coord.end(), coord.begin(), ::tolower);
370  if (coord == "cartesian" || coord == "albersusa" || coord == "equirectangular"
371  || coord == "window")
372  coord_system_type = coord;
373  else {
374  cout << "Unrecognized coordinate system \'" + coord + "\', defaulting to "
375  << "cartesian. Options:";
376  for (auto proj : map_overlay_options)
377  cout << + "\t" ;
378  coord_system_type = "cartesian";
379  }
380  }
388  const string& getCoordSystemType () {
389  return coord_system_type;
390  }
391 
402  void setWindow (int xmin, int xmax, int ymin, int ymax) {
403  setWindow(double(xmin), double(xmax), double(ymin), double(ymax));
404  }
405 
416  void setWindow (double xmin, double xmax, double ymin, double ymax) {
417  wc_window.clear();
418  wc_window.push_back(xmin);
419  wc_window.push_back(xmax);
420  wc_window.push_back(ymin);
421  wc_window.push_back(ymax);
422  }
423 
430  void visualize() {
431  std::chrono::time_point<std::chrono::system_clock> start;
432  std::chrono::time_point<std::chrono::system_clock> end;
433  std::chrono::time_point<std::chrono::system_clock> jsonbuild_start;
434  std::chrono::time_point<std::chrono::system_clock> jsonbuild_end;
435  std::chrono::time_point<std::chrono::system_clock> httprequest_start;
436  std::chrono::time_point<std::chrono::system_clock> httprequest_end;
437 
438  if (profile())
439  start = std::chrono::system_clock::now();
440 
441 
442  if (getAssignment() != lastAssignNum) { // reset if a new assignment
443  lastAssignNum = getAssignment();
444  subAssignNum = 0;
445  }
446  if (subAssignNum == 99) {
447  cout << "#sub-assignments limit(99) exceeded, visualization not generated .."
448  << endl;
449  return;
450  }
451  if (!ds_handle) {
452  cerr << "Error: Data Structure handle null! Visualization not generated.";
453  return;
454  }
455 
456  //
457  // get the JSON of the data structure
458  // each data structure is responsible for generating its JSON
459  //
460  if (profile())
461  jsonbuild_start = std::chrono::system_clock::now();
462 
463  string ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
464  if (profile())
465  jsonbuild_end = std::chrono::system_clock::now();
466 
467  //
468  // print JSON if flag is on
469  //
470  if (getVisualizeJSONFlag()) {
471  cout << "JSON[" + ds_handle->getDStype() + "]:\t" << ds_json << endl;
472  }
473 
474  if (profile())
475  httprequest_start = std::chrono::system_clock::now();
476 
477  try { // send the JSON of assignment to the server
478  ServerComm::makeRequest(BASE_URL + to_string(getAssignment()) + "." +
479  (subAssignNum > 9 ? "" : "0") + to_string(subAssignNum) + "?apikey=" + getApiKey() +
480  "&username=" + getUserName(), {"Content-Type: text/plain"}, ds_json);
481 
482  if (post_visualization_link) {
483  cout << "Success: Assignment posted to the server. " << endl
484  << "Check out your visualization at:" << endl << endl
485  << BASE_URL + to_string(getAssignment()) + "/" + getUserName() << endl << endl;
486  }
487  subAssignNum++;
488  }
489  catch (const string& error_str) {
490  cerr << "\nPosting assignment to the server failed!" << endl
491  << error_str << endl << endl;
492  cerr << "Provided Bridges Credentials:" << endl <<
493  "\t User Name: " << getUserName() << endl <<
494  "\t API Key: " << getApiKey() << endl <<
495  "\t Assignment Number: " << getAssignment() << endl;
496  }
497  if (profile())
498  httprequest_end = std::chrono::system_clock::now();
499 
500 
501 
502  if (profile()) {
503  end = std::chrono::system_clock::now();
504 
505  std::chrono::duration<double> totaltime = end - start;
506  std::chrono::duration<double> jsonbuildtime = jsonbuild_end - jsonbuild_start;
507  std::chrono::duration<double> httptime = httprequest_end - httprequest_start;
508  std::cerr << "total visualize() time:" << totaltime.count() << " seconds"
509  << " (including JSON build time: " << jsonbuildtime.count() << " seconds"
510  << " and HTTP request time: " << httptime.count() << " seconds)."
511  << std::endl;
512  }
513  }
514 
515  private:
516  string getServerURL() const {
517  return server_url;
518  }
519 
520 
521  string getJSONHeader () {
523 
524  string json_header = OPEN_CURLY +
525  QUOTE + "visual" + QUOTE + COLON + JSONencode(ds_handle->getDStype()) + COMMA +
526  QUOTE + "title" + QUOTE + COLON + JSONencode(getTitle()) + COMMA +
527  QUOTE + "description" + QUOTE + COLON + JSONencode( getDescription()) + COMMA +
528  QUOTE + "map_overlay" + QUOTE + COLON + ((map_overlay) ? "true" : "false") + COMMA +
529  QUOTE + "coord_system_type" + QUOTE + COLON + JSONencode(getCoordSystemType()) +
530  COMMA;
531 
532  if (wc_window.size() == 4) { // world coord window has been specified
533  json_header += QUOTE + string("window") + QUOTE + COLON + OPEN_BOX;
534  json_header += std::to_string(wc_window[0]) + COMMA +
535  std::to_string(wc_window[1]) + COMMA +
536  std::to_string(wc_window[2]) + COMMA + std::to_string(wc_window[3]);
537  json_header += CLOSE_BOX + COMMA;
538 
539  }
540 
541  return json_header;
542  }
543 
544  friend DataSource;
546  }; //end of class Bridges
547 
548 } // end of bridges namespace
549 #endif
void visualize()
Definition: Bridges.h:430
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
void setDescription(const string &descr)
Definition: Bridges.h:269
bool getVisualizeJSONFlag() const
Definition: Bridges.h:127
virtual const string getDStype() const =0
void setCoordSystemType(string coord)
Definition: Bridges.h:368
void setWindow(int xmin, int xmax, int ymin, int ymax)
Definition: Bridges.h:402
const string COLON
Definition: DataStructure.h:51
Bridges(unsigned int num, const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:116
STL namespace.
void setTitle(const string &t)
Definition: Bridges.h:254
void setServer(const string &server_type)
Definition: Bridges.h:335
const string OPEN_BOX
Definition: DataStructure.h:54
const string & getCoordSystemType()
Definition: Bridges.h:388
const string & getDescription() const
Definition: Bridges.h:262
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:280
void setAssignment(unsigned int num)
Definition: Bridges.h:223
This is meant to be an internal class, not something that the library user will use.
Definition: SocketConnection.h:37
Bridges(const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:96
void setDataStructure(DataStructure &ds)
Definition: Bridges.h:291
This class contains methods to connect and transmit a user&#39;s data structure representation to the Bri...
Definition: Bridges.h:39
void setUserName(string name)
Set user name.
Definition: Bridges.h:172
void postVisualizationLink(bool link_url)
Definition: Bridges.h:140
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string OPEN_CURLY
Definition: DataStructure.h:52
This class provides an API to various data sources used in BRIDGES.
Definition: DataSource.h:59
const string & getTitle() const
Definition: Bridges.h:243
const string CLOSE_BOX
Definition: DataStructure.h:55
void setMapOverlay(bool overlay_flag)
Turns on map overlay for subsequent visualizations - used with location specific datasets.
Definition: Bridges.h:355
void initialize(unsigned int num, const string &username, const string &apikey)
Sets Bridges assignment and credential information.
Definition: Bridges.h:322
void setVisualizeJSONFlag(bool flag)
Definition: Bridges.h:149
Bridges()
Definition: Bridges.h:81
const string & getApiKey() const
Definition: Bridges.h:185
DataStructure * getDataStructure()
Definition: Bridges.h:301
void setApiKey(string key)
Set API key credentials.
Definition: Bridges.h:198
const string & getUserName() const
Definition: Bridges.h:160
const string COMMA
Definition: DataStructure.h:50
void setWindow(double xmin, double xmax, double ymin, double ymax)
sets the world coordinate window defining the space of the user defined objects (or nodes) ...
Definition: Bridges.h:416
const string QUOTE
Definition: DataStructure.h:49
Definition: Array.h:9
unsigned int getAssignment() const
Definition: Bridges.h:211
std::string JSONencode(const T &d)
Definition: JSONutil.h:37