2015-10-24 16:26:43 +02:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2012-2015 The original author or authors
|
|
|
|
|
|
* ------------------------------------------------------
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
*/
|
2015-11-20 20:54:27 +01:00
|
|
|
|
package io.moquette.spi;
|
2015-10-24 16:26:43 +02:00
|
|
|
|
|
2016-02-03 23:20:15 +01:00
|
|
|
|
import io.moquette.parser.proto.messages.AbstractMessage;
|
2016-03-06 19:50:26 +01:00
|
|
|
|
import io.moquette.parser.proto.messages.PublishMessage;
|
|
|
|
|
|
import io.moquette.server.Constants;
|
2015-11-20 20:54:27 +01:00
|
|
|
|
import io.moquette.spi.impl.subscriptions.Subscription;
|
|
|
|
|
|
import io.moquette.spi.impl.subscriptions.SubscriptionsStore;
|
2015-12-06 19:31:36 +01:00
|
|
|
|
import io.moquette.spi.ISessionsStore.ClientTopicCouple;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-10-24 16:26:43 +02:00
|
|
|
|
|
2015-11-15 11:16:18 +01:00
|
|
|
|
import java.util.Collection;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
import java.util.HashSet;
|
2015-10-24 16:26:43 +02:00
|
|
|
|
import java.util.List;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
import java.util.Set;
|
2016-03-06 19:50:26 +01:00
|
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
2015-10-24 16:26:43 +02:00
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-15 11:16:18 +01:00
|
|
|
|
* Model a Session like describe on page 25 of MQTT 3.1.1 specification:
|
|
|
|
|
|
* The Session state in the Server consists of:
|
|
|
|
|
|
* <ul>
|
|
|
|
|
|
* <li>The existence of a Session, even if the rest of the Session state is empty.</li>
|
|
|
|
|
|
* <li>The Client’s subscriptions.</li>
|
|
|
|
|
|
* <li>QoS 1 and QoS 2 messages which have been sent to the Client, but have not been
|
|
|
|
|
|
* completely acknowledged.</li>
|
|
|
|
|
|
* <li>QoS 1 and QoS 2 messages pending transmission to the Client.</li>
|
|
|
|
|
|
* <li>QoS 2 messages which have been received from the Client, but have not been
|
|
|
|
|
|
* completely acknowledged.</li>
|
|
|
|
|
|
* <li>Optionally, QoS 0 messages pending transmission to the Client.</li>
|
|
|
|
|
|
* </ul>
|
|
|
|
|
|
*
|
2015-10-24 16:26:43 +02:00
|
|
|
|
* @author andrea
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class ClientSession {
|
|
|
|
|
|
|
2015-10-31 11:14:40 +01:00
|
|
|
|
private final static Logger LOG = LoggerFactory.getLogger(ClientSession.class);
|
|
|
|
|
|
|
2015-10-24 16:26:43 +02:00
|
|
|
|
public final String clientID;
|
|
|
|
|
|
|
|
|
|
|
|
private final IMessagesStore messagesStore;
|
|
|
|
|
|
|
2015-10-31 11:14:40 +01:00
|
|
|
|
private final ISessionsStore m_sessionsStore;
|
|
|
|
|
|
|
|
|
|
|
|
private Set<Subscription> subscriptions = new HashSet<>();
|
|
|
|
|
|
|
2016-02-02 22:39:11 +01:00
|
|
|
|
private volatile boolean cleanSession;
|
2015-11-08 18:25:55 +01:00
|
|
|
|
|
|
|
|
|
|
private boolean active = false;
|
|
|
|
|
|
|
2016-03-06 19:50:26 +01:00
|
|
|
|
private BlockingQueue<AbstractMessage> m_queueToPublish = new ArrayBlockingQueue<>(Constants.MAX_MESSAGE_QUEUE);
|
|
|
|
|
|
|
2015-11-08 18:25:55 +01:00
|
|
|
|
public ClientSession(String clientID, IMessagesStore messagesStore, ISessionsStore sessionsStore,
|
|
|
|
|
|
boolean cleanSession) {
|
2015-10-24 16:26:43 +02:00
|
|
|
|
this.clientID = clientID;
|
|
|
|
|
|
this.messagesStore = messagesStore;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
this.m_sessionsStore = sessionsStore;
|
2015-11-08 18:25:55 +01:00
|
|
|
|
this.cleanSession = cleanSession;
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @return the list of messages to be delivered for client related to the session.
|
|
|
|
|
|
* */
|
2015-11-18 22:19:43 +01:00
|
|
|
|
public List<IMessagesStore.StoredMessage> storedMessages() {
|
2015-11-15 11:16:18 +01:00
|
|
|
|
//read all messages from enqueued store
|
|
|
|
|
|
Collection<String> guids = this.m_sessionsStore.enqueued(clientID);
|
|
|
|
|
|
return messagesStore.listMessagesInSession(guids);
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2015-11-15 11:16:18 +01:00
|
|
|
|
* Remove the messages stored in a cleanSession false.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void removeEnqueued(String guid) {
|
|
|
|
|
|
this.m_sessionsStore.removeEnqueued(this.clientID, guid);
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
2015-11-15 11:16:18 +01:00
|
|
|
|
return "ClientSession{clientID='" + clientID + '\'' +"}";
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|
2015-10-31 11:14:40 +01:00
|
|
|
|
|
2016-07-17 14:24:19 +02:00
|
|
|
|
public boolean subscribe(Subscription newSubscription) {
|
|
|
|
|
|
LOG.info("<{}> subscribed to the topic filter <{}> with QoS {}",
|
|
|
|
|
|
newSubscription.getClientId(), newSubscription.getTopicFilter(),
|
2015-10-31 11:14:40 +01:00
|
|
|
|
AbstractMessage.QOSType.formatQoS(newSubscription.getRequestedQos()));
|
|
|
|
|
|
boolean validTopic = SubscriptionsStore.validate(newSubscription.getTopicFilter());
|
|
|
|
|
|
if (!validTopic) {
|
|
|
|
|
|
//send SUBACK with 0x80 for this topic filter
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2015-12-06 19:31:36 +01:00
|
|
|
|
ClientTopicCouple matchingCouple = new ClientTopicCouple(this.clientID, newSubscription.getTopicFilter());
|
2015-12-06 11:43:29 +01:00
|
|
|
|
Subscription existingSub = m_sessionsStore.getSubscription(matchingCouple);
|
|
|
|
|
|
//update the selected subscriptions if not present or if has a greater qos
|
|
|
|
|
|
if (existingSub == null || existingSub.getRequestedQos().byteValue() < newSubscription.getRequestedQos().byteValue()) {
|
|
|
|
|
|
if (existingSub != null) {
|
|
|
|
|
|
subscriptions.remove(newSubscription);
|
|
|
|
|
|
}
|
|
|
|
|
|
subscriptions.add(newSubscription);
|
|
|
|
|
|
m_sessionsStore.addNewSubscription(newSubscription);
|
|
|
|
|
|
}
|
2015-10-31 11:14:40 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2015-10-31 11:34:15 +01:00
|
|
|
|
|
|
|
|
|
|
public void unsubscribeFrom(String topicFilter) {
|
|
|
|
|
|
m_sessionsStore.removeSubscription(topicFilter, clientID);
|
|
|
|
|
|
Set<Subscription> subscriptionsToRemove = new HashSet<>();
|
|
|
|
|
|
for (Subscription sub : this.subscriptions) {
|
|
|
|
|
|
if (sub.getTopicFilter().equals(topicFilter)) {
|
|
|
|
|
|
subscriptionsToRemove.add(sub);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
subscriptions.removeAll(subscriptionsToRemove);
|
|
|
|
|
|
}
|
2015-11-08 18:25:55 +01:00
|
|
|
|
|
|
|
|
|
|
public void disconnect() {
|
|
|
|
|
|
if (this.cleanSession) {
|
|
|
|
|
|
//cleanup topic subscriptions
|
|
|
|
|
|
cleanSession();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//deactivate the session
|
|
|
|
|
|
deactivate();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void cleanSession() {
|
|
|
|
|
|
LOG.info("cleaning old saved subscriptions for client <{}>", this.clientID);
|
|
|
|
|
|
m_sessionsStore.wipeSubscriptions(this.clientID);
|
|
|
|
|
|
|
|
|
|
|
|
//remove also the messages stored of type QoS1/2
|
|
|
|
|
|
messagesStore.dropMessagesInSession(this.clientID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-06 19:55:04 +01:00
|
|
|
|
public boolean isCleanSession() {
|
|
|
|
|
|
return this.cleanSession;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-02-02 22:39:11 +01:00
|
|
|
|
public void cleanSession(boolean cleanSession) {
|
|
|
|
|
|
this.cleanSession = cleanSession;
|
|
|
|
|
|
this.m_sessionsStore.updateCleanStatus(this.clientID, cleanSession);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-11-08 18:25:55 +01:00
|
|
|
|
public void activate() {
|
|
|
|
|
|
this.active = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void deactivate() {
|
|
|
|
|
|
this.active = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isActive() {
|
|
|
|
|
|
return this.active;
|
|
|
|
|
|
}
|
2015-11-15 11:16:18 +01:00
|
|
|
|
|
|
|
|
|
|
public int nextPacketId() {
|
2016-01-17 13:00:36 +01:00
|
|
|
|
return this.m_sessionsStore.nextPacketID(this.clientID);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void inFlightAcknowledged(int messageID) {
|
2016-06-16 23:01:48 +02:00
|
|
|
|
LOG.trace("Acknowledging inflight, clientID <{}> messageID {}", this.clientID, messageID);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
m_sessionsStore.inFlightAck(this.clientID, messageID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void inFlightAckWaiting(String guid, int messageID) {
|
2016-06-16 23:01:48 +02:00
|
|
|
|
LOG.trace("Adding to inflight {}, guid <{}>", messageID, guid);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
m_sessionsStore.inFlight(this.clientID, messageID, guid);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-16 23:01:48 +02:00
|
|
|
|
public IMessagesStore.StoredMessage secondPhaseAcknowledged(int messageID) {
|
|
|
|
|
|
String guid = m_sessionsStore.secondPhaseAcknowledged(clientID, messageID);
|
|
|
|
|
|
return messagesStore.getMessageByGuid(guid);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void enqueueToDeliver(String guid) {
|
|
|
|
|
|
this.m_sessionsStore.bindToDeliver(guid, this.clientID);
|
|
|
|
|
|
}
|
2015-11-16 23:45:47 +01:00
|
|
|
|
|
2015-11-18 22:19:43 +01:00
|
|
|
|
public IMessagesStore.StoredMessage storedMessage(int messageID) {
|
2015-11-16 23:45:47 +01:00
|
|
|
|
final String guid = m_sessionsStore.mapToGuid(clientID, messageID);
|
|
|
|
|
|
return messagesStore.getMessageByGuid(guid);
|
|
|
|
|
|
}
|
2016-03-06 19:50:26 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Enqueue a message to be sent to the client.
|
|
|
|
|
|
* @return false if the queue is full.
|
|
|
|
|
|
* */
|
|
|
|
|
|
public boolean enqueue(PublishMessage pubMessage) {
|
|
|
|
|
|
return m_queueToPublish.offer(pubMessage);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public AbstractMessage dequeue() {
|
|
|
|
|
|
return m_queueToPublish.poll();
|
|
|
|
|
|
}
|
2016-06-16 23:01:48 +02:00
|
|
|
|
|
|
|
|
|
|
public void moveInFlightToSecondPhaseAckWaiting(int messageID) {
|
|
|
|
|
|
m_sessionsStore.moveInFlightToSecondPhaseAckWaiting(this.clientID, messageID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IMessagesStore.StoredMessage getInflightMessage(int messageID) {
|
|
|
|
|
|
return m_sessionsStore.getInflightMessage(clientID, messageID);
|
|
|
|
|
|
}
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|