00001
00002
00003 import roslib
00004 roslib.load_manifest('openrtm')
00005
00006 import sys
00007 import os
00008 import time
00009 import optparse
00010
00011 from xml.dom.minidom import parse
00012
00013 import rtctree
00014 from rtshell import rtcon
00015 from rtshell import path
00016 from rtshell import state_control_base
00017 from rtshell import rts_exceptions
00018
00019 def alive_component(path):
00020 tree=rtctree.tree.RTCTree()
00021 if tree.has_path(path) and tree.is_component(path):
00022 node = tree.get_node(path)
00023 return node.plain_state_string
00024 else:
00025 return False
00026
00027 def wait_component(cmd_path):
00028 count=0
00029 path = rtctree.path.parse_path(cmd_path)[0]
00030 node = alive_component(path)
00031 while not node and count < 30:
00032 node = alive_component(path)
00033 print "Wait for ",cmd_path
00034 count += 1
00035 time.sleep(1)
00036 if not node:
00037 raise rts_exceptions.NoSuchObjectError(cmd_path)
00038 return node
00039
00040 def check_connect(src_path, dest_path):
00041 tree=rtctree.tree.RTCTree()
00042 src_path, src_port = rtctree.path.parse_path(src_path)
00043 dest_path, dest_port = rtctree.path.parse_path(dest_path)
00044 src_node = tree.get_node(src_path)
00045 dest_node = tree.get_node(dest_path)
00046 port = src_node.get_port_by_name(src_port)
00047 for conn in port.connections:
00048 for name, p in conn.ports:
00049 tmp_dest_path, tmp_dest_port = rtctree.path.parse_path(name)
00050 if dest_path[-1] == tmp_dest_path[-1] and dest_port == tmp_dest_port:
00051 return True
00052 return False
00053
00054 def rtconnect(nameserver, tags):
00055 import re
00056 for tag in tags:
00057 source_path = nameserver+"/"+tag.attributes.get("from").value
00058 dest_path = nameserver+"/"+tag.attributes.get("to").value
00059 source_path = re.sub("\$\(arg SIMULATOR_NAME\)",simulator,source_path);
00060 dest_path = re.sub("\$\(arg SIMULATOR_NAME\)",simulator,dest_path);
00061 print "connect from %s to %s"%(source_path,dest_path)
00062 source_full_path = path.cmd_path_to_full_path(source_path)
00063 dest_full_path = path.cmd_path_to_full_path(dest_path)
00064 if tag.attributes.get("subscription_type") != None:
00065 sub_type = tag.attributes.get("subscription_type").value
00066 if not sub_type in ['flush','new','periodic']:
00067 print >>sys.stderr, sub_type+' is not a subscription type'
00068 continue
00069 else:
00070 sub_type = 'flush'
00071 if sub_type == 'new':
00072 push_policy = 'all'
00073
00074 try:
00075 wait_component(source_full_path)
00076 wait_component(dest_full_path)
00077 if check_connect(source_full_path,dest_full_path):
00078 continue
00079 except Exception, e:
00080 print >>sys.stderr, 'Could not Connect : ', e,' '
00081 return 1
00082
00083 try:
00084 sub_type = str(sub_type)
00085 props = {'dataport.subscription_type': sub_type}
00086 if sub_type == 'new':
00087 props['dataport.publisher.push_policy'] = 'all'
00088 elif sub_type == 'periodic':
00089 props['dataport.publisher.push_policy'] = 'all'
00090 if tag.attributes.get("push_rate") != None:
00091 props['dataport.push_rate'] = str(tag.attributes.get("push_rate").value)
00092 else:
00093 props['dataport.push_rate'] = str('50.0')
00094 options = optparse.Values({'verbose': False, 'id': '', 'name': None, 'properties': props})
00095 print >>sys.stderr, "Connect from",source_path,"to",dest_path,"with",options
00096 try :
00097 rtcon.connect_ports(source_path, source_full_path, dest_path, dest_full_path, options, tree=None)
00098 except Exception, e:
00099 rtcon.connect_ports([(source_path,source_full_path), (dest_path, dest_full_path)], options, tree=None)
00100 except Exception, e:
00101 print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00102 return 0
00103
00104 def rtactivate(nameserver, tags):
00105 def activate_action(object, ec_index):
00106 object.activate_in_ec(ec_index)
00107 for tag in tags:
00108 cmd_path = nameserver+"/"+tag.attributes.get("component").value
00109 full_path = path.cmd_path_to_full_path(cmd_path)
00110 print "activate %s"%(full_path)
00111 try:
00112 state = wait_component(full_path)
00113 if state == 'Active':
00114 continue
00115 else:
00116 print "[rtmlaunch] Activate <-",state,full_path
00117 except Exception, e:
00118 print >>sys.stderr, 'Could not Activate : ', e,' '
00119 return 1
00120 try:
00121 options = optparse.Values({"ec_index": 0, 'verbose': False})
00122 try :
00123 state_control_base.alter_component_state(activate_action, cmd_path, full_path, options, None)
00124 except Exception, e:
00125 state_control_base.alter_component_states(activate_action, [(cmd_path, full_path)], options, None)
00126 except Exception, e:
00127 print >>sys.stderr, '{0}: {1}'.format(os.path.basename(sys.argv[0]), e)
00128 return 1
00129 return 0
00130
00131 def main():
00132 global simulator
00133 usage = '''Usage: %prog [launchfile]'''
00134 if len(sys.argv) <= 1:
00135 print >>sys.stderr, usage
00136 return 1
00137 fullpathname = sys.argv[1]
00138 print "[rtmlaunch] starting... ",fullpathname
00139 try:
00140 parser = parse(fullpathname)
00141 except Exception,e:
00142 print e
00143 return 1
00144
00145 nameserver = os.getenv("RTCTREE_NAMESERVERS","localhost")
00146 simulator = os.getenv("SIMULATOR_NAME","Simulator")
00147 print "[rtmlaunch]", simulator
00148 while 1:
00149 print "[rtmlaunch] check connection/activation"
00150 rtconnect(nameserver, parser.getElementsByTagName("rtconnect"))
00151 rtactivate(nameserver, parser.getElementsByTagName("rtactivate"))
00152 time.sleep(10)
00153
00154 if __name__ == '__main__':
00155 main()
00156
00157
00158
00159