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.http.impl.bootstrap;
29  
30  import java.io.IOException;
31  import java.net.InetSocketAddress;
32  import java.util.concurrent.Future;
33  
34  import org.apache.hc.core5.annotation.Internal;
35  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
36  import org.apache.hc.core5.concurrent.FutureCallback;
37  import org.apache.hc.core5.function.Callback;
38  import org.apache.hc.core5.function.Decorator;
39  import org.apache.hc.core5.function.Resolver;
40  import org.apache.hc.core5.http.HttpHost;
41  import org.apache.hc.core5.http.impl.DefaultAddressResolver;
42  import org.apache.hc.core5.io.CloseMode;
43  import org.apache.hc.core5.reactor.ConnectionInitiator;
44  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
45  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
46  import org.apache.hc.core5.reactor.IOReactorConfig;
47  import org.apache.hc.core5.reactor.IOReactorService;
48  import org.apache.hc.core5.reactor.IOReactorStatus;
49  import org.apache.hc.core5.reactor.IOSession;
50  import org.apache.hc.core5.reactor.IOSessionListener;
51  import org.apache.hc.core5.util.Args;
52  import org.apache.hc.core5.util.TimeValue;
53  import org.apache.hc.core5.util.Timeout;
54  
55  /**
56   * Protocol agnostic client side I/O session initiator.
57   *
58   * @since 5.0
59   */
60  public class AsyncRequester extends AbstractConnectionInitiatorBase implements IOReactorService {
61  
62      private final DefaultConnectingIOReactor ioReactor;
63      private final Resolver<HttpHost, InetSocketAddress> addressResolver;
64  
65      @Internal
66      public AsyncRequester(
67              final IOEventHandlerFactory eventHandlerFactory,
68              final IOReactorConfig ioReactorConfig,
69              final Decorator<IOSession> ioSessionDecorator,
70              final Callback<Exception> exceptionCallback,
71              final IOSessionListener sessionListener,
72              final Callback<IOSession> sessionShutdownCallback,
73              final Resolver<HttpHost, InetSocketAddress> addressResolver) {
74          this.ioReactor = new DefaultConnectingIOReactor(
75                  eventHandlerFactory,
76                  ioReactorConfig,
77                  new DefaultThreadFactory("requester-dispatch", true),
78                  ioSessionDecorator,
79                  exceptionCallback,
80                  sessionListener,
81                  sessionShutdownCallback);
82          this.addressResolver = addressResolver != null ? addressResolver : DefaultAddressResolver.INSTANCE;
83      }
84  
85      @Override
86      ConnectionInitiator getIOReactor() {
87          return ioReactor;
88      }
89  
90      public Future<IOSession> requestSession(
91              final HttpHost host,
92              final Timeout timeout,
93              final Object attachment,
94              final FutureCallback<IOSession> callback) {
95          Args.notNull(host, "Host");
96          Args.notNull(timeout, "Timeout");
97          return connect(host, addressResolver.resolve(host), null, timeout, attachment, callback);
98      }
99  
100     @Override
101     public void start() {
102         ioReactor.start();
103     }
104 
105     @Override
106     public IOReactorStatus getStatus() {
107         return ioReactor.getStatus();
108     }
109 
110     @Override
111     public void initiateShutdown() {
112         ioReactor.initiateShutdown();
113     }
114 
115     @Override
116     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
117         ioReactor.awaitShutdown(waitTime);
118     }
119 
120     @Override
121     public void close(final CloseMode closeMode) {
122         ioReactor.close(closeMode);
123     }
124 
125     @Override
126     public void close() throws IOException {
127         ioReactor.close();
128     }
129 
130 }