realtime_server_goal_handle.h
Go to the documentation of this file.
1 // Copyright (c) 2008, Willow Garage, Inc.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of hiDOF, Inc. nor the names of its
12 // contributors may be used to endorse or promote products derived from
13 // this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
27 
29 
30 #ifndef REALTIME_TOOLS__REALTIME_SERVER_GOAL_HANDLE_H
31 #define REALTIME_TOOLS__REALTIME_SERVER_GOAL_HANDLE_H
32 
33 // Standard
34 #include <inttypes.h>
35 
36 // Boost
37 #include <boost/shared_ptr.hpp>
38 
39 // Actionlib
40 #include <actionlib/server/action_server.h>
41 
42 namespace realtime_tools
43 {
44 
45 template <class Action>
47 {
48 private:
49  ACTION_DEFINITION(Action);
50 
51  typedef actionlib::ServerGoalHandle<Action> GoalHandle;
54 
55  uint8_t state_;
56 
57  bool req_abort_;
60  ResultConstPtr req_result_;
61  FeedbackConstPtr req_feedback_;
62 
63 public:
64  GoalHandle gh_;
65  ResultPtr preallocated_result_; // Preallocated so it can be used in realtime
66  FeedbackPtr preallocated_feedback_; // Preallocated so it can be used in realtime
67 
68  RealtimeServerGoalHandle(GoalHandle &gh, const ResultPtr &preallocated_result = ResultPtr((Result*)NULL), const FeedbackPtr &preallocated_feedback = FeedbackPtr((Feedback*)NULL))
69  : req_abort_(false),
70  req_cancel_(false),
71  req_succeed_(false),
72  gh_(gh),
73  preallocated_result_(preallocated_result),
74  preallocated_feedback_(preallocated_feedback)
75  {
76  if (!preallocated_result_)
77  preallocated_result_.reset(new Result);
78  if (!preallocated_feedback_)
79  preallocated_feedback_.reset(new Feedback);
80  }
81 
82  void setAborted(ResultConstPtr result = ResultConstPtr((Result*)NULL))
83  {
84  if (!req_succeed_ && !req_abort_ && !req_cancel_)
85  {
86  req_result_ = result;
87  req_abort_ = true;
88  }
89  }
90 
91  void setCanceled(ResultConstPtr result = ResultConstPtr((Result*)NULL))
92  {
93  if (!req_succeed_ && !req_abort_ && !req_cancel_)
94  {
95  req_result_ = result;
96  req_cancel_ = true;
97  }
98  }
99 
100  void setSucceeded(ResultConstPtr result = ResultConstPtr((Result*)NULL))
101  {
102  if (!req_succeed_ && !req_abort_ && !req_cancel_)
103  {
104  req_result_ = result;
105  req_succeed_ = true;
106  }
107  }
108 
109  void setFeedback(FeedbackConstPtr feedback = FeedbackConstPtr((Feedback*)NULL))
110  {
111  req_feedback_ = feedback;
112  }
113 
114  bool valid()
115  {
116  return gh_.getGoal() != NULL;
117  }
118 
120  {
121  using namespace actionlib_msgs;
122  if (valid())
123  {
124  actionlib_msgs::GoalStatus gs = gh_.getGoalStatus();
125  if (req_abort_ && (gs.status == GoalStatus::ACTIVE ||
126  gs.status == GoalStatus::PREEMPTING))
127  {
128  if (req_result_)
129  gh_.setAborted(*req_result_);
130  else
131  gh_.setAborted();
132  }
133  else if (req_cancel_ && gs.status == GoalStatus::PREEMPTING)
134  {
135  if (req_result_)
136  gh_.setCanceled(*req_result_);
137  else
138  gh_.setCanceled();
139  }
140  else if (req_succeed_ && (gs.status == GoalStatus::ACTIVE ||
141  gs.status == GoalStatus::PREEMPTING))
142  {
143  if (req_result_)
144  gh_.setSucceeded(*req_result_);
145  else
146  gh_.setSucceeded();
147  }
148  if (req_feedback_ && gs.status == GoalStatus::ACTIVE)
149  {
150  gh_.publishFeedback(*req_feedback_);
151  }
152  }
153  }
154 };
155 
156 } // namespace
157 
158 #endif // header guard
RealtimeServerGoalHandle(GoalHandle &gh, const ResultPtr &preallocated_result=ResultPtr((Result *) NULL), const FeedbackPtr &preallocated_feedback=FeedbackPtr((Feedback *) NULL))
void setAborted(ResultConstPtr result=ResultConstPtr((Result *) NULL))
void setSucceeded(ResultConstPtr result=ResultConstPtr((Result *) NULL))
void setCanceled(ResultConstPtr result=ResultConstPtr((Result *) NULL))
actionlib::ServerGoalHandle< Action > GoalHandle
void setFeedback(FeedbackConstPtr feedback=FeedbackConstPtr((Feedback *) NULL))


realtime_tools
Author(s): Stuart Glaser
autogenerated on Mon Mar 22 2021 02:20:09