session_adv_test.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 <gtest/gtest.h>
00037 
00038 #include <ros/node.h>
00039 #include <ros/session.h>
00040 #include <boost/thread/mutex.hpp>
00041 #include <boost/shared_ptr.hpp>
00042 
00043 #include "roscpp_sessions/simple_session.h"
00044 #include "roscpp_sessions/set_variable.h"
00045 #include "roscpp_sessions/get_variable.h"
00046 #include "roscpp_sessions/add_variables.h"
00047 
00048 #include <map>
00049 
00050 using namespace std;
00051 using namespace ros;
00052 
00053 // keeps track of created variables
00054 class SimpleSessionInstance
00055 {
00056 public:
00057     void set_variable(const string& name, int val) { variables[name] = val; }
00058     int get_variable(const string& name) { return variables[name]; }
00059     void add_variables(const string& result, const string& name1, const string& name2) {
00060         variables[result] = variables[name1] + variables[name2];
00061     }
00062 
00063 private:
00064     map<string,int> variables;
00065 };
00066 
00067 class SimpleSession
00068 {
00069 public:
00070     SimpleSession()
00071     {
00072     }
00073 
00074     void advertise_sessions() {
00075         fprintf(stderr,"starting to advertise\n");
00076         Node::instance()->advertiseService("session_adv",&SimpleSession::startsession,this,1);
00077 
00078         // advertise persistent services, the protocol for these differs!
00079         Node::instance()->advertiseService("set_variable",&SimpleSession::set_variable,this,-1);
00080         Node::instance()->advertiseService("get_variable",&SimpleSession::get_variable,this,-1);
00081         Node::instance()->advertiseService("add_variables",&SimpleSession::add_variables,this,-1);
00082         fprintf(stderr,"end advertise\n");
00083     }
00084 
00085     template <class MReq>
00086     SimpleSessionInstance* getstate(const MReq& req)
00087     {
00088         if( !req.__connection_header )
00089             return NULL;
00090 
00091         boost::mutex::scoped_lock lock(map_mutex);
00092         ros::M_string::const_iterator it = req.__connection_header->find("session_adv");
00093         if( it == req.__connection_header->end() )
00094             return NULL;
00095 
00096         int sessionid = atoi(it->second.c_str());
00097         if( mapsessions.find(sessionid) == mapsessions.end() )
00098             return NULL;
00099         return mapsessions[sessionid].get();
00100     }
00101 
00102     void unadvertise_sessions() {
00103         fprintf(stderr,"starting to unadvertise add_variables\n");
00104         Node::instance()->unadvertiseService("add_variables");
00105         fprintf(stderr,"starting to unadvertise get_variables\n");
00106         Node::instance()->unadvertiseService("get_variable");
00107         fprintf(stderr,"starting to unadvertise set_variables\n");
00108         Node::instance()->unadvertiseService("set_variable");
00109         fprintf(stderr,"starting to unadvertise session_adv\n");
00110         Node::instance()->unadvertiseService("session_adv");
00111         fprintf(stderr,"end unadvertise\n");
00112     }
00113 
00114     bool startsession(roscpp_sessions::simple_session::Request& req, roscpp_sessions::simple_session::Response& res) {
00115         fprintf(stderr,"start session\n");
00116         boost::mutex::scoped_lock lock(map_mutex);
00117         
00118         if( req.sessionid ) {
00119             // destroy
00120             cout << "terminate session: " << req.sessionid << endl;
00121             mapsessions.erase(req.sessionid);
00122         }
00123         else {
00124             // start a new session with id
00125             int id = rand();
00126             ROS_ASSERT( mapsessions.find(id) == mapsessions.end() );
00127             mapsessions[id].reset(new SimpleSessionInstance());
00128             cout << "simple session " << id << " started with options " << req.options << endl;
00129             res.sessionid = id;
00130         }
00131 
00132         fprintf(stderr,"end session\n");
00133         return true;
00134     }
00135 
00136     bool set_variable(roscpp_sessions::set_variable::Request& req,
00137                       roscpp_sessions::set_variable::Response& res)
00138     {
00139         SimpleSessionInstance* pinst = getstate(req);
00140         if(pinst == NULL)
00141             return false;
00142         pinst->set_variable(req.variable,req.value);
00143         return true;
00144     }
00145 
00146     bool get_variable(roscpp_sessions::get_variable::Request& req,
00147                       roscpp_sessions::get_variable::Response& res)
00148     {
00149         SimpleSessionInstance* pinst = getstate(req);
00150         if(pinst == NULL)
00151             return false;
00152         res.result = pinst->get_variable(req.variable);
00153         return true;
00154     }
00155 
00156     bool add_variables(roscpp_sessions::add_variables::Request& req,
00157                       roscpp_sessions::add_variables::Response& res)
00158     {
00159         SimpleSessionInstance* pinst = getstate(req);
00160         if(pinst == NULL)
00161             return false;
00162         pinst->add_variables(req.result, req.variable1,req.variable2);
00163         return true;
00164     }
00165 
00166 private:
00167     map<int,boost::shared_ptr<SimpleSessionInstance> > mapsessions;
00168     boost::mutex map_mutex;
00169 };
00170 
00171 TEST(SessionAdv, Simple)
00172 {
00173     SimpleSession ss;
00174     Node::instance()->advertiseService("session_adv",&SimpleSession::startsession,&ss);
00175     usleep(400000);
00176     Node::instance()->unadvertiseService("session_adv");
00177     fprintf(stderr,"EndSimple\n");
00178 }
00179 
00180 TEST(SessionAdv, Normal)
00181 {
00182     for(int i = 0; i < 5; ++i) {
00183         SimpleSession ss;
00184         ss.advertise_sessions();
00185         usleep(200000);
00186         ss.unadvertise_sessions();
00187     }
00188     fprintf(stderr,"EndNormal\n");
00189 }
00190 
00191 int main(int argc, char** argv)
00192 {
00193     testing::InitGoogleTest(&argc, argv);
00194 
00195     ros::init(argc, argv);
00196     ros::Node n("session");
00197 
00198     int ret = RUN_ALL_TESTS();
00199     fprintf(stderr,"After RUN_ALL_TESTS\n");
00200 
00201     return ret;
00202 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


roscpp_sessions
Author(s): Rosen Diankov (rdiankov@cs.cmu.edu)
autogenerated on Sat Mar 23 2013 13:53:29