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 org.jboss.netty.channel.Channel;
19  import org.jboss.netty.channel.ChannelFactory;
20  import org.jboss.netty.channel.ChannelFuture;
21  import org.jboss.netty.channel.ChannelPipeline;
22  import org.jboss.netty.channel.ChannelSink;
23  
24  import java.net.InetSocketAddress;
25  import java.net.SocketAddress;
26  import java.nio.channels.SocketChannel;
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      @SuppressWarnings("RedundantFieldInitialization")
36      volatile int state = ST_OPEN;
37  
38      private final NioSocketChannelConfig config;
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      @Override
92      InetSocketAddress getLocalSocketAddress() throws Exception {
93          return (InetSocketAddress) channel.socket().getLocalSocketAddress();
94      }
95  
96      @Override
97      InetSocketAddress getRemoteSocketAddress() throws Exception {
98          return (InetSocketAddress) channel.socket().getRemoteSocketAddress();
99      }
100 
101     @Override
102     public ChannelFuture write(Object message, SocketAddress remoteAddress) {
103         if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
104             return super.write(message, null);
105         } else {
106             return getUnsupportedOperationFuture();
107         }
108     }
109 }