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.net.SocketAddress;
33  import java.util.List;
34  import java.util.concurrent.Future;
35  
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.ShutdownType;
44  import org.apache.hc.core5.net.NamedEndpoint;
45  import org.apache.hc.core5.reactor.ConnectionInitiator;
46  import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
47  import org.apache.hc.core5.reactor.ExceptionEvent;
48  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
49  import org.apache.hc.core5.reactor.IOReactorConfig;
50  import org.apache.hc.core5.reactor.IOReactorService;
51  import org.apache.hc.core5.reactor.IOReactorStatus;
52  import org.apache.hc.core5.reactor.IOSession;
53  import org.apache.hc.core5.reactor.IOSessionListener;
54  import org.apache.hc.core5.util.Args;
55  import org.apache.hc.core5.util.TimeValue;
56  
57  public class AsyncRequester implements IOReactorService, ConnectionInitiator {
58  
59      private final DefaultConnectingIOReactor ioReactor;
60      private final Resolver<HttpHost, InetSocketAddress> addressResolver;
61  
62      public AsyncRequester(
63              final IOEventHandlerFactory eventHandlerFactory,
64              final IOReactorConfig ioReactorConfig,
65              final Decorator<IOSession> ioSessionDecorator,
66              final IOSessionListener sessionListener,
67              final Callback<IOSession> sessionShutdownCallback,
68              final Resolver<HttpHost, InetSocketAddress> addressResolver) {
69          this.ioReactor = new DefaultConnectingIOReactor(
70                  eventHandlerFactory,
71                  ioReactorConfig,
72                  new DefaultThreadFactory("requester-dispatch", true),
73                  ioSessionDecorator,
74                  sessionListener,
75                  sessionShutdownCallback);
76          this.addressResolver = addressResolver != null ? addressResolver : DefaultAddressResolver.INSTANCE;
77      }
78  
79      @Override
80      public Future<IOSession> connect(
81              final NamedEndpoint remoteEndpoint,
82              final SocketAddress remoteAddress,
83              final SocketAddress localAddress,
84              final TimeValue timeout,
85              final Object attachment,
86              final FutureCallback<IOSession> callback) {
87          return ioReactor.connect(remoteEndpoint, remoteAddress, localAddress, timeout, attachment, callback);
88      }
89  
90      public Future<IOSession> requestSession(
91              final HttpHost host,
92              final TimeValue 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 shutdown(final ShutdownType shutdownType) {
127         ioReactor.shutdown(shutdownType);
128     }
129 
130     @Override
131     public void close() throws IOException {
132         ioReactor.close();
133     }
134 
135 }