Goby3  3.1.5a
2024.05.23
pty.h
Go to the documentation of this file.
1 // Copyright 2019-2023:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the Goby Underwater Autonomy Project Libraries
9 // ("The Goby Libraries").
10 //
11 // The Goby Libraries are free software: you can redistribute them and/or modify
12 // them under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // The Goby Libraries are distributed in the hope that they will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef GOBY_MIDDLEWARE_IO_LINE_BASED_PTY_H
25 #define GOBY_MIDDLEWARE_IO_LINE_BASED_PTY_H
26 
27 #include <istream> // for istream, basic_...
28 #include <string> // for string
29 
30 #include <boost/asio/read_until.hpp> // for async_read_until
31 #include <boost/asio/streambuf.hpp> // for streambuf
32 #include <boost/system/error_code.hpp> // for error_code
33 
34 #include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer
35 #include "goby/middleware/io/detail/pty_interface.h" // for PTYThread
36 #include "goby/middleware/io/line_based/common.h" // for match_regex
37 
38 namespace goby
39 {
40 namespace middleware
41 {
42 class Group;
43 }
44 } // namespace goby
45 namespace goby
46 {
47 namespace middleware
48 {
49 namespace protobuf
50 {
51 class PTYConfig;
52 }
53 } // namespace middleware
54 } // namespace goby
55 
56 namespace goby
57 {
58 namespace middleware
59 {
60 namespace io
61 {
65 template <const goby::middleware::Group& line_in_group,
66  const goby::middleware::Group& line_out_group,
67  PubSubLayer publish_layer = PubSubLayer::INTERPROCESS,
68  PubSubLayer subscribe_layer = PubSubLayer::INTERTHREAD,
69  template <class> class ThreadType = goby::middleware::SimpleThread,
70  bool use_indexed_groups = false>
71 class PTYThreadLineBased : public detail::PTYThread<line_in_group, line_out_group, publish_layer,
72  subscribe_layer, ThreadType, use_indexed_groups>
73 {
74  using Base = detail::PTYThread<line_in_group, line_out_group, publish_layer, subscribe_layer,
75  ThreadType, use_indexed_groups>;
76 
77  public:
82  : Base(config, index), eol_matcher_(this->cfg().end_of_line())
83  {
84  }
85 
87 
88  private:
90  void async_read() override;
91 
92  private:
93  match_regex eol_matcher_;
94  boost::asio::streambuf buffer_;
95 };
96 } // namespace io
97 } // namespace middleware
98 } // namespace goby
99 
100 template <const goby::middleware::Group& line_in_group,
101  const goby::middleware::Group& line_out_group,
102  goby::middleware::io::PubSubLayer publish_layer,
103  goby::middleware::io::PubSubLayer subscribe_layer, template <class> class ThreadType,
104  bool use_indexed_groups>
105 void goby::middleware::io::PTYThreadLineBased<line_in_group, line_out_group, publish_layer,
106  subscribe_layer, ThreadType,
107  use_indexed_groups>::async_read()
108 {
109  boost::asio::async_read_until(
110  this->mutable_socket(), buffer_, eol_matcher_,
111  [this](const boost::system::error_code& ec, std::size_t bytes_transferred)
112  {
113  if (!ec && bytes_transferred > 0)
114  {
115  std::string bytes(bytes_transferred, 0);
116  std::istream is(&buffer_);
117  is.read(&bytes[0], bytes_transferred);
118  this->handle_read_success(bytes_transferred, bytes);
119  this->async_read();
120  }
121  else
122  {
123  this->handle_read_error(ec);
124  }
125  });
126 }
127 
128 #endif
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::Thread< goby::middleware::protobuf::PTYConfig, InterVehicleForwarder< InterProcessForwarder< InterThreadTransporter > > >::cfg
const goby::middleware::protobuf::PTYConfig & cfg() const
Definition: thread.h:201
goby::acomms::abc::protobuf::config
extern ::google::protobuf::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::google::protobuf::internal::MessageTypeTraits< ::goby::acomms::abc::protobuf::Config >, 11, false > config
Definition: abc_driver.pb.h:203
goby::middleware::Thread< goby::middleware::protobuf::PTYConfig, InterVehicleForwarder< InterProcessForwarder< InterThreadTransporter > > >::index
int index() const
Definition: thread.h:145
pty_interface.h
goby::middleware::io::PTYThreadLineBased::PTYThreadLineBased
PTYThreadLineBased(const goby::middleware::protobuf::PTYConfig &config, int index=-1)
Constructs the thread.
Definition: pty.h:81
goby::middleware::SimpleThread
Implements Thread for a three layer middleware setup ([ intervehicle [ interprocess [ interthread ] ]...
Definition: simple_thread.h:43
goby::middleware::io::PubSubLayer
PubSubLayer
Definition: io_transporters.h:38
goby::middleware::io::PTYThreadLineBased
Reads/Writes strings from/to serial port using a line-based (typically ASCII) protocol with a defined...
Definition: pty.h:71
goby::middleware::protobuf::PTYConfig
Definition: pty_config.pb.h:74
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
goby::middleware::io::PTYThreadLineBased::~PTYThreadLineBased
~PTYThreadLineBased()
Definition: pty.h:86
goby::middleware::io::detail::PTYThread
Definition: pty_interface.h:78
io_interface.h
goby::middleware::io::PubSubLayer::INTERPROCESS
@ INTERPROCESS
common.h
goby::middleware::io::match_regex
Provides a matching function object for the boost::asio::async_read_until based on a std::regex.
Definition: common.h:52
goby::middleware::io::PubSubLayer::INTERTHREAD
@ INTERTHREAD