Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
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 
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         
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             
00120             cout << "terminate session: " << req.sessionid << endl;
00121             mapsessions.erase(req.sessionid);
00122         }
00123         else {
00124             
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 }