use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.
the class MQTTProtocolHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
try {
if (stopped) {
disconnect(true);
return;
}
MqttMessage message = (MqttMessage) msg;
// Disconnect if Netty codec failed to decode the stream.
if (message.decoderResult().isFailure()) {
log.debug("Bad Message Disconnecting Client.");
disconnect(true);
return;
}
connection.dataReceived();
MQTTUtil.logMessage(session.getState(), message, true);
this.protocolManager.invokeIncoming(message, this.connection);
switch(message.fixedHeader().messageType()) {
case CONNECT:
handleConnect((MqttConnectMessage) message, ctx);
break;
case PUBLISH:
handlePublish((MqttPublishMessage) message);
break;
case PUBACK:
handlePuback((MqttPubAckMessage) message);
break;
case PUBREC:
handlePubrec(message);
break;
case PUBREL:
handlePubrel(message);
break;
case PUBCOMP:
handlePubcomp(message);
break;
case SUBSCRIBE:
handleSubscribe((MqttSubscribeMessage) message);
break;
case UNSUBSCRIBE:
handleUnsubscribe((MqttUnsubscribeMessage) message);
break;
case PINGREQ:
handlePingreq();
break;
case DISCONNECT:
disconnect(false);
break;
case UNSUBACK:
case SUBACK:
case PINGRESP:
// The server does not instantiate connections therefore any CONNACK received over a connection is an invalid control message.
case CONNACK:
default:
disconnect(true);
}
} catch (Exception e) {
log.debug("Error processing Control Packet, Disconnecting Client", e);
disconnect(true);
} finally {
ReferenceCountUtil.release(msg);
}
}
use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.
the class MQTTProtocolHandler method handlePingreq.
void handlePingreq() {
MqttMessage pingResp = new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0));
sendToClient(pingResp);
}
use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.
the class MQTTInterceptorPropertiesTest method testCheckInterceptedMQTTMessageProperties.
@Test(timeout = 60000)
public void testCheckInterceptedMQTTMessageProperties() throws Exception {
final String addressQueue = name.getMethodName();
final String msgText = "Test intercepted message";
final boolean retained = true;
Map<String, Object> expectedProperties = new ArrayMap<>();
expectedProperties.put(ADDRESS, addressQueue);
expectedProperties.put(MESSAGE_TEXT, msgText);
expectedProperties.put(RETAINED, retained);
final MQTTClientProvider subscribeProvider = getMQTTClientProvider();
initializeConnection(subscribeProvider);
subscribeProvider.subscribe(addressQueue, AT_MOST_ONCE);
final CountDownLatch latch = new CountDownLatch(1);
MQTTInterceptor incomingInterceptor = new MQTTInterceptor() {
@Override
public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
System.out.println("incoming");
if (packet.getClass() == MqttPublishMessage.class) {
return checkMessageProperties(packet, expectedProperties);
} else {
return true;
}
}
};
MQTTInterceptor outgoingInterceptor = new MQTTInterceptor() {
@Override
public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
System.out.println("outgoing");
if (packet.getClass() == MqttPublishMessage.class) {
return checkMessageProperties(packet, expectedProperties);
} else {
return true;
}
}
};
server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] payload = subscribeProvider.receive(10000);
assertNotNull("Should get a message", payload);
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
final MQTTClientProvider publishProvider = getMQTTClientProvider();
initializeConnection(publishProvider);
publishProvider.publish(addressQueue, msgText.getBytes(), AT_MOST_ONCE, retained);
latch.await(10, TimeUnit.SECONDS);
subscribeProvider.disconnect();
publishProvider.disconnect();
}
use of io.netty.handler.codec.mqtt.MqttMessage in project netty by netty.
the class MqttHeartBeatClientHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
MqttFixedHeader pingreqFixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
MqttMessage pingreqMessage = new MqttMessage(pingreqFixedHeader);
ctx.writeAndFlush(pingreqMessage);
System.out.println("Sent PINGREQ");
} else {
super.userEventTriggered(ctx, evt);
}
}
use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.
the class MessageDispatcher method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (!(msg instanceof MqttMessage)) {
return;
}
Client client = clientManager.get(ctx.channel());
if (client == null) {
client = new MqttClient();
client.setCtx(ctx);
clientManager.put(ctx.channel(), client);
}
MqttMessage mqttMessage = (MqttMessage) msg;
Message message = MessageUtil.getMessage(mqttMessage);
message.setClient(client);
dispatch(message);
}
Aggregations