Goby v2
liaison_wt_thread.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include <dlfcn.h>
23 
24 #include <Wt/WAnchor>
25 #include <Wt/WHBoxLayout>
26 #include <Wt/WImage>
27 #include <Wt/WStackedWidget>
28 #include <Wt/WText>
29 #include <Wt/WVBoxLayout>
30 
31 #include "goby/common/time.h"
32 #include "goby/util/dynamic_protobuf_manager.h"
33 
34 #include "liaison_home.h"
35 #include "liaison_wt_thread.h"
36 
37 #include "goby/moos/moos_liaison_load.h"
38 
39 using goby::glog;
40 using namespace Wt;
41 using namespace goby::common::logger;
42 
43 goby::common::LiaisonWtThread::LiaisonWtThread(const Wt::WEnvironment& env) : Wt::WApplication(env)
44 {
45  // zeromq_service_.connect_inbox_slot(&LiaisonWtThread::inbox, this);
46 
47  Wt::WString title_text("goby liaison: " + liaison_cfg_.base().platform_name());
48  setTitle(title_text);
49 
50  useStyleSheet(std::string("css/fonts.css?" + common::goby_file_timestamp()));
51  useStyleSheet(std::string("css/liaison.css?" + common::goby_file_timestamp()));
52  setCssTheme("default");
53 
54  root()->setId("main");
55 
56  /*
57  * Set up the title
58  */
59  WContainerWidget* header_div = new WContainerWidget(root());
60  header_div->setId("header");
61 
62  WText* header = new WText(title_text, header_div);
63  header->setId("header");
64 
65  WImage* goby_logo = new WImage("images/gobysoft_logo_dot_org_small.png");
66  WAnchor* goby_logo_a =
67  new WAnchor("http://gobysoft.org/#/software/goby", goby_logo, header_div);
68  goby_logo_a->setId("goby_logo");
69  goby_logo_a->setStyleClass("no_ul");
70  goby_logo_a->setTarget(TargetNewWindow);
71 
72  if (!liaison_cfg_.has_upper_right_logo())
73  {
74  WImage* goby_lp_image = new WImage("images/mit-logo.gif");
75  WAnchor* goby_lp_image_a = new WAnchor("http://lamss.mit.edu", goby_lp_image, header_div);
76  goby_lp_image_a->setId("lp_logo");
77  goby_lp_image_a->setStyleClass("no_ul");
78  goby_lp_image_a->setTarget(TargetNewWindow);
79  }
80  else
81  {
82  WImage* goby_lp_image = new WImage(liaison_cfg_.upper_right_logo());
83  WAnchor* goby_lp_image_a = new WAnchor(
84  liaison_cfg_.has_upper_right_logo_link() ? liaison_cfg_.upper_right_logo_link() : "",
85  goby_lp_image, header_div);
86  goby_lp_image_a->setId("lp_logo");
87  goby_lp_image_a->setStyleClass("no_ul");
88  goby_lp_image_a->setTarget(TargetNewWindow);
89  }
90 
91  new WText("<hr/>", root());
92 
93  WContainerWidget* menu_div = new WContainerWidget(root());
94  menu_div->setStyleClass("menu");
95 
96  WContainerWidget* contents_div = new WContainerWidget(root());
97  contents_div->setId("contents");
98  contents_stack_ = new WStackedWidget(contents_div);
99  contents_stack_->setStyleClass("fill");
100 
101  /*
102  * Setup the menu
103  */
104  menu_ = new WMenu(contents_stack_, Vertical, menu_div);
105  menu_->setRenderAsList(true);
106  menu_->setStyleClass("menu");
107  menu_->setInternalPathEnabled();
108  menu_->setInternalBasePath("/");
109 
110  add_to_menu(menu_, new LiaisonHome);
111 
112  typedef std::vector<goby::common::LiaisonContainer*> (*liaison_load_func)(
114  boost::shared_ptr<zmq::context_t> zmq_context);
115 
116  for (int i = 0, n = Liaison::plugin_handles_.size(); i < n; ++i)
117  {
118  liaison_load_func liaison_load_ptr =
119  (liaison_load_func)dlsym(Liaison::plugin_handles_[i], "goby_liaison_load");
120 
121  if (liaison_load_ptr)
122  {
123  std::vector<goby::common::LiaisonContainer*> containers =
124  (*liaison_load_ptr)(liaison_cfg_, Liaison::zmq_context());
125  for (int j = 0, m = containers.size(); j < m; ++j) add_to_menu(menu_, containers[j]);
126  }
127  else
128  {
129  glog.is(WARN) &&
130  glog << "Liaison: Cannot find function 'goby_liaison_load' in plugin library."
131  << std::endl;
132  }
133  }
134 
135  menu_->itemSelected().connect(this, &LiaisonWtThread::handle_menu_selection);
136 
137  handle_menu_selection(menu_->currentItem());
138 }
139 
140 goby::common::LiaisonWtThread::~LiaisonWtThread()
141 {
142  // run on all children
143  const std::vector<WMenuItem*>& items = menu_->items();
144  for (int i = 0, n = items.size(); i < n; ++i)
145  {
146  LiaisonContainer* contents = menu_contents_[items[i]];
147  if (contents)
148  {
149  glog.is(DEBUG1) && glog << "Liaison: Cleanup : " << contents->name() << std::endl;
150  contents->cleanup();
151  }
152  }
153 }
154 
155 void goby::common::LiaisonWtThread::add_to_menu(WMenu* menu, LiaisonContainer* container)
156 {
157  Wt::WMenuItem* new_item = menu->addItem(container->name(), container);
158  menu_contents_.insert(std::make_pair(new_item, container));
159 }
160 
161 void goby::common::LiaisonWtThread::handle_menu_selection(Wt::WMenuItem* item)
162 {
163  LiaisonContainer* contents = menu_contents_[item];
164  if (contents)
165  {
166  glog.is(DEBUG1) && glog << "Liaison: Focused : " << contents->name() << std::endl;
167 
168  contents->focus();
169  }
170  else
171  {
172  glog.is(WARN) && glog << "Liaison: Invalid menu item!" << std::endl;
173  }
174 
175  // unfocus all others
176  const std::vector<WMenuItem*>& items = menu_->items();
177  for (int i = 0, n = items.size(); i < n; ++i)
178  {
179  if (items[i] != item)
180  {
181  LiaisonContainer* other_contents = menu_contents_[items[i]];
182  if (other_contents)
183  {
184  glog.is(DEBUG1) && glog << "Liaison: Unfocused : " << other_contents->name()
185  << std::endl;
186  other_contents->unfocus();
187  }
188  }
189  }
190 }
191 
192 void goby::common::LiaisonWtThread::inbox(MarshallingScheme marshalling_scheme,
193  const std::string& identifier, const void* data, int size,
194  int socket_id)
195 {
196  glog.is(DEBUG1) && glog << "LiaisonWtThread: got message with identifier: " << identifier
197  << std::endl;
198 }
std::string goby_file_timestamp()
ISO string representation of goby_time()
Definition: time.h:156
common::FlexOstream glog
Access the Goby logger through this object.