@Override public void onSuccess(byte[] value) { for (Topic topic : topics) { activeSubs.put(topic.name(), topic.qos()); } if(next!=null) { next.onSuccess(value); } } });
public MQTTFrame encode() { try { DataByteArrayOutputStream os = new DataByteArrayOutputStream(); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { os.writeShort(messageId); } for(Topic topic: topics) { MessageSupport.writeUTF(os, topic.name()); os.writeByte(topic.qos().ordinal()); } MQTTFrame frame = new MQTTFrame(); frame.header(header()); frame.commandType(TYPE); return frame.buffer(os.toBuffer()); } catch (IOException e) { throw new RuntimeException("The impossible happened"); } }
@Override public void onSuccess(byte[] value) { for (Topic topic : topics) { activeSubs.put(topic.name(), topic.qos()); } if(next!=null) { next.onSuccess(value); } } });
@Override public BindAddress apply(Topic topic) { BindAddress address = decode_destination(topic.name()); session_state.subscriptions.put(topic.name(), new Tuple2<Topic, BindAddress>(topic, address)); mqtt_consumer().addresses.put(address, topic.qos()); if (PathParser.containsWildCards(address.path())) { mqtt_consumer().wildcards.put(address.path(), topic.qos()); } return address; } });
private byte onSubscribe(final Topic topic) throws MQTTProtocolException { final String destinationName = topic.name().toString(); final QoS requestedQoS = topic.qos(); final MQTTSubscription mqttSubscription = mqttSubscriptionByTopic.get(destinationName); if (mqttSubscription != null) { if (requestedQoS != mqttSubscription.getQoS()) { // remove old subscription as the QoS has changed onUnSubscribe(destinationName); } else { return (byte) requestedQoS.ordinal(); } } try { return onSubscribe(destinationName, requestedQoS); } catch (IOException e) { throw new MQTTProtocolException("Failed while intercepting subscribe", true, e); } } }
private byte onSubscribe(final Topic topic) throws MQTTProtocolException { final String destinationName = topic.name().toString(); final QoS requestedQoS = topic.qos(); final MQTTSubscription mqttSubscription = mqttSubscriptionByTopic.get(destinationName); if (mqttSubscription != null) { if (requestedQoS != mqttSubscription.getQoS()) { // remove old subscription as the QoS has changed onUnSubscribe(destinationName); } else { return (byte) requestedQoS.ordinal(); } } try { return onSubscribe(destinationName, requestedQoS); } catch (IOException e) { throw new MQTTProtocolException("Failed while intercepting subscribe", true, e); } } }
@Override public byte onSubscribe(final Topic topic) throws MQTTProtocolException { final String destinationName = topic.name().toString(); final QoS requestedQoS = topic.qos(); final MQTTSubscription mqttSubscription = mqttSubscriptionByTopic.get(destinationName); if (mqttSubscription != null) { if (requestedQoS != mqttSubscription.getQoS()) { // remove old subscription as the QoS has changed onUnSubscribe(destinationName); } else { try { onReSubscribe(mqttSubscription); } catch (IOException e) { throw new MQTTProtocolException("Failed to find subscription strategy", true, e); } return (byte) requestedQoS.ordinal(); } } try { return onSubscribe(destinationName, requestedQoS); } catch (IOException e) { throw new MQTTProtocolException("Failed while intercepting subscribe", true, e); } }
@Override public byte onSubscribe(final Topic topic) throws MQTTProtocolException { final String destinationName = topic.name().toString(); final QoS requestedQoS = topic.qos(); final MQTTSubscription mqttSubscription = mqttSubscriptionByTopic.get(destinationName); if (mqttSubscription != null) { if (requestedQoS != mqttSubscription.getQoS()) { // remove old subscription as the QoS has changed onUnSubscribe(destinationName); } else { try { onReSubscribe(mqttSubscription); } catch (IOException e) { throw new MQTTProtocolException("Failed to find subscription strategy", true, e); } return (byte) requestedQoS.ordinal(); } } try { return onSubscribe(destinationName, requestedQoS); } catch (IOException e) { throw new MQTTProtocolException("Failed while intercepting subscribe", true, e); } }
@Override public byte onSubscribe(final Topic topic) throws MQTTProtocolException { final String destinationName = topic.name().toString(); final QoS requestedQoS = topic.qos(); final MQTTSubscription mqttSubscription = mqttSubscriptionByTopic.get(destinationName); if (mqttSubscription != null) { if (requestedQoS != mqttSubscription.getQoS()) { // remove old subscription as the QoS has changed onUnSubscribe(destinationName); } else { try { onReSubscribe(mqttSubscription); } catch (IOException e) { throw new MQTTProtocolException("Failed to find subscription strategy", true, e); } return (byte) requestedQoS.ordinal(); } } try { return onSubscribe(destinationName, requestedQoS); } catch (IOException e) { throw new MQTTProtocolException("Failed while intercepting subscribe", true, e); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { MQTTProtocolSupport.validate(topics[i].name().toString()); try { qos[i] = findSubscriptionStrategy().onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); throw new MQTTProtocolException("SUBSCRIBE command received with no topic filter"); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { MQTTProtocolSupport.validate(topics[i].name().toString()); try { qos[i] = findSubscriptionStrategy().onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); throw new MQTTProtocolException("SUBSCRIBE command received with no topic filter"); } }
void onSubscribe(SUBSCRIBE command) throws MQTTProtocolException { checkConnected(); LOG.trace("MQTT SUBSCRIBE message:{} client:{} connection:{}", command.messageId(), clientId, connectionInfo.getConnectionId()); Topic[] topics = command.topics(); if (topics != null) { byte[] qos = new byte[topics.length]; for (int i = 0; i < topics.length; i++) { MQTTProtocolSupport.validate(topics[i].name().toString()); try { qos[i] = findSubscriptionStrategy().onSubscribe(topics[i]); } catch (IOException e) { throw new MQTTProtocolException("Failed to process subscription request", true, e); } } SUBACK ack = new SUBACK(); ack.messageId(command.messageId()); ack.grantedQos(qos); try { getMQTTTransport().sendToMQTT(ack.encode()); } catch (IOException e) { LOG.warn("Couldn't send SUBACK for " + command, e); } } else { LOG.warn("No topics defined for Subscription " + command); throw new MQTTProtocolException("SUBSCRIBE command received with no topic filter"); } }
public MQTTFrame encode() { try { DataByteArrayOutputStream os = new DataByteArrayOutputStream(); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { os.writeShort(messageId); } for(Topic topic: topics) { MessageSupport.writeUTF(os, topic.name()); os.writeByte(topic.qos().ordinal()); } MQTTFrame frame = new MQTTFrame(); frame.header(header()); frame.commandType(TYPE); return frame.buffer(os.toBuffer()); } catch (IOException e) { throw new RuntimeException("The impossible happened"); } }
BindAddress address = entry._2(); TopicPB.Bean topic_pb = new TopicPB.Bean(); topic_pb.setName(topic.name()); topic_pb.setQos(topic.qos().ordinal()); topic_pb.setAddress(new UTF8Buffer(address.toString()));
@Test(timeout = 30 * 10000) public void testSubscribeMultipleTopics() throws Exception { byte[] payload = new byte[1024 * 32]; for (int i = 0; i < payload.length; i++) { payload[i] = '2'; } MQTT mqtt = createMQTTConnection(); mqtt.setClientId("MQTT-Client"); mqtt.setCleanSession(false); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); Topic[] topics = {new Topic("Topic/A", QoS.EXACTLY_ONCE), new Topic("Topic/B", QoS.EXACTLY_ONCE)}; Topic[] wildcardTopic = {new Topic("Topic/#", QoS.AT_LEAST_ONCE)}; connection.subscribe(wildcardTopic); for (Topic topic : topics) { connection.publish(topic.name().toString(), payload, QoS.AT_LEAST_ONCE, false); } int received = 0; for (int i = 0; i < topics.length; ++i) { Message message = connection.receive(); assertNotNull(message); received++; payload = message.getPayload(); String messageContent = new String(payload); LOG.info("Received message from topic: " + message.getTopic() + " Message content: " + messageContent); message.ack(); } assertEquals("Should have received " + topics.length + " messages", topics.length, received); }