View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.channel.socket.oio;
    17
    18  import java.io.OutputStream;
    19  import java.io.PushbackInputStream;
    20  import java.net.InetSocketAddress;
    21  import java.net.Socket;
    22  import java.net.SocketAddress;
    23
    24  import org.jboss.netty.channel.AbstractChannel;
    25  import org.jboss.netty.channel.Channel;
    26  import org.jboss.netty.channel.ChannelFactory;
    27  import org.jboss.netty.channel.ChannelFuture;
    28  import org.jboss.netty.channel.ChannelPipeline;
    29  import org.jboss.netty.channel.ChannelSink;
    30  import org.jboss.netty.channel.socket.DefaultSocketChannelConfig;
    31  import org.jboss.netty.channel.socket.SocketChannel;
    32  import org.jboss.netty.channel.socket.SocketChannelConfig;
    33
    34  /**
    35   *
    36   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    37   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    38   *
    39   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    40   *
    41   */
    42  abstract class OioSocketChannel extends AbstractChannel
    43                                  implements SocketChannel {
    44
    45      final Socket socket;
    46      final Object interestOpsLock = new Object();
    47      private final SocketChannelConfig config;
    48      volatile Thread workerThread;
    49      private volatile InetSocketAddress localAddress;
    50      private volatile InetSocketAddress remoteAddress;
    51
    52      OioSocketChannel(
    53              Channel parent,
    54              ChannelFactory factory,
    55              ChannelPipeline pipeline,
    56              ChannelSink sink,
    57              Socket socket) {
    58
    59          super(parent, factory, pipeline, sink);
    60
    61          this.socket = socket;
    62          config = new DefaultSocketChannelConfig(socket);
    63      }
    64
    65      public SocketChannelConfig getConfig() {
    66          return config;
    67      }
    68
    69      public InetSocketAddress getLocalAddress() {
    70          InetSocketAddress localAddress = this.localAddress;
    71          if (localAddress == null) {
    72              try {
    73                  this.localAddress = localAddress =
    74                      (InetSocketAddress) socket.getLocalSocketAddress();
    75              } catch (Throwable t) {
    76                  // Sometimes fails on a closed socket in Windows.
    77                  return null;
    78              }
    79          }
    80          return localAddress;
    81      }
    82
    83      public InetSocketAddress getRemoteAddress() {
    84          InetSocketAddress remoteAddress = this.remoteAddress;
    85          if (remoteAddress == null) {
    86              try {
    87                  this.remoteAddress = remoteAddress =
    88                      (InetSocketAddress) socket.getRemoteSocketAddress();
    89              } catch (Throwable t) {
    90                  // Sometimes fails on a closed socket in Windows.
    91                  return null;
    92              }
    93          }
    94          return remoteAddress;
    95      }
    96
    97      public boolean isBound() {
    98          return isOpen() && socket.isBound();
    99      }
    100
    101     public boolean isConnected() {
    102         return isOpen() && socket.isConnected();
    103     }
    104
    105     @Override
    106     protected boolean setClosed() {
    107         return super.setClosed();
    108     }
    109
    110     @Override
    111     protected void setInterestOpsNow(int interestOps) {
    112         super.setInterestOpsNow(interestOps);
    113     }
    114
    115     abstract PushbackInputStream getInputStream();
    116     abstract OutputStream getOutputStream();
    117
    118     @Override
    119     public ChannelFuture write(Object message, SocketAddress remoteAddress) {
    120         if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
    121             return super.write(message, null);
    122         } else {
    123             return getUnsupportedOperationFuture();
    124         }
    125     }
    126 }