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 java.io.IOException;
19  import java.net.InetSocketAddress;
20  import java.net.SocketAddress;
21  
22  import org.jboss.netty.channel.AbstractChannel;
23  import org.jboss.netty.channel.Channel;
24  import org.jboss.netty.channel.ChannelFactory;
25  import org.jboss.netty.channel.ChannelFuture;
26  import org.jboss.netty.channel.ChannelPipeline;
27  import org.jboss.netty.channel.ChannelSink;
28  import org.jboss.netty.channel.socket.Worker;
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 void setInterestOpsNow(int interestOps) {
53          super.setInterestOpsNow(interestOps);
54      }
55  
56      @Override
57      public ChannelFuture write(Object message, SocketAddress remoteAddress) {
58          if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
59              return super.write(message, null);
60          } else {
61              return super.write(message, remoteAddress);
62          }
63      }
64  
65  
66      public boolean isBound() {
67          return isOpen() && isSocketBound();
68      }
69  
70  
71      public boolean isConnected() {
72          return isOpen() && isSocketConnected();
73      }
74  
75  
76  
77      public InetSocketAddress getLocalAddress() {
78          InetSocketAddress localAddress = this.localAddress;
79          if (localAddress == null) {
80              try {
81                  this.localAddress = localAddress = getLocalSocketAddress();
82              } catch (Throwable t) {
83                  // Sometimes fails on a closed socket in Windows.
84                  return null;
85              }
86          }
87          return localAddress;
88      }
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 }