Bridges-C++  3.4.1
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  }
42  class Bridges {
43  private:
44  static bool profile() {
45  return false;
46  }
47 
48 
49  static string getDefaultServerURL() {
50  return "http://bridges-cs.herokuapp.com";
51  }
52 
53 
54  bool jsonFlag = false; // if JSON is to be printed
55 
56  // this flag will turn on all labels in the visualization
57  bool element_labelFlag = false, link_labelFlag = false;
58 
59  bool post_visualization_link = true; // post flag of visualization url
60 
61  string user_name = string(),
62  api_key = string(); // user credentials
63 
64  string map[2]; // for map overlays
65 
66  string description = string(); // visualization description
67 
68  string title = string(); // title of visualization
69 
70  unsigned int assn_num = 0; // assignment id
71 
72  DataStructure* ds_handle = nullptr; // data structure handle
73 
74  string server_url = "http://bridges-cs.herokuapp.com";
75 
76  string BASE_URL = server_url + "/assignments/";
77 
78  // map overlay options
79  string map_overlay_options[3] = {"cartesian", "albersusa", "equirectangular"};
80  bool map_overlay = false;
81  string coord_system_type = "cartesian";
82 
83  // world coordinate window
84  vector<double> wc_window;
85 
86  unsigned int lastAssignNum = 0, subAssignNum = 0;
87 
88 
89  public:
90 
91  Bridges() {
92  Bridges (0, "", "");
93  }
106  Bridges (const string& name, const string& key) {
107  Bridges (0, name, key);
108  }
129  Bridges (unsigned int num, const string& name, const string& key) {
130  setAssignment(num);
131  setUserName(name);
132  setApiKey(key);
133  setServer("live");
134  }
135 
144  void setElementLabelFlag(bool flag) {
145  element_labelFlag = flag;
146  }
155  void setLinkLabelFlag(bool flag) {
156  link_labelFlag = flag;
157  }
158 
165  bool getElementLabelFlag() const {
166  return element_labelFlag;
167  }
175  bool getLinkLabelFlag() const {
176  return link_labelFlag;
177  }
178 
185  bool getJSONFlag() const {
186  return jsonFlag;
187  }
188 
198  void postVisualizationLink(bool link_url) {
199  post_visualization_link = link_url;
200  }
201 
207  void setJSONFlag(bool flag) {
208  jsonFlag = flag;
209  }
210 
218  const string& getUserName() const {
219  return user_name;
220  }
230  void setUserName(string name) {
231  char* force = getenv("FORCE_BRIDGES_USERNAME");
232  if (force != nullptr) {
233  name = force;
234  }
235 
236  user_name = name;
237  }
243  const string& getApiKey() const {
244  return api_key;
245  }
256  void setApiKey(string key) {
257  char* force = getenv("FORCE_BRIDGES_APIKEY");
258  if (force != nullptr) {
259  key = force;
260  }
261  api_key = key;
262  }
269  unsigned int getAssignment() const {
270  return assn_num;
271  }
281  void setAssignment(unsigned int num) {
282  char* force = getenv("FORCE_BRIDGES_ASSIGNMENT");
283  if (force != nullptr) {
284  num = std::atoi(force);
285  }
286 
287  assn_num = num;
288 
289  if (assn_num != lastAssignNum) { // reset if a new assignment
290  lastAssignNum = assn_num;
291  subAssignNum = 0;
292  }
293  }
301  const string& getTitle() const {
302  return title;
303  }
304 
312  void setTitle(const string& t) {
313  title = t;
314  }
320  const string& getDescription() const {
321  return description;
322  }
327  void setDescription(const string& descr) {
328  description = descr;
329  }
330 
339  ds_handle = ds;
340  }
341 
350  setDataStructure(&ds);
351  }
352 
353 
360  return ds_handle;
361  }
362 
363 
375  void setServer(string server_type) {
376  char* force = getenv("FORCE_BRIDGES_APISERVER");
377  if (force != nullptr) {
378  server_type = force;
379  }
380 
381  if (server_type == "live")
382  server_url = "http://bridges-cs.herokuapp.com";
383  else if (server_type == "clone")
384  server_url = "http://bridges-clone.herokuapp.com";
385  else if (server_type == "games")
386  server_url = "http://bridges-games.herokuapp.com";
387  else if (server_type == "local")
388  server_url = "http://127.0.0.1:3000";
389 
390  BASE_URL = server_url + "/assignments/";
391  }
392 
400  void setMapOverlay (bool overlay_flag) {
401  map_overlay = overlay_flag;
402  }
403 
413  void setMap(string my_map, string info) {
414  map[0] = my_map;
415  map[1] = info;
416  }
417 
427  void setCoordSystemType (string coord) {
428  std::transform(coord.begin(), coord.end(), coord.begin(), ::tolower);
429  if (coord == "cartesian" || coord == "albersusa" || coord == "equirectangular"
430  || coord == "window")
431  coord_system_type = coord;
432  else {
433  cout << "Unrecognized coordinate system \'" + coord + "\', defaulting to "
434  << "cartesian. Options:";
435  for (auto proj : map_overlay_options)
436  cout << + "\t" ;
437  coord_system_type = "cartesian";
438  }
439  }
447  const string& getCoordSystemType () {
448  return coord_system_type;
449  }
450 
461  void setWindow (int xmin, int xmax, int ymin, int ymax) {
462  setWindow(double(xmin), double(xmax), double(ymin), double(ymax));
463  }
464 
475  void setWindow (double xmin, double xmax, double ymin, double ymax) {
476  wc_window.clear();
477  wc_window.push_back(xmin);
478  wc_window.push_back(xmax);
479  wc_window.push_back(ymin);
480  wc_window.push_back(ymax);
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 
501  if (getAssignment() != lastAssignNum) { // reset if a new assignment
502  lastAssignNum = getAssignment();
503  subAssignNum = 0;
504  }
505  if (subAssignNum == 99) {
506  cout << "#sub-assignments limit(99) exceeded, visualization not generated .."
507  << endl;
508  return;
509  }
510  if (!ds_handle) {
511  cerr << "Error: Data Structure handle null! Visualization not generated.";
512  return;
513  }
514 
515  //
516  // get the JSON of the data structure
517  // each data structure is responsible for generating its JSON
518  //
519  if (profile())
520  jsonbuild_start = std::chrono::system_clock::now();
521 
522  string ds_json = getJSONHeader() + ds_handle->getDataStructureRepresentation();
523  if (profile())
524  jsonbuild_end = std::chrono::system_clock::now();
525 
526  //
527  // print JSON if flag is on
528  //
529  if (getJSONFlag()) {
530  cout << "JSON[" + ds_handle->getDStype() + "]:\t" << ds_json << endl;
531  }
532 
533  if (profile())
534  httprequest_start = std::chrono::system_clock::now();
535 
536  try { // send the JSON of assignment to the server
537  ServerComm::makeRequest(BASE_URL + to_string(getAssignment()) + "." +
538  (subAssignNum > 9 ? "" : "0") + to_string(subAssignNum) + "?apikey=" + getApiKey() +
539  "&username=" + getUserName(), {"Content-Type: text/plain"}, ds_json);
540 
541  if (post_visualization_link) {
542  cout << "Success: Assignment posted to the server. " << endl
543  << "Check out your visualization at:" << endl << endl
544  << BASE_URL + to_string(getAssignment()) + "/" + getUserName() << endl << endl;
545  }
546  subAssignNum++;
547  }
548  catch (const string& error_str) {
549  cerr << "\nPosting assignment to the server failed!" << endl
550  << error_str << endl << endl;
551  cerr << "Provided Bridges Credentials:" << endl <<
552  "\t User Name: " << getUserName() << endl <<
553  "\t API Key: " << getApiKey() << endl <<
554  "\t Assignment Number: " << getAssignment() << endl;
555  }
556  catch (const HTTPException& he) {
557  cerr << "\nPosting assignment to the server failed!" << endl;
558  if (he.httpcode == 401) {
559  cerr << "Provided Bridges Credentials are incorrect:" << endl <<
560  "\t ServerURL: "<< getServerURL() <<endl<<
561  "\t User Name: " << getUserName() << endl <<
562  "\t API Key: " << getApiKey() << endl <<
563  "\t Assignment Number: " << getAssignment() << endl;
564  }
565  else if (he.httpcode == 413) {
566  cerr<<"Assignment is too large."<<endl;
567  cerr<<"In general the assignment should be smaller than 16MB once serialized to JSON."<<endl;
568  }
569  else {
570  std::cerr<<he.what()<<endl;
571  }
572  }
573  if (profile())
574  httprequest_end = std::chrono::system_clock::now();
575 
576 
577 
578  if (profile()) {
579  end = std::chrono::system_clock::now();
580 
581  std::chrono::duration<double> totaltime = end - start;
582  std::chrono::duration<double> jsonbuildtime = jsonbuild_end - jsonbuild_start;
583  std::chrono::duration<double> httptime = httprequest_end - httprequest_start;
584  std::cerr << "total visualize() time:" << totaltime.count() << " seconds"
585  << " (including JSON build time: " << jsonbuildtime.count() << " seconds"
586  << " and HTTP request time: " << httptime.count() << " seconds)."
587  << std::endl;
588  }
589  }
590 
591  private:
592  string getServerURL() const {
593  return server_url;
594  }
595 
596 
597  string getJSONHeader () {
599 
600  string json_header = OPEN_CURLY +
601  QUOTE + "visual" + QUOTE + COLON + JSONencode(ds_handle->getDStype()) + COMMA +
602  QUOTE + "title" + QUOTE + COLON + JSONencode(getTitle()) + COMMA +
603  QUOTE + "description" + QUOTE + COLON + JSONencode( getDescription()) + COMMA +
604  QUOTE + "map_overlay" + QUOTE + COLON + ((map_overlay) ? "true" : "false") + COMMA +
605  QUOTE + "map" + QUOTE + COLON + OPEN_BOX + QUOTE + map[0] + QUOTE +
606  COMMA + QUOTE + map[1] + QUOTE + CLOSE_BOX + COMMA +
607  QUOTE + "element_label_flag" + QUOTE + COLON + ((element_labelFlag) ? "true" : "false") + COMMA +
608  QUOTE + "link_label_flag" + QUOTE + COLON + ((link_labelFlag) ? "true" : "false") + COMMA +
609  QUOTE + "coord_system_type" + QUOTE + COLON + JSONencode(getCoordSystemType()) +
610  COMMA;
611 
612  if (wc_window.size() == 4) { // world coord window has been specified
613  json_header += QUOTE + string("window") + QUOTE + COLON + OPEN_BOX;
614  json_header += std::to_string(wc_window[0]) + COMMA +
615  std::to_string(wc_window[1]) + COMMA +
616  std::to_string(wc_window[2]) + COMMA + std::to_string(wc_window[3]);
617  json_header += CLOSE_BOX + COMMA;
618 
619  }
620 
621  return json_header;
622  }
623 
624  friend DataSource;
626  }; //end of class Bridges
627 
628 } // end of bridges namespace
629 #endif
void visualize()
Definition: Bridges.h:489
virtual const char * what() const noexcept
Definition: ServerComm.h:35
This is the superclass of all data structure types in BRIDGES.
Definition: DataStructure.h:73
void setDescription(const string &descr)
Definition: Bridges.h:327
long httpcode
Definition: ServerComm.h:19
void setElementLabelFlag(bool flag)
Flag that controls if labels of elements (nodes) are to be turned on.
Definition: Bridges.h:144
virtual const string getDStype() const =0
void setCoordSystemType(string coord)
Definition: Bridges.h:427
void setWindow(int xmin, int xmax, int ymin, int ymax)
Definition: Bridges.h:461
void setMap(string my_map, string info)
Sets the type of map overlay to use.
Definition: Bridges.h:413
const string COLON
Definition: DataStructure.h:51
Bridges(unsigned int num, const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:129
void setTitle(const string &t)
Definition: Bridges.h:312
const string OPEN_BOX
Definition: DataStructure.h:54
const string & getCoordSystemType()
Definition: Bridges.h:447
const string & getDescription() const
Definition: Bridges.h:320
void setDataStructure(DataStructure *ds)
Definition: Bridges.h:338
void setAssignment(unsigned int num)
Definition: Bridges.h:281
This is meant to be an internal class, not something that the library user will use.
Definition: SocketConnection.h:48
Bridges(const string &name, const string &key)
constructor to bridges
Definition: Bridges.h:106
void setDataStructure(DataStructure &ds)
Definition: Bridges.h:349
This class contains methods to connect and transmit a user's data structure representation to the Bri...
Definition: Bridges.h:42
void setUserName(string name)
Set user name.
Definition: Bridges.h:230
void postVisualizationLink(bool link_url)
Definition: Bridges.h:198
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
const string & getTitle() const
Definition: Bridges.h:301
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:400
bool getElementLabelFlag() const
status of flag for element labels
Definition: Bridges.h:165
Bridges()
Definition: Bridges.h:91
const string & getApiKey() const
Definition: Bridges.h:243
DataStructure * getDataStructure()
Definition: Bridges.h:359
void setApiKey(string key)
Set API key credentials.
Definition: Bridges.h:256
const string & getUserName() const
Definition: Bridges.h:218
bool getJSONFlag() const
Definition: Bridges.h:185
void setLinkLabelFlag(bool flag)
Flag that controls if labels of links(edges) are to be turned on/off.
Definition: Bridges.h:155
Definition: ServerComm.h:17
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:475
const string QUOTE
Definition: DataStructure.h:49
bool getLinkLabelFlag() const
Return status of flag for link labels.
Definition: Bridges.h:175
void setServer(string server_type)
Definition: Bridges.h:375
Definition: Array.h:9
void setJSONFlag(bool flag)
Definition: Bridges.h:207
unsigned int getAssignment() const
Definition: Bridges.h:269
std::string JSONencode(const T &d)
Definition: JSONutil.h:37