1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }