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 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
00064
00065 return ret;
00066 }
00067
00068 public void deserialize(byte [] data) {
00069
00070
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 }