View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.reactor;
29  
30  import java.io.IOException;
31  import java.nio.channels.SelectionKey;
32  import java.nio.channels.SocketChannel;
33  
34  import org.apache.hc.core5.io.CloseMode;
35  import org.apache.hc.core5.io.SocketTimeoutExceptionFactory;
36  import org.apache.hc.core5.util.Timeout;
37  
38  final class InternalConnectChannel extends InternalChannel {
39  
40      private final SelectionKey key;
41      private final SocketChannel socketChannel;
42      private final IOSessionRequest sessionRequest;
43      private final long creationTimeMillis;
44      private final InternalDataChannelFactory dataChannelFactory;
45  
46      InternalConnectChannel(
47              final SelectionKey key,
48              final SocketChannel socketChannel,
49              final IOSessionRequest sessionRequest,
50              final InternalDataChannelFactory dataChannelFactory) {
51          super();
52          this.key = key;
53          this.socketChannel = socketChannel;
54          this.sessionRequest = sessionRequest;
55          this.creationTimeMillis = System.currentTimeMillis();
56          this.dataChannelFactory = dataChannelFactory;
57      }
58  
59      @Override
60      void onIOEvent(final int readyOps) throws IOException {
61          if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
62              if (socketChannel.isConnectionPending()) {
63                  socketChannel.finishConnect();
64              }
65              //check out connectTimeout
66              final long now = System.currentTimeMillis();
67              if (checkTimeout(now)) {
68                  final InternalDataChannel dataChannel = dataChannelFactory.create(
69                          key,
70                          socketChannel,
71                          sessionRequest.remoteEndpoint,
72                          sessionRequest.attachment);
73                  key.attach(dataChannel);
74                  sessionRequest.completed(dataChannel);
75                  dataChannel.handleIOEvent(SelectionKey.OP_CONNECT);
76              }
77          }
78      }
79  
80      @Override
81      Timeout getTimeout() {
82          return sessionRequest.timeout;
83      }
84  
85      @Override
86      long getLastReadTime() {
87          return creationTimeMillis;
88      }
89  
90      @Override
91      void onTimeout(final Timeout timeout) throws IOException {
92          sessionRequest.failed(SocketTimeoutExceptionFactory.create(timeout));
93          close();
94      }
95  
96      @Override
97      void onException(final Exception cause) {
98          sessionRequest.failed(cause);
99      }
100 
101     @Override
102     public void close() throws IOException {
103         key.cancel();
104         socketChannel.close();
105     }
106 
107     @Override
108     public void close(final CloseMode closeMode) {
109         try {
110             close();
111         } catch (final IOException ignore) {
112         }
113     }
114 }