2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2017-03-19 22:01:23 +01:00
|
|
|
* Copyright (c) 2012-2017 The original author or authors
|
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:19:03 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server;
|
2012-07-11 23:44:05 +02:00
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
import java.util.NoSuchElementException;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-01-12 17:25:46 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2016-11-16 22:18:30 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-01-11 11:23:24 +01:00
|
|
|
import io.moquette.server.netty.AutoFlushHandler;
|
|
|
|
|
import io.moquette.server.netty.NettyUtils;
|
2017-01-12 17:25:46 +01:00
|
|
|
import io.moquette.server.netty.metrics.BytesMetrics;
|
|
|
|
|
import io.moquette.server.netty.metrics.BytesMetricsHandler;
|
|
|
|
|
import io.moquette.server.netty.metrics.MessageMetrics;
|
|
|
|
|
import io.moquette.server.netty.metrics.MessageMetricsHandler;
|
|
|
|
|
import io.netty.channel.Channel;
|
2017-01-11 11:23:24 +01:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
import static io.netty.channel.ChannelFutureListener.CLOSE;
|
|
|
|
|
import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE;
|
|
|
|
|
import static io.netty.channel.ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE;
|
|
|
|
|
|
2012-07-11 23:44:05 +02:00
|
|
|
/**
|
2017-02-01 15:19:03 +01:00
|
|
|
* Value object to maintain the information of single connection, like ClientID, Channel, and clean
|
|
|
|
|
* session flag.
|
2012-07-11 23:44:05 +02:00
|
|
|
*/
|
|
|
|
|
public class ConnectionDescriptor {
|
2016-11-16 22:18:30 +01:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ConnectionDescriptor.class);
|
|
|
|
|
|
|
|
|
|
public enum ConnectionState {
|
2017-02-01 15:19:03 +01:00
|
|
|
// Connection states
|
|
|
|
|
DISCONNECTED,
|
|
|
|
|
SENDACK,
|
|
|
|
|
SESSION_CREATED,
|
|
|
|
|
MESSAGES_REPUBLISHED,
|
|
|
|
|
ESTABLISHED,
|
|
|
|
|
// Disconnection states
|
|
|
|
|
SUBSCRIPTIONS_REMOVED,
|
|
|
|
|
MESSAGES_DROPPED,
|
|
|
|
|
INTERCEPTORS_NOTIFIED;
|
2016-11-16 22:18:30 +01:00
|
|
|
}
|
2016-11-27 19:34:51 +01:00
|
|
|
|
2015-11-10 22:24:35 +01:00
|
|
|
public final String clientID;
|
2017-01-11 11:23:24 +01:00
|
|
|
private final Channel channel;
|
2015-11-10 22:24:35 +01:00
|
|
|
public final boolean cleanSession;
|
2016-11-16 22:18:30 +01:00
|
|
|
private final AtomicReference<ConnectionState> channelState = new AtomicReference<>(ConnectionState.DISCONNECTED);
|
2016-11-27 19:34:51 +01:00
|
|
|
|
2016-01-17 11:11:36 +01:00
|
|
|
public ConnectionDescriptor(String clientID, Channel session, boolean cleanSession) {
|
2015-11-10 22:24:35 +01:00
|
|
|
this.clientID = clientID;
|
2016-01-24 19:17:31 +01:00
|
|
|
this.channel = session;
|
2015-11-10 22:24:35 +01:00
|
|
|
this.cleanSession = cleanSession;
|
2012-07-11 23:44:05 +02:00
|
|
|
}
|
2017-02-01 15:19:03 +01:00
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
public void writeAndFlush(Object payload) {
|
2018-04-25 19:39:12 +02:00
|
|
|
this.channel.writeAndFlush(payload).addListener(FIRE_EXCEPTION_ON_FAILURE);
|
2017-01-11 11:23:24 +01:00
|
|
|
}
|
2017-02-01 15:19:03 +01:00
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
public boolean doesNotUseChannel(Channel channel) {
|
2017-02-01 15:19:03 +01:00
|
|
|
return !(this.channel.equals(channel));
|
2017-01-11 11:23:24 +01:00
|
|
|
}
|
2017-02-01 15:19:03 +01:00
|
|
|
|
|
|
|
|
public boolean close() {
|
2018-04-27 15:56:26 +02:00
|
|
|
LOG.debug("Closing connection descriptor. MqttClientId = {}.", clientID);
|
2017-02-01 15:19:03 +01:00
|
|
|
final boolean success = assignState(ConnectionState.INTERCEPTORS_NOTIFIED, ConnectionState.DISCONNECTED);
|
|
|
|
|
if (!success) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-04-25 19:39:12 +02:00
|
|
|
this.channel.close().addListener(CLOSE_ON_FAILURE);
|
2017-02-01 15:19:03 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
public String getUsername() {
|
2017-02-01 15:19:03 +01:00
|
|
|
return NettyUtils.userName(this.channel);
|
2017-01-11 11:23:24 +01:00
|
|
|
}
|
2013-12-14 12:33:38 +01:00
|
|
|
|
2016-11-16 22:18:30 +01:00
|
|
|
public void abort() {
|
2017-02-01 15:19:03 +01:00
|
|
|
LOG.info("Closing connection descriptor. MqttClientId = {}.", clientID);
|
|
|
|
|
// try {
|
|
|
|
|
// this.channel.disconnect().sync();
|
2018-04-25 19:39:12 +02:00
|
|
|
this.channel.close().addListener(CLOSE_ON_FAILURE); // .sync();
|
2017-02-01 15:19:03 +01:00
|
|
|
// } catch (InterruptedException e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
2016-11-16 22:18:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean assignState(ConnectionState expected, ConnectionState newState) {
|
2018-04-27 15:56:26 +02:00
|
|
|
LOG.debug("Updating state of connection descriptor. MqttClientId = {}, expectedState = {}, newState = {}.",
|
|
|
|
|
clientID, expected, newState);
|
2017-02-01 15:19:03 +01:00
|
|
|
boolean retval = channelState.compareAndSet(expected, newState);
|
|
|
|
|
if (!retval) {
|
2018-04-27 15:56:26 +02:00
|
|
|
LOG.error("Unable to update state of connection descriptor. CId = {}, expectedState = {}, newState = {}.",
|
|
|
|
|
clientID, expected, newState);
|
2017-02-01 15:19:03 +01:00
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
2016-11-16 22:18:30 +01:00
|
|
|
|
2013-12-14 12:33:38 +01:00
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
2017-09-12 19:48:42 +02:00
|
|
|
return "ConnectionDescriptor{" + "clientID=" + clientID + ", removeTemporaryQoS2=" + cleanSession + ", state="
|
2017-02-01 15:19:03 +01:00
|
|
|
+ channelState.get() + '}';
|
2013-12-14 12:33:38 +01:00
|
|
|
}
|
2015-12-27 13:19:16 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
2017-02-01 15:19:03 +01:00
|
|
|
if (this == o)
|
|
|
|
|
return true;
|
|
|
|
|
if (o == null || getClass() != o.getClass())
|
|
|
|
|
return false;
|
2015-12-27 13:19:16 +01:00
|
|
|
|
|
|
|
|
ConnectionDescriptor that = (ConnectionDescriptor) o;
|
|
|
|
|
|
2017-02-01 15:19:03 +01:00
|
|
|
if (clientID != null ? !clientID.equals(that.clientID) : that.clientID != null)
|
|
|
|
|
return false;
|
2016-01-24 19:17:31 +01:00
|
|
|
return !(channel != null ? !channel.equals(that.channel) : that.channel != null);
|
2015-12-27 13:19:16 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-12 17:25:46 +01:00
|
|
|
public BytesMetrics getBytesMetrics() {
|
|
|
|
|
return BytesMetricsHandler.getBytesMetrics(channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MessageMetrics getMessageMetrics() {
|
|
|
|
|
return MessageMetricsHandler.getMessageMetrics(channel);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 13:19:16 +01:00
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
int result = clientID != null ? clientID.hashCode() : 0;
|
2016-01-24 19:17:31 +01:00
|
|
|
result = 31 * result + (channel != null ? channel.hashCode() : 0);
|
2015-12-27 13:19:16 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
2012-07-11 23:44:05 +02:00
|
|
|
}
|