改了一波
This commit is contained in:
@@ -1,23 +1,37 @@
|
||||
package org.codenil.comm.connections;
|
||||
|
||||
import org.codenil.comm.message.DisconnectMessage;
|
||||
import org.codenil.comm.message.DisconnectReason;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import org.codenil.comm.RemotePeer;
|
||||
import org.codenil.comm.message.Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public abstract class AbstractPeerConnection implements PeerConnection {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AbstractPeerConnection.class);
|
||||
|
||||
protected final PeerConnectionEvents connectionEvents;
|
||||
private final AtomicBoolean disconnected = new AtomicBoolean(false);
|
||||
private final AtomicBoolean terminatedImmediately = new AtomicBoolean(false);
|
||||
private final String remoteIdentifier;
|
||||
|
||||
protected AbstractPeerConnection(final PeerConnectionEvents connectionEvents) {
|
||||
this.connectionEvents = connectionEvents;
|
||||
protected final AtomicBoolean disconnected = new AtomicBoolean(false);
|
||||
|
||||
protected final AtomicBoolean terminatedImmediately = new AtomicBoolean(false);
|
||||
|
||||
private RemotePeer remotePeer;
|
||||
|
||||
protected AbstractPeerConnection(
|
||||
final String remoteIdentifier) {
|
||||
this.remoteIdentifier = remoteIdentifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String pkiId() {
|
||||
if(Objects.isNull(remotePeer)) {
|
||||
throw new IllegalStateException("connection not complated yet");
|
||||
}
|
||||
return remotePeer.pkiId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -26,32 +40,34 @@ public abstract class AbstractPeerConnection implements PeerConnection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void terminateConnection() {
|
||||
if (terminatedImmediately.compareAndSet(false, true)) {
|
||||
if (disconnected.compareAndSet(false, true)) {
|
||||
connectionEvents.dispatchDisconnect(this);
|
||||
}
|
||||
// Always ensure the context gets closed immediately even if we previously sent a disconnect
|
||||
// message and are waiting to close.
|
||||
closeConnectionImmediately();
|
||||
logger.atTrace()
|
||||
.setMessage("Terminating connection, reason {}")
|
||||
.addArgument(this)
|
||||
.log();
|
||||
}
|
||||
public String remoteIdentifier() {
|
||||
return remoteIdentifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(DisconnectReason reason) {
|
||||
if (disconnected.compareAndSet(false, true)) {
|
||||
connectionEvents.dispatchDisconnect(this);
|
||||
doSendMessage(DisconnectMessage.create(reason));
|
||||
closeConnection();
|
||||
}
|
||||
public boolean disconnected() {
|
||||
return disconnected.get() || terminatedImmediately.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RemotePeer remotePeer() {
|
||||
return remotePeer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRemotePeer(RemotePeer remotePeer) {
|
||||
this.remotePeer = remotePeer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceHandler(String name, ChannelHandler newHandler) {
|
||||
doReplaceHandler(name, newHandler);
|
||||
}
|
||||
|
||||
protected abstract void doSendMessage(final Message message);
|
||||
|
||||
protected abstract void doReplaceHandler(String name, ChannelHandler newHandler);
|
||||
|
||||
protected abstract void closeConnection();
|
||||
|
||||
protected abstract void closeConnectionImmediately();
|
||||
|
||||
Reference in New Issue
Block a user