Message.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2008, 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 // author: Jason Wolfe
00036 
00037 
00038 package ros.communication;
00039 
00040 import java.nio.ByteBuffer;
00041 import java.nio.ByteOrder;
00042 
00043 public abstract class Message implements Cloneable {
00044         public static String __s_getDataType() { throw new UnsupportedOperationException(); }
00045         public static String __s_getMD5Sum() { throw new UnsupportedOperationException(); }
00046         public static String __s_getMessageDefinition() { throw new UnsupportedOperationException(); }  
00047         
00048         public abstract String getDataType();
00049         public abstract String getMD5Sum();
00050         public String getServerMD5Sum() {throw new UnsupportedOperationException();}
00051         public abstract String getMessageDefinition();
00052 
00053         public abstract int    serializationLength();
00054         public abstract void   serialize(ByteBuffer bb, int seq);
00055         public abstract void   deserialize(ByteBuffer bb);
00056 
00057         public byte [] serialize(int seq) {
00058                 int len = serializationLength();
00059                 ByteBuffer bb = ByteBuffer.allocate(len).order(ByteOrder.LITTLE_ENDIAN);
00060                 serialize(bb, seq);
00061                 byte [] ret = bb.array();
00062                 if (ret.length != len) throw new RuntimeException("Non-matching serialization length!");
00063                 //                      System.out.println("Wrote " + ret.length + " bytes: ");
00064                 //                      for(int i = 0; i < ret.length; i++) System.out.format("%x,", ret[i]);
00065                 return ret;
00066         }
00067         
00068         public void deserialize(byte [] data) {
00069                 //                      System.out.println("Read " + data.length + " bytes: ");
00070                 //                      for(int i = 0; i < Math.min(100, data.length); i++) System.out.format("%x,", data[i]);
00071                 ByteBuffer bb = ByteBuffer.wrap(data);
00072                 deserialize(bb.asReadOnlyBuffer().order(ByteOrder.LITTLE_ENDIAN));
00073         }
00074                 
00075         public Message clone() {
00076                 try {
00077                         return (Message) super.clone();
00078                 } catch (CloneNotSupportedException e) {
00079                         throw new RuntimeException("Clone of message not supported?!");
00080                 }
00081         }
00082         
00083         public abstract void setTo(Message m);
00084         
00085         public static class Serialization {
00086                 public static String readString(ByteBuffer bb) {
00087                         int len = bb.getInt();
00088                         byte[]  bytes = new byte[len];
00089                         bb.get(bytes);
00090                         return new String(bytes);
00091                 }
00092 
00093                 public static Time readTime(ByteBuffer bb)  {
00094                         Time t = new Time(bb.getInt(), bb.getInt());
00095                         return t;
00096                 }
00097 
00098                 public static Duration readDuration(ByteBuffer bb)  {
00099                         Duration t = new Duration(bb.getInt(), bb.getInt());
00100                         return t;
00101                 }
00102 
00103                 
00104                 public static void writeString(ByteBuffer bb, String s)  {
00105                         byte [] bytes = s.getBytes();
00106                         bb.putInt(bytes.length);
00107                         bb.put(bytes);
00108                 }
00109                 
00110                 public static void writeTime(ByteBuffer bb, Time o)   {
00111                         bb.putInt(o.secs);
00112                         bb.putInt(o.nsecs);
00113                 }
00114 
00115                 public static void writeDuration(ByteBuffer bb, Duration o)   {
00116                         bb.putInt(o.secs);
00117                         bb.putInt(o.nsecs);
00118                 }
00119         }
00120 }


rosjava_jni
Author(s): Jason Wolfe (jawolfe@willowgarage.com), Nicholas Butko (nbutko@cogsci.ucsd.edu), Lorenz Moesenlechner (moesenle@in.tum.de)
autogenerated on Thu Jan 2 2014 11:07:00