Package org.fusesource.mqtt.codec

Examples of org.fusesource.mqtt.codec.MQTTFrame


        DataInputStream dis = new DataInputStream(stream);
        return unmarshal(dis);
    }

    public void marshal(Object command, DataOutput dataOut) throws IOException {
        MQTTFrame frame = (MQTTFrame) command;
        dataOut.write(frame.header());

        int remaining = 0;
        for (Buffer buffer : frame.buffers) {
            remaining += buffer.length;
        }
View Full Code Here


            if (length > 0) {
                byte[] data = new byte[length];
                dataIn.readFully(data);
                Buffer body = new Buffer(data);
                return new MQTTFrame(body).header(header);
            } else {
                return new MQTTFrame().header(header);
            }
        }
        return null;
    }
View Full Code Here

                        send(new SUBSCRIBE().topics(topics.toArray(new Topic[topics.size()])), null);
                    }

                    // Replay any un-acked requests..
                    for (Map.Entry<Short, Request> entry : originalRequests.entrySet()) {
                        MQTTFrame frame = entry.getValue().frame;
                        frame.dup(true); // set the dup flag as these frames were previously transmitted.
                        send(entry.getValue());
                    }

                    // Replay the original overflow
                    for (Request request : originalOverflow) {
View Full Code Here

            this.transport.suspendRead();
        }
        this.transport.setTransportListener(new DefaultTransportListener() {
            @Override
            public void onTransportCommand(Object command) {
                MQTTFrame frame = (MQTTFrame) command;
                mqtt.tracer.onReceive(frame);
                processFrame(frame);
            }
            @Override
            public void onRefill() {
                onRefillCalled =true;
                drainOverflow();
            }

            @Override
            public void onTransportFailure(IOException error) {
                handleSessionFailure(error);
            }
        });
        pingedAt = 0;
        if(mqtt.getKeepAlive()>0) {
            heartBeatMonitor = new HeartBeatMonitor();
            heartBeatMonitor.setWriteInterval((mqtt.getKeepAlive() * 1000) / 2);
            heartBeatMonitor.setTransport(this.transport);
            heartBeatMonitor.suspendRead(); // to match the suspended state of the transport.
            heartBeatMonitor.setOnKeepAlive(new Task() {
                @Override
                public void run() {
                    // Don't care if the offer is rejected, just means we have data outbound.
                    if(!disconnected && pingedAt==0) {
                        MQTTFrame encoded = new PINGREQ().encode();
                        if(CallbackConnection.this.transport.offer(encoded)) {
                            mqtt.tracer.onSend(encoded);
                            final long now = System.currentTimeMillis();
                            final long suspends = suspendChanges.get();
                            pingedAt = now;
View Full Code Here

        };
       
        // Pop the frame into a request so it we get notified
        // of any failures so we continue to stop the transport.
        if(transport!=null) {
            MQTTFrame frame = new DISCONNECT().encode();
            send(new Request(getNextMessageId(), frame, cb));
        } else {
            cb.onSuccess(null);
        }
    }
View Full Code Here

                    onFailure(error);
                }

                @Override
                public void onTransportCommand(Object command) {
                    MQTTFrame response = (MQTTFrame) command;
                    mqtt.tracer.onReceive(response);
                    try {
                        switch (response.messageType()) {
                            case CONNACK.TYPE:
                                CONNACK connack = new CONNACK().decode(response);
                                switch (connack.code()) {
                                    case CONNECTION_ACCEPTED:
                                        mqtt.tracer.debug("MQTT login accepted");
                                        onSessionEstablished(transport);
                                        cb.onSuccess(null);
                                        listener.onConnected();
                                        queue.execute(new Task() {
                                            @Override
                                            public void run() {
                                                drainOverflow();
                                            }
                                        });
                                        break;
                                    default:
                                        mqtt.tracer.debug("MQTT login rejected");
                                        // Bad creds or something. No point in reconnecting.
                                        transport.stop(NOOP);
                                        cb.onFailure(new IOException("Could not connect: " + connack.code()));
                                }
                                break;
                            default:
                                mqtt.tracer.debug("Received unexpected MQTT frame: %d", response.messageType());
                                // Naughty MQTT server? No point in reconnecting.
                                transport.stop(NOOP);
                                cb.onFailure(new IOException("Could not connect. Received unexpected command: " + response.messageType()));

                        }
                    } catch (ProtocolException e) {
                        mqtt.tracer.debug("Protocol error: %s", e);
                        transport.stop(NOOP);
                        cb.onFailure(e);
                    }
                }
            });
            transport.resumeRead();
            if( mqtt.connect.clientId() == null ) {
                String id = hex(transport.getLocalAddress())+Long.toHexString(System.currentTimeMillis()/1000);
                if(id.length() > 23) {
                    id = id.substring(0,23);
                }
                mqtt.connect.clientId(utf8(id));
            }
            MQTTFrame encoded = mqtt.connect.encode();
            boolean accepted = transport.offer(encoded);
            mqtt.tracer.onSend(encoded);
            mqtt.tracer.debug("Logging in");
            assert accepted: "First frame should always be accepted by the transport";
        }
View Full Code Here

                LOG.warn("While waiting for StompSocket to be properly started, we got interrupted!! Should be okay, but you could see race conditions...");
            }
        }

        try {
            MQTTFrame frame = (MQTTFrame)wireFormat.unmarshal(new ByteSequence(bytes, offset, length));
            protocolConverter.onMQTTCommand(frame);
        } catch (Exception e) {
            onException(IOExceptionSupport.create(e));
        }
    }
View Full Code Here

        } else {
            frameContents = currentBuffer;
            currentBuffer = null;
        }

        MQTTFrame frame = new MQTTFrame(frameContents).header(header);
        frameSink.onFrame(frame);
    }
View Full Code Here

    }

    @Override
    public void onCommand(Object command) {
        try {
            MQTTFrame frame = (MQTTFrame) command;
            if (trace) {
                TRACE.trace("Received: " + toString(frame));
            }
            protocolConverter.onMQTTCommand(frame);
        } catch (IOException e) {
View Full Code Here

                LOG.warn("While waiting for StompSocket to be properly started, we got interrupted!! Should be okay, but you could see race conditions...");
            }
        }

        try {
            MQTTFrame frame = (MQTTFrame)wireFormat.unmarshal(new ByteSequence(bytes, offset, length));
            getProtocolConverter().onMQTTCommand(frame);
        } catch (Exception e) {
            onException(IOExceptionSupport.create(e));
        }
    }
View Full Code Here

TOP

Related Classes of org.fusesource.mqtt.codec.MQTTFrame

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.