Refine search
private void processFrame(MQTTFrame frame) { try { switch(frame.messageType()) { case PUBLISH.TYPE: { PUBLISH publish = new PUBLISH().decode(frame); toReceiver(publish); break; throw new ProtocolException("Unexpected MQTT command type: "+frame.messageType());
public void publish(UTF8Buffer topic, Buffer payload, QoS qos, boolean retain, Callback<Void> cb) { queue.assertExecuting(); if( disconnected ) { cb.onFailure(createDisconnectedError()); return; } PUBLISH command = new PUBLISH().qos(qos).retain(retain); command.topicName(topic).payload(payload); send(command, cb); }
public MQTTFrame encode() { try { DataByteArrayOutputStream os = new DataByteArrayOutputStream(); MessageSupport.writeUTF(os, topicName); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { os.writeShort(messageId); } MQTTFrame frame = new MQTTFrame(); frame.header(header()); frame.commandType(TYPE); if(payload!=null && payload.length!=0) { os.write(payload); } frame.buffer(os.toBuffer()); return frame; } catch (IOException e) { throw new RuntimeException("The impossible happened"); } }
ActiveMQMessage convertMessage(PUBLISH command) throws JMSException { ActiveMQBytesMessage msg = new ActiveMQBytesMessage(); msg.setProducerId(producerId); MessageId id = new MessageId(producerId, publisherIdGenerator.getNextSequenceId()); msg.setMessageId(id); LOG.trace("MQTT-->ActiveMQ: MQTT_MSGID:{} client:{} connection:{} ActiveMQ_MSGID:{}", command.messageId(), clientId, connectionInfo.getConnectionId(), msg.getMessageId()); msg.setTimestamp(System.currentTimeMillis()); msg.setPriority((byte) Message.DEFAULT_PRIORITY); msg.setPersistent(command.qos() != QoS.AT_MOST_ONCE && !command.retain()); msg.setIntProperty(QOS_PROPERTY_NAME, command.qos().ordinal()); if (command.retain()) { msg.setBooleanProperty(RETAIN_PROPERTY, true); } ActiveMQDestination destination; synchronized (activeMQDestinationMap) { destination = activeMQDestinationMap.get(command.topicName().toString()); if (destination == null) { String topicName = MQTTProtocolSupport.convertMQTTToActiveMQ(command.topicName().toString()); destination = new ActiveMQTopic(topicName); activeMQDestinationMap.put(command.topicName().toString(), destination); } } msg.setJMSDestination(destination); msg.writeBytes(command.payload().data, command.payload().offset, command.payload().length); return msg; }
public void onTransportError() { if (connect != null) { if (connected.get()) { if (connect.willTopic() != null && connect.willMessage() != null && !willSent) { willSent = true; try { PUBLISH publish = new PUBLISH(); publish.topicName(connect.willTopic()); publish.qos(connect.willQos()); publish.messageId(packetIdGenerator.getNextSequenceId(getClientId())); publish.payload(connect.willMessage()); publish.retain(connect.willRetain()); ActiveMQMessage message = convertMessage(publish); message.setProducerId(producerId); message.onSend(); sendToActiveMQ(message, null); } catch (Exception e) { LOG.warn("Failed to publish Will Message " + connect.willMessage()); } } // remove connection info sendToActiveMQ(connectionInfo.createRemoveCommand(), null); } } }
MessageAck ack = sub.createMessageAck(md); PUBLISH publish = sub.createPublish((ActiveMQMessage) md.getMessage()); switch (publish.qos()) { case AT_LEAST_ONCE: case EXACTLY_ONCE: publish.dup(publish.dup() || md.getMessage().isRedelivered()); case AT_MOST_ONCE: consumerAcks.put(publish.messageId(), ack); publish.messageId(), clientId, connectionInfo.getConnectionId()); getMQTTTransport().sendToMQTT(publish.encode()); if (ack != null && !sub.expectAck(publish)) { getMQTTTransport().sendToActiveMQ(ack);
@Override public void onResponse(MQTTProtocolConverter converter, Response response) throws IOException { if (response.isException()) { LOG.warn("Failed to send MQTT Publish: ", command, ((ExceptionResponse) response).getException()); } else { PUBREC ack = new PUBREC(); ack.messageId(command.messageId()); synchronized (publisherRecs) { publisherRecs.put(command.messageId(), ack); } LOG.trace("MQTT Snd PUBACK message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); converter.getMQTTTransport().sendToMQTT(ack.encode()); } } };
@Override public void onReceive(MQTTFrame frame) { LOG.info("Client received:\n" + frame); if (frame.messageType() == PUBLISH.TYPE) { PUBLISH publish = new PUBLISH(); try { publish.decode(frame); LOG.info("PUBLISH " + publish); } catch (ProtocolException e) { fail("Error decoding publish " + e.getMessage()); } if (publishMap.get(publish.messageId()) != null) { assertTrue(publish.dup()); } publishMap.put(publish.messageId(), publish); } }
return Scala2Java.none(); } else { PUBLISH publish = new PUBLISH(); publish.topicName(new UTF8Buffer(destination_parser.encode_destination(delivery.sender().head()))); if (delivery.redeliveries() > 0) { publish.dup(true); publish.payload(((RawMessage) delivery.message()).payload()); } else { if (publish_body) { try { publish.payload(delivery.message().getBodyAs(Buffer.class)); } catch (FilterException e) { log.error(e, "Internal Server Error: Could not covert message body to a Buffer"); publish.payload(delivery.message().encoded()); publish.qos(qos); short id = to_message_id(clean_session ? publish.messageId(id); Request request = new Request(id, publish, ack); Request prev = in_flight_publishes.put(id, request); publish.qos(QoS.AT_MOST_ONCE); return Scala2Java.some(new Request((short) 0, publish, ack));
@Override public void onResponse(MQTTProtocolConverter converter, Response response) throws IOException { if (response.isException()) { Throwable error = ((ExceptionResponse) response).getException(); LOG.warn("Failed to send MQTT Publish: ", command, error.getMessage()); LOG.trace("Error trace: {}", error); } switch (command.qos()) { case AT_LEAST_ONCE: PUBACK ack = new PUBACK(); ack.messageId(command.messageId()); LOG.trace("MQTT Snd PUBACK message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); converter.getMQTTTransport().sendToMQTT(ack.encode()); break; case EXACTLY_ONCE: PUBREC req = new PUBREC(); req.messageId(command.messageId()); synchronized (publisherRecs) { publisherRecs.put(command.messageId(), req); } LOG.trace("MQTT Snd PUBREC message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); converter.getMQTTTransport().sendToMQTT(req.encode()); break; default: break; } } };
void onMQTTPublish(PUBLISH command) throws IOException, JMSException { checkConnected(); LOG.trace("MQTT Rcv PUBLISH message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); //Both version 3.1 and 3.1.1 do not allow the topic name to contain a wildcard in the publish packet if (containsMqttWildcard(command.topicName().toString())) { // [MQTT-3.3.2-2]: The Topic Name in the PUBLISH Packet MUST NOT contain wildcard characters getMQTTTransport().onException(IOExceptionSupport.create("The topic name must not contain wildcard characters.", null)); return; } ActiveMQMessage message = convertMessage(command); message.setProducerId(producerId); message.onSend(); sendToActiveMQ(message, createResponseHandler(command)); }
static private String toString(MQTTFrame frame) { if( frame == null ) return null; try { switch (frame.messageType()) { case PINGREQ.TYPE: return new PINGREQ().decode(frame).toString(); case PINGRESP.TYPE: return new PINGRESP().decode(frame).toString(); case CONNECT.TYPE: return new CONNECT().decode(frame).toString(); case DISCONNECT.TYPE: return new DISCONNECT().decode(frame).toString(); case SUBSCRIBE.TYPE: return new SUBSCRIBE().decode(frame).toString(); case UNSUBSCRIBE.TYPE: return new UNSUBSCRIBE().decode(frame).toString(); case PUBLISH.TYPE: return new PUBLISH().decode(frame).toString(); case PUBACK.TYPE: return new PUBACK().decode(frame).toString(); case PUBREC.TYPE: return new PUBREC().decode(frame).toString(); case PUBREL.TYPE: return new PUBREL().decode(frame).toString(); case PUBCOMP.TYPE: return new PUBCOMP().decode(frame).toString(); case CONNACK.TYPE: return new CONNACK().decode(frame).toString(); case SUBACK.TYPE: return new SUBACK().decode(frame).toString(); default: return frame.toString(); } } catch (Throwable e) { LOG.warn(e.getMessage(), e); return frame.toString(); } }
short setPacketId(MQTTSubscription subscription, ActiveMQMessage message, PUBLISH publish) { // subscription key final StringBuilder subscriptionKey = new StringBuilder(); subscriptionKey.append(subscription.getConsumerInfo().getDestination().getPhysicalName()) .append(':').append(message.getJMSMessageID()); final String keyStr = subscriptionKey.toString(); Short packetId; synchronized (activemqToPacketIds) { packetId = activemqToPacketIds.get(keyStr); if (packetId == null) { packetId = getNextSequenceId(); activemqToPacketIds.put(keyStr, packetId); packetIdsToActivemq.put(packetId, keyStr); } else { // mark publish as duplicate! publish.dup(true); } } publish.messageId(packetId); return packetId; }
public void on_mqtt_publish(final PUBLISH publish) { if ((publish.qos() == QoS.EXACTLY_ONCE) && session_state.received_message_ids.contains(publish.messageId())) { PUBREC response = new PUBREC(); response.messageId(publish.messageId()); send(response); return; MqttProducerRoute route = producerRoutes.get(publish.topicName()); if (route == null) { final SimpleAddress destination = decode_destination(publish.topicName()); final MqttProducerRoute froute = route = new MqttProducerRoute(destination, handler);
public PUBLISH() { qos(QoS.AT_LEAST_ONCE); }
if (publish.qos() == QoS.AT_LEAST_ONCE) { ack = new AtLeastOnceProducerAck(publish); } else if (publish.qos() == QoS.EXACTLY_ONCE) { ack = new ExactlyOnceProducerAck(publish); delivery.message_$eq(new RawMessage(publish.payload())); delivery.persistent_$eq(publish.qos().ordinal() > 0); delivery.size_$eq(publish.payload().length); delivery.ack_$eq(Scala2Java.toScala(ack)); if (publish.retain()) { if (delivery.size() == 0) { delivery.retain_$eq(RetainRemove$.MODULE$);
public PUBLISH decode(MQTTFrame frame) throws ProtocolException { assert(frame.buffers.length == 1); header(frame.header()); DataByteArrayInputStream is = new DataByteArrayInputStream(frame.buffers[0]); topicName = MessageSupport.readUTF(is); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { messageId = is.readShort(); } payload = is.readBuffer(is.available()); if( payload == null ) { payload = new Buffer(0); } return this; }