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 io.netty.channel.udt.nio;
17  
18  import com.barchart.udt.TypeUDT;
19  import com.barchart.udt.nio.SocketChannelUDT;
20  import io.netty.buffer.ByteBuf;
21  import io.netty.channel.Channel;
22  import io.netty.channel.ChannelException;
23  import io.netty.channel.ChannelFuture;
24  import io.netty.channel.FileRegion;
25  import io.netty.channel.RecvByteBufAllocator;
26  import io.netty.channel.nio.AbstractNioByteChannel;
27  import io.netty.channel.udt.DefaultUdtChannelConfig;
28  import io.netty.channel.udt.UdtChannel;
29  import io.netty.channel.udt.UdtChannelConfig;
30  import io.netty.util.internal.SocketUtils;
31  import io.netty.util.internal.logging.InternalLogger;
32  import io.netty.util.internal.logging.InternalLoggerFactory;
33  
34  import java.io.IOException;
35  import java.net.InetSocketAddress;
36  import java.net.SocketAddress;
37  import java.security.AccessController;
38  import java.security.PrivilegedActionException;
39  import java.security.PrivilegedExceptionAction;
40  
41  import static java.nio.channels.SelectionKey.OP_CONNECT;
42  
43  /**
44   * Byte Channel Connector for UDT Streams.
45   *
46   * @deprecated The UDT transport is no longer maintained and will be removed.
47   */
48  @Deprecated
49  public class NioUdtByteConnectorChannel extends AbstractNioByteChannel implements UdtChannel {
50  
51      private static final InternalLogger logger =
52              InternalLoggerFactory.getInstance(NioUdtByteConnectorChannel.class);
53  
54      private final UdtChannelConfig config;
55  
56      public NioUdtByteConnectorChannel() {
57          this(TypeUDT.STREAM);
58      }
59  
60      public NioUdtByteConnectorChannel(final Channel parent, final SocketChannelUDT channelUDT) {
61          super(parent, channelUDT);
62          try {
63              channelUDT.configureBlocking(false);
64              switch (channelUDT.socketUDT().status()) {
65              case INIT:
66              case OPENED:
67                  config = new DefaultUdtChannelConfig(this, channelUDT, true);
68                  break;
69              default:
70                  config = new DefaultUdtChannelConfig(this, channelUDT, false);
71                  break;
72              }
73          } catch (final Exception e) {
74              try {
75                  channelUDT.close();
76              } catch (final Exception e2) {
77                  if (logger.isWarnEnabled()) {
78                      logger.warn("Failed to close channel.", e2);
79                  }
80              }
81              throw new ChannelException("Failed to configure channel.", e);
82          }
83      }
84  
85      public NioUdtByteConnectorChannel(final SocketChannelUDT channelUDT) {
86          this(null, channelUDT);
87      }
88  
89      public NioUdtByteConnectorChannel(final TypeUDT type) {
90          this(NioUdtProvider.newConnectorChannelUDT(type));
91      }
92  
93      @Override
94      public UdtChannelConfig config() {
95          return config;
96      }
97  
98      @Override
99      protected void doBind(final SocketAddress localAddress) throws Exception {
100         privilegedBind(javaChannel(), localAddress);
101     }
102 
103     @Override
104     protected void doClose() throws Exception {
105         javaChannel().close();
106     }
107 
108     @Override
109     protected boolean doConnect(final SocketAddress remoteAddress,
110                                 final SocketAddress localAddress) throws Exception {
111         doBind(localAddress != null? localAddress : new InetSocketAddress(0));
112         boolean success = false;
113         try {
114             final boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
115             if (!connected) {
116                 selectionKey().interestOps(
117                         selectionKey().interestOps() | OP_CONNECT);
118             }
119             success = true;
120             return connected;
121         } finally {
122             if (!success) {
123                 doClose();
124             }
125         }
126     }
127 
128     @Override
129     protected void doDisconnect() throws Exception {
130         doClose();
131     }
132 
133     @Override
134     protected void doFinishConnect() throws Exception {
135         if (javaChannel().finishConnect()) {
136             selectionKey().interestOps(
137                     selectionKey().interestOps() & ~OP_CONNECT);
138         } else {
139             throw new Error(
140                     "Provider error: failed to finish connect. Provider library should be upgraded.");
141         }
142     }
143 
144     @Override
145     protected int doReadBytes(final ByteBuf byteBuf) throws Exception {
146         final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
147         allocHandle.attemptedBytesRead(byteBuf.writableBytes());
148         return byteBuf.writeBytes(javaChannel(), allocHandle.attemptedBytesRead());
149     }
150 
151     @Override
152     protected int doWriteBytes(final ByteBuf byteBuf) throws Exception {
153         final int expectedWrittenBytes = byteBuf.readableBytes();
154         return byteBuf.readBytes(javaChannel(), expectedWrittenBytes);
155     }
156 
157     @Override
158     protected ChannelFuture shutdownInput() {
159         return newFailedFuture(new UnsupportedOperationException("shutdownInput"));
160     }
161 
162     @Override
163     protected long doWriteFileRegion(FileRegion region) throws Exception {
164         throw new UnsupportedOperationException();
165     }
166 
167     @Override
168     public boolean isActive() {
169         final SocketChannelUDT channelUDT = javaChannel();
170         return channelUDT.isOpen() && channelUDT.isConnectFinished();
171     }
172 
173     @Override
174     protected SocketChannelUDT javaChannel() {
175         return (SocketChannelUDT) super.javaChannel();
176     }
177 
178     @Override
179     protected SocketAddress localAddress0() {
180         return javaChannel().socket().getLocalSocketAddress();
181     }
182 
183     @Override
184     protected SocketAddress remoteAddress0() {
185         return javaChannel().socket().getRemoteSocketAddress();
186     }
187 
188     @Override
189     public InetSocketAddress localAddress() {
190         return (InetSocketAddress) super.localAddress();
191     }
192 
193     @Override
194     public InetSocketAddress remoteAddress() {
195         return (InetSocketAddress) super.remoteAddress();
196     }
197 
198     private static void privilegedBind(final SocketChannelUDT socketChannel, final SocketAddress localAddress)
199             throws IOException {
200         try {
201             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
202                 @Override
203                 public Void run() throws IOException {
204                     socketChannel.bind(localAddress);
205                     return null;
206                 }
207             });
208         } catch (PrivilegedActionException e) {
209             throw (IOException) e.getCause();
210         }
211     }
212 
213 }