View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18
    19  package org.activeio.net;
    20
    21  import java.io.IOException;
    22  import java.net.InetAddress;
    23  import java.net.Socket;
    24  import java.net.SocketAddress;
    25  import java.net.SocketException;
    26  import java.nio.ByteBuffer;
    27  import java.nio.channels.SocketChannel;
    28
    29  import org.activeio.Disposable;
    30  import org.activeio.packet.ByteBufferPacket;
    31
    32  /***
    33   * Base class for the Asynch and Synch implementations of NIO channels.
    34   *
    35   * @version $Revision$
    36   */
    37  public class NIOBaseChannel implements SocketMetadata, Disposable {
    38
    39  	protected final SocketChannel socketChannel;
    40      protected final Socket socket;
    41  	private final boolean useDirect;
    42      private int curentSoTimeout;
    43  	private boolean disposed;
    44      private final String name;
    45
    46      protected NIOBaseChannel(SocketChannel socketChannel, boolean useDirect) throws IOException {
    47          this.socketChannel = socketChannel;
    48  		this.useDirect = useDirect;
    49  		this.socket = this.socketChannel.socket();
    50
    51          if( useDirect ) {
    52              socket.setSendBufferSize(ByteBufferPacket.DEFAULT_DIRECT_BUFFER_SIZE);
    53              socket.setReceiveBufferSize(ByteBufferPacket.DEFAULT_DIRECT_BUFFER_SIZE);
    54          } else {
    55              socket.setSendBufferSize(ByteBufferPacket.DEFAULT_BUFFER_SIZE);
    56              socket.setReceiveBufferSize(ByteBufferPacket.DEFAULT_BUFFER_SIZE);
    57          }
    58
    59          this.name = "NIO Socket Connection: "+getLocalSocketAddress()+" -> "+getRemoteSocketAddress();
    60      }
    61
    62      protected ByteBuffer allocateBuffer() {
    63          if( useDirect ) {
    64              return ByteBuffer.allocateDirect(ByteBufferPacket.DEFAULT_DIRECT_BUFFER_SIZE);
    65          } else {
    66              return ByteBuffer.allocate(ByteBufferPacket.DEFAULT_BUFFER_SIZE);
    67          }
    68      }
    69
    70      protected void setSoTimeout(int i) throws SocketException {
    71          if( curentSoTimeout != i ) {
    72              socket.setSoTimeout(i);
    73              curentSoTimeout = i;
    74          }
    75      }
    76      public Object narrow(Class target) {
    77          if( target.isAssignableFrom(getClass()) ) {
    78              return this;
    79          }
    80          return null;
    81      }
    82
    83      public String toString() {
    84          return name;
    85      }
    86
    87  	public void dispose() {
    88          if (disposed)
    89              return;
    90
    91          try {
    92              socketChannel.close();
    93          } catch (IOException ignore) {
    94          }
    95          disposed = true;
    96  	}
    97
    98      /***
    99       * @see org.activeio.Channel#flush()
    100      */
    101     public void flush() throws IOException {
    102     }
    103
    104     public InetAddress getInetAddress() {
    105         return socket.getInetAddress();
    106     }
    107     public boolean getKeepAlive() throws SocketException {
    108         return socket.getKeepAlive();
    109     }
    110     public InetAddress getLocalAddress() {
    111         return socket.getLocalAddress();
    112     }
    113     public int getLocalPort() {
    114         return socket.getLocalPort();
    115     }
    116     public SocketAddress getLocalSocketAddress() {
    117         return socket.getLocalSocketAddress();
    118     }
    119     public boolean getOOBInline() throws SocketException {
    120         return socket.getOOBInline();
    121     }
    122     public int getPort() {
    123         return socket.getPort();
    124     }
    125     public int getReceiveBufferSize() throws SocketException {
    126         return socket.getReceiveBufferSize();
    127     }
    128     public SocketAddress getRemoteSocketAddress() {
    129         return socket.getRemoteSocketAddress();
    130     }
    131     public boolean getReuseAddress() throws SocketException {
    132         return socket.getReuseAddress();
    133     }
    134     public int getSendBufferSize() throws SocketException {
    135         return socket.getSendBufferSize();
    136     }
    137     public int getSoLinger() throws SocketException {
    138         return socket.getSoLinger();
    139     }
    140     public int getSoTimeout() throws SocketException {
    141         return socket.getSoTimeout();
    142     }
    143     public boolean getTcpNoDelay() throws SocketException {
    144         return socket.getTcpNoDelay();
    145     }
    146     public int getTrafficClass() throws SocketException {
    147         return socket.getTrafficClass();
    148     }
    149     public boolean isBound() {
    150         return socket.isBound();
    151     }
    152     public boolean isClosed() {
    153         return socket.isClosed();
    154     }
    155     public boolean isConnected() {
    156         return socket.isConnected();
    157     }
    158     public void setKeepAlive(boolean on) throws SocketException {
    159         socket.setKeepAlive(on);
    160     }
    161     public void setOOBInline(boolean on) throws SocketException {
    162         socket.setOOBInline(on);
    163     }
    164     public void setReceiveBufferSize(int size) throws SocketException {
    165         socket.setReceiveBufferSize(size);
    166     }
    167     public void setReuseAddress(boolean on) throws SocketException {
    168         socket.setReuseAddress(on);
    169     }
    170     public void setSendBufferSize(int size) throws SocketException {
    171         socket.setSendBufferSize(size);
    172     }
    173     public void setSoLinger(boolean on, int linger) throws SocketException {
    174         socket.setSoLinger(on, linger);
    175     }
    176     public void setTcpNoDelay(boolean on) throws SocketException {
    177         socket.setTcpNoDelay(on);
    178     }
    179     public void setTrafficClass(int tc) throws SocketException {
    180         socket.setTrafficClass(tc);
    181     }
    182 }