Java Code Examples for io.netty.handler.codec.mqtt.MqttQoS#valueOf()

The following examples show how to use io.netty.handler.codec.mqtt.MqttQoS#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProcedureProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
public void processPublish(Channel channel, BorkerMessage bMsgInfo) {
	MqttQoS qosLevel = MqttQoS.valueOf(bMsgInfo.getIQosLevel());
	String clientId = NettyUtil.getClientId(channel);
	int packetId = bMsgInfo.getSourceMsgId();
	String topicName = bMsgInfo.getTopicName();
	byte[] msgBytes = bMsgInfo.getMsgBytes();

	boolean bSaveRelMsg = getMustStoreRelMessage(qosLevel);
	if (bSaveRelMsg) {
		ProcedureMessage relMsgObj = ProcedureMessage.builder().sourceClientId(clientId)
				.sourceMsgId(packetId).topicName(topicName).iQosLevel(qosLevel.value()).msgBytes(msgBytes)
				//.borkerMsgId(bMsgInfo.getBorkerMsgId())
				.build();
		procedureData.putPubRelMessage(clientId, relMsgObj);
	}
}
 
Example 2
Source File: ConsumerProcess.java    From ext-opensource-netty with Mozilla Public License 2.0 6 votes vote down vote up
public void sendSubscribMessageForInternal(InternalMessage msg) {
	String clientId = msg.getDestClientId();	
	MqttSession mqttSession = sessionService.getSession(clientId);
	if (mqttSession == null) {
		return;
	}
	
	MqttQoS respQoS = MqttQoS.valueOf(msg.getRespQoS()); 
	boolean bSaveMsg = getMustStorePubMessage(respQoS);
	boolean bSend = true;
	
	BorkerMessage bMsgInfo = BorkerMessage.builder()
			.topicName(msg.getTopicName())
			.msgBytes(msg.getMsgBytes())
			.dup(msg.isDup())
			.retain(msg.isRetain())
			.build();
	sendSubscribMessage(clientId, mqttSession.channel(), respQoS, bSaveMsg, bSend, bMsgInfo);
}
 
Example 3
Source File: SessionRegistry.java    From cassandana with Apache License 2.0 5 votes vote down vote up
private Session.Will createWill(MqttConnectMessage msg) {
    final ByteBuf willPayload = Unpooled.copiedBuffer(msg.payload().willMessageInBytes());
    final String willTopic = msg.payload().willTopic();
    final boolean retained = msg.variableHeader().isWillRetain();
    final MqttQoS qos = MqttQoS.valueOf(msg.variableHeader().willQos());
    return new Session.Will(willTopic, willPayload, qos, retained);
}
 
Example 4
Source File: ClientProtocolUtil.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
public static List<MqttTopicSubscription> getSubscribeTopics(SubscribeMessage[] sMsgObj) {
	if (sMsgObj != null) {
		List<MqttTopicSubscription> list = new LinkedList<>();
		for (SubscribeMessage sb : sMsgObj) {
			MqttTopicSubscription mqttTopicSubscription = new MqttTopicSubscription(sb.getTopic(),
					MqttQoS.valueOf(sb.getQos()));
			list.add(mqttTopicSubscription);
		}
		return list;
	} else {
		return null;
	}
}
 
Example 5
Source File: MessageUtil.java    From iot-mqtt with Apache License 2.0 5 votes vote down vote up
public static MqttPublishMessage getPubMessage(Message message,boolean dup,int qos,int messageId){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH,dup,MqttQoS.valueOf(qos),false,0);
    MqttPublishVariableHeader publishVariableHeader = new MqttPublishVariableHeader((String) message.getHeader(MessageHeader.TOPIC),messageId);
    ByteBuf heapBuf;
    if(message.getPayload() == null){
        heapBuf = Unpooled.EMPTY_BUFFER;
    }else{
        heapBuf = Unpooled.wrappedBuffer((byte[])message.getPayload());
    }
    return new MqttPublishMessage(fixedHeader,publishVariableHeader,heapBuf);
}
 
Example 6
Source File: MqttProtocolHandler.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private void storeWillMessage(String clientID, MqttConnectMessage msg) {
    if (msg.variableHeader().isWillFlag()) {
        MqttQoS willQos = MqttQoS.valueOf(msg.variableHeader().willQos());
        byte[] willPayload = msg.payload().willMessageInBytes();
        ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(willPayload.length).put(willPayload).flip();
        WillMessage will = new WillMessage(msg.payload().willTopic(), bb, msg.variableHeader().isWillRetain(), willQos);
        willStore.put(clientID, will);
        LOG.info("Latest will message stored for client: <{}>", clientID);
    }
}
 
Example 7
Source File: MqttSink.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private CompletionStage<?> send(AtomicReference<MqttClient> reference, Message<?> msg) {
    MqttClient client = reference.get();
    String actualTopicToBeUsed = this.topic;
    MqttQoS actualQoS = MqttQoS.valueOf(this.qos);
    boolean isRetain = false;

    if (msg instanceof SendingMqttMessage) {
        MqttMessage<?> mm = ((SendingMqttMessage<?>) msg);
        actualTopicToBeUsed = mm.getTopic() == null ? topic : mm.getTopic();
        actualQoS = mm.getQosLevel() == null ? actualQoS : mm.getQosLevel();
        isRetain = mm.isRetain();
    }

    if (actualTopicToBeUsed == null) {
        log.ignoringNoTopicSet();
        return CompletableFuture.completedFuture(msg);
    }

    return client.publish(actualTopicToBeUsed, convert(msg.getPayload()), actualQoS, false, isRetain)
            .onItemOrFailure().produceUni((s, f) -> {
                if (f != null) {
                    return Uni.createFrom().completionStage(msg.nack(f).thenApply(x -> msg));
                } else {
                    return Uni.createFrom().completionStage(msg.ack().thenApply(x -> msg));
                }
            })
            .subscribeAsCompletionStage();
}
 
Example 8
Source File: MqttSessionRegistry.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
private MqttSession.Will createWill(MqttConnectMessage msg) {
    final ByteBuf willPayload = Unpooled.copiedBuffer(msg.payload().willMessageInBytes());
    final String willTopic = msg.payload().willTopic();
    final boolean retained = msg.variableHeader().isWillRetain();
    final MqttQoS qos = MqttQoS.valueOf(msg.variableHeader().willQos());
    return new MqttSession.Will(willTopic, willPayload, qos, retained);
}
 
Example 9
Source File: MqttDeviceAwareSessionContext.java    From Groza with Apache License 2.0 5 votes vote down vote up
public MqttQoS getQoSForTopic(String topic) {
    List<Integer> qosList = mqttQoSMap.entrySet()
            .stream()
            .filter(entry -> entry.getKey().matches(topic))
            .map(Map.Entry::getValue)
            .collect(Collectors.toList());
    if (!qosList.isEmpty()) {
        return MqttQoS.valueOf(qosList.get(0));
    } else {
        return MqttQoS.AT_LEAST_ONCE;
    }
}
 
Example 10
Source File: ConnectReceiver.java    From lannister with Apache License 2.0 5 votes vote down vote up
private Message newWill(String clientId, MqttConnectMessage conn) {
	if (!conn.variableHeader().isWillFlag()) { return null; } // [MQTT-3.1.2-12]

	return new Message(-1, conn.payload().willTopic(), clientId,
			conn.payload().willMessage().getBytes(CharsetUtil.UTF_8),
			MqttQoS.valueOf(conn.variableHeader().willQos()), conn.variableHeader().isWillRetain());
}
 
Example 11
Source File: Message.java    From lannister with Apache License 2.0 5 votes vote down vote up
@Override
public void readData(ObjectDataInput in) throws IOException {
	id = in.readInt();
	topicName = in.readUTF();
	publisherId = in.readUTF();
	message = in.readByteArray();

	int rawInt = in.readInt();
	qos = rawInt != Byte.MIN_VALUE ? MqttQoS.valueOf(rawInt) : null;

	isRetain = in.readBoolean();
}
 
Example 12
Source File: OutboundMessageStatus.java    From lannister with Apache License 2.0 5 votes vote down vote up
@Override
public void readData(ObjectDataInput in) throws IOException {
	super.readData(in);

	Byte rawByte = in.readByte();
	status = rawByte != Byte.MIN_VALUE ? Status.valueOf(rawByte) : null;

	int rawInt = in.readInt();
	qos = rawInt != Byte.MIN_VALUE ? MqttQoS.valueOf(rawInt) : null;
}
 
Example 13
Source File: TopicSubscription.java    From lannister with Apache License 2.0 5 votes vote down vote up
@Override
public void readData(ObjectDataInput in) throws IOException {
	clientId = in.readUTF();
	topicFilter = in.readUTF();

	int rawInt = in.readInt();
	qos = rawInt != Integer.MIN_VALUE ? MqttQoS.valueOf(rawInt) : null;
}
 
Example 14
Source File: MQTTProtocolHandler.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 15
Source File: MessagePublisher.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
public void publish2Subscriber(String name, String clientID, MqttSession session, Consumer consumer, int qos) throws Exception {
    PullResult result = consume.getMessage(
            consumer,
            1,
            1000 * 60 * 2
    );
    String topicName = result.getTopic();
    List<ByteBuffer> buffers = result.getBuffers();
    if (buffers != null && buffers.size() > 0) {
        BrokerMessage brokerMessage = Serializer.readBrokerMessage(buffers.get(0));
        MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(
                MqttMessageType.PUBLISH,
                false,
                MqttQoS.valueOf(MqttMessageSerializer.getLowerQos(MqttMessageSerializer.readExtension(brokerMessage), qos)),
                false,
                0
        );
        int packageId = session.getMessageAcknowledgedZone().acquireAcknowledgedPosition(brokerMessage);
        MqttPublishMessage publishMsg = (MqttPublishMessage) MqttMessageFactory.newMessage(
                mqttFixedHeader,
                new MqttPublishVariableHeader(topicName, packageId),
                Unpooled.wrappedBuffer(brokerMessage.getByteBody()));

        boolean isActive = connectionManager.isConnected(clientID);
        if (isActive) {
            MqttConnection connection = connectionManager.getConnection(clientID);
            Channel channel = connection.getChannel();
            if (channel.isActive() && channel.isOpen()) {
                channel.writeAndFlush(publishMsg).addListener((ChannelFutureListener) channelFuture -> {
                    if (channelFuture.isSuccess()) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("推送消息成功: {}", publishMsg);
                        }
                    } else {
                        LOG.error("publish message error, thread: <{}>, clientID: <{}>, message: <{}>, cause: <{}>", name, clientID, brokerMessage, channelFuture.cause());
                        throw new Exception(channelFuture.cause());
                    }
                });
            }
        }
    }
}