public void resendNotAckedPublishes() { final Session session = sessionRegistry.retrieve(getClientId()); session.resendInflightNotAcked(); }
public void writabilityChanged() { if (channel.isWritable()) { LOG.debug("Channel {} is again writable", channel); final Session session = sessionRegistry.retrieve(getClientId()); session.writabilityChanged(); } }
private void processPubComp(MqttMessage msg) { final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); final Session session = sessionRegistry.retrieve(getClientId()); session.processPubComp(messageID); }
private void processPubRec(MqttMessage msg) { final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); final Session session = sessionRegistry.retrieve(getClientId()); session.processPubRec(messageID); }
private void processPubAck(MqttMessage msg) { final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); Session session = sessionRegistry.retrieve(getClientId()); session.pubAckReceived(messageID); }
void sendPublish(MqttPublishMessage publishMsg) { final int packetId = publishMsg.variableHeader().packetId(); final String topicName = publishMsg.variableHeader().topicName(); final String clientId = getClientId(); MqttQoS qos = publishMsg.fixedHeader().qosLevel(); if (LOG.isTraceEnabled()) { LOG.trace("Sending PUBLISH({}) message. MessageId={}, CId={}, topic={}, payload={}", qos, packetId, clientId, topicName, DebugUtils.payload2Str(publishMsg.payload())); } else { LOG.debug("Sending PUBLISH({}) message. MessageId={}, CId={}, topic={}", qos, packetId, clientId, topicName); } sendIfWritableElseDrop(publishMsg); }
private void processPubRel(MqttMessage msg) { final Session session = sessionRegistry.retrieve(getClientId()); final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); session.receivedPubRelQos2(messageID); sendPubCompMessage(messageID); }
/** * First phase of a publish QoS2 protocol, sent by publisher to the broker. Publish to all interested * subscribers. */ void receivedPublishQos2(MQTTConnection connection, MqttPublishMessage mqttPublishMessage, String username) { LOG.trace("Processing PUBREL message on connection: {}", connection); final Topic topic = new Topic(mqttPublishMessage.variableHeader().topicName()); final ByteBuf payload = mqttPublishMessage.payload(); final String clientId = connection.getClientId(); if (!authorizator.canWrite(topic, username, clientId)) { LOG.error("MQTT client is not authorized to publish on topic. CId={}, topic: {}", clientId, topic); return; } publish2Subscribers(payload, topic, EXACTLY_ONCE); final boolean retained = mqttPublishMessage.fixedHeader().isRetain(); if (retained) { if (!payload.isReadable()) { retainedRepository.cleanRetained(topic); } else { // before wasn't stored retainedRepository.retain(topic, mqttPublishMessage); } } String clientID = connection.getClientId(); interceptor.notifyTopicPublished(mqttPublishMessage, clientID, username); }
void receivedPublishQos1(MQTTConnection connection, Topic topic, String username, ByteBuf payload, int messageID, boolean retain, MqttPublishMessage msg) { // verify if topic can be write topic.getTokens(); if (!topic.isValid()) { LOG.warn("Invalid topic format, force close the connection"); connection.dropConnection(); return; } final String clientId = connection.getClientId(); if (!authorizator.canWrite(topic, username, clientId)) { LOG.error("MQTT client: {} is not authorized to publish on topic: {}", clientId, topic); return; } publish2Subscribers(payload, topic, AT_LEAST_ONCE); connection.sendPubAck(messageID); if (retain) { if (!payload.isReadable()) { retainedRepository.cleanRetained(topic); } else { // before wasn't stored retainedRepository.retain(topic, msg); } } interceptor.notifyTopicPublished(msg, clientId, username); }
public void unsubscribe(List<String> topics, MQTTConnection mqttConnection, int messageId) { final String clientID = mqttConnection.getClientId(); for (String t : topics) { Topic topic = new Topic(t); boolean validTopic = topic.isValid(); if (!validTopic) { // close the connection, not valid topicFilter is a protocol violation mqttConnection.dropConnection(); LOG.warn("Topic filter is not valid. CId={}, topics: {}, offending topic filter: {}", clientID, topics, topic); return; } LOG.trace("Removing subscription. CId={}, topic={}", clientID, topic); subscriptions.removeSubscription(topic, clientID); // TODO remove the subscriptions to Session // clientSession.unsubscribeFrom(topic); String username = NettyUtils.userName(mqttConnection.channel); interceptor.notifyTopicUnsubscribed(topic.toString(), clientID, username); } // ack the client mqttConnection.sendUnsubAckMessage(topics, clientID, messageId); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); //assertTrue(matchedSubscriptions.size() >=1); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); // assertTrue(matchedSubscriptions.contains(expectedSubscription)); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
final String username = NettyUtils.userName(channel); final String topicName = msg.variableHeader().topicName(); final String clientId = getClientId(); LOG.trace("Processing PUBLISH message. CId={}, topic: {}, messageId: {}, qos: {}", clientId, topicName, msg.variableHeader().packetId(), qos);
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
@Test public void testZeroByteClientIdWithCleanSession() { // Connect message with clean session set to true and client id is null. MqttConnectMessage msg = MqttMessageBuilders.connect() .protocolVersion(MqttVersion.MQTT_3_1_1) .clientId(null) .cleanSession(true) .build(); sut.processConnect(msg); assertEqualsConnAck("Connection must be accepted", CONNECTION_ACCEPTED, channel.readOutbound()); assertNotNull("unique clientid must be generated", sut.getClientId()); assertTrue("clean session flag must be true", sessionRegistry.retrieve(sut.getClientId()).isClean()); assertTrue("Connection must be open", channel.isOpen()); }
@Test public void cleanRetainedMessageStoreWhenPublishWithRetainedQos0IsReceived() { connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); // publish a QoS1 retained message final ByteBuf anyPayload = Unpooled.copiedBuffer("Any payload", Charset.defaultCharset()); final MqttPublishMessage publishMsg = MqttMessageBuilders.publish() .payload(Unpooled.copiedBuffer("Any payload", Charset.defaultCharset())) .qos(MqttQoS.AT_LEAST_ONCE) .retained(true) .topicName(NEWS_TOPIC) .build(); sut.receivedPublishQos1(connection, new Topic(NEWS_TOPIC), TEST_USER, anyPayload, 1, true, publishMsg); assertMessageIsRetained(NEWS_TOPIC, anyPayload); // publish a QoS0 retained message // Exercise final ByteBuf qos0Payload = Unpooled.copiedBuffer("QoS0 payload", Charset.defaultCharset()); sut.receivedPublishQos0(new Topic(NEWS_TOPIC), TEST_USER, connection.getClientId(), qos0Payload, true, MqttMessageBuilders.publish() .payload(qos0Payload) .qos(MqttQoS.AT_MOST_ONCE) .retained(false) .topicName(NEWS_TOPIC).build()); // Verify assertTrue("Retained message for topic /news must be cleared", retainedRepository.isEmpty()); }