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
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 boost::shared_ptr<mongo::DBClientConnection> makeDbConnection (const ros::NodeHandle& nh,
00059 const string& host,
00060 const unsigned& port)
00061 {
00062
00063 const string db_host =
00064 (host=="") ?
00065 getParam<string>(nh, "warehouse_host", "localhost") :
00066 host;
00067 const int db_port =
00068 (port==0) ?
00069 getParam<int>(nh, "warehouse_port", 27017) :
00070 port;
00071
00072 const string db_address = (boost::format("%1%:%2%") % db_host % db_port).str();
00073 boost::shared_ptr<mongo::DBClientConnection> conn;
00074 while (ros::ok())
00075 {
00076 conn.reset(new mongo::DBClientConnection());
00077 try
00078 {
00079 ROS_DEBUG_STREAM_NAMED ("init", "Connecting to db at " << db_address);
00080 conn->connect(db_address);
00081 break;
00082 }
00083 catch (mongo::ConnectException& e)
00084 {
00085 ros::Duration(1.0).sleep();
00086 }
00087 }
00088 if (conn->isFailed())
00089 throw DbConnectException();
00090
00091 return conn;
00092
00093 }
00094
00095 void dropDatabase (const string& db_name)
00096 {
00097 ros::NodeHandle nh;
00098 boost::shared_ptr<mongo::DBClientConnection> c = makeDbConnection(nh);
00099 c->dropDatabase(db_name);
00100 }
00101
00102 string messageType (mongo::DBClientConnection& conn,
00103 const string& db, const string& coll)
00104 {
00105 const string ns = db+".ros_message_collections";
00106 auto_ptr<mongo::DBClientCursor> cursor = conn.query(ns, BSON("name" << coll));
00107 mongo::BSONObj obj = cursor->next();
00108 return obj.getStringField("type");
00109 }
00110
00111 }