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