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.List;
33  import java.util.Set;
34  import java.util.concurrent.Future;
35  
36  import org.apache.hc.core5.annotation.Internal;
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.reactor.ConnectionAcceptor;
43  import org.apache.hc.core5.reactor.ConnectionInitiator;
44  import org.apache.hc.core5.reactor.DefaultListeningIOReactor;
45  import org.apache.hc.core5.reactor.ExceptionEvent;
46  import org.apache.hc.core5.reactor.IOEventHandlerFactory;
47  import org.apache.hc.core5.reactor.IOReactorConfig;
48  import org.apache.hc.core5.reactor.IOReactorService;
49  import org.apache.hc.core5.reactor.IOReactorStatus;
50  import org.apache.hc.core5.reactor.IOSession;
51  import org.apache.hc.core5.reactor.IOSessionListener;
52  import org.apache.hc.core5.reactor.ListenerEndpoint;
53  import org.apache.hc.core5.util.TimeValue;
54  
55  /**
56   * Protocol agnostic server side I/O session handler.
57   */
58  public class AsyncServer extends AbstractConnectionInitiatorBase implements IOReactorService, ConnectionAcceptor {
59  
60      private final DefaultListeningIOReactor ioReactor;
61  
62      @Internal
63      public AsyncServer(
64              final IOEventHandlerFactory eventHandlerFactory,
65              final IOReactorConfig ioReactorConfig,
66              final Decorator<IOSession> ioSessionDecorator,
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                  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 List<ExceptionEvent> getExceptionLog() {
120         return ioReactor.getExceptionLog();
121     }
122 
123     @Override
124     public void initiateShutdown() {
125         ioReactor.initiateShutdown();
126     }
127 
128     @Override
129     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
130         ioReactor.awaitShutdown(waitTime);
131     }
132 
133     @Override
134     public void close(final CloseMode closeMode) {
135         ioReactor.close(closeMode);
136     }
137 
138     @Override
139     public void close() throws IOException {
140         ioReactor.close();
141     }
142 
143 }