2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2017-01-04 14:37:29 +01:00
|
|
|
* Copyright (c) 2012-2017 The original author or authorsgetRockQuestions()
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2017-02-01 15:46:15 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.spi;
|
2012-09-01 16:02:48 +02:00
|
|
|
|
2017-02-14 17:23:04 +01:00
|
|
|
import io.moquette.spi.impl.subscriptions.Topic;
|
2017-01-28 10:06:30 +01:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.buffer.Unpooled;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttQoS;
|
2014-08-30 15:59:59 +02:00
|
|
|
import java.io.Serializable;
|
2013-11-25 09:34:36 +01:00
|
|
|
import java.nio.ByteBuffer;
|
2012-09-01 16:02:48 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-30 23:36:00 +02:00
|
|
|
* Defines the SPI to be implemented by a StorageService that handle persistence of messages
|
2012-09-01 16:02:48 +02:00
|
|
|
*/
|
2014-06-30 23:36:00 +02:00
|
|
|
public interface IMessagesStore {
|
2012-09-01 16:02:48 +02:00
|
|
|
|
2015-10-24 16:26:43 +02:00
|
|
|
class StoredMessage implements Serializable {
|
2017-02-01 15:46:15 +01:00
|
|
|
|
2017-01-18 22:46:04 +01:00
|
|
|
private static final long serialVersionUID = 1755296138639817304L;
|
2017-01-28 10:06:30 +01:00
|
|
|
final MqttQoS m_qos;
|
2014-08-30 15:59:59 +02:00
|
|
|
final byte[] m_payload;
|
|
|
|
|
final String m_topic;
|
2015-11-18 22:19:43 +01:00
|
|
|
private boolean m_retained;
|
|
|
|
|
private String m_clientID;
|
2017-02-01 15:46:15 +01:00
|
|
|
// Optional attribute, available only fo QoS 1 and 2
|
2015-11-18 22:19:43 +01:00
|
|
|
private Integer m_msgID;
|
2016-09-23 11:51:25 +02:00
|
|
|
private MessageGUID m_guid;
|
2014-08-30 15:59:59 +02:00
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
public StoredMessage(byte[] message, MqttQoS qos, String topic) {
|
2014-08-30 15:59:59 +02:00
|
|
|
m_qos = qos;
|
|
|
|
|
m_payload = message;
|
|
|
|
|
m_topic = topic;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
public MqttQoS getQos() {
|
2014-08-30 15:59:59 +02:00
|
|
|
return m_qos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ByteBuffer getPayload() {
|
|
|
|
|
return (ByteBuffer) ByteBuffer.allocate(m_payload.length).put(m_payload).flip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getTopic() {
|
|
|
|
|
return m_topic;
|
|
|
|
|
}
|
2015-11-18 22:19:43 +01:00
|
|
|
|
2016-09-23 11:51:25 +02:00
|
|
|
public void setGuid(MessageGUID guid) {
|
2015-11-18 22:19:43 +01:00
|
|
|
this.m_guid = guid;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 11:51:25 +02:00
|
|
|
public MessageGUID getGuid() {
|
2015-11-18 22:19:43 +01:00
|
|
|
return m_guid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getClientID() {
|
|
|
|
|
return m_clientID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setClientID(String m_clientID) {
|
|
|
|
|
this.m_clientID = m_clientID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMessageID(Integer messageID) {
|
|
|
|
|
this.m_msgID = messageID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Integer getMessageID() {
|
|
|
|
|
return m_msgID;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
public ByteBuf getMessage() {
|
|
|
|
|
return Unpooled.copiedBuffer(m_payload);
|
2015-11-18 22:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setRetained(boolean retained) {
|
|
|
|
|
this.m_retained = retained;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isRetained() {
|
|
|
|
|
return m_retained;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
2017-02-01 15:46:15 +01:00
|
|
|
return "PublishEvent{" + "m_msgID=" + m_msgID + ", clientID='" + m_clientID + '\'' + ", m_retain="
|
|
|
|
|
+ m_retained + ", m_qos=" + m_qos + ", m_topic='" + m_topic + '\'' + '}';
|
2015-11-18 22:19:43 +01:00
|
|
|
}
|
2014-08-30 15:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-01 16:02:48 +02:00
|
|
|
/**
|
|
|
|
|
* Used to initialize all persistent store structures
|
2017-02-01 15:46:15 +01:00
|
|
|
*/
|
2012-09-01 16:02:48 +02:00
|
|
|
void initStore();
|
|
|
|
|
|
2014-05-20 23:02:52 +02:00
|
|
|
/**
|
2017-02-01 15:46:15 +01:00
|
|
|
* Persist the message. If the message is empty then the topic is cleaned, else it's stored.
|
|
|
|
|
*
|
|
|
|
|
* @param topic
|
|
|
|
|
* for the retained.
|
|
|
|
|
* @param guid
|
|
|
|
|
* of the message to mark as retained.
|
2014-05-20 23:02:52 +02:00
|
|
|
*/
|
2017-02-14 17:23:04 +01:00
|
|
|
void storeRetained(Topic topic, MessageGUID guid);
|
2012-09-01 16:02:48 +02:00
|
|
|
|
2014-05-20 23:02:52 +02:00
|
|
|
/**
|
|
|
|
|
* Return a list of retained messages that satisfy the condition.
|
2017-02-01 15:46:15 +01:00
|
|
|
*
|
|
|
|
|
* @param condition
|
|
|
|
|
* the condition to match during the search.
|
2017-01-04 12:52:42 +01:00
|
|
|
* @return the collection of matching messages.
|
2014-05-20 23:02:52 +02:00
|
|
|
*/
|
2012-09-01 16:02:48 +02:00
|
|
|
Collection<StoredMessage> searchMatching(IMatchingCondition condition);
|
|
|
|
|
|
2015-11-15 11:16:18 +01:00
|
|
|
/**
|
|
|
|
|
* Persist the message.
|
2017-02-01 15:46:15 +01:00
|
|
|
*
|
|
|
|
|
* @param storedMessage
|
|
|
|
|
* the message to store for future usage.
|
2015-11-15 11:16:18 +01:00
|
|
|
* @return the unique id in the storage (guid).
|
2017-02-01 15:46:15 +01:00
|
|
|
*/
|
2017-01-04 12:52:42 +01:00
|
|
|
MessageGUID storePublishForFuture(StoredMessage storedMessage);
|
2012-09-01 16:18:09 +02:00
|
|
|
|
2015-01-30 00:28:01 +01:00
|
|
|
void dropMessagesInSession(String clientID);
|
2012-09-01 16:18:09 +02:00
|
|
|
|
2016-09-23 11:51:25 +02:00
|
|
|
StoredMessage getMessageByGuid(MessageGUID guid);
|
2015-11-16 23:45:47 +01:00
|
|
|
|
2017-02-14 17:23:04 +01:00
|
|
|
void cleanRetained(Topic topic);
|
2017-01-12 17:00:18 +01:00
|
|
|
|
|
|
|
|
int getPendingPublishMessages(String clientID);
|
2012-09-01 16:02:48 +02:00
|
|
|
}
|