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
00031
00032
00033
00034
00035
00036
00037
00038
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
00053
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()