Bridges-C++  3.4.4
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 namespace bridges {
16  using namespace bridges::datastructure;
17 
18  namespace game {
19  class SocketConnection;
20  }
43  class Bridges {
44  private:
45  static bool profile() {
46  return false;
47  }
48 
49  static string getDefaultServerURL() {
50  return "http://bridges-cs.herokuapp.com";
51  }
52 
53  bool jsonFlag = false; // if JSON is to be printed
54 
55  // this flag will turn on all labels in the visualization
56  bool element_labelFlag = false, link_labelFlag = false;
57 
58  bool post_visualization_link = true; // post flag of visualization url
59 
60  string user_name = string(),
61  api_key = string(); // user credentials
62 
63  string map[2]; // for map overlays
64 
65  string description = string(); // visualization description
66 
67  string title = string(); // title of visualization
68 
69  unsigned int assn_num = 0; // assignment id
70 
71  DataStructure* ds_handle = nullptr; // data structure handle
72 
73  string server_url = "http://bridges-cs.herokuapp.com";
74 
75  string BASE_URL = server_url + "/assignments/";
76 
77  // map overlay options
78  string map_overlay_options[3] = {"cartesian", "albersusa", "equirectangular"};
79  bool map_overlay = false;
80  string coord_system_type = "cartesian";
81 
82  // world coordinate window
83  vector<double> wc_window;
84 
85  unsigned int lastAssignNum = 0, subAssignNum = 0;
86 
87  public:
88 
89  Bridges() {
90  Bridges (0, "", "");
91  }
104  Bridges (const string& name, const string& key) {
105  Bridges (0, name, key);
106  }
127  Bridges (unsigned int num, const string& name, const string& key) {
128  setAssignment(num);
129  setUserName(name);
130  setApiKey(key);
131  setServer("live");
132  }
133 
142  void setElementLabelFlag(bool flag) {
143  element_labelFlag = flag;
144  }
153  void setLinkLabelFlag(bool flag) {
154  link_labelFlag = flag;
155  }
156 
163  bool getElementLabelFlag() const {
164  return element_labelFlag;
165  }
173  bool getLinkLabelFlag() const {
174  return link_labelFlag;
175  }
176 
183  bool getJSONFlag() const {
184  return jsonFlag;
185  }
186 
196  void postVisualizationLink(bool link_url) {
197  post_visualization_link = link_url;
198  }
199 
205  void setJSONFlag(bool flag) {
206  jsonFlag = flag;
207  }
208 
216  const string& getUserName() const {
217  return user_name;
218  }
228  void setUserName(string name) {
229  char* force = getenv("FORCE_BRIDGES_USERNAME");
230  if (force != nullptr) {
231  name = force;
232  }
233 
234  user_name = name;
235  }
241  const string& getApiKey() const {
242  return api_key;
243  }
254  void setApiKey(string key) {
255  char* force = getenv("FORCE_BRIDGES_APIKEY");
256  if (force != nullptr) {
257  key = force;
258  }
259  api_key = key;
260  }
267  unsigned int getAssignment() const {
268  return assn_num;
269  }
279  void setAssignment(unsigned int num) {
280  char* force = getenv("FORCE_BRIDGES_ASSIGNMENT");
281  if (force != nullptr) {
282  num = std::atoi(force);
283  }
284 
285  assn_num = num;
286 
287  if (assn_num != lastAssignNum) { // reset if a new assignment
288  lastAssignNum = assn_num;
289  subAssignNum = 0;
290  }
291  }
299  const string& getTitle() const {
300  return title;
301  }
302 
310  void setTitle(const string& t) {
311  title = t;
312  }
318  const string& getDescription() const {
319  return description;
320  }
325  void setDescription(const string& descr) {
326  description = descr;
327  }
328 
337  ds_handle = ds;
338  }
339 
348  setDataStructure(&ds);
349  }
350 
357  return ds_handle;
358  }
359 
371  void setServer(string server_type) {
372  char* force = getenv("FORCE_BRIDGES_APISERVER");
373  if (force != nullptr) {
374  server_type = force;
375  }
376 
377  if (server_type == "live")
378  server_url = "http://bridges-cs.herokuapp.com";
379  else if (server_type == "clone")
380  server_url = "http://bridges-clone.herokuapp.com";
381  else if (server_type == "games")
382  server_url = "http://bridges-games.herokuapp.com";
383  else if (server_type == "local")
384  server_url = "http://127.0.0.1:3000";
385 
386  BASE_URL = server_url + "/assignments/";
387  }
388 
396  void setMapOverlay (bool overlay_flag) {
397  map_overlay = overlay_flag;
398  }
399 
409  void setMap(string my_map, string info) {
410  map[0] = my_map;
411  map[1] = info;
412  }
413 
423  void setCoordSystemType (string coord) {
424  std::transform(coord.begin(), coord.end(), coord.begin(), ::tolower);
425  if (coord == "cartesian" || coord == "albersusa" || coord == "equirectangular"
426  || coord == "window")
427  coord_system_type = coord;
428  else {
429  cout << "Unrecognized coordinate system \'" + coord + "\', defaulting to "
430  << "cartesian. Options:";
431  for (auto proj : map_overlay_options)
432  cout << + "\t" ;
433  coord_system_type = "cartesian";
434  }
435  }
443  const string& getCoordSystemType () {
444  return coord_system_type;
445  }
446 
457  void setWindow (int xmin, int xmax, int ymin, int ymax) {
458  setWindow(double(xmin), double(xmax), double(ymin), double(ymax));
459  }
460 
471  void setWindow (double xmin, double xmax, double ymin, double ymax) {
472  wc_window.clear();
473  wc_window.push_back(xmin);
474  wc_window.push_back(xmax);
475  wc_window.push_back(ymin);
476  wc_window.push_back(ymax);
477  }
478 
479  string getVisualizeURL() const {
480  return BASE_URL + to_string(getAssignment()) + "/" + getUserName();
481  }
482 
489  void visualize() {
490  std::chrono::time_point<std::chrono::system_clock> start;
491  std::chrono::time_point<std::chrono::system_clock> end;
492  std::chrono::time_point<std::chrono::system_clock> jsonbuild_start;
493  std::chrono::time_point<std::chrono::system_clock> jsonbuild_end;
494  std::chrono::time_point<std::chrono::system_clock> httprequest_start;
495  std::chrono::time_point<std::chrono::system_clock> httprequest_end;
496 
497  if (profile())
498  start = std::chrono::system_clock::now();
499 
500  if (getAssignment() != lastAssignNum) { // reset if a new assignment
501  lastAssignNum = getAssignment();
502  subAssignNum = 0;
503  }
504  if (subAssignNum == 99) {
505  cout << "#sub-assignments limit(99) exceeded, visualization not generated .."
506  << endl;
507  return;
508  }
509  if (!ds_handle) {
510  cerr << "Error: Data Structure handle null! Visualization not generated.";
511  return;
512  }
513 
514  //
515  // get the JSON of the data structure
516  // each data structure is responsible for generating its JSON
517  //
518  if (profile())
519  jsonbuild_start = std::chrono::system_clock::now();
520 
521  // THIS IS BAD - NEEDS FIXING!!!
522  string ds_json;
523  if (ds_handle->getDStype() == "Scene") {
524  string ds_part_json = ds_handle->getDataStructureRepresentation();
525  // erase open curly brace
526  ds_part_json.erase(0, 1);
527  ds_json = getJSONHeader() + ds_part_json;
528  }
529  else
530  ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
531  if (profile())
532  jsonbuild_end = std::chrono::system_clock::now();
533 
534  //
535  // print JSON if flag is on
536  //
537  if (getJSONFlag()) {
538  cout << "JSON[" + ds_handle->getDStype() + "]:\t" << ds_json << endl;
539  }
540 
541  if (profile())
542  httprequest_start = std::chrono::system_clock::now();
543 
544  try { // send the JSON of assignment to the server
545  ServerComm::makeRequest(BASE_URL + to_string(getAssignment()) + "." +
546  (subAssignNum > 9 ? "" : "0") + to_string(subAssignNum) + "?apikey=" + getApiKey() +
547  "&username=" + getUserName(), {"Content-Type: text/plain"}, ds_json);
548 
549  if (post_visualization_link) {
550  cout << "Success: Assignment posted to the server. " << endl
551  << "Check out your visualization at:" << endl << endl
552  << getVisualizeURL() << endl << endl;
553  }
554  subAssignNum++;
555  }
556  catch (const string& error_str) {
557  cerr << "\nPosting assignment to the server failed!" << endl
558  << error_str << endl << endl;
559  cerr << "Provided Bridges Credentials:" << endl <<
560  "\t User Name: " << getUserName() << endl <<
561  "\t API Key: " << getApiKey() << endl <<
562  "\t Assignment Number: " << getAssignment() << endl;
563  }
564  catch (const HTTPException& he) {
565  cerr << "\nPosting assignment to the server failed!" << endl;
566  if (he.httpcode == 401) {
567  cerr << "Provided Bridges Credentials are incorrect:" << endl <<
568  "\t ServerURL: " << getServerURL() << endl <<
569  "\t User Name: " << getUserName() << endl <<
570  "\t API Key: " << getApiKey() << endl <<
571  "\t Assignment Number: " << getAssignment() << endl;
572  }
573  else if (he.httpcode == 413) {
574  cerr << "Assignment is too large." << endl;
575  cerr << "In general the assignment should be smaller than 16MB once serialized to JSON." << endl;
576  }
577  else {
578  std::cerr << he.what() << endl;
579  }
580  }
581  if (profile())
582  httprequest_end = std::chrono::system_clock::now();
583 
584  if (profile()) {
585  end = std::chrono::system_clock::now();
586 
587  std::chrono::duration<double> totaltime = end - start;
588  std::chrono::duration<double> jsonbuildtime = jsonbuild_end - jsonbuild_start;
589  std::chrono::duration<double> httptime = httprequest_end - httprequest_start;
590  std::cerr << "total visualize() time:" << totaltime.count() << " seconds"
591  << " (including JSON build time: " << jsonbuildtime.count() << " seconds"
592  << " and HTTP request time: " << httptime.count() << " seconds)."
593  << std::endl;
594  }
595  }
596 
597  private:
598  string getServerURL() const {
599  return server_url;
600  }
601 
602  string getJSONHeader () {
604 
605  string json_header = OPEN_CURLY +
606  QUOTE + "visual" + QUOTE + COLON + JSONencode(ds_handle->getDStype()) + COMMA +
607  QUOTE + "title" + QUOTE + COLON + JSONencode(getTitle()) + COMMA +
608  QUOTE + "description" + QUOTE + COLON + JSONencode( getDescription()) + COMMA +
609  QUOTE + "map_overlay" + QUOTE + COLON + ((map_overlay) ? "true" : "false") + COMMA +
610  QUOTE + "map" + QUOTE + COLON + OPEN_BOX + QUOTE + map[0] + QUOTE +
611  COMMA + QUOTE + map[1] + QUOTE + CLOSE_BOX + COMMA +
612  QUOTE + "element_label_flag" + QUOTE + COLON + ((element_labelFlag) ? "true" : "false") + COMMA +
613  QUOTE + "link_label_flag" + QUOTE + COLON + ((link_labelFlag) ? "true" : "false") + COMMA +
614  QUOTE + "coord_system_type" + QUOTE + COLON + JSONencode(getCoordSystemType()) +
615  COMMA;
616 
617  if (wc_window.size() == 4) { // world coord window has been specified
618  json_header += QUOTE + string("window") + QUOTE + COLON + OPEN_BOX;
619  json_header += std::to_string(wc_window[0]) + COMMA +
620  std::to_string(wc_window[1]) + COMMA +
621  std::to_string(wc_window[2]) + COMMA + std::to_string(wc_window[3]);
622  json_header += CLOSE_BOX + COMMA;
623 
624  }
625 
626  return json_header;
627  }
628 
629  friend DataSource;
631  }; //end of class Bridges
632 
633 } // end of bridges namespace
634 #endif
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:43
string getVisualizeURL() const
Definition: Bridges.h:479
void setWindow(int xmin, int xmax, int ymin, int ymax)
Definition: Bridges.h:457
bool getLinkLabelFlag() const
Return status of flag for link labels.
Definition: Bridges.h:173
Bridges(const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:104
void setMapOverlay(bool overlay_flag)
Turns on map overlay for subsequent visualizations - used with location specific datasets.
Definition: Bridges.h:396
void visualize()
Definition: Bridges.h:489
void setUserName(string name)
Set user name.
Definition: Bridges.h:228
bool getElementLabelFlag() const
status of flag for element labels
Definition: Bridges.h:163
void setDataStructure(DataStructure &ds)
Definition: Bridges.h:347
void setServer(string server_type)
Definition: Bridges.h:371
void setApiKey(string key)
Set API key credentials.
Definition: Bridges.h:254
DataStructure * getDataStructure()
Definition: Bridges.h:356
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:336
void postVisualizationLink(bool link_url)
Definition: Bridges.h:196
const string & getTitle() const
Definition: Bridges.h:299
const string & getApiKey() const
Definition: Bridges.h:241
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:471
void setDescription(const string &descr)
Definition: Bridges.h:325
unsigned int getAssignment() const
Definition: Bridges.h:267
Bridges(unsigned int num, const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:127
Bridges()
Definition: Bridges.h:89
const string & getCoordSystemType()
Definition: Bridges.h:443
void setLinkLabelFlag(bool flag)
Flag that controls if labels of links(edges) are to be turned on/off.
Definition: Bridges.h:153
void setTitle(const string &t)
Definition: Bridges.h:310
void setAssignment(unsigned int num)
Definition: Bridges.h:279
void setMap(string my_map, string info)
Sets the type of map overlay to use.
Definition: Bridges.h:409
void setCoordSystemType(string coord)
Definition: Bridges.h:423
const string & getUserName() const
Definition: Bridges.h:216
void setJSONFlag(bool flag)
Definition: Bridges.h:205
bool getJSONFlag() const
Definition: Bridges.h:183
const string & getDescription() const
Definition: Bridges.h:318
void setElementLabelFlag(bool flag)
Flag that controls if labels of elements (nodes) are to be turned on.
Definition: Bridges.h:142
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
virtual const string getDStype() const =0
This is meant to be an internal class, not something that the library user will use.
Definition: SocketConnection.h:48
std::string JSONencode(const T &d)
Definition: JSONutil.h:36
Definition: Array.h:9
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 COLON
Definition: DataStructure.h:51
const string OPEN_BOX
Definition: DataStructure.h:54
const string COMMA
Definition: DataStructure.h:50
const string OPEN_CURLY
Definition: DataStructure.h:52
const string CLOSE_BOX
Definition: DataStructure.h:55
const string QUOTE
Definition: DataStructure.h:49
Definition: ServerComm.h:17
virtual const char * what() const noexcept
Definition: ServerComm.h:35
long httpcode
Definition: ServerComm.h:19