SMACC2
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() << "] Creating asynchronous onEntry thread");
30 this->onEntryThread_ = std::async(std::launch::async, [=] {
31 this->onEntry();
32 this->postFinishEventFn_();
33 RCLCPP_INFO_STREAM(
34 getLogger(), "[" << getName() << "] onEntry asynchronous thread was finished.");
35 return 0;
36 });
37}
38
39void SmaccAsyncClientBehavior::waitFutureIfNotFinished(std::future<int> & threadfut)
40{
41 try
42 {
43 rclcpp::Rate r(100);
44 while (rclcpp::ok())
45 {
46 bool valid = threadfut.valid();
47 if (valid)
48 {
49 auto status = threadfut.wait_for(std::chrono::milliseconds(20));
50 if (status == std::future_status::ready)
51 {
52 threadfut.get();
53 break;
54 }
55 }
56
57 //r.sleep();
58 rclcpp::sleep_for(100ms);
59 // rclcpp::spin_some(getNode());
60 RCLCPP_WARN_THROTTLE(
61 getLogger(), *(getNode()->get_clock()), 1000,
62 "[%s] waiting for finishing client behavior, before leaving the state. Is the client "
63 "behavior stuck?",
64 demangleType(typeid(*this)).c_str());
65 }
66 }
67 catch (const std::exception & e)
68 {
69 RCLCPP_DEBUG(
70 getLogger(),
71 "[SmaccAsyncClientBehavior] trying to join function, but it was already finished.");
72 }
73}
74
76{
77 RCLCPP_INFO_STREAM(getLogger(), "[" << getName() << "] onExit - join async onEntry thread");
78
80
81 RCLCPP_INFO_STREAM(
82 getLogger(), "[" << getName() << "] onExit - Creating asynchronous onExit thread");
83 this->onExitThread_ = std::async(std::launch::async, [=] {
84 this->onExit();
85 return 0;
86 });
87}
88
90{
91 RCLCPP_DEBUG_STREAM(
92 getLogger(),
93 "[" << getName()
94 << "] Destroying client behavior- Waiting finishing of asynchronous onExit thread");
95 try
96 {
97 this->onExitThread_.get();
98 }
99 catch (...)
100 {
101 RCLCPP_DEBUG(
102 getLogger(),
103 "[SmaccAsyncClientBehavior] trying to Join onExit function, but it was already finished.");
104 }
105
106 RCLCPP_DEBUG_STREAM(
107 getLogger(),
108 "[" << getName()
109 << "] Destroying client behavior- onExit thread finished. Proccedding destruction.");
110}
111
113
115
117
118} // namespace smacc2
virtual rclcpp::Node::SharedPtr getNode()
void waitFutureIfNotFinished(std::future< int > &threadfut)
std::string demangleType(const std::type_info *tinfo)