mongo_ros.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  */
00030 
00039 #include <mongo_ros/mongo_ros.h>
00040 #include <mongo_ros/exceptions.h>
00041 
00042 namespace mongo_ros
00043 {
00044 
00045 using std::string;
00046 
00048 template <class P>
00049 P getParam (const ros::NodeHandle& nh, const string& name, const P& default_val) 
00050 {
00051   P val;
00052   nh.param(name, val, default_val);
00053   ROS_DEBUG_STREAM_NAMED ("init", "Initialized " << name << " to " << val <<
00054                           " (default was " << default_val << ")");
00055   return val;
00056 }
00057 
00058 string getHost (ros::NodeHandle nh, const string& host="")
00059 {
00060   const string db_host =
00061     (host=="") ?
00062     getParam<string>(nh, "warehouse_host", "localhost") :
00063     host;
00064   return db_host;
00065 }
00066 
00067 int getPort (ros::NodeHandle nh, const int port=0)
00068 {
00069   const int db_port =
00070     (port==0) ?
00071     getParam<int>(nh, "warehouse_port", 27017) :
00072     port;
00073   return db_port;
00074 }
00075 
00076 boost::shared_ptr<mongo::DBClientConnection>
00077 makeDbConnection (const ros::NodeHandle& nh, const string& host,
00078                   const unsigned& port, const float timeout)
00079 {
00080   // The defaults should match the ones used by mongodb/wrapper.py
00081   const string db_host = getHost(nh, host);
00082   const int db_port = getPort(nh, port);
00083 
00084   const string db_address = (boost::format("%1%:%2%") % db_host % db_port).str();
00085   boost::shared_ptr<mongo::DBClientConnection> conn;
00086   
00087   const ros::WallTime end = ros::WallTime::now() + ros::WallDuration(timeout);
00088   
00089   while (ros::ok() && ros::WallTime::now()<end)
00090   {
00091     conn.reset(new mongo::DBClientConnection());
00092     try
00093     {
00094       ROS_DEBUG_STREAM_NAMED ("init", "Connecting to db at " << db_address);
00095       conn->connect(db_address);
00096       if (!conn->isFailed())
00097         break;
00098     }
00099     catch (mongo::ConnectException& e)
00100     {
00101       ros::Duration(1.0).sleep();
00102     }
00103   }
00104   if (conn->isFailed() || ros::WallTime::now()>end)
00105     throw DbConnectException();
00106 
00107   return conn;
00108 }
00109 
00110 void dropDatabase (const string& db_name)
00111 {
00112   dropDatabase(db_name, "", 0, 60.0);
00113 }
00114 
00115 void dropDatabase (const string& db, const string& host, const unsigned port,
00116                    const float timeout)
00117 {
00118   ros::NodeHandle nh;
00119   boost::shared_ptr<mongo::DBClientConnection> c =
00120     makeDbConnection(nh, host, port, timeout);
00121   c->dropDatabase(db);
00122 }
00123 
00124 string messageType (mongo::DBClientConnection& conn,
00125                     const string& db, const string& coll)
00126 {
00127   const string ns = db+".ros_message_collections";
00128   std::auto_ptr<mongo::DBClientCursor> cursor = conn.query(ns, BSON("name" << coll));
00129   mongo::BSONObj obj = cursor->next();
00130   return obj.getStringField("type");
00131 }
00132 
00133 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


mongo_ros
Author(s): Bhaskara Marthi
autogenerated on Tue Sep 3 2013 13:20:12