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);
}
use of io.netty.handler.codec.mqtt.MqttMessage in project traccar by tananaev.
the class IotmProtocolDecoder method decode.
@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
if (msg instanceof MqttConnectMessage) {
MqttConnectMessage message = (MqttConnectMessage) msg;
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, message.payload().clientIdentifier());
MqttConnectReturnCode returnCode = deviceSession != null ? MqttConnectReturnCode.CONNECTION_ACCEPTED : MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
MqttMessage response = MqttMessageBuilders.connAck().returnCode(returnCode).build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
} else if (msg instanceof MqttSubscribeMessage) {
MqttSubscribeMessage message = (MqttSubscribeMessage) msg;
MqttMessage response = MqttMessageBuilders.subAck().packetId((short) message.variableHeader().messageId()).build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
} else if (msg instanceof MqttPublishMessage) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
MqttPublishMessage message = (MqttPublishMessage) msg;
ByteBuf buf = message.payload();
// structure version
buf.readUnsignedByte();
while (buf.readableBytes() > 1) {
int type = buf.readUnsignedByte();
int length = buf.readUnsignedShortLE();
ByteBuf record = buf.readSlice(length);
if (type == 1) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(record.readUnsignedIntLE() * 1000));
while (record.readableBytes() > 0) {
int sensorType = record.readUnsignedByte();
int sensorId = record.readUnsignedShortLE();
if (sensorType == 14) {
position.setValid(true);
position.setLatitude(record.readFloatLE());
position.setLongitude(record.readFloatLE());
position.setSpeed(UnitsConverter.knotsFromKph(record.readUnsignedShortLE()));
position.set(Position.KEY_HDOP, record.readUnsignedByte());
position.set(Position.KEY_SATELLITES, record.readUnsignedByte());
position.setCourse(record.readUnsignedShortLE());
position.setAltitude(record.readShortLE());
} else {
if (sensorType == 3) {
continue;
}
decodeSensor(position, record, sensorType, sensorId);
}
}
positions.add(position);
} else if (type == 3) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, new Date(record.readUnsignedIntLE() * 1000));
// function identifier
record.readUnsignedByte();
position.set(Position.KEY_EVENT, record.readUnsignedByte());
positions.add(position);
}
}
// checksum
buf.readUnsignedByte();
MqttMessage response = MqttMessageBuilders.pubAck().packetId((short) message.variableHeader().packetId()).build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return positions.isEmpty() ? null : positions;
}
return null;
}
use of io.netty.handler.codec.mqtt.MqttMessage in project thingsboard by thingsboard.
the class MqttPingHandler method sendPingReq.
private void sendPingReq(Channel channel) {
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
channel.writeAndFlush(new MqttMessage(fixedHeader));
if (this.pingRespTimeout != null) {
this.pingRespTimeout = channel.eventLoop().schedule(() -> {
MqttFixedHeader fixedHeader2 = new MqttFixedHeader(MqttMessageType.DISCONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
channel.writeAndFlush(new MqttMessage(fixedHeader2)).addListener(ChannelFutureListener.CLOSE);
// TODO: what do when the connection is closed ?
}, this.keepaliveSeconds, TimeUnit.SECONDS);
}
}
use of io.netty.handler.codec.mqtt.MqttMessage in project thingsboard by thingsboard.
the class MqttTransportHandler method createUnSubAckMessage.
private MqttMessage createUnSubAckMessage(int msgId) {
MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(UNSUBACK, false, AT_MOST_ONCE, false, 0);
MqttMessageIdVariableHeader mqttMessageIdVariableHeader = MqttMessageIdVariableHeader.from(msgId);
return new MqttMessage(mqttFixedHeader, mqttMessageIdVariableHeader);
}
use of io.netty.handler.codec.mqtt.MqttMessage in project thingsboard by thingsboard.
the class MqttTransportHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
log.trace("[{}] Processing msg: {}", sessionId, msg);
if (address == null) {
address = getAddress(ctx);
}
try {
if (msg instanceof MqttMessage) {
MqttMessage message = (MqttMessage) msg;
if (message.decoderResult().isSuccess()) {
processMqttMsg(ctx, message);
} else {
log.error("[{}] Message decoding failed: {}", sessionId, message.decoderResult().cause().getMessage());
ctx.close();
}
} else {
log.debug("[{}] Received non mqtt message: {}", sessionId, msg.getClass().getSimpleName());
ctx.close();
}
} finally {
ReferenceCountUtil.safeRelease(msg);
}
}
Aggregations