SMACC2
Loading...
Searching...
No Matches
smacc_client_async_behavior.cpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
22
23using namespace std::chrono_literals;
24
25namespace smacc2
26{
28{
29 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onEntry thread started");
30 this->onEntryThread_ = std::async(
31 std::launch::async,
32 [=]
33 {
34 this->onEntry();
35 this->postFinishEventFn_();
36 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onEntry thread finished");
37 return 0;
38 });
39}
40
42 std::optional<std::future<int>> & threadfut, bool requestFinish)
43{
44 try
45 {
46 rclcpp::Rate r(100);
47 while (rclcpp::ok())
48 {
49 //bool valid = threadfut.valid();
50 if (threadfut && threadfut->valid())
51 {
52 auto status = threadfut->wait_for(std::chrono::milliseconds(20));
53 if (status == std::future_status::ready)
54 // auto status = threadfut->wait();
55 // if(status)
56 {
57 // done
58 threadfut->get();
59 break;
60 }
61 else
62 {
63 // in progress
65 getLogger(),
66 "[" << getName()
67 << "] fut valid but waiting for asynchronous onEntry thread to finish: state: "
68 << (int)status);
69 }
70 }
71 else
72 {
74 getLogger(),
75 "[" << getName()
76 << "] waiting future onEntryThread. It was not even created. Skipping wait.");
77 break;
78 }
79
80 // r.sleep();
81 rclcpp::sleep_for(100ms);
82 // rclcpp::spin_some(getNode());
84 getLogger(), *(getNode()->get_clock()), 1000,
85 "[%s] waiting for finishing client behavior, before leaving the state. Is the client "
86 "behavior stuck? requesting force finish",
87 this->getName().c_str());
88
90 }
91 }
92 catch (const std::exception & e)
93 {
95 getLogger(),
96 "[SmaccAsyncClientBehavior] trying to join function, but it was already finished.");
97 }
98}
99
104
106{
107 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] onExit - join async onEntry thread");
108 this->waitOnEntryThread(true);
109
111 getLogger(), "[" << getName() << "] onExit - Creating asynchronous onExit thread");
112 this->onExitThread_ = std::async(
113 std::launch::async,
114 [=]
115 {
116 this->onExit();
117 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] asynchronous onExit done.");
118 return 0;
119 });
120}
121
123{
125 getLogger(), "[" << getName()
126 << "] Destroying client behavior- Waiting finishing of asynchronous onExit "
127 "thread");
128 try
129 {
130 if (this->onExitThread_)
131 {
132 // this->onExitThread_->get();
133 this->onExitThread_->get();
134 }
135 //this->onExitThread_->get();
136 }
137 catch (...)
138 {
140 getLogger(),
141 "[SmaccAsyncClientBehavior] trying to Join onExit function, but it was already "
142 "finished.");
143 }
144
146 getLogger(), "[" << getName()
147 << "] Destroying client behavior- onExit thread finished. Proccedding "
148 "destruction.");
149}
150
152
154
156
158{
160 getLogger(), *(getNode()->get_clock()), 1000,
161 "[" << getName() << "] " << ((uint64_t)this) << " requestForceFinish");
163}
164
166{
167 std::string shut = "";
169 {
170 shut = "True";
171 }
172 else
173 {
174 shut = "False";
175 }
176 // RCLCPP_FATAL_STREAM_THROTTLE(
177 // getLogger(), *(getNode()->get_clock()), 1000, "[" << getName() << "] " << ((uint64_t) this ) << " Is requestForceFinish active? " << shut );
179}
180
181} // namespace smacc2
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
std::optional< std::future< int > > onExitThread_
std::optional< std::future< int > > onEntryThread_
void waitFutureIfNotFinished(std::optional< std::future< int > > &threadfut, bool requestFinish)