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.SocketAddress;
32  import java.util.Set;
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.io.CloseMode;
41  import org.apache.hc.core5.reactor.ConnectionAcceptor;
42  import org.apache.hc.core5.reactor.ConnectionInitiator;
43  import org.apache.hc.core5.reactor.DefaultListeningIOReactor;
44  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
45  import org.apache.hc.core5.reactor.IOReactorConfig;
46  import org.apache.hc.core5.reactor.IOReactorService;
47  import org.apache.hc.core5.reactor.IOReactorStatus;
48  import org.apache.hc.core5.reactor.IOSession;
49  import org.apache.hc.core5.reactor.IOSessionListener;
50  import org.apache.hc.core5.reactor.ListenerEndpoint;
51  import org.apache.hc.core5.reactor.ProtocolIOSession;
52  import org.apache.hc.core5.util.TimeValue;
53  
54  /**
55   * Protocol agnostic server side I/O session handler.
56   */
57  public class AsyncServer extends AbstractConnectionInitiatorBase implements IOReactorService, ConnectionAcceptor {
58  
59      private final DefaultListeningIOReactor ioReactor;
60  
61      @Internal
62      public AsyncServer(
63              final IOEventHandlerFactory eventHandlerFactory,
64              final IOReactorConfig ioReactorConfig,
65              final Decorator<ProtocolIOSession> ioSessionDecorator,
66              final Callback<Exception> exceptionCallback,
67              final IOSessionListener sessionListener,
68              final Callback<IOSession> sessionShutdownCallback) {
69          this.ioReactor = new DefaultListeningIOReactor(
70                  eventHandlerFactory,
71                  ioReactorConfig,
72                  new DefaultThreadFactory("server-dispatch", true),
73                  new DefaultThreadFactory("server-listener", true),
74                  ioSessionDecorator,
75                  exceptionCallback,
76                  sessionListener,
77                  sessionShutdownCallback);
78      }
79  
80      @Override
81      public void start() {
82          ioReactor.start();
83      }
84  
85      @Override
86      ConnectionInitiator getIOReactor() {
87          return ioReactor;
88      }
89  
90      @Override
91      public Future<ListenerEndpoint> listen(final SocketAddress address, final FutureCallback<ListenerEndpoint> callback) {
92          return ioReactor.listen(address, callback);
93      }
94  
95      public Future<ListenerEndpoint> listen(final SocketAddress address) {
96          return ioReactor.listen(address, null);
97      }
98  
99      @Override
100     public void pause() throws IOException {
101         ioReactor.pause();
102     }
103 
104     @Override
105     public void resume() throws IOException {
106         ioReactor.resume();
107     }
108 
109     @Override
110     public Set<ListenerEndpoint> getEndpoints() {
111         return ioReactor.getEndpoints();
112     }
113 
114     @Override
115     public IOReactorStatus getStatus() {
116         return ioReactor.getStatus();
117     }
118 
119     @Override
120     public void initiateShutdown() {
121         ioReactor.initiateShutdown();
122     }
123 
124     @Override
125     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
126         ioReactor.awaitShutdown(waitTime);
127     }
128 
129     @Override
130     public void close(final CloseMode closeMode) {
131         ioReactor.close(closeMode);
132     }
133 
134     @Override
135     public void close() throws IOException {
136         ioReactor.close();
137     }
138 
139 }