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 */
017package org.apache.activemq.transport.mqtt;
018
019import java.io.DataInput;
020import java.io.DataInputStream;
021import java.io.DataOutput;
022import java.io.DataOutputStream;
023import java.io.IOException;
024
025import org.apache.activemq.util.ByteArrayInputStream;
026import org.apache.activemq.util.ByteArrayOutputStream;
027import org.apache.activemq.util.ByteSequence;
028import org.apache.activemq.wireformat.WireFormat;
029import org.fusesource.hawtbuf.Buffer;
030import org.fusesource.mqtt.codec.MQTTFrame;
031
032/**
033 * Implements marshalling and unmarsalling the <a
034 * href="http://mqtt.org/">MQTT</a> protocol.
035 */
036public class MQTTWireFormat implements WireFormat {
037
038    static final int MAX_MESSAGE_LENGTH = 1024 * 1024 * 256;
039    static final long DEFAULT_CONNECTION_TIMEOUT = 30000L;
040
041    private int version = 1;
042
043    private int maxFrameSize = MAX_MESSAGE_LENGTH;
044
045    @Override
046    public ByteSequence marshal(Object command) throws IOException {
047        ByteArrayOutputStream baos = new ByteArrayOutputStream();
048        DataOutputStream dos = new DataOutputStream(baos);
049        marshal(command, dos);
050        dos.close();
051        return baos.toByteSequence();
052    }
053
054    @Override
055    public Object unmarshal(ByteSequence packet) throws IOException {
056        ByteArrayInputStream stream = new ByteArrayInputStream(packet);
057        DataInputStream dis = new DataInputStream(stream);
058        return unmarshal(dis);
059    }
060
061    @Override
062    public void marshal(Object command, DataOutput dataOut) throws IOException {
063        MQTTFrame frame = (MQTTFrame) command;
064        dataOut.write(frame.header());
065
066        int remaining = 0;
067        for (Buffer buffer : frame.buffers) {
068            remaining += buffer.length;
069        }
070        do {
071            byte digit = (byte) (remaining & 0x7F);
072            remaining >>>= 7;
073            if (remaining > 0) {
074                digit |= 0x80;
075            }
076            dataOut.write(digit);
077        } while (remaining > 0);
078        for (Buffer buffer : frame.buffers) {
079            dataOut.write(buffer.data, buffer.offset, buffer.length);
080        }
081    }
082
083    @Override
084    public Object unmarshal(DataInput dataIn) throws IOException {
085        byte header = dataIn.readByte();
086
087        byte digit;
088        int multiplier = 1;
089        int length = 0;
090        do {
091            digit = dataIn.readByte();
092            length += (digit & 0x7F) * multiplier;
093            multiplier <<= 7;
094        }
095        while ((digit & 0x80) != 0);
096
097        if (length >= 0) {
098            if (length > getMaxFrameSize()) {
099                throw new IOException("The maximum message length was exceeded");
100            }
101
102            if (length > 0) {
103                byte[] data = new byte[length];
104                dataIn.readFully(data);
105                Buffer body = new Buffer(data);
106                return new MQTTFrame(body).header(header);
107            } else {
108                return new MQTTFrame().header(header);
109            }
110        }
111        return null;
112    }
113
114    /**
115     * @param the version of the wire format
116     */
117    @Override
118    public void setVersion(int version) {
119        this.version = version;
120    }
121
122    /**
123     * @return the version of the wire format
124     */
125    @Override
126    public int getVersion() {
127        return this.version;
128    }
129
130    /**
131     * @return the maximum number of bytes a single MQTT message frame is allowed to be.
132     */
133    public int getMaxFrameSize() {
134        return maxFrameSize;
135    }
136
137    /**
138     * Sets the maximum frame size for an incoming MQTT frame.  The protocl limit is
139     * 256 megabytes and this value cannot be set higher.
140     *
141     * @param maxFrameSize
142     *        the maximum allowed frame size for a single MQTT frame.
143     */
144    public void setMaxFrameSize(int maxFrameSize) {
145        this.maxFrameSize = Math.min(MAX_MESSAGE_LENGTH, maxFrameSize);
146    }
147}