2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2015-08-08 19:36:14 +02:00
|
|
|
* Copyright (c) 2012-2015 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.
|
|
|
|
|
*/
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server;
|
2012-07-11 23:44:05 +02:00
|
|
|
|
2016-01-17 11:11:36 +01:00
|
|
|
import io.netty.channel.Channel;
|
2016-11-16 22:18:30 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2016-01-17 11:11:36 +01:00
|
|
|
|
2012-07-11 23:44:05 +02:00
|
|
|
/**
|
2016-11-05 18:16:59 +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
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @author andrea
|
|
|
|
|
*/
|
|
|
|
|
public class ConnectionDescriptor {
|
2016-11-16 22:18:30 +01:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ConnectionDescriptor.class);
|
|
|
|
|
|
|
|
|
|
public enum ConnectionState {
|
2016-11-17 22:26:26 +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
|
|
|
}
|
2012-07-11 23:44:05 +02:00
|
|
|
|
2015-11-10 22:24:35 +01:00
|
|
|
public final String clientID;
|
2016-01-24 19:17:31 +01:00
|
|
|
public 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);
|
2012-07-11 23:44:05 +02: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
|
|
|
}
|
2013-12-14 12:33:38 +01:00
|
|
|
|
2016-11-16 22:18:30 +01:00
|
|
|
public void abort() {
|
|
|
|
|
//TODO fire and event to the channel or simply close it?
|
|
|
|
|
LOG.error("TODO send an abort event to the channel");
|
|
|
|
|
this.channel.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean assignState(ConnectionState expected, ConnectionState newState) {
|
|
|
|
|
return channelState.compareAndSet(expected, newState);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-14 12:33:38 +01:00
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
2016-11-16 22:18:30 +01:00
|
|
|
return "ConnectionDescriptor{" + "clientID=" + clientID +
|
|
|
|
|
", cleanSession=" + cleanSession +
|
|
|
|
|
", state=" + channelState.get() +
|
|
|
|
|
'}';
|
2013-12-14 12:33:38 +01:00
|
|
|
}
|
2015-12-27 13:19:16 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
if (this == o) return true;
|
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
|
|
ConnectionDescriptor that = (ConnectionDescriptor) o;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
}
|