00001 // own class header first: https://stackoverflow.com/a/2762596/8224054 00002 #include "ros1_ros_cpptemplate/service.hpp" 00003 00004 #include <std_msgs/Int32.h> 00005 #include <sstream> 00006 #include <string> 00007 00008 namespace ros1_ros_cpptemplate 00009 { 00010 using ros1_cpptemplate::AtomicFibonacci; 00011 using ros1_cpptemplate::AtomicFibonacciPtr; 00012 00013 Service::Service(ros1_cpptemplate::AtomicFibonacciPtr atomic_fibonacci, 00014 ros::NodeHandle& node_handle, 00015 const std::string& service_topic_name) 00016 { 00017 int queue_size = 1; 00018 bool latched = false; 00019 service_ = node_handle.advertiseService("fibonacci_service", &Service::serve, 00020 this); 00021 atomic_fibonacci_ = atomic_fibonacci; 00022 } 00023 00024 Service::~Service() 00025 { 00026 } 00027 00028 bool Service::serve(ros1_template_msgs::Answer::Request &request, 00029 ros1_template_msgs::Answer::Response &response) 00030 { 00031 ROS_INFO_STREAM("Service called with question: " << request.question); 00032 00033 std::stringstream log_prefix_string_stream; 00034 log_prefix_string_stream << "Serving on " << service_.getService() << ": "; 00035 std::string log_prefix = log_prefix_string_stream.str(); 00036 00037 int next_number = atomic_fibonacci_->nextAndLog(log_prefix); 00038 response.answer = next_number; 00039 00040 return true; 00041 } 00042 00043 } // namespace