mock_simple_server.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # Copyright (c) 2009, Willow Garage, Inc.
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Willow Garage, Inc. nor the names of its
00014 #       contributors may be used to endorse or promote products derived from
00015 #       this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027 # POSSIBILITY OF SUCH DAMAGE.
00028 from __future__ import print_function
00029 
00030 import time
00031 import rospy
00032 
00033 from actionlib.simple_action_server import SimpleActionServer
00034 from actionlib.server_goal_handle import ServerGoalHandle
00035 from actionlib.msg import TestRequestAction, TestRequestGoal, TestRequestResult
00036 
00037 
00038 class RefSimpleServer(SimpleActionServer):
00039     def __init__(self, name):
00040         SimpleActionServer.__init__(self, name, TestRequestAction, self.execute_cb, False)
00041         self.start()
00042 
00043     def execute_cb(self, goal):
00044         rospy.logdebug("Goal:\n" + str(goal))
00045         result = TestRequestResult(goal.the_result, True)
00046 
00047         if goal.pause_status > rospy.Duration(0.0):
00048             rospy.loginfo("Locking the action server for %.3f seconds" % goal.pause_status.to_sec())
00049             status_continue_time = rospy.get_rostime() + goal.pause_status
00050             # Takes the action server lock to prevent status from
00051             # being published (simulates a freeze).
00052             with self.action_server.lock:
00053                 while rospy.get_rostime() < status_continue_time:
00054                     time.sleep(0.02)
00055                 rospy.loginfo("Unlocking the action server")
00056 
00057         terminate_time = rospy.get_rostime() + goal.delay_terminate
00058         while rospy.get_rostime() < terminate_time:
00059             time.sleep(0.02)
00060             if not goal.ignore_cancel:
00061                 if self.is_preempt_requested():
00062                     self.set_preempted(result, goal.result_text)
00063                     return
00064 
00065         rospy.logdebug("Terminating goal as: %i" % goal.terminate_status)
00066 
00067         if goal.terminate_status == TestRequestGoal.TERMINATE_SUCCESS:
00068             self.set_succeeded(result, goal.result_text)
00069         elif goal.terminate_status == TestRequestGoal.TERMINATE_ABORTED:
00070             self.set_aborted(result, goal.result_text)
00071         elif goal.terminate_status == TestRequestGoal.TERMINATE_REJECTED:
00072             rospy.logerr("Simple action server cannot reject goals")
00073             self.set_aborted(None, "Simple action server cannot reject goals")
00074         elif goal.terminate_status == TestRequestGoal.TERMINATE_DROP:
00075             rospy.loginfo("About to drop the goal.  This should produce an error message.")
00076             return
00077         elif goal.terminate_status == TestRequestGoal.TERMINATE_EXCEPTION:
00078             rospy.loginfo("About to throw an exception.  This should produce an error message.")
00079             raise Exception("Terminating by throwing an exception")
00080         elif goal.terminate_status == TestRequestGoal.TERMINATE_LOSE:
00081             # Losing the goal requires messing about in the action server's innards
00082             for i, s in enumerate(self.action_server.status_list):
00083                 if s.status.goal_id.id == self.current_goal.goal.goal_id.id:
00084                     del self.action_server.status_list[i]
00085                     break
00086             self.current_goal = ServerGoalHandle()
00087         else:
00088             rospy.logerr("Don't know how to terminate as %d" % goal.terminate_status)
00089             self.set_aborted(None, "Don't know how to terminate as %d" % goal.terminate_status)
00090 
00091 
00092 if __name__ == '__main__':
00093     rospy.init_node("ref_simple_server")
00094     ref_server = RefSimpleServer("test_request_action")
00095     print("Spinning")
00096     rospy.spin()


actionlib
Author(s): Eitan Marder-Eppstein, Vijay Pradeep, Mikael Arguedas
autogenerated on Sat Feb 16 2019 03:21:28