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