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.List;
33  import java.util.concurrent.Future;
34  
35  import org.apache.hc.core5.annotation.Internal;
36  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
37  import org.apache.hc.core5.concurrent.FutureCallback;
38  import org.apache.hc.core5.function.Callback;
39  import org.apache.hc.core5.function.Decorator;
40  import org.apache.hc.core5.function.Resolver;
41  import org.apache.hc.core5.http.HttpHost;
42  import org.apache.hc.core5.http.impl.DefaultAddressResolver;
43  import org.apache.hc.core5.io.CloseMode;
44  import org.apache.hc.core5.reactor.ConnectionInitiator;
45  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
46  import org.apache.hc.core5.reactor.ExceptionEvent;
47  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
48  import org.apache.hc.core5.reactor.IOReactorConfig;
49  import org.apache.hc.core5.reactor.IOReactorService;
50  import org.apache.hc.core5.reactor.IOReactorStatus;
51  import org.apache.hc.core5.reactor.IOSession;
52  import org.apache.hc.core5.reactor.IOSessionListener;
53  import org.apache.hc.core5.util.Args;
54  import org.apache.hc.core5.util.TimeValue;
55  import org.apache.hc.core5.util.Timeout;
56  
57  /**
58   * Protocol agnostic client side I/O session initiator.
59   *
60   * @since 5.0
61   */
62  public class AsyncRequester extends AbstractConnectionInitiatorBase implements IOReactorService {
63  
64      private final DefaultConnectingIOReactor ioReactor;
65      private final Resolver<HttpHost, InetSocketAddress> addressResolver;
66  
67      @Internal
68      public AsyncRequester(
69              final IOEventHandlerFactory eventHandlerFactory,
70              final IOReactorConfig ioReactorConfig,
71              final Decorator<IOSession> ioSessionDecorator,
72              final IOSessionListener sessionListener,
73              final Callback<IOSession> sessionShutdownCallback,
74              final Resolver<HttpHost, InetSocketAddress> addressResolver) {
75          this.ioReactor = new DefaultConnectingIOReactor(
76                  eventHandlerFactory,
77                  ioReactorConfig,
78                  new DefaultThreadFactory("requester-dispatch", true),
79                  ioSessionDecorator,
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 List<ExceptionEvent> getExceptionLog() {
112         return ioReactor.getExceptionLog();
113     }
114 
115     @Override
116     public void initiateShutdown() {
117         ioReactor.initiateShutdown();
118     }
119 
120     @Override
121     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
122         ioReactor.awaitShutdown(waitTime);
123     }
124 
125     @Override
126     public void close(final CloseMode closeMode) {
127         ioReactor.close(closeMode);
128     }
129 
130     @Override
131     public void close() throws IOException {
132         ioReactor.close();
133     }
134 
135 }