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.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.ShutdownType;
41  import org.apache.hc.core5.net.NamedEndpoint;
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  public class AsyncServer implements IOReactorService, ConnectionInitiator, ConnectionAcceptor {
56  
57      private final DefaultListeningIOReactor ioReactor;
58  
59      public AsyncServer(
60              final IOEventHandlerFactory eventHandlerFactory,
61              final IOReactorConfig ioReactorConfig,
62              final Decorator<IOSession> ioSessionDecorator,
63              final IOSessionListener sessionListener,
64              final Callback<IOSession> sessionShutdownCallback) {
65          this.ioReactor = new DefaultListeningIOReactor(
66                  eventHandlerFactory,
67                  ioReactorConfig,
68                  new DefaultThreadFactory("server-dispatch", true),
69                  new DefaultThreadFactory("server-listener", true),
70                  ioSessionDecorator,
71                  sessionListener,
72                  sessionShutdownCallback);
73      }
74  
75      @Override
76      public void start() {
77          ioReactor.start();
78      }
79  
80      @Override
81      public Future<IOSession> connect(
82              final NamedEndpoint remoteEndpoint,
83              final SocketAddress remoteAddress,
84              final SocketAddress localAddress,
85              final TimeValue timeout,
86              final Object attachment,
87              final FutureCallback<IOSession> callback) {
88          return ioReactor.connect(remoteEndpoint, remoteAddress, localAddress, timeout, attachment, callback);
89      }
90  
91      @Override
92      public Future<ListenerEndpoint> listen(final SocketAddress address, final FutureCallback<ListenerEndpoint> callback) {
93          return ioReactor.listen(address, callback);
94      }
95  
96      public Future<ListenerEndpoint> listen(final SocketAddress address) {
97          return ioReactor.listen(address, null);
98      }
99  
100     @Override
101     public void pause() throws IOException {
102         ioReactor.pause();
103     }
104 
105     @Override
106     public void resume() throws IOException {
107         ioReactor.resume();
108     }
109 
110     @Override
111     public Set<ListenerEndpoint> getEndpoints() {
112         return ioReactor.getEndpoints();
113     }
114 
115     @Override
116     public IOReactorStatus getStatus() {
117         return ioReactor.getStatus();
118     }
119 
120     @Override
121     public List<ExceptionEvent> getExceptionLog() {
122         return ioReactor.getExceptionLog();
123     }
124 
125     @Override
126     public void initiateShutdown() {
127         ioReactor.initiateShutdown();
128     }
129 
130     @Override
131     public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
132         ioReactor.awaitShutdown(waitTime);
133     }
134 
135     @Override
136     public void shutdown(final ShutdownType shutdownType) {
137         ioReactor.shutdown(shutdownType);
138     }
139 
140     @Override
141     public void close() throws IOException {
142         ioReactor.close();
143     }
144 
145 }