use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.
the class MQTTProtocolHandler method send.
protected void send(int messageId, String topicName, int qosLevel, boolean isRetain, ByteBuf payload, int deliveryCount) {
boolean redelivery = qosLevel == 0 ? false : (deliveryCount > 0);
MqttFixedHeader header = new MqttFixedHeader(MqttMessageType.PUBLISH, redelivery, MqttQoS.valueOf(qosLevel), isRetain, 0);
MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(topicName, messageId);
MqttMessage publish = new MqttPublishMessage(header, varHeader, payload);
sendToClient(publish);
}
use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.
the class ProduceMessageIntegrationTest method test.
@Test
public void test() {
/* handle the CONNECT message */
MqttConnectMessage connectMessage = getConnectMessage();
producerChannel.writeInbound(connectMessage);
MqttConnAckMessage connAckMessage = producerChannel.readOutbound();
Client client = clientManager.get(producerChannel);
Assert.assertNotNull(client);
Assert.assertEquals(producerId, client.getId());
Assert.assertEquals(producerChannel, client.getCtx().channel());
Assert.assertTrue(client.isConnected());
Assert.assertTrue(producerChannel.isOpen());
Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, connAckMessage.variableHeader().connectReturnCode());
producerChannel.releaseInbound();
/* handle the PUBLISH message when there is no online Consumers */
MqttPublishMessage publishMessage = getMqttPublishMessage();
producerChannel.writeInbound(publishMessage);
MqttMessage pubackMessage = producerChannel.readOutbound();
/* qos 0 should have no PUBACK message */
Assert.assertNull(pubackMessage);
/*
* the message would be discarded simply because there is no Consumers
* and the topic should be created
* */
Assert.assertTrue(subscriptionStore.hasTopic(topicName));
producerChannel.releaseInbound();
/* handle the PUBLISH message when there are online Consumers */
Mockito.when(subscriptionStore.get(topicName)).thenReturn(mockedSubscriptions);
publishMessage = getMqttPublishMessage();
byte[] publishMessagePayload = new byte[publishMessage.payload().readableBytes()];
publishMessage.payload().getBytes(0, publishMessagePayload);
producerChannel.writeInbound(publishMessage);
MqttPublishMessage forwardedMessage = consumerChannel.readOutbound();
byte[] forwardedMessagePayload = new byte[forwardedMessage.payload().readableBytes()];
forwardedMessage.payload().getBytes(0, forwardedMessagePayload);
Assert.assertNotNull(forwardedMessage);
Assert.assertEquals(publishMessage.variableHeader().topicName(), forwardedMessage.variableHeader().topicName());
Assert.assertEquals(publishMessage.variableHeader().packetId(), forwardedMessage.variableHeader().packetId());
Assert.assertArrayEquals(publishMessagePayload, forwardedMessagePayload);
}
use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.
the class MqttPingreqMessageHandler method handleMessage.
/**
* handle the PINGREQ message from client
* <ol>
* <li>check client exists</li>
* <li>check client is connected</li>
* <li>generate the PINGRESP message</li>
* <li>send the PINGRESP message to the client</li>
* </ol>
*
* @param message
* @return
*/
@Override
public void handleMessage(Message message) {
Client client = message.getClient();
if (client == null || !client.isConnected()) {
return;
}
MqttMessage pingreqMessage = (MqttMessage) message.getPayload();
MqttMessage pingrespMessage = MessageUtil.getMqttPingrespMessage(pingreqMessage);
client.getCtx().writeAndFlush(pingrespMessage);
}
use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.
the class MessageUtil method getMessage.
public static Message getMessage(MqttMessage mqttMessage) {
Message message = new Message();
message.setPayload(mqttMessage);
switch(mqttMessage.fixedHeader().messageType()) {
case CONNECT:
message.setType(Message.Type.MQTT_CONNECT);
break;
case CONNACK:
message.setType(Message.Type.MQTT_CONNACK);
break;
case PUBLISH:
message.setType(Message.Type.MQTT_PUBLISH);
break;
case PUBACK:
message.setType(Message.Type.MQTT_PUBACK);
break;
case PUBREC:
message.setType(Message.Type.MQTT_PUBREC);
break;
case PUBREL:
message.setType(Message.Type.MQTT_PUBREL);
break;
case PUBCOMP:
message.setType(Message.Type.MQTT_PUBCOMP);
case SUBSCRIBE:
message.setType(Message.Type.MQTT_SUBSCRIBE);
break;
case SUBACK:
message.setType(Message.Type.MQTT_SUBACK);
break;
case UNSUBSCRIBE:
message.setType(Message.Type.MQTT_UNSUBSCRIBE);
break;
case UNSUBACK:
message.setType(Message.Type.MQTT_UNSUBACK);
break;
case PINGREQ:
message.setType(Message.Type.MQTT_PINGREQ);
break;
case PINGRESP:
message.setType(Message.Type.MQTT_PINGRESP);
break;
case DISCONNECT:
message.setType(Message.Type.MQTT_DISCONNECT);
break;
default:
}
return message;
}
use of io.netty.handler.codec.mqtt.MqttMessage in project netty by netty.
the class MqttHeartBeatBrokerHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
MqttMessage mqttMessage = (MqttMessage) msg;
System.out.println("Received MQTT message: " + mqttMessage);
switch(mqttMessage.fixedHeader().messageType()) {
case CONNECT:
MqttFixedHeader connackFixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED, false);
MqttConnAckMessage connack = new MqttConnAckMessage(connackFixedHeader, mqttConnAckVariableHeader);
ctx.writeAndFlush(connack);
break;
case PINGREQ:
MqttFixedHeader pingreqFixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0);
MqttMessage pingResp = new MqttMessage(pingreqFixedHeader);
ctx.writeAndFlush(pingResp);
break;
case DISCONNECT:
ctx.close();
break;
default:
System.out.println("Unexpected message type: " + mqttMessage.fixedHeader().messageType());
ReferenceCountUtil.release(msg);
ctx.close();
}
}
Aggregations