sync_db_name.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2008, Willow Garage, Inc.
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of Willow Garage, Inc. nor the names of its
00019 #    contributors may be used to endorse or promote products derived
00020 #    from this software without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 #
00035 # Author: Bhaskara Marthi
00036 
00037 # Node that generates a unique name for the collection namespace used in the
00038 # world model and inserts some metadata into it.
00039 
00040 import roslib
00041 roslib.load_manifest('semanticmodel')
00042 
00043 import rospy
00044 import pymongo as pm
00045 import time
00046 import std_srvs.srv
00047 import std_msgs.msg
00048 import semanticmodel.srv as sms
00049 import sys
00050 import semanticmodel.db as sdb
00051 
00052 # Look in the db 'runs' collection for the largest existing run id
00053 # and create namespace with an id one larger
00054 def update_collection_namespace():
00055     global COLLECTION_NAMESPACE
00056     global BAGFILE
00057     db = CONN[DB_NAME]
00058     next_id = sdb.generate_run_id(db)
00059     COLLECTION_NAMESPACE = "run{0}".format(next_id)
00060     while not rospy.is_shutdown():
00061         t = rospy.Time.now().to_sec()
00062         if t>0.0:
00063             rospy.loginfo("Setting run namespace to {0}".format(next_id))
00064             db.runs.insert({'id': next_id, 'bag_file': BAGFILE,
00065                          'creation_time': rospy.Time.now().to_sec()})
00066             break
00067         else:
00068             rospy.loginfo("Waiting till ros time is > 0")
00069             time.sleep(2.0)
00070 
00071 def get_collection_namespace(req):
00072     return {'db_name': DB_NAME, 'collection_namespace': COLLECTION_NAMESPACE}
00073        
00074 
00075 rospy.init_node('sync_db_name')
00076 
00077 HOST = rospy.get_param('warehouse_host', 'bab.willowgarage.com')
00078 PORT = rospy.get_param('warehouse_port', 27017)
00079 DB_NAME = rospy.get_param('semantic_db_name', 'semantic_world_model')
00080 
00081 
00082 rospy.loginfo("Starting up sync_db_name.  Expect db server at {0}:{1} and db name {2}".
00083     format(HOST, PORT, DB_NAME))
00084 COLLECTION_NAMESPACE = None
00085 CONN = None
00086 
00087 
00088 if '--online' in sys.argv:
00089     BAGFILE = 'online_not_a_bag'
00090 else:
00091     while not rospy.is_shutdown():
00092         try:
00093             BAGFILE = rospy.get_param('current_bagfile')
00094             break
00095         except:
00096             rospy.logwarn("Waiting for there to exist a parameter called current_bagfile")
00097         time.sleep(2.0)
00098 
00099 rospy.loginfo("Recording bagfile name {0}".format(BAGFILE))
00100 
00101 
00102 rospy.loginfo("Attempting to connect to db at {0}:{1}".
00103               format(HOST, PORT))
00104 
00105 while not rospy.is_shutdown():
00106     try:
00107         CONN=pm.Connection(host=HOST, port=PORT)
00108         rospy.loginfo("Connected to db")
00109         break
00110     except Exception as e:
00111         rospy.logwarn("Failed to connect to MongoDB at {1}:{2}; retrying.  Exception was: {0}".
00112                       format(e, HOST, PORT))
00113         rospy.sleep(2.0)
00114         
00115 update_collection_namespace()
00116 get_name_srv = rospy.Service('get_collection_namespace', 
00117                              sms.GetCollectionNamespace, 
00118                              get_collection_namespace)
00119 rospy.spin()    


semanticmodel
Author(s): Julian ("Mac") Mason
autogenerated on Thu Dec 12 2013 12:39:10