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