1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 long creationTimeMillis;
45 private final InternalDataChannelFactory dataChannelFactory;
46
47 InternalConnectChannel(
48 final SelectionKey key,
49 final SocketChannel socketChannel,
50 final IOSessionRequest sessionRequest,
51 final InternalDataChannelFactory dataChannelFactory) {
52 super();
53 this.key = key;
54 this.socketChannel = socketChannel;
55 this.sessionRequest = sessionRequest;
56 this.creationTimeMillis = System.currentTimeMillis();
57 this.dataChannelFactory = dataChannelFactory;
58 }
59
60 @Override
61 void onIOEvent(final int readyOps) throws IOException {
62 if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
63 if (socketChannel.isConnectionPending()) {
64 socketChannel.finishConnect();
65 }
66
67 final long now = System.currentTimeMillis();
68 if (checkTimeout(now)) {
69 final InternalDataChannel dataChannel = dataChannelFactory.create(
70 key,
71 socketChannel,
72 sessionRequest.remoteEndpoint,
73 sessionRequest.attachment);
74 key.attach(dataChannel);
75 sessionRequest.completed(dataChannel);
76 dataChannel.handleIOEvent(SelectionKey.OP_CONNECT);
77 }
78 }
79 }
80
81 @Override
82 Timeout getTimeout() {
83 return sessionRequest.timeout;
84 }
85
86 @Override
87 long getLastEventTime() {
88 return creationTimeMillis;
89 }
90
91 @Override
92 void onTimeout(final Timeout timeout) throws IOException {
93 sessionRequest.failed(SocketTimeoutExceptionFactory.create(timeout));
94 close();
95 }
96
97 @Override
98 void onException(final Exception cause) {
99 sessionRequest.failed(cause);
100 }
101
102 @Override
103 public void close() throws IOException {
104 key.cancel();
105 socketChannel.close();
106 }
107
108 @Override
109 public void close(final CloseMode closeMode) {
110 key.cancel();
111 Closer.closeQuietly(socketChannel);
112 }
113
114 @Override
115 public String toString() {
116 return sessionRequest.toString();
117 }
118
119 }