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  
41      public NioSocketChannel(
42              Channel parent, ChannelFactory factory,
43              ChannelPipeline pipeline, ChannelSink sink,
44              SocketChannel socket, NioWorker worker) {
45          super(parent, factory, pipeline, sink, worker, socket);
46          config = new DefaultNioSocketChannelConfig(socket.socket());
47      }
48  
49      @Override
50      public NioWorker getWorker() {
51          return (NioWorker) super.getWorker();
52      }
53  
54      @Override
55      public NioSocketChannelConfig getConfig() {
56          return config;
57      }
58  
59      @Override
60      public boolean isOpen() {
61          return state >= ST_OPEN;
62      }
63  
64      public boolean isBound() {
65          return state >= ST_BOUND;
66      }
67  
68      public boolean isConnected() {
69          return state == ST_CONNECTED;
70      }
71  
72      final void setBound() {
73          assert state == ST_OPEN : "Invalid state: " + state;
74          state = ST_BOUND;
75      }
76  
77      final void setConnected() {
78          if (state != ST_CLOSED) {
79              state = ST_CONNECTED;
80          }
81      }
82  
83      @Override
84      protected boolean setClosed() {
85          if (super.setClosed()) {
86              state = ST_CLOSED;
87              return true;
88          }
89          return false;
90      }
91  
92  
93      @Override
94      InetSocketAddress getLocalSocketAddress() throws Exception {
95          return (InetSocketAddress) channel.socket().getLocalSocketAddress();
96      }
97  
98      @Override
99      InetSocketAddress getRemoteSocketAddress() throws Exception {
100         return (InetSocketAddress) channel.socket().getRemoteSocketAddress();
101     }
102 
103 
104     @Override
105     public ChannelFuture write(Object message, SocketAddress remoteAddress) {
106         if (remoteAddress == null || remoteAddress.equals(getRemoteAddress())) {
107             return super.write(message, null);
108         } else {
109             return getUnsupportedOperationFuture();
110         }
111     }
112 }