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