2015-10-24 16:26:43 +02:00
|
|
|
|
/*
|
2017-03-19 22:01:23 +01:00
|
|
|
|
* Copyright (c) 2012-2017 The original author or authors
|
2015-10-24 16:26:43 +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:45:06 +01:00
|
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
|
package io.moquette.spi;
|
2015-10-24 16:26:43 +02:00
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
import io.moquette.spi.ISubscriptionsStore.ClientTopicCouple;
|
2015-11-20 20:54:27 +01:00
|
|
|
|
import io.moquette.spi.impl.subscriptions.Subscription;
|
2017-02-14 17:23:04 +01:00
|
|
|
|
import io.moquette.spi.impl.subscriptions.Topic;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-04-20 23:14:23 +02:00
|
|
|
|
|
2015-10-31 11:14:40 +01:00
|
|
|
|
import java.util.HashSet;
|
2017-06-19 22:40:34 +02:00
|
|
|
|
import java.util.Queue;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
import java.util.Set;
|
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
|
|
|
|
*/
|
|
|
|
|
|
public class ClientSession {
|
|
|
|
|
|
|
2017-04-04 21:29:38 +02:00
|
|
|
|
class OutboundFlightZone {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-20 23:14:23 +02:00
|
|
|
|
* Save the binding messageID, clientID - message
|
|
|
|
|
|
*
|
2017-04-04 21:29:38 +02:00
|
|
|
|
* @param messageID the packet ID used in transmission
|
2017-04-20 23:14:23 +02:00
|
|
|
|
* @param msg the message to put in flight zone
|
2017-04-04 21:29:38 +02:00
|
|
|
|
*/
|
2017-04-20 23:14:23 +02:00
|
|
|
|
void waitingAck(int messageID, IMessagesStore.StoredMessage msg) {
|
2017-04-04 21:29:38 +02:00
|
|
|
|
if (LOG.isTraceEnabled()) {
|
2017-04-20 23:14:23 +02:00
|
|
|
|
LOG.trace("Adding to inflight {}, guid <{}>", messageID, msg.getGuid());
|
2017-04-04 21:29:38 +02:00
|
|
|
|
}
|
2017-04-20 23:14:23 +02:00
|
|
|
|
m_sessionsStore.inFlight(ClientSession.this.clientID, messageID, msg);
|
2017-04-04 21:29:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
IMessagesStore.StoredMessage acknowledged(int messageID) {
|
2017-04-04 21:29:38 +02:00
|
|
|
|
if (LOG.isTraceEnabled())
|
|
|
|
|
|
LOG.trace("Acknowledging inflight, clientID <{}> messageID {}", ClientSession.this.clientID, messageID);
|
2017-04-20 23:14:23 +02:00
|
|
|
|
return m_sessionsStore.inFlightAck(ClientSession.this.clientID, messageID);
|
2017-04-04 21:29:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class InboundFlightZone {
|
|
|
|
|
|
|
|
|
|
|
|
public IMessagesStore.StoredMessage lookup(int messageID) {
|
|
|
|
|
|
return m_sessionsStore.inboundInflight(clientID, messageID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
public void waitingRel(int messageID, IMessagesStore.StoredMessage msg) {
|
|
|
|
|
|
m_sessionsStore.markAsInboundInflight(clientID, messageID, msg);
|
2017-04-04 21:29:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-12 17:40:32 +02:00
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ClientSession.class);
|
2015-10-31 11:14:40 +01:00
|
|
|
|
|
2015-10-24 16:26:43 +02:00
|
|
|
|
public final String clientID;
|
|
|
|
|
|
|
2015-10-31 11:14:40 +01:00
|
|
|
|
private final ISessionsStore m_sessionsStore;
|
|
|
|
|
|
|
|
|
|
|
|
private Set<Subscription> subscriptions = new HashSet<>();
|
|
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
private final ISubscriptionsStore subscriptionsStore;
|
2016-02-02 22:39:11 +01:00
|
|
|
|
private volatile boolean cleanSession;
|
2015-11-08 18:25:55 +01:00
|
|
|
|
|
2017-02-01 15:45:06 +01:00
|
|
|
|
// private BlockingQueue<AbstractMessage> m_queueToPublish = new
|
|
|
|
|
|
// ArrayBlockingQueue<>(Constants.MAX_MESSAGE_QUEUE);
|
2017-04-04 21:29:38 +02:00
|
|
|
|
private final OutboundFlightZone outboundFlightZone;
|
|
|
|
|
|
private final InboundFlightZone inboundFlightZone;
|
2016-03-06 19:50:26 +01:00
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
public ClientSession(String clientID, ISessionsStore sessionsStore, ISubscriptionsStore subscriptionsStore,
|
|
|
|
|
|
boolean cleanSession) {
|
2015-10-24 16:26:43 +02:00
|
|
|
|
this.clientID = clientID;
|
2015-10-31 11:14:40 +01:00
|
|
|
|
this.m_sessionsStore = sessionsStore;
|
2017-04-20 23:14:23 +02:00
|
|
|
|
this.subscriptionsStore = subscriptionsStore;
|
2015-11-08 18:25:55 +01:00
|
|
|
|
this.cleanSession = cleanSession;
|
2017-04-04 21:29:38 +02:00
|
|
|
|
this.outboundFlightZone = new OutboundFlightZone();
|
|
|
|
|
|
this.inboundFlightZone = new InboundFlightZone();
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-02-01 15:45:06 +01:00
|
|
|
|
* Return the list of persisted publishes for the given clientID. For QoS1 and QoS2 with clean
|
|
|
|
|
|
* session flag, this method return the list of missed publish events while the client was
|
|
|
|
|
|
* disconnected.
|
2017-01-09 21:53:26 +01:00
|
|
|
|
*
|
2015-10-24 16:26:43 +02:00
|
|
|
|
* @return the list of messages to be delivered for client related to the session.
|
2017-02-01 15:45:06 +01:00
|
|
|
|
*/
|
2017-06-19 22:40:34 +02:00
|
|
|
|
public Queue<IMessagesStore.StoredMessage> queue() {
|
|
|
|
|
|
LOG.info("Retrieving enqueued messages. CId={}", clientID);
|
2017-01-09 21:53:26 +01:00
|
|
|
|
return this.m_sessionsStore.queue(clientID);
|
2015-10-24 16:26:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String toString() {
|
2017-02-01 15:45:06 +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) {
|
2017-04-04 21:29:38 +02:00
|
|
|
|
LOG.info("Adding new subscription. ClientId={}, topics={}, qos={}", newSubscription.getClientId(),
|
|
|
|
|
|
newSubscription.getTopicFilter(), newSubscription.getRequestedQos());
|
2017-02-14 17:23:04 +01:00
|
|
|
|
boolean validTopic = newSubscription.getTopicFilter().isValid();
|
2015-10-31 11:14:40 +01:00
|
|
|
|
if (!validTopic) {
|
2017-04-04 21:29:38 +02:00
|
|
|
|
LOG.warn("The topic filter is not valid. ClientId={}, topics={}", newSubscription.getClientId(),
|
|
|
|
|
|
newSubscription.getTopicFilter());
|
2017-02-01 15:45:06 +01:00
|
|
|
|
// send SUBACK with 0x80 for this topic filter
|
2015-10-31 11:14:40 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2015-12-06 19:31:36 +01:00
|
|
|
|
ClientTopicCouple matchingCouple = new ClientTopicCouple(this.clientID, newSubscription.getTopicFilter());
|
2017-04-20 23:14:23 +02:00
|
|
|
|
Subscription existingSub = subscriptionsStore.getSubscription(matchingCouple);
|
2017-02-01 15:45:06 +01:00
|
|
|
|
// update the selected subscriptions if not present or if has a greater qos
|
2017-01-28 10:06:30 +01:00
|
|
|
|
if (existingSub == null || existingSub.getRequestedQos().value() < newSubscription.getRequestedQos().value()) {
|
2015-12-06 11:43:29 +01:00
|
|
|
|
if (existingSub != null) {
|
2017-03-25 16:05:45 +01:00
|
|
|
|
LOG.info("Subscription already existed with a lower QoS value. It will be updated. ClientId={}, " +
|
|
|
|
|
|
"topics={}, existingQos={}, newQos={}", newSubscription.getClientId(),
|
|
|
|
|
|
newSubscription.getTopicFilter(), existingSub.getRequestedQos(), newSubscription.getRequestedQos());
|
2015-12-06 11:43:29 +01:00
|
|
|
|
subscriptions.remove(newSubscription);
|
|
|
|
|
|
}
|
|
|
|
|
|
subscriptions.add(newSubscription);
|
2017-04-20 23:14:23 +02:00
|
|
|
|
subscriptionsStore.addNewSubscription(newSubscription);
|
2015-12-06 11:43:29 +01:00
|
|
|
|
}
|
2015-10-31 11:14:40 +01:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2015-10-31 11:34:15 +01:00
|
|
|
|
|
2017-02-14 17:23:04 +01:00
|
|
|
|
public void unsubscribeFrom(Topic topicFilter) {
|
2017-03-25 16:05:45 +01:00
|
|
|
|
LOG.info("Removing subscription. ClientID={}, topics={}", clientID, topicFilter);
|
2017-04-20 23:14:23 +02:00
|
|
|
|
subscriptionsStore.removeSubscription(topicFilter, clientID);
|
2015-10-31 11:34:15 +01:00
|
|
|
|
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) {
|
2017-03-25 16:05:45 +01:00
|
|
|
|
LOG.info("Client disconnected. Removing its subscriptions. ClientId={}", clientID);
|
2017-02-01 15:45:06 +01:00
|
|
|
|
// cleanup topic subscriptions
|
2015-11-08 18:25:55 +01:00
|
|
|
|
cleanSession();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void cleanSession() {
|
2017-05-31 18:37:05 +02:00
|
|
|
|
m_sessionsStore.cleanSession(this.clientID);
|
2015-11-08 18:25:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
public IMessagesStore.StoredMessage inFlightAcknowledged(int messageID) {
|
|
|
|
|
|
return outboundFlightZone.acknowledged(messageID);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-04 21:29:38 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Mark the message identified by guid as publish in flight.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return the packetID for the message in flight.
|
|
|
|
|
|
* */
|
2017-04-20 23:14:23 +02:00
|
|
|
|
public int inFlightAckWaiting(IMessagesStore.StoredMessage msg) {
|
|
|
|
|
|
LOG.debug("Adding message ot inflight zone. ClientId={}", clientID);
|
2017-04-04 21:29:38 +02:00
|
|
|
|
int messageId = ClientSession.this.nextPacketId();
|
2017-04-20 23:14:23 +02:00
|
|
|
|
outboundFlightZone.waitingAck(messageId, msg);
|
2017-04-04 21:29:38 +02:00
|
|
|
|
return messageId;
|
2015-11-15 11:16:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-16 23:01:48 +02:00
|
|
|
|
public IMessagesStore.StoredMessage secondPhaseAcknowledged(int messageID) {
|
2017-04-20 23:14:23 +02:00
|
|
|
|
return m_sessionsStore.secondPhaseAcknowledged(clientID, messageID);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-09 21:53:26 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Enqueue a message to be sent to the client.
|
2017-02-01 15:45:06 +01:00
|
|
|
|
*
|
|
|
|
|
|
* @param message
|
|
|
|
|
|
* the message to enqueue.
|
|
|
|
|
|
*/
|
2017-01-09 21:53:26 +01:00
|
|
|
|
public void enqueue(IMessagesStore.StoredMessage message) {
|
|
|
|
|
|
this.m_sessionsStore.queue(this.clientID).add(message);
|
2015-11-15 11:16:18 +01:00
|
|
|
|
}
|
2015-11-16 23:45:47 +01:00
|
|
|
|
|
2017-04-04 21:29:38 +02:00
|
|
|
|
public IMessagesStore.StoredMessage inboundInflight(int messageID) {
|
|
|
|
|
|
return inboundFlightZone.lookup(messageID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
public void markAsInboundInflight(int messageID, IMessagesStore.StoredMessage msg) {
|
|
|
|
|
|
inboundFlightZone.waitingRel(messageID, msg);
|
2015-11-16 23:45:47 +01:00
|
|
|
|
}
|
2016-03-06 19:50:26 +01:00
|
|
|
|
|
2017-04-20 23:14:23 +02:00
|
|
|
|
public void moveInFlightToSecondPhaseAckWaiting(int messageID, IMessagesStore.StoredMessage msg) {
|
|
|
|
|
|
m_sessionsStore.moveInFlightToSecondPhaseAckWaiting(this.clientID, messageID, msg);
|
2016-06-16 23:01:48 +02:00
|
|
|
|
}
|
2017-01-12 17:00:18 +01:00
|
|
|
|
|
|
|
|
|
|
public Set<Subscription> getSubscriptions() {
|
|
|
|
|
|
return subscriptions;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getPendingPublishMessagesNo() {
|
|
|
|
|
|
return m_sessionsStore.getPendingPublishMessagesNo(clientID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getSecondPhaseAckPendingMessages() {
|
|
|
|
|
|
return m_sessionsStore.getSecondPhaseAckPendingMessages(clientID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int getInflightMessagesNo() {
|
|
|
|
|
|
return m_sessionsStore.getInflightMessagesNo(clientID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-01 15:45:06 +01:00
|
|
|
|
}
|