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.nio;
17  
18  import java.net.InetSocketAddress;
19  import java.net.SocketAddress;
20  import java.nio.channels.SocketChannel;
21  
22  import org.jboss.netty.channel.Channel;
23  import org.jboss.netty.channel.ChannelFactory;
24  import org.jboss.netty.channel.ChannelFuture;
25  import org.jboss.netty.channel.ChannelPipeline;
26  import org.jboss.netty.channel.ChannelSink;
27  
28  public class NioSocketChannel extends AbstractNioChannel<SocketChannel>
29                                  implements org.jboss.netty.channel.socket.SocketChannel {
30  
31      private static final int ST_OPEN = 0;
32      private static final int ST_BOUND = 1;
33      private static final int ST_CONNECTED = 2;
34      private static final int ST_CLOSED = -1;
35      volatile int state = ST_OPEN;
36  
37      private final NioSocketChannelConfig config;
38  
39  
40      public NioSocketChannel(
41              Channel parent, ChannelFactory factory,
42              ChannelPipeline pipeline, ChannelSink sink,
43              SocketChannel socket, NioWorker worker) {
44          super(parent, factory, pipeline, sink, worker, socket);
45          config = new DefaultNioSocketChannelConfig(socket.socket());
46      }
47  
48      @Override
49      public NioWorker getWorker() {
50          return (NioWorker) super.getWorker();
51      }
52  
53      @Override
54      public NioSocketChannelConfig getConfig() {
55          return config;
56      }
57  
58      @Override
59      public boolean isOpen() {
60          return state >= ST_OPEN;
61      }
62  
63      public boolean isBound() {
64          return state >= ST_BOUND;
65      }
66  
67      public boolean isConnected() {
68          return state == ST_CONNECTED;
69      }
70  
71      final void setBound() {
72          assert state == ST_OPEN : "Invalid state: " + state;
73          state = ST_BOUND;
74      }
75  
76      final void setConnected() {
77          if (state != ST_CLOSED) {
78              state = ST_CONNECTED;
79          }
80      }
81  
82      @Override
83      protected boolean setClosed() {
84          if (super.setClosed()) {
85              state = ST_CLOSED;
86              return true;
87          }
88          return false;
89      }
90  
91  
92      @Override
93      InetSocketAddress getLocalSocketAddress() throws Exception {
94          return (InetSocketAddress) channel.socket().getLocalSocketAddress();
95      }
96  
97      @Override
98      InetSocketAddress getRemoteSocketAddress() throws Exception {
99          return (InetSocketAddress) channel.socket().getRemoteSocketAddress();
100     }
101 
102 
103     @Override
104     public ChannelFuture write(Object message, SocketAddress remoteAddress) {
105         if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
106             return super.write(message, null);
107         } else {
108             return getUnsupportedOperationFuture();
109         }
110     }
111 }