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 import roslib; roslib.load_manifest('rosjava_jni')
00040 import genmsg_java
00041
00042 import sys
00043 import os
00044 import traceback
00045
00046
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
00137
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