001 package toolbus.communication;
002
003 import java.net.Socket;
004 import java.nio.channels.SocketChannel;
005
006 import toolbus.logging.ILogger;
007 import toolbus.logging.IToolBusLoggerConstants;
008 import toolbus.logging.LoggerFactory;
009
010 /**
011 * Interface that defines all methods that are required for connection handlers. They need to
012 * facilitate in obtaining a reference to read and write multiplexers and supply methods for closing
013 * a connection.
014 *
015 * @author Arnold Lankamp
016 */
017 public abstract class AbstractConnectionHandler{
018
019 /**
020 * Returns a reference to the read multiplexer.
021 *
022 * @return A reference to the read multiplexer.
023 */
024 public abstract IReadMultiplexer getReadMultiplexer();
025
026 /**
027 * Returns a reference to the write multiplexer.
028 *
029 * @return A reference to the write multiplexer.
030 */
031 public abstract IWriteMultiplexer getWriteMultiplexer();
032
033 /**
034 * Closes the connection associated with the given socket channel. This method executes a
035 * controlled shutdown of the connection. The socket channel will be deregistered (if needed)
036 * and will be closed.
037 *
038 * @param socketChannel
039 * The socket channel, who's associated socket should be closed.
040 */
041 public abstract void closeConnection(SocketChannel socketChannel);
042
043 /**
044 * Closes the connection associated with the given socket channel. This method should be invoked
045 * after an error occurs during the usage of a socket / socket channel.
046 *
047 * @param socketChannel
048 * The socket channel, who's associated socket should be closed.
049 * @param socketIOHandler
050 * The socket I/O handler associated with the connection that was disconnected.
051 */
052 public void closeDueToException(SocketChannel socketChannel, SocketIOHandler socketIOHandler){
053 socketIOHandler.exceptionOccured();
054
055 Socket socket = socketChannel.socket();
056 LoggerFactory.log("The connection with the application running on: " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " will be closed, because it caused an exception.", ILogger.ERROR, IToolBusLoggerConstants.COMMUNICATION);
057
058 closeConnection(socketChannel);
059 }
060
061 /**
062 * Closes the connection associated with the given socket channel. This method should be invoked
063 * when an unexpected disconnect occurred on the given socket / socket channel.
064 *
065 * @param socketChannel
066 * The socket channel, who's associated socket should be closed.
067 * @param socketIOHandler
068 * The socket I/O handler associated with the connection that was disconnected.
069 */
070 public void closeDueToDisconnect(SocketChannel socketChannel, SocketIOHandler socketIOHandler){
071 socketIOHandler.exceptionOccured();
072
073 Socket socket = socketChannel.socket();
074 LoggerFactory.log("The application running on: " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + ", was disconnected. The connection will now be closed.", ILogger.ERROR, IToolBusLoggerConstants.COMMUNICATION);
075
076 closeConnection(socketChannel);
077 }
078 }