$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2009, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 00035 ## ROS message source code generation for C++ 00036 ## 00037 ## Converts ROS .msg files in a package into C++ source code implementations. 00038 00039 import roslib; roslib.load_manifest('rosjava_jni') 00040 import genmsg_java 00041 00042 import sys 00043 import os 00044 import traceback 00045 00046 # roslib.msgs contains the utilities for parsing .msg specifications. It is meant to have no rospy-specific knowledge 00047 import roslib.srvs 00048 import roslib.packages 00049 import roslib.gentools 00050 00051 from cStringIO import StringIO 00052 00053 def write_begin(s, spec, file): 00054 """ 00055 Writes the beginning of the header file: a comment saying it's auto-generated and the include guards 00056 00057 @param s: The stream to write to 00058 @type s: stream 00059 @param spec: The spec 00060 @type spec: roslib.srvs.SrvSpec 00061 @param file: The file this service is being generated for 00062 @type file: str 00063 """ 00064 s.write("/* Auto-generated by genmsg_cpp for file %s */\n"%(file)) 00065 s.write('\npackage ros.pkg.%s.srv;\n' % spec.package) 00066 s.write('\nimport java.nio.ByteBuffer;\n') 00067 00068 def write_end(s, spec): 00069 """ 00070 Writes the end of the header file: the ending of the include guards 00071 00072 @param s: The stream to write to 00073 @type s: stream 00074 @param spec: The spec 00075 @type spec: roslib.srvs.SrvSpec 00076 """ 00077 pass 00078 00079 def generate(srv_path, output_base_path=None): 00080 """ 00081 Generate a service 00082 00083 @param srv_path: the path to the .srv file 00084 @type srv_path: str 00085 """ 00086 (package_dir, package) = roslib.packages.get_dir_pkg(srv_path) 00087 (_, spec) = roslib.srvs.load_from_file(srv_path, package) 00088 00089 s = StringIO() 00090 write_begin(s, spec, srv_path) 00091 s.write('\n') 00092 00093 gendeps_dict = roslib.gentools.get_dependencies(spec, spec.package) 00094 md5sum = roslib.gentools.compute_md5(gendeps_dict) 00095 00096 s.write(""" 00097 public class %(srv_name)s extends ros.communication.Service<%(srv_name)s.Request, %(srv_name)s.Response> { 00098 00099 public static java.lang.String __s_getDataType() { return "%(srv_data_type)s"; } 00100 public static java.lang.String __s_getMD5Sum() { return "%(srv_md5sum)s"; } 00101 00102 public java.lang.String getDataType() { return %(srv_name)s.__s_getDataType(); } 00103 public java.lang.String getMD5Sum() { return %(srv_name)s.__s_getMD5Sum(); } 00104 00105 public %(srv_name)s.Request createRequest() { 00106 return new %(srv_name)s.Request(); 00107 } 00108 00109 public %(srv_name)s.Response createResponse() { 00110 return new %(srv_name)s.Response(); 00111 } 00112 00113 """ % {'srv_name': spec.short_name, 00114 'srv_data_type': spec.full_name, 00115 'srv_md5sum': md5sum}) 00116 00117 request_data_type = '"%s/%s"' % (spec.package, spec.request.short_name) 00118 response_data_type = '"%s/%s"' % (spec.package, spec.response.short_name) 00119 spec.request.short_name = 'Request' 00120 spec.response.short_name = 'Response' 00121 genmsg_java.write_class(s, spec.request, {'ServerMD5Sum': '"%s"' % md5sum, 00122 'DataType': request_data_type}, True) 00123 s.write('\n') 00124 genmsg_java.write_class(s, spec.response, {'ServerMD5Sum': '"%s"' % md5sum, 00125 'DataType': response_data_type}, True) 00126 s.write('\n} //class\n') 00127 00128 write_end(s, spec) 00129 00130 if output_base_path: 00131 output_dir = '%s/ros/pkg/%s/srv'%(output_base_path, package) 00132 else: 00133 output_dir = '%s/srv_gen/java/ros/pkg/%s/srv'%(package_dir, package) 00134 00135 if (not os.path.exists(output_dir)): 00136 # if we're being run concurrently, the above test can report false but os.makedirs can still fail if 00137 # another copy just created the directory 00138 try: 00139 os.makedirs(output_dir) 00140 except OSError, e: 00141 pass 00142 00143 f = open('%s/%s.java'%(output_dir, spec.short_name), 'w') 00144 print >> f, s.getvalue() 00145 00146 s.close() 00147 00148 def generate_services(argv): 00149 if not os.path.exists(argv[-1]) or os.path.isdir(argv[-1]): 00150 for arg in argv[1:-1]: 00151 generate(arg, argv[-1]) 00152 else: 00153 for arg in argv[1:]: 00154 generate(arg) 00155 00156 if __name__ == "__main__": 00157 roslib.msgs.set_verbose(False) 00158 generate_services(sys.argv) 00159