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  package org.apache.hc.core5.http.impl.bootstrap;
28  
29  import java.io.IOException;
30  import java.net.ServerSocket;
31  import java.net.Socket;
32  import java.util.concurrent.ExecutorService;
33  import java.util.concurrent.atomic.AtomicBoolean;
34  
35  import org.apache.hc.core5.http.ExceptionListener;
36  import org.apache.hc.core5.http.impl.io.HttpService;
37  import org.apache.hc.core5.http.io.HttpConnectionFactory;
38  import org.apache.hc.core5.http.io.HttpServerConnection;
39  import org.apache.hc.core5.http.io.SocketConfig;
40  
41  class RequestListener implements Runnable {
42  
43      private final SocketConfig socketConfig;
44      private final ServerSocket serverSocket;
45      private final HttpService httpService;
46      private final HttpConnectionFactory<? extends HttpServerConnection> connectionFactory;
47      private final ExceptionListener exceptionListener;
48      private final ExecutorService executorService;
49      private final AtomicBoolean terminated;
50  
51      public RequestListener(
52              final SocketConfig socketConfig,
53              final ServerSocket serversocket,
54              final HttpService httpService,
55              final HttpConnectionFactory<? extends HttpServerConnection> connectionFactory,
56              final ExceptionListener exceptionListener,
57              final ExecutorService executorService) {
58          this.socketConfig = socketConfig;
59          this.serverSocket = serversocket;
60          this.connectionFactory = connectionFactory;
61          this.httpService = httpService;
62          this.exceptionListener = exceptionListener;
63          this.executorService = executorService;
64          this.terminated = new AtomicBoolean(false);
65      }
66  
67      @Override
68      public void run() {
69          try {
70              while (!isTerminated() && !Thread.interrupted()) {
71                  final Socket socket = this.serverSocket.accept();
72                  socket.setSoTimeout(this.socketConfig.getSoTimeout().toMillisecondsIntBound());
73                  socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
74                  socket.setTcpNoDelay(this.socketConfig.isTcpNoDelay());
75                  if (this.socketConfig.getRcvBufSize() > 0) {
76                      socket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
77                  }
78                  if (this.socketConfig.getSndBufSize() > 0) {
79                      socket.setSendBufferSize(this.socketConfig.getSndBufSize());
80                  }
81                  if (this.socketConfig.getSoLinger().toSeconds() >= 0) {
82                      socket.setSoLinger(true, this.socketConfig.getSoLinger().toSecondsIntBound());
83                  }
84                  final HttpServerConnection conn = this.connectionFactory.createConnection(socket);
85                  final Worker/http/impl/bootstrap/Worker.html#Worker">Worker worker = new Worker(this.httpService, conn, this.exceptionListener);
86                  this.executorService.execute(worker);
87              }
88          } catch (final Exception ex) {
89              this.exceptionListener.onError(ex);
90          }
91      }
92  
93      public boolean isTerminated() {
94          return this.terminated.get();
95      }
96  
97      public void terminate() throws IOException {
98          if (this.terminated.compareAndSet(false, true)) {
99              this.serverSocket.close();
100         }
101     }
102 
103 }