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.io.IOException;
31  import java.net.SocketAddress;
32  import java.nio.channels.SocketChannel;
33  import java.util.Set;
34  import java.util.concurrent.Future;
35  import java.util.concurrent.ThreadFactory;
36  
37  import org.apache.hc.core5.concurrent.DefaultThreadFactory;
38  import org.apache.hc.core5.concurrent.FutureCallback;
39  import org.apache.hc.core5.function.Callback;
40  import org.apache.hc.core5.function.Decorator;
41  import org.apache.hc.core5.io.CloseMode;
42  import org.apache.hc.core5.util.Args;
43  import org.apache.hc.core5.util.TimeValue;
44  
45  /**
46   * Multi-core I/O reactor that can ask as both {@link ConnectionInitiator}
47   * and {@link ConnectionAcceptor}. Internally this I/O reactor distributes newly created
48   * I/O session equally across multiple I/O worker threads for a more optimal resource
49   * utilization and a better I/O performance. Usually it is recommended to have
50   * one worker I/O reactor per physical CPU core.
51   *
52   * @since 4.0
53   */
54  public class DefaultListeningIOReactor extends AbstractIOReactorBase implements ConnectionAcceptor {
55  
56      private final static ThreadFactory DISPATCH_THREAD_FACTORY = new DefaultThreadFactory("I/O server dispatch", true);
57      private final static ThreadFactory LISTENER_THREAD_FACTORY = new DefaultThreadFactory("I/O listener", true);
58  
59      private final int workerCount;
60      private final SingleCoreIOReactor[] workers;
61      private final SingleCoreListeningIOReactor listener;
62      private final MultiCoreIOReactor ioReactor;
63      private final IOWorkers.Selector workerSelector;
64  
65      /**
66       * Creates an instance of DefaultListeningIOReactor with the given configuration.
67       *
68       * @param eventHandlerFactory the factory to create I/O event handlers.
69       * @param ioReactorConfig I/O reactor configuration.
70       * @param listenerThreadFactory the factory to create listener thread.
71       *   Can be {@code null}.
72       *
73       * @since 5.0
74       */
75      public DefaultListeningIOReactor(
76              final IOEventHandlerFactory eventHandlerFactory,
77              final IOReactorConfig ioReactorConfig,
78              final ThreadFactory dispatchThreadFactory,
79              final ThreadFactory listenerThreadFactory,
80              final Decorator<IOSession> ioSessionDecorator,
81              final Callback<Exception> exceptionCallback,
82              final IOSessionListener sessionListener,
83              final Callback<IOSession> sessionShutdownCallback) {
84          Args.notNull(eventHandlerFactory, "Event handler factory");
85          this.workerCount = ioReactorConfig != null ? ioReactorConfig.getIoThreadCount() : IOReactorConfig.DEFAULT.getIoThreadCount();
86          this.workers = new SingleCoreIOReactor[workerCount];
87          final Thread[] threads = new Thread[workerCount + 1];
88          for (int i = 0; i < this.workers.length; i++) {
89              final SingleCoreIOReactoractor.html#SingleCoreIOReactor">SingleCoreIOReactor dispatcher = new SingleCoreIOReactor(
90                      exceptionCallback,
91                      eventHandlerFactory,
92                      ioReactorConfig != null ? ioReactorConfig : IOReactorConfig.DEFAULT,
93                      ioSessionDecorator,
94                      sessionListener,
95                      sessionShutdownCallback);
96              this.workers[i] = dispatcher;
97              threads[i + 1] = (dispatchThreadFactory != null ? dispatchThreadFactory : DISPATCH_THREAD_FACTORY).newThread(new IOReactorWorker(dispatcher));
98          }
99          final IOReactortor.html#IOReactor">IOReactor[] ioReactors = new IOReactor[this.workerCount + 1];
100         System.arraycopy(this.workers, 0, ioReactors, 1, this.workerCount);
101         this.listener = new SingleCoreListeningIOReactor(exceptionCallback, ioReactorConfig, new Callback<SocketChannel>() {
102 
103             @Override
104             public void execute(final SocketChannel channel) {
105                 enqueueChannel(channel);
106             }
107 
108         });
109         ioReactors[0] = this.listener;
110         threads[0] = (listenerThreadFactory != null ? listenerThreadFactory : LISTENER_THREAD_FACTORY).newThread(new IOReactorWorker(listener));
111 
112         this.ioReactor = new MultiCoreIOReactor(ioReactors, threads);
113 
114         workerSelector = IOWorkers.newSelector(workers);
115     }
116 
117     /**
118      * Creates an instance of DefaultListeningIOReactor with the given configuration.
119      *
120      * @param eventHandlerFactory the factory to create I/O event handlers.
121      * @param config I/O reactor configuration.
122      *   Can be {@code null}.
123      *
124      * @since 5.0
125      */
126     public DefaultListeningIOReactor(
127             final IOEventHandlerFactory eventHandlerFactory,
128             final IOReactorConfig config,
129             final Callback<IOSession> sessionShutdownCallback) {
130         this(eventHandlerFactory, config, null, null, null, null, null, sessionShutdownCallback);
131     }
132 
133     /**
134      * Creates an instance of DefaultListeningIOReactor with default configuration.
135      *
136      * @param eventHandlerFactory the factory to create I/O event handlers.
137      *
138      * @since 5.0
139      */
140     public DefaultListeningIOReactor(final IOEventHandlerFactory eventHandlerFactory) {
141         this(eventHandlerFactory, null, null);
142     }
143 
144     @Override
145     public void start() {
146         ioReactor.start();
147     }
148 
149     @Override
150     public Future<ListenerEndpoint> listen(final SocketAddress address, final FutureCallback<ListenerEndpoint> callback) {
151         return listener.listen(address, callback);
152     }
153 
154     public Future<ListenerEndpoint> listen(final SocketAddress address) {
155         return listen(address, null);
156     }
157 
158     @Override
159     public Set<ListenerEndpoint> getEndpoints() {
160         return listener.getEndpoints();
161     }
162 
163     @Override
164     public void pause() throws IOException {
165         listener.pause();
166     }
167 
168     @Override
169     public void resume() throws IOException {
170         listener.resume();
171     }
172 
173     @Override
174     public IOReactorStatus getStatus() {
175         return ioReactor.getStatus();
176     }
177 
178     @Override
179     IOWorkers.Selector getWorkerSelector() {
180         return workerSelector;
181     }
182 
183     private void enqueueChannel(final SocketChannel socketChannel) {
184         try {
185             workerSelector.next().enqueueChannel(socketChannel);
186         } catch (final IOReactorShutdownException ex) {
187             initiateShutdown();
188         }
189     }
190 
191 
192     @Override
193     public void initiateShutdown() {
194         ioReactor.initiateShutdown();
195     }
196 
197     @Override
198     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
199         ioReactor.awaitShutdown(waitTime);
200     }
201 
202     @Override
203     public void close(final CloseMode closeMode) {
204         ioReactor.close(closeMode);
205     }
206 
207     @Override
208     public void close() throws IOException {
209         ioReactor.close();
210     }
211 
212 }