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.net.SocketAddress;
31  import java.util.concurrent.ExecutionException;
32  import java.util.concurrent.Future;
33  import java.util.concurrent.TimeUnit;
34  import java.util.concurrent.TimeoutException;
35  import java.util.concurrent.atomic.AtomicReference;
36  
37  import org.apache.hc.core5.concurrent.BasicFuture;
38  import org.apache.hc.core5.concurrent.FutureCallback;
39  import org.apache.hc.core5.io.ModalCloseable;
40  import org.apache.hc.core5.io.CloseMode;
41  import org.apache.hc.core5.net.NamedEndpoint;
42  import org.apache.hc.core5.util.TimeValue;
43  
44  final class IOSessionRequest implements Future<IOSession> {
45  
46      final NamedEndpoint remoteEndpoint;
47      final SocketAddress remoteAddress;
48      final SocketAddress localAddress;
49      final TimeValue timeout;
50      final Object attachment;
51      final BasicFuture<IOSession> future;
52  
53      private final AtomicReference<ModalCloseable> closeableRef;
54  
55      public IOSessionRequest(
56              final NamedEndpoint remoteEndpoint,
57              final SocketAddress remoteAddress,
58              final SocketAddress localAddress,
59              final TimeValue timeout,
60              final Object attachment,
61              final FutureCallback<IOSession> callback) {
62          super();
63          this.remoteEndpoint = remoteEndpoint;
64          this.remoteAddress = remoteAddress;
65          this.localAddress = localAddress;
66          this.timeout = timeout;
67          this.attachment = attachment;
68          this.future = new BasicFuture<>(callback);
69          this.closeableRef = new AtomicReference<>(null);
70      }
71  
72      public void completed(final ProtocolIOSession ioSession) {
73          future.completed(ioSession);
74          closeableRef.set(null);
75      }
76  
77      public void failed(final Exception cause) {
78          future.failed(cause);
79          closeableRef.set(null);
80      }
81  
82      public boolean cancel() {
83          final boolean cancelled = future.cancel();
84          final ModalCloseable closeable = closeableRef.getAndSet(null);
85          if (cancelled && closeable != null) {
86              closeable.close(CloseMode.IMMEDIATE);
87          }
88          return cancelled;
89      }
90  
91      @Override
92      public boolean cancel(final boolean mayInterruptIfRunning) {
93          return cancel();
94      }
95  
96      @Override
97      public boolean isCancelled() {
98          return future.isCancelled();
99      }
100 
101     public void assign(final ModalCloseable closeable) {
102         closeableRef.set(closeable);
103     }
104 
105     @Override
106     public boolean isDone() {
107         return future.isDone();
108     }
109 
110     @Override
111     public IOSession get() throws InterruptedException, ExecutionException {
112         return future.get();
113     }
114 
115     @Override
116     public IOSession get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
117         return future.get(timeout, unit);
118     }
119 
120 }