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.Closer;
36  import org.apache.hc.core5.io.SocketTimeoutExceptionFactory;
37  import org.apache.hc.core5.util.Timeout;
38  
39  final class InternalConnectChannel extends InternalChannel {
40  
41      private final SelectionKey key;
42      private final SocketChannel socketChannel;
43      private final IOSessionRequest sessionRequest;
44      private final InternalDataChannel dataChannel;
45      private final IOEventHandlerFactory eventHandlerFactory;
46      private final IOReactorConfig reactorConfig;
47      private final long creationTimeMillis;
48  
49      InternalConnectChannel(
50              final SelectionKey key,
51              final SocketChannel socketChannel,
52              final IOSessionRequest sessionRequest,
53              final InternalDataChannel dataChannel,
54              final IOEventHandlerFactory eventHandlerFactory,
55              final IOReactorConfig reactorConfig) {
56          super();
57          this.key = key;
58          this.socketChannel = socketChannel;
59          this.sessionRequest = sessionRequest;
60          this.dataChannel = dataChannel;
61          this.eventHandlerFactory = eventHandlerFactory;
62          this.reactorConfig = reactorConfig;
63          this.creationTimeMillis = System.currentTimeMillis();
64      }
65  
66      @Override
67      void onIOEvent(final int readyOps) throws IOException {
68          if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
69              if (socketChannel.isConnectionPending()) {
70                  socketChannel.finishConnect();
71              }
72              //check out connectTimeout
73              final long now = System.currentTimeMillis();
74              if (checkTimeout(now)) {
75                  key.attach(dataChannel);
76                  if (reactorConfig.getSocksProxyAddress() == null) {
77                      dataChannel.upgrade(eventHandlerFactory.createHandler(dataChannel, sessionRequest.attachment));
78                      sessionRequest.completed(dataChannel);
79                      dataChannel.handleIOEvent(SelectionKey.OP_CONNECT);
80                  } else {
81                      final IOEventHandler ioEventHandler = new SocksProxyProtocolHandler(
82                              dataChannel, sessionRequest, eventHandlerFactory, reactorConfig);
83                      dataChannel.upgrade(ioEventHandler);
84                      ioEventHandler.connected(dataChannel);
85                  }
86              }
87          }
88      }
89  
90      @Override
91      Timeout getTimeout() {
92          return sessionRequest.timeout;
93      }
94  
95      @Override
96      long getLastEventTime() {
97          return creationTimeMillis;
98      }
99  
100     @Override
101     void onTimeout(final Timeout timeout) throws IOException {
102         sessionRequest.failed(SocketTimeoutExceptionFactory.create(timeout));
103         close();
104     }
105 
106     @Override
107     void onException(final Exception cause) {
108         sessionRequest.failed(cause);
109     }
110 
111     @Override
112     public void close() throws IOException {
113         key.cancel();
114         socketChannel.close();
115     }
116 
117     @Override
118     public void close(final CloseMode closeMode) {
119         key.cancel();
120         Closer.closeQuietly(socketChannel);
121     }
122 
123     @Override
124     public String toString() {
125         return sessionRequest.toString();
126     }
127 
128 }