Goby3  3.1.5a
2024.05.23
interface.h
Go to the documentation of this file.
1 // Copyright 2010-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
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_UTIL_LINEBASEDCOMMS_INTERFACE_H
26 #define GOBY_UTIL_LINEBASEDCOMMS_INTERFACE_H
27 
28 #include <deque> // for deque
29 #include <memory> // for shared_ptr
30 #include <mutex> // for mutex
31 #include <string> // for string
32 #include <thread> // for thread
33 
34 #include <boost/bind/bind.hpp> // for bind_t, list_av_1<...
35 
36 #include "goby/middleware/group.h"
38 #include "goby/util/asio_compat.h"
40 #include "goby/util/protobuf/linebasedcomms.pb.h" // for Datagram
41 
42 namespace boost
43 {
44 namespace system
45 {
46 class error_code;
47 } // namespace system
48 } // namespace boost
49 
50 namespace goby
51 {
52 namespace util
53 {
54 namespace groups
55 {
56 constexpr goby::middleware::Group linebasedcomms_in{"goby::util::LineBasedInterface::in"};
57 constexpr goby::middleware::Group linebasedcomms_out{"goby::util::LineBasedInterface::out"};
58 } // namespace groups
59 
62 {
63  public:
64  LineBasedInterface(const std::string& delimiter);
65  virtual ~LineBasedInterface();
66 
67  // start the connection
68  void start();
69  // close the connection cleanly
70  void close();
71  // is the connection alive and well?
72  bool active()
73  {
74  // ensure we've received any status messages first
75  interthread_.poll(std::chrono::seconds(0));
76  return active_;
77  }
78 
79  void sleep(int sec);
80 
82  {
85  };
86 
90  bool readline(std::string* s, AccessOrder order = OLDEST_FIRST);
92 
93  // write a line to the buffer
94  void write(const std::string& s)
95  {
96  protobuf::Datagram datagram;
97  datagram.set_data(s);
98  write(datagram);
99  }
100 
101  void write(const protobuf::Datagram& msg);
102 
103  // empties the read buffer
104  void clear();
105 
106  void set_delimiter(const std::string& s) { delimiter_ = s; }
107  std::string delimiter() const { return delimiter_; }
108 
109  protected:
110  virtual void do_start() = 0;
111  virtual void do_close() = 0;
112 
113  virtual std::string local_endpoint() = 0;
114  virtual std::string remote_endpoint() { return ""; };
115 
116  void set_active(bool active) { active_ = active; }
117 
118  std::string& delimiter() { return delimiter_; }
119  std::deque<goby::util::protobuf::Datagram>& in() { return in_; }
120 
122 
123  int index() { return index_; }
124 
125  virtual void do_subscribe() = 0;
126 
127  goby::middleware::DynamicGroup& in_group() { return in_group_; }
128  goby::middleware::DynamicGroup& out_group() { return out_group_; }
129 
130  bool io_thread_ready() { return io_thread_ready_; }
131 
132  private:
133  void subscribe();
134  void poll();
135 
136  private:
137  std::string delimiter_;
138  std::deque<protobuf::Datagram> in_; // buffered read data
139 
140  bool active_; // remains true while this object is still operating
141 
142  int index_;
143  static std::atomic<int> count_;
144 
145  // DynamicGroups must outlive interthread_ since they hold the std::string used by the underlying Group
148 
150 
151  bool io_thread_ready_{false};
152 
153  std::thread::id current_thread_;
154 };
155 
156 } // namespace util
157 } // namespace goby
158 
159 #endif
goby::util::LineBasedInterface::LineBasedInterface
LineBasedInterface(const std::string &delimiter)
goby::util::units::rpm::system
boost::units::make_system< boost::units::angle::revolution_base_unit, boost::units::metric::minute_base_unit >::type system
Definition: system.hpp:45
goby::util::LineBasedInterface::remote_endpoint
virtual std::string remote_endpoint()
Definition: interface.h:114
goby::util::LineBasedInterface::clear
void clear()
goby::util::LineBasedInterface::NEWEST_FIRST
@ NEWEST_FIRST
Definition: interface.h:83
goby::util::LineBasedInterface::AccessOrder
AccessOrder
Definition: interface.h:81
goby::util::LineBasedInterface::in
std::deque< goby::util::protobuf::Datagram > & in()
Definition: interface.h:119
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::util::LineBasedInterface::~LineBasedInterface
virtual ~LineBasedInterface()
goby::util::protobuf::Datagram::set_data
void set_data(const ::std::string &value)
Definition: linebasedcomms.pb.h:268
group.h
goby::util::LineBasedInterface::start
void start()
goby::util::protobuf::Datagram
Definition: linebasedcomms.pb.h:73
boost
Definition: udp_driver.h:41
goby::util::LineBasedInterface::out_group
goby::middleware::DynamicGroup & out_group()
Definition: interface.h:128
goby::middleware::InterThreadTransporter
A transporter for the interthread layer.
Definition: interthread.h:56
goby::util::LineBasedInterface::delimiter
std::string & delimiter()
Definition: interface.h:118
goby::util::groups::linebasedcomms_in
constexpr goby::middleware::Group linebasedcomms_in
Definition: interface.h:56
goby::util::LineBasedInterface::do_start
virtual void do_start()=0
goby::util::LineBasedInterface::interthread
goby::middleware::InterThreadTransporter & interthread()
Definition: interface.h:121
goby::util::LineBasedInterface::readline
bool readline(std::string *s, AccessOrder order=OLDEST_FIRST)
returns string line (including delimiter)
goby::util::LineBasedInterface::local_endpoint
virtual std::string local_endpoint()=0
linebasedcomms.pb.h
goby::middleware::PollerInterface::poll
int poll(const std::chrono::time_point< Clock, Duration > &timeout=std::chrono::time_point< Clock, Duration >::max())
poll for data. Blocks until a data event occurs or a timeout when a particular time has been reached
Definition: interface.h:345
interthread.h
thread_stub.h
goby::util::LineBasedInterface::index
int index()
Definition: interface.h:123
goby::util::LineBasedInterface::close
void close()
goby::util::LineBasedInterface::in_group
goby::middleware::DynamicGroup & in_group()
Definition: interface.h:127
goby::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:1327
goby::util::LineBasedInterface::io_thread_ready
bool io_thread_ready()
Definition: interface.h:130
asio_compat.h
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
goby::util::LineBasedInterface::delimiter
std::string delimiter() const
Definition: interface.h:107
goby::util::LineBasedInterface::write
void write(const std::string &s)
Definition: interface.h:94
goby::util::LineBasedInterface::OLDEST_FIRST
@ OLDEST_FIRST
Definition: interface.h:84
goby::util::groups::linebasedcomms_out
constexpr goby::middleware::Group linebasedcomms_out
Definition: interface.h:57
goby::util::LineBasedInterface::set_delimiter
void set_delimiter(const std::string &s)
Definition: interface.h:106
goby::util::LineBasedInterface::sleep
void sleep(int sec)
goby::util::LineBasedInterface
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial,...
Definition: interface.h:61
goby::util::LineBasedInterface::do_close
virtual void do_close()=0
goby::util::LineBasedInterface::active
bool active()
Definition: interface.h:72
goby::middleware::DynamicGroup
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition: group.h:119
goby::util::LineBasedInterface::set_active
void set_active(bool active)
Definition: interface.h:116
goby::util::LineBasedInterface::do_subscribe
virtual void do_subscribe()=0