View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.transport.socket.nio;
    21
    22  import java.net.InetSocketAddress;
    23  import java.net.Socket;
    24  import java.net.SocketException;
    25  import java.nio.channels.SocketChannel;
    26
    27  import org.apache.mina.core.RuntimeIoException;
    28  import org.apache.mina.core.buffer.IoBuffer;
    29  import org.apache.mina.core.file.FileRegion;
    30  import org.apache.mina.core.service.DefaultTransportMetadata;
    31  import org.apache.mina.core.service.IoProcessor;
    32  import org.apache.mina.core.service.IoService;
    33  import org.apache.mina.core.service.TransportMetadata;
    34  import org.apache.mina.core.session.IoSession;
    35  import org.apache.mina.transport.socket.AbstractSocketSessionConfig;
    36  import org.apache.mina.transport.socket.SocketSessionConfig;
    37
    38  /**
    39   * An {@link IoSession} for socket transport (TCP/IP).
    40   *
    41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    42   */
    43  class NioSocketSession extends NioSession {
    44      static final TransportMetadata METADATA = new DefaultTransportMetadata("nio", "socket", false, true,
    45              InetSocketAddress.class, SocketSessionConfig.class, IoBuffer.class, FileRegion.class);
    46
    47      private Socket getSocket() {
    48          return ((SocketChannel) channel).socket();
    49      }
    50
    51      /**
    52       *
    53       * Creates a new instance of NioSocketSession.
    54       *
    55       * @param service the associated IoService
    56       * @param processor the associated IoProcessor
    57       * @param ch the used channel
    58       */
    59      public NioSocketSession(IoService service, IoProcessor<NioSession> processor, SocketChannel channel) {
    60          super(processor, service, channel);
    61          config = new SessionConfigImpl();
    62          this.config.setAll(service.getSessionConfig());
    63      }
    64
    65      public TransportMetadata getTransportMetadata() {
    66          return METADATA;
    67      }
    68
    69      /**
    70       * {@inheritDoc}
    71       */
    72      public SocketSessionConfig getConfig() {
    73          return (SocketSessionConfig) config;
    74      }
    75
    76      @Override
    77      SocketChannel getChannel() {
    78          return (SocketChannel) channel;
    79      }
    80
    81      /**
    82       * {@inheritDoc}
    83       */
    84      public InetSocketAddress getRemoteAddress() {
    85          if (channel == null) {
    86              return null;
    87          }
    88
    89          Socket socket = getSocket();
    90
    91          if (socket == null) {
    92              return null;
    93          }
    94
    95          return (InetSocketAddress) socket.getRemoteSocketAddress();
    96      }
    97
    98      /**
    99       * {@inheritDoc}
    100      */
    101     public InetSocketAddress getLocalAddress() {
    102         if (channel == null) {
    103             return null;
    104         }
    105
    106         Socket socket = getSocket();
    107
    108         if (socket == null) {
    109             return null;
    110         }
    111
    112         return (InetSocketAddress) socket.getLocalSocketAddress();
    113     }
    114
    115     @Override
    116     public InetSocketAddress getServiceAddress() {
    117         return (InetSocketAddress) super.getServiceAddress();
    118     }
    119
    120     private class SessionConfigImpl extends AbstractSocketSessionConfig {
    121         public boolean isKeepAlive() {
    122             try {
    123                 return getSocket().getKeepAlive();
    124             } catch (SocketException e) {
    125                 throw new RuntimeIoException(e);
    126             }
    127         }
    128
    129         public void setKeepAlive(boolean on) {
    130             try {
    131                 getSocket().setKeepAlive(on);
    132             } catch (SocketException e) {
    133                 throw new RuntimeIoException(e);
    134             }
    135         }
    136
    137         public boolean isOobInline() {
    138             try {
    139                 return getSocket().getOOBInline();
    140             } catch (SocketException e) {
    141                 throw new RuntimeIoException(e);
    142             }
    143         }
    144
    145         public void setOobInline(boolean on) {
    146             try {
    147                 getSocket().setOOBInline(on);
    148             } catch (SocketException e) {
    149                 throw new RuntimeIoException(e);
    150             }
    151         }
    152
    153         public boolean isReuseAddress() {
    154             try {
    155                 return getSocket().getReuseAddress();
    156             } catch (SocketException e) {
    157                 throw new RuntimeIoException(e);
    158             }
    159         }
    160
    161         public void setReuseAddress(boolean on) {
    162             try {
    163                 getSocket().setReuseAddress(on);
    164             } catch (SocketException e) {
    165                 throw new RuntimeIoException(e);
    166             }
    167         }
    168
    169         public int getSoLinger() {
    170             try {
    171                 return getSocket().getSoLinger();
    172             } catch (SocketException e) {
    173                 throw new RuntimeIoException(e);
    174             }
    175         }
    176
    177         public void setSoLinger(int linger) {
    178             try {
    179                 if (linger < 0) {
    180                     getSocket().setSoLinger(false, 0);
    181                 } else {
    182                     getSocket().setSoLinger(true, linger);
    183                 }
    184             } catch (SocketException e) {
    185                 throw new RuntimeIoException(e);
    186             }
    187         }
    188
    189         public boolean isTcpNoDelay() {
    190             if (!isConnected()) {
    191                 return false;
    192             }
    193
    194             try {
    195                 return getSocket().getTcpNoDelay();
    196             } catch (SocketException e) {
    197                 throw new RuntimeIoException(e);
    198             }
    199         }
    200
    201         public void setTcpNoDelay(boolean on) {
    202             try {
    203                 getSocket().setTcpNoDelay(on);
    204             } catch (SocketException e) {
    205                 throw new RuntimeIoException(e);
    206             }
    207         }
    208
    209         /**
    210          * {@inheritDoc}
    211          */
    212         public int getTrafficClass() {
    213             try {
    214                 return getSocket().getTrafficClass();
    215             } catch (SocketException e) {
    216                 throw new RuntimeIoException(e);
    217             }
    218         }
    219
    220         /**
    221          * {@inheritDoc}
    222          */
    223         public void setTrafficClass(int tc) {
    224             try {
    225                 getSocket().setTrafficClass(tc);
    226             } catch (SocketException e) {
    227                 throw new RuntimeIoException(e);
    228             }
    229         }
    230
    231         public int getSendBufferSize() {
    232             try {
    233                 return getSocket().getSendBufferSize();
    234             } catch (SocketException e) {
    235                 throw new RuntimeIoException(e);
    236             }
    237         }
    238
    239         public void setSendBufferSize(int size) {
    240             try {
    241                 getSocket().setSendBufferSize(size);
    242             } catch (SocketException e) {
    243                 throw new RuntimeIoException(e);
    244             }
    245         }
    246
    247         public int getReceiveBufferSize() {
    248             try {
    249                 return getSocket().getReceiveBufferSize();
    250             } catch (SocketException e) {
    251                 throw new RuntimeIoException(e);
    252             }
    253         }
    254
    255         public void setReceiveBufferSize(int size) {
    256             try {
    257                 getSocket().setReceiveBufferSize(size);
    258             } catch (SocketException e) {
    259                 throw new RuntimeIoException(e);
    260             }
    261         }
    262     }
    263 }