25 #ifndef GOBY_ZEROMQ_LIAISON_LIAISON_CONTAINER_H
26 #define GOBY_ZEROMQ_LIAISON_LIAISON_CONTAINER_H
31 #include <Wt/WContainerWidget>
50 return "liaison_internal_publish_socket";
54 return "liaison_internal_subscribe_socket";
62 setStyleClass(
"fill");
75 const Wt::WString&
name() {
return name_.text(); }
85 template <
typename Derived,
typename GobyThread>
91 static std::atomic<int> index(0);
95 auto thread_lambda = [
this, cfg]() {
97 std::lock_guard<std::mutex> l(goby_thread_mutex);
99 std::make_unique<GobyThread>(
static_cast<Derived*
>(
this), cfg, index_);
104 goby_thread_->run(thread_alive_);
108 thread_exception_ = std::current_exception();
112 std::lock_guard<std::mutex> l(goby_thread_mutex);
113 goby_thread_.reset();
117 thread_ = std::unique_ptr<std::thread>(
new std::thread(thread_lambda));
122 comms_timer_.setInterval(1 / cfg.
update_freq() * 1.0e3);
123 comms_timer_.timeout().connect(
125 comms_timer_.start();
130 thread_alive_ =
false;
133 if (thread_exception_)
137 std::rethrow_exception(thread_exception_);
146 std::lock_guard<std::mutex> l(comms_to_wt_mutex);
147 comms_to_wt_queue.push(func);
152 std::lock_guard<std::mutex> l(wt_to_comms_mutex);
153 while (!wt_to_comms_queue.empty())
155 wt_to_comms_queue.front()();
156 wt_to_comms_queue.pop();
163 std::lock_guard<std::mutex> l(goby_thread_mutex);
164 return goby_thread_.get();
169 std::lock_guard<std::mutex> l(wt_to_comms_mutex);
170 wt_to_comms_queue.push(func);
175 std::lock_guard<std::mutex> l(comms_to_wt_mutex);
176 while (!comms_to_wt_queue.empty())
178 comms_to_wt_queue.front()();
179 comms_to_wt_queue.pop();
186 comms_timer_.setInterval(1 / hertz * 1.0e3);
187 comms_timer_.start();
193 std::queue<std::function<
void()>> comms_to_wt_queue;
195 std::queue<std::function<
void()>> wt_to_comms_queue;
199 std::unique_ptr<GobyThread> goby_thread_{
nullptr};
202 std::unique_ptr<std::thread> thread_;
203 std::atomic<bool> thread_alive_{
true};
204 std::exception_ptr thread_exception_;
206 Wt::WTimer comms_timer_;
209 template <
typename WtContainer>
218 container_(container)
226 container_->process_from_wt();
230 WtContainer* container_;