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      public boolean isBound() {
66          return isOpen() && isSocketBound();
67      }
68  
69      public boolean isConnected() {
70          return isOpen() && isSocketConnected();
71      }
72  
73      public InetSocketAddress getLocalAddress() {
74          InetSocketAddress localAddress = this.localAddress;
75          if (localAddress == null) {
76              try {
77                  this.localAddress = localAddress = getLocalSocketAddress();
78              } catch (Throwable t) {
79                  // Sometimes fails on a closed socket in Windows.
80                  return null;
81              }
82          }
83          return localAddress;
84      }
85  
86      public InetSocketAddress getRemoteAddress() {
87          InetSocketAddress remoteAddress = this.remoteAddress;
88          if (remoteAddress == null) {
89              try {
90                  this.remoteAddress = remoteAddress =
91                      getRemoteSocketAddress();
92              } catch (Throwable t) {
93                  // Sometimes fails on a closed socket in Windows.
94                  return null;
95              }
96          }
97          return remoteAddress;
98      }
99  
100     abstract boolean isSocketBound();
101 
102     abstract boolean isSocketConnected();
103 
104     abstract boolean isSocketClosed();
105 
106     abstract InetSocketAddress getLocalSocketAddress() throws Exception;
107 
108     abstract InetSocketAddress getRemoteSocketAddress() throws Exception;
109 
110     abstract void closeSocket() throws IOException;
111 
112 }