service_server_test.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <swri_roscpp/service_server.h>
00003 
00004 #include <diagnostic_updater/diagnostic_updater.h>
00005 
00006 #include <std_srvs/Empty.h>
00007 
00008 namespace du = diagnostic_updater;
00009 
00010 // Alias type for easier access to DiagnosticStatus enumerations.
00011 typedef diagnostic_msgs::DiagnosticStatus DS;
00012 
00013 class ServiceServerTest
00014 {
00015   ros::NodeHandle nh_;
00016   ros::WallTimer init_timer_;
00017   ros::Timer diag_timer_;
00018 
00019   du::Updater diagnostic_updater_;
00020   
00021   swri::ServiceServer test1_srv_;
00022   swri::ServiceServer test2_srv_;
00023   swri::ServiceServer test3_srv_;
00024 
00025   bool test1_result_;
00026   
00027  public:
00028   ServiceServerTest()
00029     :
00030     test1_result_(true)
00031   {
00032     // Setup a one-shot timer to initialize the node after a brief
00033     // delay so that /rosout is always fully initialized.
00034     ROS_INFO("Starting initialization timer...");
00035     init_timer_ = nh_.createWallTimer(ros::WallDuration(1.0),
00036                                       &ServiceServerTest::initialize,
00037                                       this,
00038                                       true);
00039   }
00040 
00041   void initialize(const ros::WallTimerEvent &ignored)
00042   {
00043     test1_srv_.setInstrumentPerClient(true);
00044     test1_srv_ = swri::ServiceServer(nh_, "test_service1",
00045                                      &ServiceServerTest::handleService1,
00046                                      this);
00047 
00048     test2_srv_.setInstrumentPerClient(false);
00049     test2_srv_ = swri::ServiceServer(nh_, "test_service2",
00050                                      &ServiceServerTest::handleService2,
00051                                      this);
00052 
00053     test3_srv_.setLogCalls(true);
00054     test3_srv_.setInstrumentPerClient(false);
00055     test3_srv_ = swri::ServiceServer(nh_, "test_service3",
00056                                      &ServiceServerTest::handleService3,
00057                                      this);
00058     
00059     
00060     diagnostic_updater_.setHardwareID("none");
00061     diagnostic_updater_.add(
00062       "swri::ServiceServer test service 1", this,
00063       &ServiceServerTest::service1Diagnostics);
00064 
00065     diagnostic_updater_.add(
00066       "swri::ServiceServer test service 2", this,
00067       &ServiceServerTest::service2Diagnostics);
00068 
00069     diagnostic_updater_.add(
00070       "swri::ServiceServer test service 3", this,
00071       &ServiceServerTest::service3Diagnostics);
00072     
00073     diag_timer_ = nh_.createTimer(ros::Duration(1.0),
00074                                   &ServiceServerTest::handleDiagnosticsTimer,
00075                                   this);
00076   }
00077 
00078   bool handleService1(std_srvs::Empty::Request &req,
00079                       std_srvs::Empty::Response &res)
00080   {
00081     ROS_INFO("test service 1 called. returning %s",
00082              test1_result_ ? "true" : "false");
00083     return test1_result_;
00084   }
00085 
00086   bool handleService2(ros::ServiceEvent<std_srvs::Empty::Request,
00087                                         std_srvs::Empty::Response> &event)
00088   {
00089     ROS_INFO("test service 2 called");
00090     test1_result_ = !test1_result_;
00091     return true;
00092   }
00093 
00094   bool handleService3(const std::string &name,
00095                       const std_srvs::Empty::Request &request,
00096                       std_srvs::Empty::Response &response)
00097   {
00098     ROS_INFO("test service 3 called by %s", name.c_str());
00099     return true;
00100   }
00101   
00102   void handleDiagnosticsTimer(const ros::TimerEvent &ignored)
00103   {
00104     diagnostic_updater_.update();
00105   }
00106 
00107   void service1Diagnostics(du::DiagnosticStatusWrapper& status) // NOLINT
00108   {
00109     status.summary(DS::OK, "No errors reported.");
00110     test1_srv_.appendDiagnostics(status, "Test Service 1",
00111                                  swri::ServiceServer::DIAG_ALL);
00112   }
00113 
00114   void service2Diagnostics(du::DiagnosticStatusWrapper& status) // NOLINT
00115   {
00116     status.summary(DS::OK, "No errors reported.");
00117     test2_srv_.appendDiagnostics(status, "Test Service 2",
00118                                  swri::ServiceServer::DIAG_ALL);
00119   }  
00120 
00121   void service3Diagnostics(du::DiagnosticStatusWrapper& status) // NOLINT
00122   {
00123     status.summary(DS::OK, "No errors reported.");
00124     test3_srv_.appendDiagnostics(status, "Test Service 3",
00125                                  swri::ServiceServer::DIAG_ALL);
00126   }  
00127 };  // class ServiceServerTest
00128 
00129 int main(int argc, char **argv)
00130 {
00131   ros::init(argc, argv, "service_server_test");
00132 
00133   ServiceServerTest node;
00134   ros::spin();
00135   
00136   return 0;  
00137 }


swri_roscpp
Author(s):
autogenerated on Tue Oct 3 2017 03:19:27