YARP
Yet Another Robot Platform
Terminator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include <yarp/os/Network.h>
11 #include <yarp/os/Port.h>
12 #include <yarp/os/Terminator.h>
13 #include <yarp/os/Vocab.h>
17 
18 #include <cstdio>
19 
20 
21 using namespace yarp::os::impl;
22 using namespace yarp::os;
23 
24 namespace {
25 YARP_OS_LOG_COMPONENT(TERMINATOR, "yarp.os.Terminator")
26 }
27 
28 
29 bool Terminator::terminateByName(const char* name)
30 {
31  if (name == nullptr) {
32  return false;
33  }
34 
35  std::string s(name);
36 
37  if (s.find("/quit") == std::string::npos) {
38  // name doesn't include /quit
39  // old mechanism won't work, let's try new
40  PortCommand pc('\0', "i");
41  NetworkBase::sendMessage(s, pc, true);
42  return true;
43  }
44 
45  if (name[0] != '/') {
46  s.clear();
47  s += "/";
48  s += name;
49  }
50 
51  Bottle cmd("quit");
52  Bottle reply;
54  if (!c.isValid()) {
55  yCError(TERMINATOR, "Terminator port not found");
56  return false;
57  }
58  ContactStyle style;
59  style.quiet = false;
60  style.carrier = "text_ack";
61  style.expectReply = true;
62  NetworkBase::write(c, cmd, reply, style);
63 
64  return true;
65 }
66 
67 #define TermineeHelper Port
68 #define HELPER(x) (*((TermineeHelper*)(x)))
69 
70 Terminee::Terminee(const char* name)
71 {
72  ok = false;
73  implementation = nullptr;
74  if (name == nullptr) {
75  quit = true;
76  yCError(TERMINATOR, "Terminator: Please supply a proper port name");
77  return;
78  }
79 
80  std::string s(name);
81  if (name[0] != '/') {
82  s.clear();
83  s += "/";
84  s += name;
85  }
86 
88  yCAssert(TERMINATOR, implementation != nullptr);
90  ok = helper.open(s);
91  if (!ok) {
92  quit = true;
93  yCError(TERMINATOR, "Kill port conflict: make sure you supply a distinct --name /PORTNAME");
94  } else {
95  quit = false;
96  start();
97  }
98 }
99 
101 {
103  quit = true;
104  helper.interrupt();
105 }
106 
108 {
110  if (!quit) {
111  Terminator::terminateByName(helper.getName().c_str());
112  }
113 
114  stop();
115 
116  if (implementation != nullptr) {
117  delete &HELPER(implementation);
118  }
119 }
120 
121 
123 {
125  while (!isStopping() && !quit) {
126  Bottle cmd;
127  Bottle reply;
128  bool ok = helper.read(cmd, true);
129  if (!ok) {
130  continue;
131  }
132  if (cmd.get(0).asString() == "quit") {
133  quit = true;
134  reply.addVocab(yarp::os::createVocab('o', 'k'));
135  } else {
136  reply.addVocab(yarp::os::createVocab('h', 'u', 'h', '?'));
137  }
138  helper.reply(reply);
139  }
140 }
141 
142 bool Terminee::waitQuit() const
143 {
144  // not yet implemented
145  return false;
146 }
147 
148 bool Terminee::mustQuit() const
149 {
150  return quit;
151 }
152 
153 
154 bool Terminee::isOk() const
155 {
156  return ok;
157 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
TermineeHelper
#define TermineeHelper
Definition: Terminator.cpp:67
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
Network.h
yarp::os::ContactStyle
Preferences for how to communicate with a contact.
Definition: ContactStyle.h:27
Port.h
yarp::os::NetworkBase::write
static bool write(const Contact &contact, PortWriter &cmd, PortReader &reply, bool admin=false, bool quiet=false, double timeout=-1)
Send a single command to a port and await a single response.
Definition: Network.cpp:1229
yarp::os::Terminee::~Terminee
virtual ~Terminee()
Destructor.
Definition: Terminator.cpp:107
SocketTwoWayStream.h
HELPER
#define HELPER(x)
Definition: Terminator.cpp:68
yarp::os::impl::PortCommand
Simple Readable and Writable object representing a command to a YARP port.
Definition: PortCommand.h:29
LogComponent.h
yarp::os::Terminee::onStop
void onStop() override
Call-back, called while halting the thread (before join).
Definition: Terminator.cpp:100
yarp::os::Terminee::waitQuit
bool waitQuit() const
Call this method to wait for a quit message.
Definition: Terminator.cpp:142
yarp::os::Terminee::run
void run() override
Main body of the new thread.
Definition: Terminator.cpp:122
yarp::os::ContactStyle::expectReply
bool expectReply
Specify whether you expect a reply to a message.
Definition: ContactStyle.h:61
yarp::os::NetworkBase::queryName
static Contact queryName(const std::string &name)
Find out information about a registered name.
Definition: Network.cpp:998
yarp::os::Terminee::isOk
bool isOk() const
Check whether the message mechanism is ok.
Definition: Terminator.cpp:154
PortCommand.h
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::os::Terminee::mustQuit
bool mustQuit() const
Call this method to see whether a quit message has been received.
Definition: Terminator.cpp:148
yarp::os::ContactStyle::quiet
bool quiet
Suppress all outputs and warnings.
Definition: ContactStyle.h:39
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::Bottle::addVocab
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
Terminator.h
classes to handle graceful process termination.
yarp::os::Terminator::terminateByName
static bool terminateByName(const char *name)
Send a quit message to a specific socket port.
Definition: Terminator.cpp:29
yCAssert
#define yCAssert(component, x)
Definition: LogComponent.h:172
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
yarp::os::ContactStyle::carrier
std::string carrier
Request that communication be made using a particular carrier.
Definition: ContactStyle.h:56
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::Contact::isValid
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:301
yarp::os::Terminee::Terminee
Terminee(const char *name)
Constructor.
Definition: Terminator.cpp:70
Vocab.h
yarp::os::Bottle::read
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Definition: Bottle.cpp:243
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
implementation
RandScalar * implementation(void *t)
Definition: RandnScalar.cpp:20
yarp::os::NetworkBase::sendMessage
static int sendMessage(const std::string &port, yarp::os::PortWriter &writable, bool silent=false)
Just a reminder to sendMessage with temporary output parameter that will be discarded.
Definition: Network.cpp:1473
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
yarp::os::impl
The components from which ports and connections are built.