Goby3  3.1.5a
2024.05.23
tcp_client_interface.h
Go to the documentation of this file.
1 // Copyright 2020-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_DETAIL_TCP_CLIENT_INTERFACE_H
25 #define GOBY_MIDDLEWARE_IO_DETAIL_TCP_CLIENT_INTERFACE_H
26 
27 #include <memory> // for shared_ptr, __sh...
28 #include <string> // for string, to_string
29 
30 #include "goby/middleware/io/detail/io_interface.h" // for PubSubLayer, IOT...
31 #include "goby/middleware/protobuf/io.pb.h" // for IOData, TCPEndPo...
32 #include "goby/middleware/protobuf/tcp_config.pb.h" // for TCPClientConfig
33 #include <boost/asio/ip/tcp.hpp> // for tcp, tcp::endpoint
34 
35 namespace goby
36 {
37 namespace middleware
38 {
39 class Group;
40 }
41 } // namespace goby
42 
43 namespace goby
44 {
45 namespace middleware
46 {
47 namespace io
48 {
49 namespace detail
50 {
51 template <const goby::middleware::Group& line_in_group,
52  const goby::middleware::Group& line_out_group, PubSubLayer publish_layer,
53  PubSubLayer subscribe_layer, typename Config, template <class> class ThreadType,
54  bool use_indexed_groups = false>
56  : public IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer, Config,
57  boost::asio::ip::tcp::socket, ThreadType, use_indexed_groups>
58 {
59  using Base = IOThread<line_in_group, line_out_group, publish_layer, subscribe_layer, Config,
60  boost::asio::ip::tcp::socket, ThreadType, use_indexed_groups>;
61 
62  using ConfigType = Config;
63 
64  public:
67  TCPClientThread(const Config& config, int index = -1)
68  : Base(config, index,
69  std::string("tcp: ") + config.remote_address() + ":" +
70  std::to_string(config.remote_port()))
71  {
72  boost::asio::ip::tcp::resolver resolver(this->mutable_io());
73  remote_endpoint_ = *resolver.resolve(
74  {this->cfg().remote_address(), std::to_string(this->cfg().remote_port()),
75  boost::asio::ip::resolver_query_base::numeric_service});
76 
78  this->interthread().template publish<line_in_group>(ready);
79  }
80 
81  ~TCPClientThread() override
82  {
83  auto event = std::make_shared<goby::middleware::protobuf::TCPClientEvent>();
84  if (this->index() != -1)
85  event->set_index(this->index());
87  *event->mutable_local_endpoint() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
88  *event->mutable_remote_endpoint() =
89  endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
91  << "Event: " << event->ShortDebugString() << std::endl;
92  this->publish_in(event);
93  }
94 
95  protected:
96  void insert_endpoints(std::shared_ptr<goby::middleware::protobuf::IOData>& io_msg)
97  {
98  *io_msg->mutable_tcp_src() = endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
99  *io_msg->mutable_tcp_dest() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
100  }
101 
102  private:
103  void async_write(std::shared_ptr<const goby::middleware::protobuf::IOData> io_msg) override
104  {
105  basic_async_write(this, io_msg);
106  }
107 
109  void open_socket() override
110  {
111  this->mutable_socket().connect(remote_endpoint_);
112 
113  auto event = std::make_shared<goby::middleware::protobuf::TCPClientEvent>();
114  if (this->index() != -1)
115  event->set_index(this->index());
117  *event->mutable_local_endpoint() = endpoint_convert<protobuf::TCPEndPoint>(local_endpoint_);
118  *event->mutable_remote_endpoint() =
119  endpoint_convert<protobuf::TCPEndPoint>(remote_endpoint_);
121  << "Event: " << event->ShortDebugString() << std::endl;
122  this->publish_in(event);
123 
124  local_endpoint_ = this->mutable_socket().local_endpoint();
125  }
126 
127  private:
128  boost::asio::ip::tcp::endpoint remote_endpoint_;
129  boost::asio::ip::tcp::endpoint local_endpoint_;
130 };
131 
132 } // namespace detail
133 } // namespace io
134 } // namespace middleware
135 } // namespace goby
136 
137 #endif
io.pb.h
goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, Config, boost::asio::ip::tcp::socket, ThreadType, false >::mutable_socket
boost::asio::ip::tcp::socket & mutable_socket()
Access the (mutable) socket (or serial_port) object.
Definition: io_interface.h:212
goby::middleware::io::detail::TCPClientThread::insert_endpoints
void insert_endpoints(std::shared_ptr< goby::middleware::protobuf::IOData > &io_msg)
Definition: tcp_client_interface.h:96
goby::middleware::protobuf::TCPClientEvent::EVENT_DISCONNECT
static const Event EVENT_DISCONNECT
Definition: io.pb.h:1282
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, Config, boost::asio::ip::tcp::socket, ThreadType, false >::mutable_io
boost::asio::io_context & mutable_io()
Definition: io_interface.h:220
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
detail
detail namespace with internal helper functions
Definition: json.hpp:246
group
goby::util::logger::GroupSetter group(std::string n)
Definition: logger_manipulators.h:134
goby::util::FlexOstream::is_debug2
bool is_debug2()
Definition: flex_ostream.h:85
goby::middleware::io::detail::TCPClientThread::TCPClientThread
TCPClientThread(const Config &config, int index=-1)
Constructs the thread.
Definition: tcp_client_interface.h:67
tcp_config.pb.h
goby::middleware::io::detail::TCPClientThread
Definition: tcp_client_interface.h:55
goby::middleware::protobuf::TCPClientEvent::EVENT_CONNECT
static const Event EVENT_CONNECT
Definition: io.pb.h:1280
goby::middleware::io::PubSubLayer
PubSubLayer
Definition: io_transporters.h:38
to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:24301
goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, Config, boost::asio::ip::tcp::socket, ThreadType, false >::glog_group
const std::string & glog_group()
Definition: io_interface.h:234
goby::middleware::io::detail::IOThread< line_in_group, line_out_group, publish_layer, subscribe_layer, Config, boost::asio::ip::tcp::socket, ThreadType, false >::basic_async_write
friend void basic_async_write(IOThreadImplementation *this_thread, std::shared_ptr< const goby::middleware::protobuf::IOData > io_msg)
Definition: io_interface.h:261
goby::middleware::Group
Class for grouping publications in the Goby middleware. Analogous to "topics" in ROS,...
Definition: group.h:59
goby::middleware::io::detail::TCPClientThread::~TCPClientThread
~TCPClientThread() override
Definition: tcp_client_interface.h:81
io_interface.h
goby::middleware::io::detail::IOThread
Definition: io_interface.h:79
goby::middleware::protobuf::IOData::mutable_tcp_dest
::goby::middleware::protobuf::TCPEndPoint * mutable_tcp_dest()
Definition: io.pb.h:2078
goby::middleware::io::ThreadState::SUBSCRIPTIONS_COMPLETE
@ SUBSCRIPTIONS_COMPLETE
goby::glog
util::FlexOstream glog
Access the Goby logger through this object.
goby::middleware::to_string
std::string to_string(goby::middleware::protobuf::Layer layer)
Definition: common.h:44
goby::middleware::protobuf::IOData::mutable_tcp_src
::goby::middleware::protobuf::TCPEndPoint * mutable_tcp_src()
Definition: io.pb.h:1990