Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
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
40#include "goby/util/protobuf/linebasedcomms.pb.h" // for Datagram
41
42namespace boost
43{
44namespace system
45{
46class error_code;
47} // namespace system
48} // namespace boost
49
50namespace goby
51{
52namespace util
53{
54namespace groups
55{
56constexpr goby::middleware::Group linebasedcomms_in{"goby::util::LineBasedInterface::in"};
57constexpr goby::middleware::Group linebasedcomms_out{"goby::util::LineBasedInterface::out"};
58} // namespace groups
59
62{
63 public:
64 LineBasedInterface(const std::string& delimiter);
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
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
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
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
Implementation of Group for dynamic (run-time) instantiations. Use Group directly for static (compile...
Definition group.h:120
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition group.h:60
A transporter for the interthread layer.
Definition interthread.h:59
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
basic interface class for all the derived serial (and networking mimics) line-based nodes (serial,...
Definition interface.h:62
std::string delimiter() const
Definition interface.h:107
goby::middleware::InterThreadTransporter & interthread()
Definition interface.h:121
virtual std::string remote_endpoint()
Definition interface.h:114
void write(const protobuf::Datagram &msg)
goby::middleware::DynamicGroup & in_group()
Definition interface.h:127
std::deque< goby::util::protobuf::Datagram > & in()
Definition interface.h:119
bool readline(protobuf::Datagram *msg, AccessOrder order=OLDEST_FIRST)
void set_active(bool active)
Definition interface.h:116
void write(const std::string &s)
Definition interface.h:94
goby::middleware::DynamicGroup & out_group()
Definition interface.h:128
void set_delimiter(const std::string &s)
Definition interface.h:106
bool readline(std::string *s, AccessOrder order=OLDEST_FIRST)
returns string line (including delimiter)
virtual std::string local_endpoint()=0
LineBasedInterface(const std::string &delimiter)
void set_data(ArgT0 &&arg0, ArgT... args)
constexpr goby::middleware::Group linebasedcomms_in
Definition interface.h:56
constexpr goby::middleware::Group linebasedcomms_out
Definition interface.h:57
boost::units::make_system< boost::units::angle::revolution_base_unit, boost::units::metric::minute_base_unit >::type system
Definition system.hpp:45
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg