Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
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
31
35
36namespace goby
37{
38namespace middleware
39{
40namespace terminate
41{
47inline std::pair<bool, protobuf::TerminateResponse>
48check_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
75template <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;
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
const std::string & target_name() const
void set_target_name(ArgT0 &&arg0, ArgT... args)
void subscribe_terminate(bool do_quit=true)
Definition terminate.h:78
constexpr goby::middleware::Group terminate_request
Definition groups.h:36
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
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.