author | Eugene Ostroukhov <eugeneo@symbian.org> |
Wed, 27 Jan 2010 15:45:27 -0800 | |
changeset 52 | f577ea64429e |
parent 2 | e4420d2515f1 |
permissions | -rw-r--r-- |
2 | 1 |
// Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 |
// Use of this source code is governed by a BSD-style license that can be |
|
3 |
// found in the LICENSE file. |
|
4 |
||
5 |
package org.chromium.sdk.internal.transport; |
|
6 |
||
7 |
import java.io.BufferedReader; |
|
8 |
import java.io.BufferedWriter; |
|
9 |
import java.io.IOException; |
|
10 |
import java.io.InputStreamReader; |
|
11 |
import java.io.OutputStreamWriter; |
|
12 |
import java.io.Reader; |
|
13 |
import java.io.Writer; |
|
14 |
import java.net.Socket; |
|
15 |
import java.net.SocketAddress; |
|
16 |
import java.util.concurrent.BlockingQueue; |
|
17 |
import java.util.concurrent.LinkedBlockingQueue; |
|
18 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
19 |
import java.util.logging.Level; |
|
20 |
import java.util.logging.Logger; |
|
21 |
||
22 |
import org.chromium.sdk.ConnectionLogger; |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
23 |
import org.chromium.sdk.LineReader; |
2 | 24 |
import org.chromium.sdk.internal.transport.Message.MalformedMessageException; |
25 |
||
26 |
/** |
|
27 |
* The low-level network agent handling the reading and writing of Messages |
|
28 |
* using the debugger socket. |
|
29 |
* |
|
30 |
* This class is thread-safe. |
|
31 |
*/ |
|
32 |
public class SocketConnection implements Connection { |
|
33 |
||
34 |
/** |
|
35 |
* A thread that can be gracefully interrupted by a third party. |
|
36 |
* <p> |
|
37 |
* Unfortunately there is no standard way of interrupting I/O in Java. See Bug #4514257 |
|
38 |
* on Java Bug Database (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4514257). |
|
39 |
*/ |
|
40 |
private static abstract class InterruptibleThread extends Thread { |
|
41 |
||
42 |
protected volatile boolean isTerminated = false; |
|
43 |
||
44 |
InterruptibleThread(String name) { |
|
45 |
super(name); |
|
46 |
} |
|
47 |
||
48 |
@Override |
|
49 |
public synchronized void start() { |
|
50 |
this.isTerminated = false; |
|
51 |
super.start(); |
|
52 |
} |
|
53 |
||
54 |
@Override |
|
55 |
public synchronized void interrupt() { |
|
56 |
this.isTerminated = true; |
|
57 |
super.interrupt(); |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
* Character encoding used in the socket data interchange. |
|
63 |
*/ |
|
64 |
private static final String SOCKET_CHARSET = "UTF-8"; |
|
65 |
||
66 |
/** |
|
67 |
* A thread writing client-supplied messages into the debugger socket. |
|
68 |
*/ |
|
69 |
private class WriterThread extends InterruptibleThread { |
|
70 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
71 |
private final ConnectionLogger.LoggableWriter writer; |
2 | 72 |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
73 |
public WriterThread(ConnectionLogger.LoggableWriter writer) { |
2 | 74 |
super("WriterThread"); |
75 |
this.writer = writer; |
|
76 |
} |
|
77 |
||
78 |
@Override |
|
79 |
public void run() { |
|
80 |
while (!isTerminated && isAttached.get()) { |
|
81 |
try { |
|
82 |
handleOutboundMessage(outboundQueue.take()); |
|
83 |
} catch (InterruptedException e) { |
|
84 |
// interrupt called on this thread, exit on isTerminated |
|
85 |
} |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
private void handleOutboundMessage(Message message) { |
|
90 |
try { |
|
91 |
LOGGER.log(Level.FINER, "-->{0}", message); |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
92 |
message.sendThrough(writer.getWriter()); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
93 |
writer.markSeparatorForLog(); |
2 | 94 |
} catch (IOException e) { |
95 |
SocketConnection.this.shutdown(e, false); |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
private static abstract class MessageItem { |
|
101 |
abstract void report(NetListener listener); |
|
102 |
abstract boolean isEos(); |
|
103 |
} |
|
104 |
private static final MessageItem EOS = new MessageItem() { |
|
105 |
@Override |
|
106 |
void report(NetListener listener) { |
|
107 |
LOGGER.log(Level.FINER, "<--EOS"); |
|
108 |
listener.eosReceived(); |
|
109 |
} |
|
110 |
@Override |
|
111 |
boolean isEos() { |
|
112 |
return true; |
|
113 |
} |
|
114 |
}; |
|
115 |
private static class RegularMessageItem extends MessageItem { |
|
116 |
private final Message message; |
|
117 |
RegularMessageItem(Message message) { |
|
118 |
this.message = message; |
|
119 |
} |
|
120 |
@Override |
|
121 |
void report(NetListener listener) { |
|
122 |
LOGGER.log(Level.FINER, "<--{0}", message); |
|
123 |
listener.messageReceived(message); |
|
124 |
} |
|
125 |
@Override |
|
126 |
boolean isEos() { |
|
127 |
return false; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* A thread reading data from the debugger socket. |
|
133 |
*/ |
|
134 |
private class ReaderThread extends InterruptibleThread { |
|
135 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
136 |
private final ConnectionLogger.LoggableReader reader; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
137 |
private final ConnectionLogger.LoggableWriter handshakeWriter; |
2 | 138 |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
139 |
public ReaderThread(ConnectionLogger.LoggableReader reader, |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
140 |
ConnectionLogger.LoggableWriter handshakeWriter) { |
2 | 141 |
super("ReaderThread"); |
142 |
this.reader = reader; |
|
143 |
this.handshakeWriter = handshakeWriter; |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
147 |
public void run() { |
|
148 |
Exception breakException; |
|
149 |
try { |
|
150 |
/** The thread that dispatches the inbound messages (to avoid queue growth.) */ |
|
151 |
startResponseDispatcherThread(); |
|
152 |
||
153 |
if (connectionLogger != null) { |
|
154 |
connectionLogger.start(); |
|
155 |
} |
|
156 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
157 |
handshaker.perform(reader.getReader(), handshakeWriter.getWriter()); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
158 |
|
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
159 |
reader.markSeparatorForLog(); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
160 |
handshakeWriter.markSeparatorForLog(); |
2 | 161 |
|
162 |
startWriterThread(); |
|
163 |
||
164 |
while (!isTerminated && isAttached.get()) { |
|
165 |
Message message; |
|
166 |
try { |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
167 |
message = Message.fromBufferedReader(reader.getReader()); |
2 | 168 |
} catch (MalformedMessageException e) { |
169 |
LOGGER.log(Level.SEVERE, "Malformed protocol message", e); |
|
170 |
continue; |
|
171 |
} |
|
172 |
if (message == null) { |
|
173 |
LOGGER.fine("End of stream"); |
|
174 |
break; |
|
175 |
} |
|
176 |
inboundQueue.add(new RegularMessageItem(message)); |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
177 |
reader.markSeparatorForLog(); |
2 | 178 |
} |
179 |
breakException = null; |
|
180 |
} catch (IOException e) { |
|
181 |
breakException = e; |
|
182 |
} finally { |
|
183 |
inboundQueue.add(EOS); |
|
184 |
} |
|
185 |
if (!isInterrupted()) { |
|
186 |
SocketConnection.this.shutdown(breakException, false); |
|
187 |
} |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* A thread dispatching V8 responses (to avoid locking the ReaderThread.) |
|
193 |
*/ |
|
194 |
private class ResponseDispatcherThread extends Thread { |
|
195 |
||
196 |
public ResponseDispatcherThread() { |
|
197 |
super("ResponseDispatcherThread"); |
|
198 |
} |
|
199 |
||
200 |
@Override |
|
201 |
public void run() { |
|
202 |
MessageItem messageItem; |
|
203 |
try { |
|
204 |
while (true) { |
|
205 |
messageItem = inboundQueue.take(); |
|
206 |
try { |
|
207 |
messageItem.report(listener); |
|
208 |
} catch (Exception e) { |
|
209 |
LOGGER.log(Level.SEVERE, "Exception in message listener", e); |
|
210 |
} |
|
211 |
if (messageItem.isEos()) { |
|
212 |
if (connectionLogger != null) { |
|
213 |
connectionLogger.handleEos(); |
|
214 |
} |
|
215 |
break; |
|
216 |
} |
|
217 |
} |
|
218 |
} catch (InterruptedException e) { |
|
219 |
// terminate thread |
|
220 |
} |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
/** The class logger. */ |
|
225 |
private static final Logger LOGGER = Logger.getLogger(SocketConnection.class.getName()); |
|
226 |
||
227 |
/** Lameduck shutdown delay in ms. */ |
|
228 |
private static final int LAMEDUCK_DELAY_MS = 1000; |
|
229 |
||
230 |
/** The input stream buffer size. */ |
|
231 |
private static final int INPUT_BUFFER_SIZE_BYTES = 65536; |
|
232 |
||
233 |
private static final NetListener NULL_LISTENER = new NetListener() { |
|
234 |
public void connectionClosed() { |
|
235 |
} |
|
236 |
||
237 |
public void eosReceived() { |
|
238 |
} |
|
239 |
||
240 |
public void messageReceived(Message message) { |
|
241 |
} |
|
242 |
}; |
|
243 |
||
244 |
/** Whether the agent is currently attached to a remote browser. */ |
|
245 |
private AtomicBoolean isAttached = new AtomicBoolean(false); |
|
246 |
||
247 |
/** The communication socket. */ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
248 |
private Socket socket; |
2 | 249 |
|
250 |
/** The socket reader. */ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
251 |
private ConnectionLogger.LoggableReader reader; |
2 | 252 |
|
253 |
/** The socket writer. */ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
254 |
private ConnectionLogger.LoggableWriter writer; |
2 | 255 |
|
256 |
private final ConnectionLogger connectionLogger; |
|
257 |
||
258 |
/** Handshaker used to establish connection. */ |
|
259 |
private final Handshaker handshaker; |
|
260 |
||
261 |
/** The listener to report network events to. */ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
262 |
private volatile NetListener listener; |
2 | 263 |
|
264 |
/** The inbound message queue. */ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
265 |
private final BlockingQueue<MessageItem> inboundQueue = new LinkedBlockingQueue<MessageItem>(); |
2 | 266 |
|
267 |
/** The outbound message queue. */ |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
268 |
private final BlockingQueue<Message> outboundQueue = new LinkedBlockingQueue<Message>(); |
2 | 269 |
|
270 |
/** The socket endpoint. */ |
|
271 |
private final SocketAddress socketEndpoint; |
|
272 |
||
273 |
/** The thread that processes the outbound queue. */ |
|
274 |
private WriterThread writerThread; |
|
275 |
||
276 |
/** The thread that processes the inbound queue. */ |
|
277 |
private ReaderThread readerThread; |
|
278 |
||
279 |
/** Connection attempt timeout in ms. */ |
|
280 |
private final int connectionTimeoutMs; |
|
281 |
||
282 |
public SocketConnection(SocketAddress endpoint, int connectionTimeoutMs, |
|
283 |
ConnectionLogger connectionLogger, Handshaker handshaker) { |
|
284 |
this.socketEndpoint = endpoint; |
|
285 |
this.connectionTimeoutMs = connectionTimeoutMs; |
|
286 |
this.connectionLogger = connectionLogger; |
|
287 |
this.handshaker = handshaker; |
|
288 |
} |
|
289 |
||
290 |
void attach() throws IOException { |
|
291 |
this.socket = new Socket(); |
|
292 |
this.socket.connect(socketEndpoint, connectionTimeoutMs); |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
293 |
final Writer streamWriter = new OutputStreamWriter(socket.getOutputStream(), SOCKET_CHARSET); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
294 |
final Reader streamReader = new InputStreamReader(socket.getInputStream(), SOCKET_CHARSET); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
295 |
|
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
296 |
ConnectionLogger.LoggableReader loggableReader = new ConnectionLogger.LoggableReader() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
297 |
private final LineReader lineReader; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
298 |
{ |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
299 |
final BufferedReader bufferedReader = |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
300 |
new BufferedReader(streamReader, INPUT_BUFFER_SIZE_BYTES); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
301 |
lineReader = new LineReader() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
302 |
public int read(char[] cbuf, int off, int len) throws IOException { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
303 |
return bufferedReader.read(cbuf, off, len); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
304 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
305 |
public String readLine() throws IOException { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
306 |
return bufferedReader.readLine(); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
307 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
308 |
}; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
309 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
310 |
|
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
311 |
public LineReader getReader() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
312 |
return lineReader; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
313 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
314 |
|
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
315 |
public void markSeparatorForLog() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
316 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
317 |
}; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
318 |
|
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
319 |
ConnectionLogger.LoggableWriter loggableWriter = new ConnectionLogger.LoggableWriter() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
320 |
private final BufferedWriter bufferedWriter = new BufferedWriter(streamWriter); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
321 |
public Writer getWriter() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
322 |
return bufferedWriter; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
323 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
324 |
public void markSeparatorForLog() { |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
325 |
} |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
326 |
}; |
2 | 327 |
|
328 |
if (connectionLogger != null) { |
|
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
329 |
loggableWriter = connectionLogger.wrapWriter(loggableWriter); |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
330 |
loggableReader = connectionLogger.wrapReader(loggableReader); |
2 | 331 |
connectionLogger.setConnectionCloser(new ConnectionLogger.ConnectionCloser() { |
332 |
public void closeConnection() { |
|
333 |
close(); |
|
334 |
} |
|
335 |
}); |
|
336 |
} |
|
337 |
||
52
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
338 |
this.writer = loggableWriter; |
f577ea64429e
Migrated to unmodified Chromium Development Tools version
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
2
diff
changeset
|
339 |
this.reader = loggableReader; |
2 | 340 |
isAttached.set(true); |
341 |
||
342 |
this.readerThread = new ReaderThread(reader, writer); |
|
343 |
// We do not start WriterThread until handshake is done (see ReaderThread) |
|
344 |
this.writerThread = null; |
|
345 |
readerThread.setDaemon(true); |
|
346 |
readerThread.start(); |
|
347 |
} |
|
348 |
||
349 |
void detach(boolean lameduckMode) { |
|
350 |
shutdown(null, lameduckMode); |
|
351 |
} |
|
352 |
||
353 |
void sendMessage(Message message) { |
|
354 |
outboundQueue.add(message); |
|
355 |
} |
|
356 |
||
357 |
private boolean isAttached() { |
|
358 |
return isAttached.get(); |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* The method is synchronized so that it does not get called |
|
363 |
* from the {Reader,Writer}Thread when the underlying socket is |
|
364 |
* closed in another invocation of this method. |
|
365 |
*/ |
|
366 |
private void shutdown(Exception cause, boolean lameduckMode) { |
|
367 |
if (!isAttached.compareAndSet(true, false)) { |
|
368 |
// already shut down |
|
369 |
return; |
|
370 |
} |
|
371 |
LOGGER.log(Level.INFO, "Shutdown requested", cause); |
|
372 |
||
373 |
if (lameduckMode) { |
|
374 |
Thread terminationThread = new Thread("ServiceThreadTerminator") { |
|
375 |
@Override |
|
376 |
public void run() { |
|
377 |
interruptServiceThreads(); |
|
378 |
} |
|
379 |
}; |
|
380 |
terminationThread.setDaemon(true); |
|
381 |
terminationThread.start(); |
|
382 |
try { |
|
383 |
terminationThread.join(LAMEDUCK_DELAY_MS); |
|
384 |
} catch (InterruptedException e) { |
|
385 |
// fall through |
|
386 |
} |
|
387 |
} else { |
|
388 |
interruptServiceThreads(); |
|
389 |
} |
|
390 |
||
391 |
try { |
|
392 |
socket.shutdownInput(); |
|
393 |
} catch (IOException e) { |
|
394 |
// ignore |
|
395 |
} |
|
396 |
try { |
|
397 |
socket.shutdownOutput(); |
|
398 |
} catch (IOException e) { |
|
399 |
// ignore |
|
400 |
} |
|
401 |
||
402 |
try { |
|
403 |
socket.close(); |
|
404 |
} catch (IOException e) { |
|
405 |
// ignore |
|
406 |
} |
|
407 |
listener.connectionClosed(); |
|
408 |
} |
|
409 |
||
410 |
private void interruptServiceThreads() { |
|
411 |
interruptThread(writerThread); |
|
412 |
interruptThread(readerThread); |
|
413 |
} |
|
414 |
||
415 |
private void startWriterThread() { |
|
416 |
if (writerThread != null) { |
|
417 |
throw new IllegalStateException(); |
|
418 |
} |
|
419 |
writerThread = new WriterThread(writer); |
|
420 |
writerThread.setDaemon(true); |
|
421 |
writerThread.start(); |
|
422 |
} |
|
423 |
||
424 |
private ResponseDispatcherThread startResponseDispatcherThread() { |
|
425 |
ResponseDispatcherThread dispatcherThread; |
|
426 |
dispatcherThread = new ResponseDispatcherThread(); |
|
427 |
dispatcherThread.setDaemon(true); |
|
428 |
dispatcherThread.start(); |
|
429 |
return dispatcherThread; |
|
430 |
} |
|
431 |
||
432 |
private void interruptThread(Thread thread) { |
|
433 |
try { |
|
434 |
if (thread != null) { |
|
435 |
thread.interrupt(); |
|
436 |
} |
|
437 |
} catch (SecurityException e) { |
|
438 |
// ignore |
|
439 |
} |
|
440 |
} |
|
441 |
||
442 |
public void close() { |
|
443 |
if (isAttached()) { |
|
444 |
detach(true); |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
public boolean isConnected() { |
|
449 |
return isAttached(); |
|
450 |
} |
|
451 |
||
452 |
public void send(Message message) { |
|
453 |
checkAttached(); |
|
454 |
sendMessage(message); |
|
455 |
} |
|
456 |
||
457 |
public void setNetListener(NetListener netListener) { |
|
458 |
if (this.listener != null && netListener != this.listener) { |
|
459 |
throw new IllegalStateException("Cannot change NetListener"); |
|
460 |
} |
|
461 |
this.listener = netListener != null |
|
462 |
? netListener |
|
463 |
: NULL_LISTENER; |
|
464 |
} |
|
465 |
||
466 |
public void start() throws IOException { |
|
467 |
try { |
|
468 |
if (!isAttached()) { |
|
469 |
attach(); |
|
470 |
} |
|
471 |
} catch (IOException e) { |
|
472 |
listener.connectionClosed(); |
|
473 |
throw e; |
|
474 |
} |
|
475 |
} |
|
476 |
||
477 |
private void checkAttached() { |
|
478 |
if (!isAttached()) { |
|
479 |
throw new IllegalStateException("Connection not attached"); |
|
480 |
} |
|
481 |
} |
|
482 |
} |