simple_session_client.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2008, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 // author: Rosen Diankov
00036 #include <ros/node_handle.h>
00037 #include <ros/master.h>
00038 #include <ros/session.h>
00039 #include <boost/thread/mutex.hpp>
00040 #include <boost/shared_ptr.hpp>
00041 
00042 #include "session_tutorials/simple_session.h"
00043 #include "session_tutorials/set_variable.h"
00044 #include "session_tutorials/get_variable.h"
00045 #include "session_tutorials/add_variables.h"
00046 #include <map>
00047 
00048 using namespace std;
00049 using namespace ros;
00050 
00051 boost::shared_ptr<ros::NodeHandle> s_pmasternode;
00052 
00053 class SimpleSessionClient
00054 {
00055 public:
00056     SimpleSessionClient()
00057     {
00058         session_tutorials::simple_session::Request req;
00059         session_tutorials::simple_session::Response res;
00060         req.options = 1234;
00061         handle = session::create_session("simple_session",req,res);
00062 
00063         ROS_INFO("session %s:%d open", handle->GetSessionName().c_str(), handle->GetSessionId());
00064         testsync();
00065         testasync();
00066 
00067         ROS_INFO("closing all connections and resuming session");
00068         session::abstractSessionHandle newhandle(new session::Session<session_tutorials::simple_session::Request, session_tutorials::simple_session::Response>(handle->GetSessionName(), handle->GetSessionId()));
00069         handle = newhandle;
00070         testsync();
00071 
00072         handle->terminate();
00073         handle.reset();
00074     }
00075 
00076     void testasync() {
00077         Time start_time = Time::now();
00078         int N = 10000;
00079         for(int i = 0; i < N; ++i) {
00080             int a = rand(), b = rand();
00081             setreq.variable = "a";
00082             setreq.value = a;
00083             handle->call("set_variable",setreq,setres,true);
00084 
00085             setreq.variable = "b";
00086             setreq.value = b;
00087             handle->call("set_variable",setreq,setres,true);
00088         }
00089 
00090         ROS_INFO("made %d async calls in %fs", 2*N,(float)(Time::now()-start_time).toSec());
00091 
00092         // reset
00093         getreq.variable = "a";
00094         handle->call("get_variable",getreq,getres);
00095         ROS_INFO("flushed all async calls");
00096     }
00097 
00098     void testsync() {
00099         Time start_time = Time::now();
00100         int N = 5000;
00101         for(int i = 0; i < N; ++i) {
00102             int a = rand(), b = rand(), c = a+b;
00103             setreq.variable = "a";
00104             setreq.value = a;
00105             handle->call("set_variable",setreq,setres);
00106 
00107             setreq.variable = "b";
00108             setreq.value = b;
00109             handle->call("set_variable",setreq,setres);
00110 
00111             getreq.variable = "a";
00112             handle->call("get_variable",getreq,getres);
00113             ROS_ASSERT(getres.result==a);
00114             //printf("a=%d\n", getres.result);
00115 
00116             addreq.variable1 = "a";
00117             addreq.variable2 = "b";
00118             addreq.result = "c";
00119             handle->call("add_variables",addreq,addres);
00120 
00121             getreq.variable = "c";
00122             handle->call("get_variable",getreq,getres);
00123             //ROS_INFO("%d + %d = %d(res=%d)", a, b, c, getres.result);
00124             ROS_ASSERT(getres.result==c);
00125             //printf("c=%d\n", getres.result);
00126         }
00127 
00128         ROS_INFO("made %d sync calls in %fs", 5*N,(float)(Time::now()-start_time).toSec());
00129     }
00130 
00131 private:
00132     session::abstractSessionHandle handle;
00133     session_tutorials::set_variable::Request setreq;
00134     session_tutorials::set_variable::Response setres;
00135     session_tutorials::get_variable::Request getreq;
00136     session_tutorials::get_variable::Response getres;
00137     session_tutorials::add_variables::Request addreq;
00138     session_tutorials::add_variables::Response addres;
00139 };
00140 
00141 int main(int argc, char **argv)
00142 {
00143   ros::init(argc, argv,"simple_session_client");
00144 
00145   s_pmasternode.reset(new ros::NodeHandle());
00146   if( !ros::master::check() )
00147       return 1;
00148 
00149   boost::shared_ptr<SimpleSessionClient> client(new SimpleSessionClient());
00150   ros::spin();
00151   client.reset();
00152   s_pmasternode.reset();
00153   return 0;
00154 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


session_tutorials
Author(s): Rosen Diankov / rdiankov at cs.cmu.edu
autogenerated on Sat Mar 23 2013 22:50:29