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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/**
* $Id$
*
* Author:: Francis Cianfrocca (gmail: blackhedd)
* Homepage:: http://rubyeventmachine.com
* Date:: 15 Jul 2007
*
* See EventMachine and EventMachine::Connection for documentation and
* usage examples.
*
*
*----------------------------------------------------------------------------
*
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
* Gmail: blackhedd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of either: 1) the GNU General Public License
* as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version; or 2) Ruby's License.
*
* See the file COPYING for complete licensing information.
*
*---------------------------------------------------------------------------
*
*
*/
/**
*
*/
package com.rubyeventmachine;
/**
* @author francis
*
*/
import java.nio.channels.*;
import java.nio.*;
import java.util.*;
import java.io.*;
import java.net.Socket;
import javax.net.ssl.*;
import javax.net.ssl.SSLEngineResult.*;
import java.lang.reflect.Field;
import java.security.*;
public class EventableSocketChannel implements EventableChannel {
Selector selector;
SelectionKey channelKey;
SocketChannel channel;
long binding;
LinkedList<ByteBuffer> outboundQ;
long outboundS;
boolean bCloseScheduled;
boolean bConnectPending;
boolean bWatchOnly;
boolean bAttached;
boolean bNotifyReadable;
boolean bNotifyWritable;
boolean bPaused;
SSLEngine sslEngine;
SSLContext sslContext;
public EventableSocketChannel (SocketChannel sc, long _binding, Selector sel) {
channel = sc;
binding = _binding;
selector = sel;
bCloseScheduled = false;
bConnectPending = false;
bWatchOnly = false;
bAttached = false;
bNotifyReadable = false;
bNotifyWritable = false;
outboundQ = new LinkedList<ByteBuffer>();
outboundS = 0;
}
public long getBinding() {
return binding;
}
public SocketChannel getChannel() {
return channel;
}
public void register() throws ClosedChannelException {
if (channelKey == null) {
int events = currentEvents();
channelKey = channel.register(selector, events, this);
}
}
/**
* Terminate with extreme prejudice. Don't assume there will be another pass through
* the reactor core.
*/
public void close() {
if (channelKey != null) {
channelKey.cancel();
channelKey = null;
}
if (bAttached) {
// attached channels are copies, so reset the file descriptor to prevent java from close()ing it
Field f;
FileDescriptor fd;
try {
/* do _NOT_ clobber fdVal here, it will break epoll/kqueue on jdk6!
* channelKey.cancel() above does not occur until the next call to select
* and if fdVal is gone, we will continue to get events for this fd.
*
* instead, remove fdVal in cleanup(), which is processed via DetachedConnections,
* after UnboundConnections but before NewConnections.
*/
f = channel.getClass().getDeclaredField("fd");
f.setAccessible(true);
fd = (FileDescriptor) f.get(channel);
f = fd.getClass().getDeclaredField("fd");
f.setAccessible(true);
f.set(fd, -1);
} catch (java.lang.NoSuchFieldException e) {
e.printStackTrace();
} catch (java.lang.IllegalAccessException e) {
e.printStackTrace();
}
return;
}
try {
channel.close();
} catch (IOException e) {
}
}
public void cleanup() {
if (bAttached) {
Field f;
try {
f = channel.getClass().getDeclaredField("fdVal");
f.setAccessible(true);
f.set(channel, -1);
} catch (java.lang.NoSuchFieldException e) {
e.printStackTrace();
} catch (java.lang.IllegalAccessException e) {
e.printStackTrace();
}
}
channel = null;
}
public void scheduleOutboundData (ByteBuffer bb) {
if (!bCloseScheduled && bb.remaining() > 0) {
if (sslEngine != null) {
try {
ByteBuffer b = ByteBuffer.allocate(32*1024); // TODO, preallocate this buffer.
sslEngine.wrap(bb, b);
b.flip();
outboundQ.addLast(b);
outboundS += b.remaining();
} catch (SSLException e) {
throw new RuntimeException ("ssl error");
}
}
else {
outboundQ.addLast(bb);
outboundS += bb.remaining();
}
updateEvents();
}
}
public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
throw new RuntimeException ("datagram sends not supported on this channel");
}
/**
* Called by the reactor when we have selected readable.
*/
public void readInboundData (ByteBuffer bb) throws IOException {
if (channel.read(bb) == -1)
throw new IOException ("eof");
}
public long getOutboundDataSize() { return outboundS; }
/**
* Called by the reactor when we have selected writable.
* Return false to indicate an error that should cause the connection to close.
* TODO, VERY IMPORTANT: we're here because we selected writable, but it's always
* possible to become unwritable between the poll and when we get here. The way
* this code is written, we're depending on a nonblocking write NOT TO CONSUME
* the whole outbound buffer in this case, rather than firing an exception.
* We should somehow verify that this is indeed Java's defined behavior.
* @return
*/
public boolean writeOutboundData() throws IOException {
ByteBuffer[] bufs = new ByteBuffer[64];
int i;
long written, toWrite;
while (!outboundQ.isEmpty()) {
i = 0;
toWrite = 0;
written = 0;
while (i < 64 && !outboundQ.isEmpty()) {
bufs[i] = outboundQ.removeFirst();
toWrite += bufs[i].remaining();
i++;
}
if (toWrite > 0)
written = channel.write(bufs, 0, i);
outboundS -= written;
// Did we consume the whole outbound buffer? If yes,
// pop it off and keep looping. If no, the outbound network
// buffers are full, so break out of here.
if (written < toWrite) {
while (i > 0 && bufs[i-1].remaining() > 0) {
outboundQ.addFirst(bufs[i-1]);
i--;
}
break;
}
}
if (outboundQ.isEmpty() && !bCloseScheduled) {
updateEvents();
}
// ALWAYS drain the outbound queue before triggering a connection close.
// If anyone wants to close immediately, they're responsible for clearing
// the outbound queue.
return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
}
public void setConnectPending() {
bConnectPending = true;
updateEvents();
}
/**
* Called by the reactor when we have selected connectable.
* Return false to indicate an error that should cause the connection to close.
*/
public boolean finishConnecting() throws IOException {
channel.finishConnect();
bConnectPending = false;
updateEvents();
return true;
}
public boolean scheduleClose (boolean afterWriting) {
// TODO: What the hell happens here if bConnectPending is set?
if (!afterWriting) {
outboundQ.clear();
outboundS = 0;
}
if (outboundQ.isEmpty())
return true;
else {
updateEvents();
bCloseScheduled = true;
return false;
}
}
public void startTls() {
if (sslEngine == null) {
try {
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null); // TODO, fill in the parameters.
sslEngine = sslContext.createSSLEngine(); // TODO, should use the parameterized version, to get Kerb stuff and session re-use.
sslEngine.setUseClientMode(false);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
} catch (KeyManagementException e) {
throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
}
}
System.out.println ("Starting TLS");
}
public ByteBuffer dispatchInboundData (ByteBuffer bb) throws SSLException {
if (sslEngine != null) {
if (true) throw new RuntimeException ("TLS currently unimplemented");
System.setProperty("javax.net.debug", "all");
ByteBuffer w = ByteBuffer.allocate(32*1024); // TODO, WRONG, preallocate this buffer.
SSLEngineResult res = sslEngine.unwrap(bb, w);
if (res.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
Runnable r;
while ((r = sslEngine.getDelegatedTask()) != null) {
r.run();
}
}
System.out.println (bb);
w.flip();
return w;
}
else
return bb;
}
public void setCommInactivityTimeout (long seconds) {
// TODO
System.out.println ("SOCKET: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
}
public Object[] getPeerName () {
Socket sock = channel.socket();
return new Object[]{ sock.getPort(), sock.getInetAddress().getHostAddress() };
}
public Object[] getSockName () {
Socket sock = channel.socket();
return new Object[]{ sock.getLocalPort(),
sock.getLocalAddress().getHostAddress() };
}
public void setWatchOnly() {
bWatchOnly = true;
updateEvents();
}
public boolean isWatchOnly() { return bWatchOnly; }
public void setAttached() {
bAttached = true;
}
public boolean isAttached() { return bAttached; }
public void setNotifyReadable (boolean mode) {
bNotifyReadable = mode;
updateEvents();
}
public boolean isNotifyReadable() { return bNotifyReadable; }
public void setNotifyWritable (boolean mode) {
bNotifyWritable = mode;
updateEvents();
}
public boolean isNotifyWritable() { return bNotifyWritable; }
public boolean pause() {
if (bWatchOnly) {
throw new RuntimeException ("cannot pause/resume 'watch only' connections, set notify readable/writable instead");
}
boolean old = bPaused;
bPaused = true;
updateEvents();
return !old;
}
public boolean resume() {
if (bWatchOnly) {
throw new RuntimeException ("cannot pause/resume 'watch only' connections, set notify readable/writable instead");
}
boolean old = bPaused;
bPaused = false;
updateEvents();
return old;
}
public boolean isPaused() {
return bPaused;
}
private void updateEvents() {
if (channelKey == null)
return;
int events = currentEvents();
if (channelKey.interestOps() != events) {
channelKey.interestOps(events);
}
}
private int currentEvents() {
int events = 0;
if (bWatchOnly)
{
if (bNotifyReadable)
events |= SelectionKey.OP_READ;
if (bNotifyWritable)
events |= SelectionKey.OP_WRITE;
}
else if (!bPaused)
{
if (bConnectPending)
events |= SelectionKey.OP_CONNECT;
else {
events |= SelectionKey.OP_READ;
if (!outboundQ.isEmpty())
events |= SelectionKey.OP_WRITE;
}
}
return events;
}
}
|