Goby3  3.1.5a
2024.05.23
terminate.h
Go to the documentation of this file.
1 // Copyright 2018-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_TERMINATE_TERMINATE_H
25 #define GOBY_MIDDLEWARE_TERMINATE_TERMINATE_H
26 
27 #include <sys/types.h>
28 #include <unistd.h>
29 
30 #include "goby/util/debug_logger.h"
31 
35 
36 namespace goby
37 {
38 namespace middleware
39 {
40 namespace terminate
41 {
47 inline std::pair<bool, protobuf::TerminateResponse>
48 check_terminate(const protobuf::TerminateRequest& request, const std::string& app_name)
49 {
51  resp.set_target_name(app_name);
52  unsigned pid = getpid();
53  resp.set_target_pid(pid);
54 
55  bool match = false;
56 
57  if (request.has_target_name() && request.target_name() == app_name)
58  {
61  << "Received request matching our app name to cleanly quit() from goby_terminate"
62  << std::endl;
63  match = true;
64  }
65  else if (request.has_target_pid() && request.target_pid() == pid)
66  {
68  goby::glog << "Received request matching our PID to cleanly quit() from goby_terminate"
69  << std::endl;
70  match = true;
71  }
72  return std::make_pair(match, resp);
73 }
74 
75 template <typename Derived> class Application
76 {
77  protected:
78  void subscribe_terminate(bool do_quit = true)
79  {
80  // handle goby_terminate request
81  static_cast<Derived*>(this)
82  ->interprocess()
85  [this, do_quit](const goby::middleware::protobuf::TerminateRequest& request)
86  {
87  bool match = false;
89  std::tie(match, resp) = goby::middleware::terminate::check_terminate(
90  request, static_cast<Derived*>(this)->app_cfg().app().name());
91  if (match)
92  {
93  static_cast<Derived*>(this)
94  ->interprocess()
95  .template publish<goby::middleware::groups::terminate_response>(resp);
96  if (do_quit)
97  static_cast<Derived*>(this)->quit();
98  }
99  });
100  }
101 };
102 
103 } // namespace terminate
104 } // namespace middleware
105 } // namespace goby
106 
107 #endif
goby::middleware::groups::terminate_request
constexpr goby::middleware::Group terminate_request
Definition: groups.h:36
terminate.pb.h
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
protobuf.h
goby::middleware::protobuf::TerminateResponse
Definition: terminate.pb.h:234
goby::util::FlexOstream::is_debug2
bool is_debug2()
Definition: flex_ostream.h:85
goby::middleware::protobuf::TerminateResponse::set_target_name
void set_target_name(const ::std::string &value)
Definition: terminate.pb.h:659
goby::middleware::protobuf::TerminateRequest
Definition: terminate.pb.h:102
goby::middleware::protobuf::TerminateRequest::has_target_name
bool has_target_name() const
Definition: terminate.pb.h:548
goby::middleware::terminate::check_terminate
std::pair< bool, protobuf::TerminateResponse > check_terminate(const protobuf::TerminateRequest &request, const std::string &app_name)
Checks if the terminate request is for this application, either by target_name or PID.
Definition: terminate.h:48
groups.h
goby::middleware::protobuf::TerminateRequest::target_pid
::google::protobuf::uint32 target_pid() const
Definition: terminate.pb.h:627
goby::middleware::protobuf::TerminateRequest::has_target_pid
bool has_target_pid() const
Definition: terminate.pb.h:614
debug_logger.h
goby::middleware::terminate::Application::subscribe_terminate
void subscribe_terminate(bool do_quit=true)
Definition: terminate.h:78
goby::middleware::terminate::Application
Definition: terminate.h:75
goby::middleware::protobuf::TerminateRequest::target_name
const ::std::string & target_name() const
Definition: terminate.pb.h:561
goby::middleware::protobuf::TerminateResponse::set_target_pid
void set_target_pid(::google::protobuf::uint32 value)
Definition: terminate.pb.h:725
goby::glog
util::FlexOstream glog
Access the Goby logger through this object.