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 #include <mongo_ros/message_collection.h>
00042 
00043 namespace mongo_ros
00044 {
00045 
00046 using std::string;
00047 
00049 template <class P>
00050 P getParam (const ros::NodeHandle& nh, const string& name, const P& default_val) 
00051 {
00052   P val;
00053   nh.param(name, val, default_val);
00054   ROS_DEBUG_STREAM_NAMED ("init", "Initialized " << name << " to " << val <<
00055                           " (default was " << default_val << ")");
00056   return val;
00057 }
00058 
00059 string getHost (ros::NodeHandle nh, const string& host="")
00060 {
00061   const string db_host =
00062     (host=="") ?
00063     getParam<string>(nh, "warehouse_host", "localhost") :
00064     host;
00065   return db_host;
00066 }
00067 
00068 int getPort (ros::NodeHandle nh, const int port=0)
00069 {
00070   const int db_port =
00071     (port==0) ?
00072     getParam<int>(nh, "warehouse_port", 27017) :
00073     port;
00074   return db_port;
00075 }
00076 
00077 boost::shared_ptr<mongo::DBClientConnection>
00078 makeDbConnection (const ros::NodeHandle& nh, const string& host,
00079                   const unsigned& port, const float timeout)
00080 {
00081   // The defaults should match the ones used by mongodb/wrapper.py
00082   const string db_host = getHost(nh, host);
00083   const int db_port = getPort(nh, port);
00084 
00085   const string db_address = (boost::format("%1%:%2%") % db_host % db_port).str();
00086   boost::shared_ptr<mongo::DBClientConnection> conn;
00087   
00088   const ros::WallTime end = ros::WallTime::now() + ros::WallDuration(timeout);
00089   
00090   while (ros::ok() && ros::WallTime::now()<end)
00091   {
00092     conn.reset(new mongo::DBClientConnection());
00093     try
00094     {
00095       ROS_DEBUG_STREAM_NAMED ("init", "Connecting to db at " << db_address);
00096       conn->connect(db_address);
00097       if (!conn->isFailed())
00098         break;
00099     }
00100     catch (mongo::ConnectException& e)
00101     {
00102       ros::Duration(1.0).sleep();
00103     }
00104   }
00105   if (conn->isFailed() || ros::WallTime::now()>end)
00106     throw DbConnectException();
00107 
00108   ROS_DEBUG_STREAM_NAMED("init", "Successfully connected to db");
00109   return conn;
00110 }
00111 
00112 void dropDatabase (const string& db_name)
00113 {
00114   dropDatabase(db_name, "", 0, 60.0);
00115 }
00116 
00117 void dropDatabase (const string& db, const string& host, const unsigned port,
00118                    const float timeout)
00119 {
00120   ros::NodeHandle nh;
00121   boost::shared_ptr<mongo::DBClientConnection> c =
00122     makeDbConnection(nh, host, port, timeout);
00123   c->dropDatabase(db);
00124 }
00125 
00126 string messageType (mongo::DBClientConnection& conn,
00127                     const string& db, const string& coll)
00128 {
00129   const string ns = db+".ros_message_collections";
00130   std::auto_ptr<mongo::DBClientCursor> cursor = conn.query(ns, BSON("name" << coll));
00131   mongo::BSONObj obj = cursor->next();
00132   return obj.getStringField("type");
00133 }
00134 
00135 } // namespace


warehouse_ros
Author(s): Bhaskara Marthi
autogenerated on Fri Apr 22 2016 04:08:50