View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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 org.jboss.netty.channel.AbstractChannel;
19  import org.jboss.netty.channel.Channel;
20  import org.jboss.netty.channel.ChannelFactory;
21  import org.jboss.netty.channel.ChannelFuture;
22  import org.jboss.netty.channel.ChannelPipeline;
23  import org.jboss.netty.channel.ChannelSink;
24  import org.jboss.netty.channel.socket.Worker;
25  
26  import java.io.IOException;
27  import java.net.InetSocketAddress;
28  import java.net.SocketAddress;
29  
30  abstract class AbstractOioChannel extends AbstractChannel {
31      private volatile InetSocketAddress localAddress;
32      volatile InetSocketAddress remoteAddress;
33      volatile Thread workerThread;
34      volatile Worker worker;
35  
36      final Object interestOpsLock = new Object();
37  
38      AbstractOioChannel(
39              Channel parent,
40              ChannelFactory factory,
41              ChannelPipeline pipeline,
42              ChannelSink sink) {
43          super(parent, factory, pipeline, sink);
44      }
45  
46      @Override
47      protected boolean setClosed() {
48          return super.setClosed();
49      }
50  
51      @Override
52      protected int getInternalInterestOps() {
53          return super.getInternalInterestOps();
54      }
55  
56      @Override
57      protected void setInternalInterestOps(int interestOps) {
58          super.setInternalInterestOps(interestOps);
59      }
60  
61      @Override
62      public ChannelFuture write(Object message, SocketAddress remoteAddress) {
63          if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
64              return super.write(message, null);
65          } else {
66              return super.write(message, remoteAddress);
67          }
68      }
69  
70      public boolean isBound() {
71          return isOpen() && isSocketBound();
72      }
73  
74      public boolean isConnected() {
75          return isOpen() && isSocketConnected();
76      }
77  
78      public InetSocketAddress getLocalAddress() {
79          InetSocketAddress localAddress = this.localAddress;
80          if (localAddress == null) {
81              try {
82                  this.localAddress = localAddress = getLocalSocketAddress();
83              } catch (Throwable t) {
84                  // Sometimes fails on a closed socket in Windows.
85                  return null;
86              }
87          }
88          return localAddress;
89      }
90  
91      public InetSocketAddress getRemoteAddress() {
92          InetSocketAddress remoteAddress = this.remoteAddress;
93          if (remoteAddress == null) {
94              try {
95                  this.remoteAddress = remoteAddress =
96                      getRemoteSocketAddress();
97              } catch (Throwable t) {
98                  // Sometimes fails on a closed socket in Windows.
99                  return null;
100             }
101         }
102         return remoteAddress;
103     }
104 
105     abstract boolean isSocketBound();
106 
107     abstract boolean isSocketConnected();
108 
109     abstract boolean isSocketClosed();
110 
111     abstract InetSocketAddress getLocalSocketAddress() throws Exception;
112 
113     abstract InetSocketAddress getRemoteSocketAddress() throws Exception;
114 
115     abstract void closeSocket() throws IOException;
116 
117 }