forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApacheUnixSocket.java
More file actions
321 lines (270 loc) · 8.74 KB
/
Copy pathApacheUnixSocket.java
File metadata and controls
321 lines (270 loc) · 8.74 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.github.dockerjava.jaxrs;
/*
* Copyright (c) 2014 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.SocketChannel;
import java.util.ArrayDeque;
import java.util.Queue;
import org.newsclub.net.unix.AFUNIXSocket;
/**
* Provides a socket that wraps an org.newsclub.net.unix.AFUNIXSocket and delays setting options until the socket is connected. This is
* necessary because the Apache HTTP client attempts to set options prior to connecting the socket, which doesn't work for Unix sockets
* since options are being set on the underlying file descriptor. Until the socket is connected, the file descriptor doesn't exist.
*
* This class also noop's any calls to setReuseAddress, which is called by the Apache client but isn't supported by AFUnixSocket.
*/
public class ApacheUnixSocket extends Socket {
private final AFUNIXSocket inner;
private final Queue<SocketOptionSetter> optionsToSet = new ArrayDeque<SocketOptionSetter>();
public ApacheUnixSocket() throws IOException {
this.inner = AFUNIXSocket.newInstance();
}
@Override
public void connect(final SocketAddress endpoint) throws IOException {
inner.connect(endpoint);
setAllSocketOptions();
}
@Override
public void connect(final SocketAddress endpoint, final int timeout) throws IOException {
inner.connect(endpoint, timeout);
setAllSocketOptions();
}
@Override
public void bind(final SocketAddress bindpoint) throws IOException {
inner.bind(bindpoint);
setAllSocketOptions();
}
@Override
public InetAddress getInetAddress() {
return inner.getInetAddress();
}
@Override
public InetAddress getLocalAddress() {
return inner.getLocalAddress();
}
@Override
public int getPort() {
return inner.getPort();
}
@Override
public int getLocalPort() {
return inner.getLocalPort();
}
@Override
public SocketAddress getRemoteSocketAddress() {
return inner.getRemoteSocketAddress();
}
@Override
public SocketAddress getLocalSocketAddress() {
return inner.getLocalSocketAddress();
}
@Override
public SocketChannel getChannel() {
return inner.getChannel();
}
@Override
public InputStream getInputStream() throws IOException {
return inner.getInputStream();
}
@Override
public OutputStream getOutputStream() throws IOException {
return inner.getOutputStream();
}
private void setSocketOption(final SocketOptionSetter s) throws SocketException {
if (inner.isConnected()) {
s.run();
} else {
if (!optionsToSet.offer(s)) {
throw new SocketException("Failed to queue option");
}
}
}
private void setAllSocketOptions() throws SocketException {
for (SocketOptionSetter s : optionsToSet) {
s.run();
}
}
@Override
public void setTcpNoDelay(final boolean on) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setTcpNoDelay(on);
}
});
}
@Override
public boolean getTcpNoDelay() throws SocketException {
return inner.getTcpNoDelay();
}
@Override
public void setSoLinger(final boolean on, final int linger) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setSoLinger(on, linger);
}
});
}
@Override
public int getSoLinger() throws SocketException {
return inner.getSoLinger();
}
@Override
public void sendUrgentData(final int data) throws IOException {
inner.sendUrgentData(data);
}
@Override
public void setOOBInline(final boolean on) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setOOBInline(on);
}
});
}
@Override
public boolean getOOBInline() throws SocketException {
return inner.getOOBInline();
}
@Override
public synchronized void setSoTimeout(final int timeout) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setSoTimeout(timeout);
}
});
}
@Override
public synchronized int getSoTimeout() throws SocketException {
return inner.getSoTimeout();
}
@Override
public synchronized void setSendBufferSize(final int size) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setSendBufferSize(size);
}
});
}
@Override
public synchronized int getSendBufferSize() throws SocketException {
return inner.getSendBufferSize();
}
@Override
public synchronized void setReceiveBufferSize(final int size) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setReceiveBufferSize(size);
}
});
}
@Override
public synchronized int getReceiveBufferSize() throws SocketException {
return inner.getReceiveBufferSize();
}
@Override
public void setKeepAlive(final boolean on) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setKeepAlive(on);
}
});
}
@Override
public boolean getKeepAlive() throws SocketException {
return inner.getKeepAlive();
}
@Override
public void setTrafficClass(final int tc) throws SocketException {
setSocketOption(new SocketOptionSetter() {
@Override
public void run() throws SocketException {
inner.setTrafficClass(tc);
}
});
}
@Override
public int getTrafficClass() throws SocketException {
return inner.getTrafficClass();
}
@Override
public void setReuseAddress(final boolean on) throws SocketException {
// not supported: Apache client tries to set it, but we want to just ignore it
}
@Override
public boolean getReuseAddress() throws SocketException {
return inner.getReuseAddress();
}
@Override
public synchronized void close() throws IOException {
inner.close();
}
@Override
public void shutdownInput() throws IOException {
inner.shutdownInput();
}
@Override
public void shutdownOutput() throws IOException {
inner.shutdownOutput();
}
@Override
public String toString() {
return inner.toString();
}
@Override
public boolean isConnected() {
return inner.isConnected();
}
@Override
public boolean isBound() {
return inner.isBound();
}
@Override
public boolean isClosed() {
return inner.isClosed();
}
@Override
public boolean isInputShutdown() {
return inner.isInputShutdown();
}
@Override
public boolean isOutputShutdown() {
return inner.isOutputShutdown();
}
@Override
public void setPerformancePreferences(final int connectionTime, final int latency, final int bandwidth) {
inner.setPerformancePreferences(connectionTime, latency, bandwidth);
}
interface SocketOptionSetter {
void run() throws SocketException;
}
}