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
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
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 }