001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.transport.mqtt;
018    
019    import java.io.DataInput;
020    import java.io.DataInputStream;
021    import java.io.DataOutput;
022    import java.io.DataOutputStream;
023    import java.io.IOException;
024    
025    import org.apache.activemq.util.ByteArrayInputStream;
026    import org.apache.activemq.util.ByteArrayOutputStream;
027    import org.apache.activemq.util.ByteSequence;
028    import org.apache.activemq.wireformat.WireFormat;
029    import org.fusesource.hawtbuf.Buffer;
030    import org.fusesource.mqtt.codec.MQTTFrame;
031    
032    /**
033     * Implements marshalling and unmarsalling the <a
034     * href="http://mqtt.org/">MQTT</a> protocol.
035     */
036    public class MQTTWireFormat implements WireFormat {
037    
038        static final int MAX_MESSAGE_LENGTH = 1024 * 1024 * 256;
039    
040        private int version = 1;
041    
042        public ByteSequence marshal(Object command) throws IOException {
043            ByteArrayOutputStream baos = new ByteArrayOutputStream();
044            DataOutputStream dos = new DataOutputStream(baos);
045            marshal(command, dos);
046            dos.close();
047            return baos.toByteSequence();
048        }
049    
050        public Object unmarshal(ByteSequence packet) throws IOException {
051            ByteArrayInputStream stream = new ByteArrayInputStream(packet);
052            DataInputStream dis = new DataInputStream(stream);
053            return unmarshal(dis);
054        }
055    
056        public void marshal(Object command, DataOutput dataOut) throws IOException {
057            MQTTFrame frame = (MQTTFrame) command;
058            dataOut.write(frame.header());
059    
060            int remaining = 0;
061            for (Buffer buffer : frame.buffers) {
062                remaining += buffer.length;
063            }
064            do {
065                byte digit = (byte) (remaining & 0x7F);
066                remaining >>>= 7;
067                if (remaining > 0) {
068                    digit |= 0x80;
069                }
070                dataOut.write(digit);
071            } while (remaining > 0);
072            for (Buffer buffer : frame.buffers) {
073                dataOut.write(buffer.data, buffer.offset, buffer.length);
074            }
075        }
076    
077        public Object unmarshal(DataInput dataIn) throws IOException {
078            byte header = dataIn.readByte();
079    
080            byte digit;
081            int multiplier = 1;
082            int length = 0;
083            do {
084                digit = dataIn.readByte();
085                length += (digit & 0x7F) * multiplier;
086                multiplier <<= 7;
087            }
088            while ((digit & 0x80) != 0);
089    
090            if (length >= 0) {
091                if (length > MAX_MESSAGE_LENGTH) {
092                    throw new IOException("The maximum message length was exceeded");
093                }
094    
095                if (length > 0) {
096                    byte[] data = new byte[length];
097                    dataIn.readFully(data);
098                    Buffer body = new Buffer(data);
099                    return new MQTTFrame(body).header(header);
100                } else {
101                    return new MQTTFrame().header(header);
102                }
103            }
104            return null;
105        }
106    
107        /**
108         * @param the version of the wire format
109         */
110        public void setVersion(int version) {
111            this.version = version;
112        }
113    
114        /**
115         * @return the version of the wire format
116         */
117        public int getVersion() {
118            return this.version;
119        }
120    }