Goby3  3.1.5a
2024.05.23
poller.h
Go to the documentation of this file.
1 // Copyright 2017-2021:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 // Ryan Govostes <rgovostes+git@gmail.com>
7 //
8 //
9 // This file is part of the Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // The Goby Libraries are distributed in the hope that they will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_MIDDLEWARE_TRANSPORT_POLLER_H
26 #define GOBY_MIDDLEWARE_TRANSPORT_POLLER_H
27 
28 #include "interface.h"
29 
30 namespace goby
31 {
32 namespace middleware
33 {
37 template <typename Transporter> class Poller : public PollerInterface
38 {
39  protected:
42  : // we want the same mutex and cv all the way up
44  inner_poller ? inner_poller->poll_mutex() : std::make_shared<std::timed_mutex>(),
45  inner_poller ? inner_poller->cv() : std::make_shared<std::condition_variable_any>()),
46  inner_poller_(inner_poller)
47  {
48  }
49 
51  PollerInterface* inner_poller() { return inner_poller_; }
52 
53  private:
54  int _transporter_poll(std::unique_ptr<std::unique_lock<std::timed_mutex> >& lock) override
55  {
56  // work from the inside out
57  int inner_poll_items = 0;
58  if (inner_poller_) // recursively call inner poll
59  inner_poll_items +=
60  static_cast<PollerInterface*>(inner_poller_)->_transporter_poll(lock);
61 
62  int poll_items = 0;
63  if (!inner_poll_items)
64  poll_items += static_cast<Transporter*>(this)->_poll(lock);
65 
66  // goby::glog.is(goby::util::logger::DEBUG3) && goby::glog << "Poller::transporter_poll(): " << typeid(*this).name() << " this: " << this << " (" << poll_items << " items) "<< " inner_poller_: " << inner_poller_ << " (" << inner_poll_items << " items) " << std::endl;
67 
68  return inner_poll_items + poll_items;
69  }
70 
71  private:
72  PollerInterface* inner_poller_;
73 };
74 } // namespace goby
75 } // namespace goby
76 
77 #endif
goby::middleware::PollerInterface::PollerInterface
PollerInterface(std::shared_ptr< std::timed_mutex > poll_mutex, std::shared_ptr< std::condition_variable_any > cv)
Definition: interface.h:169
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::Poller::Poller
Poller(PollerInterface *inner_poller=nullptr)
Construct this Poller with a pointer to the inner Poller (unless this is the innermost Poller)
Definition: poller.h:41
goby::middleware::Poller
Utility class for allowing the various Goby middleware transporters to poll the underlying transport ...
Definition: poller.h:37
goby::util::logger_lock::lock
@ lock
Definition: flex_ostreambuf.h:62
goby::middleware::Poller::inner_poller
PollerInterface * inner_poller()
Definition: poller.h:51
goby::middleware::PollerInterface::poll_mutex
std::shared_ptr< std::timed_mutex > poll_mutex()
access the mutex used for poll synchronization
Definition: interface.h:160
goby::middleware::PollerInterface::cv
std::shared_ptr< std::condition_variable_any > cv()
access the condition variable used for poll synchronization
Definition: interface.h:166
interface.h
goby::middleware::PollerInterface
Defines the common interface for polling for data on Goby transporters.
Definition: interface.h:139