public void onSuccess(Void value) { mqtt.tracer.debug("Restoring MQTT connection state"); // Setup a new overflow so that the replay can be sent out before the original overflow list. LinkedList<Request> originalOverflow = overflow; Map<Short, Request> originalRequests = requests; overflow = new LinkedList<Request>(); requests = new ConcurrentHashMap<Short, Request>(); // Restore any active subscriptions. if (!activeSubs.isEmpty()) { ArrayList<Topic> topics = new ArrayList<Topic>(activeSubs.size()); for (Map.Entry<UTF8Buffer, QoS> entry : activeSubs.entrySet()) { topics.add(new Topic(entry.getKey(), entry.getValue())); } 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) { // Stuff in the overflow never got sent out.. so no need to set the dup flag send(request); } }
@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); }
Map<Long, Topic> topicsById = new HashMap<>(); while (rs.next()) { Long topicId = rs.getLong(1); String topicName = rs.getString(2); Long subTopicId = rs.getLong(3); String subTopicName = rs.getString(4); Topic topic = topicsById.get(topicId); if (topic == null) { topic = new Topic(topicId, topicName); topicsById.put(topicId, topic); } topic.addSubTopic(new SubTopic(subTopicId, subTopicName); } Collection<Topic> allTopics = topicsById.values();
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"); } }
@Override public Option<BindAddress> apply(UTF8Buffer topicName) { Tuple2<Topic, BindAddress> removed = session_state.subscriptions.remove(topicName); if (removed != null) { Topic topic = removed._1(); BindAddress address = removed._2(); mqtt_consumer().addresses.remove(address); if (PathParser.containsWildCards(address.path())) { mqtt_consumer().wildcards.remove(address.path(), topic.qos()); } return Scala2Java.some(address); } else { return Scala2Java.none(); } } });
@Test(timeout = 60 * 1000) public void testNoMessageReceivedAfterUnsubscribeMQTT() throws Exception { Topic[] topics = {new Topic("TopicA", QoS.EXACTLY_ONCE)}; connectionPub.publish(topics[0].name().toString(), payload.getBytes(), QoS.EXACTLY_ONCE, false); connectionPub.publish(topics[0].name().toString(), payload.getBytes(), QoS.EXACTLY_ONCE, false);
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"); } }
private void connectMqtt() throws Exception { String clientId = this.topologyName + "-" + this.context.getThisComponentId() + "-" + this.context.getThisTaskId(); MQTT client = MqttUtils.configureClient(this.options, clientId, this.keyStoreLoader); this.connection = client.callbackConnection(); this.connection.listener(this); this.connection.connect(new ConnectCallback()); while (!this.mqttConnected && !this.mqttConnectFailed) { LOG.info("Waiting for connection..."); Thread.sleep(500); } if (this.mqttConnected) { List<String> topicList = this.options.getTopics(); Topic[] topics = new Topic[topicList.size()]; QoS qos = MqttUtils.qosFromInt(this.options.getQos()); for (int i = 0; i < topicList.size(); i++) { topics[i] = new Topic(topicList.get(i), qos); } connection.subscribe(topics, new SubscribeCallback()); } }
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"); } }
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 SUBSCRIBE decode(MQTTFrame frame) throws ProtocolException { assert(frame.buffers.length == 1); header(frame.header()); DataByteArrayInputStream is = new DataByteArrayInputStream(frame.buffers[0]); QoS qos = qos(); if(qos != QoS.AT_MOST_ONCE) { messageId = is.readShort(); } ArrayList<Topic> list = new ArrayList<Topic>(); while(is.available() > 0) { Topic topic = new Topic(MessageSupport.readUTF(is), QoS.values()[is.readByte()]); list.add(topic); } topics = list.toArray(new Topic[list.size()]); return this; }
main.topics.add(new Topic(shift(argl), qos)); } else { stderr("Invalid usage: unknown option: " + arg);
@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; } });
m_subscriber = m_mqtt.blockingConnection(); m_subscriber.connect(); Topic[] topics = new Topic[]{new Topic(willTestamentTopic, QoS.AT_MOST_ONCE)}; m_subscriber.subscribe(topics);
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); } } }