YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Terminator.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
8
9#include <yarp/os/Network.h>
10#include <yarp/os/Port.h>
13
14
15using namespace yarp::os::impl;
16using namespace yarp::os;
17
18namespace {
19YARP_OS_LOG_COMPONENT(TERMINATOR, "yarp.os.Terminator")
20
21/*
22 * Server side of the TerminatorMsgs thrift service.
23 */
24class TerminatorServer :
26{
27public:
28 explicit TerminatorServer(volatile bool& quitFlag) :
29 m_quit(quitFlag)
30 {
31 }
32
33 bool quit() override
34 {
35 m_quit = true;
36 return true;
37 }
38
39private:
40 volatile bool& m_quit;
41};
42
43} // namespace
44
45
46bool Terminator::terminateByName(const char* name)
47{
48 if (name == nullptr) {
49 return false;
50 }
51
52 std::string s(name);
53
54 if (name[0] != '/') {
55 s.clear();
56 s += "/";
57 s += name;
58 }
59
61 if (!c.isValid()) {
62 yCError(TERMINATOR, "Terminator port not found");
63 return false;
64 }
65 c.setCarrier("tcp");
66
67 // Client side of the TerminatorMsgs thrift service: the commands are
68 // sent through a (not registered) port connected to the Terminee port.
69 Port port;
70 if (!port.openFake("terminator")) {
71 yCError(TERMINATOR, "Cannot open the terminator client port");
72 return false;
73 }
74 if (!port.addOutput(c)) {
75 yCError(TERMINATOR, "Cannot connect to %s", s.c_str());
76 return false;
77 }
78 TerminatorMsgs client;
79 client.yarp().attachAsClient(port);
80 if (!client.quit()) {
81 yCError(TERMINATOR, "The quit request has not been accepted by %s", s.c_str());
82 return false;
83 }
84
85 return true;
86}
87
88#define TermineeHelper Port
89#define HELPER(x) (*((TermineeHelper*)(x)))
90
91Terminee::Terminee(const char* name)
92{
93 ok = false;
94 implementation = nullptr;
95 if (name == nullptr) {
96 quit = true;
97 yCError(TERMINATOR, "Terminator: Please supply a proper port name");
98 return;
99 }
100
101 std::string s(name);
102 if (name[0] != '/') {
103 s.clear();
104 s += "/";
105 s += name;
106 }
107
111 ok = helper.open(s);
112 if (!ok) {
113 quit = true;
114 yCError(TERMINATOR, "Kill port conflict: make sure you supply a distinct --name /PORTNAME");
115 } else {
116 quit = false;
117 start();
118 }
119}
120
122{
124 quit = true;
125 helper.interrupt();
126}
127
129{
131 if (!quit) {
132 Terminator::terminateByName(helper.getName().c_str());
133 }
134
135 stop();
136
137 if (implementation != nullptr) {
138 delete &HELPER(implementation);
139 }
140}
141
142
144{
146 TerminatorServer server(quit);
147 while (!isStopping() && !quit) {
148 // The thrift service reads the command and writes the reply
149 // (including the errors, e.g. for unknown commands).
150 helper.read(server);
151 }
152}
153
155{
156 // not yet implemented
157 return false;
158}
159
161{
162 return quit;
163}
164
165
166bool Terminee::isOk() const
167{
168 return ok;
169}
#define HELPER(x)
#define TermineeHelper
classes to handle graceful process termination.
A mini-server for performing network communication in the background.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
Represents how to reach a part of a YARP network.
Definition Contact.h:33
static Contact queryName(const std::string &name)
Find out information about a registered name.
Definition Network.cpp:1083
A mini-server for network communication.
Definition Port.h:46
bool addOutput(const std::string &name) override
Add an output connection to the specified port.
Definition Port.cpp:320
bool openFake(const std::string &name)
Start port without making it accessible from the network.
Definition Port.cpp:74
Commands accepted by the port opened by yarp::os::Terminee, and sent by yarp::os::Terminator::termina...
virtual bool quit()
Ask the process which owns the port to start a graceful quit procedure.
static bool terminateByName(const char *name)
Send a quit message to a specific socket port.
void run() override
Main body of the new thread.
volatile bool ok
Definition Terminator.h:51
Terminee(const char *name)
Constructor.
volatile bool quit
Definition Terminator.h:50
virtual ~Terminee()
Destructor.
bool isOk() const
Check whether the message mechanism is ok.
bool mustQuit() const
Call this method to see whether a quit message has been received.
void onStop() override
Call-back, called while halting the thread (before join).
bool waitQuit() const
Call this method to wait for a quit message.
bool stop()
Stop the thread.
Definition Thread.cpp:81
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition Thread.cpp:99
bool start()
Start the new thread running.
Definition Thread.cpp:93
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
#define yCError(component,...)
#define yCAssert(component, x)
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.
Definition dirs.h:16