sonixlabs-eventmachine-java 1.0.0.rc.7-java → 1.0.3.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -12
- data/.travis.yml +12 -0
- data/CHANGELOG.md +33 -0
- data/Gemfile +0 -1
- data/README.md +2 -2
- data/README_JP.md +10 -2
- data/eventmachine.gemspec +10 -7
- data/ext/binder.cpp +1 -1
- data/ext/cmain.cpp +11 -0
- data/ext/ed.cpp +22 -7
- data/ext/ed.h +5 -5
- data/ext/em.cpp +64 -66
- data/ext/em.h +10 -5
- data/ext/eventmachine.h +1 -0
- data/ext/extconf.rb +4 -3
- data/ext/fastfilereader/extconf.rb +1 -1
- data/ext/rubymain.cpp +22 -1
- data/ext/ssl.cpp +1 -1
- data/java/.classpath +1 -3
- data/java/.gitignore +1 -0
- data/java/src/com/rubyeventmachine/DatagramPacket.java +13 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +502 -561
- data/java/src/com/rubyeventmachine/EventCallback.java +7 -0
- data/java/src/com/rubyeventmachine/EventCode.java +26 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +102 -42
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +146 -161
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +371 -334
- data/java/src/com/rubyeventmachine/SslBox.java +310 -0
- data/lib/em/iterator.rb +5 -44
- data/lib/em/processes.rb +1 -1
- data/lib/em/protocols/httpclient.rb +2 -2
- data/lib/em/protocols/line_protocol.rb +2 -2
- data/lib/em/protocols/smtpclient.rb +1 -1
- data/lib/em/protocols/smtpserver.rb +10 -7
- data/lib/em/protocols/stomp.rb +5 -2
- data/lib/em/protocols.rb +1 -0
- data/lib/em/version.rb +1 -1
- data/lib/eventmachine.rb +35 -14
- data/lib/jeventmachine.rb +65 -18
- data/rakelib/package.rake +15 -18
- data/tests/server.crt +36 -0
- data/tests/server.key +51 -0
- data/tests/test_attach.rb +24 -0
- data/tests/test_connection_count.rb +21 -1
- data/tests/test_epoll.rb +15 -0
- data/tests/test_httpclient2.rb +5 -0
- data/tests/test_idle_connection.rb +6 -4
- data/tests/test_iterator.rb +97 -0
- data/tests/test_line_protocol.rb +33 -0
- data/tests/test_pause.rb +24 -0
- data/tests/test_set_sock_opt.rb +1 -1
- data/tests/test_ssl_echo_data.rb +60 -0
- data/tests/test_ssl_methods.rb +15 -7
- data/tests/test_ssl_verify.rb +4 -4
- data/tests/test_stomp.rb +37 -0
- data/tests/test_system.rb +42 -0
- metadata +45 -42
- data/lib/rubyeventmachine.jar +0 -0
@@ -1,588 +1,529 @@
|
|
1
1
|
/**
|
2
2
|
* $Id$
|
3
|
-
*
|
3
|
+
*
|
4
4
|
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
5
|
* Homepage:: http://rubyeventmachine.com
|
6
6
|
* Date:: 15 Jul 2007
|
7
|
-
*
|
7
|
+
*
|
8
8
|
* See EventMachine and EventMachine::Connection for documentation and
|
9
9
|
* usage examples.
|
10
|
-
*
|
10
|
+
*
|
11
11
|
*
|
12
12
|
*----------------------------------------------------------------------------
|
13
13
|
*
|
14
14
|
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
15
|
* Gmail: blackhedd
|
16
|
-
*
|
16
|
+
*
|
17
17
|
* This program is free software; you can redistribute it and/or modify
|
18
18
|
* it under the terms of either: 1) the GNU General Public License
|
19
19
|
* as published by the Free Software Foundation; either version 2 of the
|
20
20
|
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
-
*
|
21
|
+
*
|
22
22
|
* See the file COPYING for complete licensing information.
|
23
23
|
*
|
24
24
|
*---------------------------------------------------------------------------
|
25
25
|
*
|
26
|
-
*
|
26
|
+
*
|
27
27
|
*/
|
28
28
|
|
29
29
|
package com.rubyeventmachine;
|
30
30
|
|
31
|
-
import java.io
|
32
|
-
import java.
|
33
|
-
import java.
|
34
|
-
import java.nio
|
35
|
-
import java.
|
36
|
-
import java.
|
37
|
-
import java.
|
38
|
-
import java.
|
31
|
+
import java.io.IOException;
|
32
|
+
import java.net.InetSocketAddress;
|
33
|
+
import java.net.SocketAddress;
|
34
|
+
import java.nio.ByteBuffer;
|
35
|
+
import java.nio.channels.ClosedChannelException;
|
36
|
+
import java.nio.channels.DatagramChannel;
|
37
|
+
import java.nio.channels.SelectionKey;
|
38
|
+
import java.nio.channels.Selector;
|
39
|
+
import java.nio.channels.ServerSocketChannel;
|
40
|
+
import java.nio.channels.SocketChannel;
|
41
|
+
import java.security.KeyManagementException;
|
42
|
+
import java.security.KeyStore;
|
43
|
+
import java.security.NoSuchAlgorithmException;
|
44
|
+
import java.util.ArrayList;
|
45
|
+
import java.util.Date;
|
46
|
+
import java.util.HashMap;
|
47
|
+
import java.util.Iterator;
|
48
|
+
import java.util.TreeMap;
|
49
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
39
50
|
|
40
51
|
public class EmReactor {
|
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
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
}
|
519
|
-
|
520
|
-
public void signalLoopbreak() {
|
521
|
-
loopBreaker.set(true);
|
522
|
-
if (mySelector != null)
|
523
|
-
mySelector.wakeup();
|
524
|
-
}
|
525
|
-
|
526
|
-
public void startTls (long sig) throws NoSuchAlgorithmException, KeyManagementException {
|
527
|
-
Connections.get(sig).startTls();
|
528
|
-
}
|
529
|
-
|
530
|
-
public void setTimerQuantum (int mills) {
|
531
|
-
if (mills < 5 || mills > 2500)
|
532
|
-
throw new RuntimeException ("attempt to set invalid timer-quantum value: "+mills);
|
533
|
-
timerQuantum = mills;
|
534
|
-
}
|
535
|
-
|
536
|
-
public Object[] getPeerName (long sig) {
|
537
|
-
return Connections.get(sig).getPeerName();
|
538
|
-
}
|
539
|
-
|
540
|
-
public Object[] getSockName (long sig) {
|
541
|
-
return Connections.get(sig).getSockName();
|
542
|
-
}
|
543
|
-
|
544
|
-
public long attachChannel (SocketChannel sc, boolean watch_mode) {
|
545
|
-
long b = createBinding();
|
546
|
-
|
547
|
-
EventableSocketChannel ec = new EventableSocketChannel (sc, b, mySelector);
|
548
|
-
|
549
|
-
ec.setAttached();
|
550
|
-
if (watch_mode)
|
551
|
-
ec.setWatchOnly();
|
552
|
-
|
553
|
-
Connections.put (b, ec);
|
554
|
-
NewConnections.add (b);
|
555
|
-
|
556
|
-
return b;
|
557
|
-
}
|
558
|
-
|
559
|
-
public SocketChannel detachChannel (long sig) {
|
560
|
-
EventableSocketChannel ec = (EventableSocketChannel) Connections.get (sig);
|
561
|
-
if (ec != null) {
|
562
|
-
UnboundConnections.add (sig);
|
563
|
-
return ec.getChannel();
|
564
|
-
} else {
|
565
|
-
return null;
|
566
|
-
}
|
567
|
-
}
|
568
|
-
|
569
|
-
public void setNotifyReadable (long sig, boolean mode) {
|
570
|
-
((EventableSocketChannel) Connections.get(sig)).setNotifyReadable(mode);
|
571
|
-
}
|
572
|
-
|
573
|
-
public void setNotifyWritable (long sig, boolean mode) {
|
574
|
-
((EventableSocketChannel) Connections.get(sig)).setNotifyWritable(mode);
|
575
|
-
}
|
576
|
-
|
577
|
-
public boolean isNotifyReadable (long sig) {
|
578
|
-
return Connections.get(sig).isNotifyReadable();
|
579
|
-
}
|
580
|
-
|
581
|
-
public boolean isNotifyWritable (long sig) {
|
582
|
-
return Connections.get(sig).isNotifyWritable();
|
583
|
-
}
|
584
|
-
|
585
|
-
public int getConnectionCount() {
|
586
|
-
return Connections.size() + Acceptors.size();
|
587
|
-
}
|
52
|
+
private Selector mySelector;
|
53
|
+
private TreeMap<Long, ArrayList<Long>> Timers;
|
54
|
+
private HashMap<Long, EventableChannel<?>> Connections;
|
55
|
+
private HashMap<Long, ServerSocketChannel> Acceptors;
|
56
|
+
private ArrayList<Long> NewConnections;
|
57
|
+
private ArrayList<Long> UnboundConnections;
|
58
|
+
private ArrayList<EventableSocketChannel> DetachedConnections;
|
59
|
+
|
60
|
+
private boolean bRunReactor;
|
61
|
+
private long BindingIndex;
|
62
|
+
private AtomicBoolean loopBreaker;
|
63
|
+
private int timerQuantum;
|
64
|
+
private EventCallback callback;
|
65
|
+
|
66
|
+
public EmReactor(EventCallback callback) {
|
67
|
+
this.callback = callback;
|
68
|
+
Timers = new TreeMap<Long, ArrayList<Long>>();
|
69
|
+
Connections = new HashMap<Long, EventableChannel<?>>();
|
70
|
+
Acceptors = new HashMap<Long, ServerSocketChannel>();
|
71
|
+
NewConnections = new ArrayList<Long>();
|
72
|
+
UnboundConnections = new ArrayList<Long>();
|
73
|
+
DetachedConnections = new ArrayList<EventableSocketChannel>();
|
74
|
+
|
75
|
+
BindingIndex = 0;
|
76
|
+
loopBreaker = new AtomicBoolean();
|
77
|
+
loopBreaker.set(false);
|
78
|
+
timerQuantum = 98;
|
79
|
+
}
|
80
|
+
|
81
|
+
public void run() {
|
82
|
+
try {
|
83
|
+
mySelector = Selector.open();
|
84
|
+
bRunReactor = true;
|
85
|
+
} catch (IOException e) {
|
86
|
+
throw new RuntimeException ("Could not open selector", e);
|
87
|
+
}
|
88
|
+
|
89
|
+
while (bRunReactor) {
|
90
|
+
runLoopbreaks();
|
91
|
+
if (!bRunReactor) break;
|
92
|
+
|
93
|
+
runTimers();
|
94
|
+
if (!bRunReactor) break;
|
95
|
+
|
96
|
+
removeUnboundConnections();
|
97
|
+
checkIO();
|
98
|
+
addNewConnections();
|
99
|
+
processIO();
|
100
|
+
}
|
101
|
+
|
102
|
+
close();
|
103
|
+
}
|
104
|
+
|
105
|
+
void addNewConnections() {
|
106
|
+
for (EventableSocketChannel ec : DetachedConnections) {
|
107
|
+
ec.cleanup();
|
108
|
+
}
|
109
|
+
DetachedConnections.clear();
|
110
|
+
|
111
|
+
for (long b : NewConnections) {
|
112
|
+
EventableChannel<?> ec = Connections.get(b);
|
113
|
+
if (ec != null) {
|
114
|
+
try {
|
115
|
+
ec.register();
|
116
|
+
} catch (ClosedChannelException e) {
|
117
|
+
UnboundConnections.add (ec.getBinding());
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
NewConnections.clear();
|
122
|
+
}
|
123
|
+
|
124
|
+
void removeUnboundConnections() {
|
125
|
+
for (long b : UnboundConnections) {
|
126
|
+
EventableChannel<?> ec = Connections.remove(b);
|
127
|
+
if (ec != null) {
|
128
|
+
callback.trigger(b, EventCode.EM_CONNECTION_UNBOUND, null, (long) 0);
|
129
|
+
ec.close();
|
130
|
+
|
131
|
+
EventableSocketChannel sc = (EventableSocketChannel) ec;
|
132
|
+
if (sc != null && sc.isAttached())
|
133
|
+
DetachedConnections.add (sc);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
UnboundConnections.clear();
|
137
|
+
}
|
138
|
+
|
139
|
+
void checkIO() {
|
140
|
+
long timeout;
|
141
|
+
|
142
|
+
if (NewConnections.size() > 0) {
|
143
|
+
timeout = -1;
|
144
|
+
} else if (!Timers.isEmpty()) {
|
145
|
+
long now = new Date().getTime();
|
146
|
+
long k = Timers.firstKey();
|
147
|
+
long diff = k-now;
|
148
|
+
|
149
|
+
if (diff <= 0)
|
150
|
+
timeout = -1; // don't wait, just poll once
|
151
|
+
else
|
152
|
+
timeout = diff;
|
153
|
+
} else {
|
154
|
+
timeout = 0; // wait indefinitely
|
155
|
+
}
|
156
|
+
|
157
|
+
try {
|
158
|
+
if (timeout == -1)
|
159
|
+
mySelector.selectNow();
|
160
|
+
else
|
161
|
+
mySelector.select(timeout);
|
162
|
+
} catch (IOException e) {
|
163
|
+
e.printStackTrace();
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
void processIO() {
|
168
|
+
Iterator<SelectionKey> it = mySelector.selectedKeys().iterator();
|
169
|
+
while (it.hasNext()) {
|
170
|
+
SelectionKey k = it.next();
|
171
|
+
it.remove();
|
172
|
+
if (k.isConnectable()) {
|
173
|
+
isConnectable(k);
|
174
|
+
}
|
175
|
+
else if (k.isAcceptable()) {
|
176
|
+
isAcceptable(k);
|
177
|
+
}
|
178
|
+
else {
|
179
|
+
if (k.isWritable())
|
180
|
+
isWritable(k);
|
181
|
+
|
182
|
+
if (k.isReadable())
|
183
|
+
isReadable(k);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
void isAcceptable (SelectionKey k) {
|
189
|
+
ServerSocketChannel ss = (ServerSocketChannel) k.channel();
|
190
|
+
SocketChannel sn;
|
191
|
+
long b;
|
192
|
+
|
193
|
+
for (int n = 0; n < 10; n++) {
|
194
|
+
try {
|
195
|
+
sn = ss.accept();
|
196
|
+
if (sn == null)
|
197
|
+
break;
|
198
|
+
} catch (IOException e) {
|
199
|
+
e.printStackTrace();
|
200
|
+
k.cancel();
|
201
|
+
|
202
|
+
ServerSocketChannel server = Acceptors.remove(k.attachment());
|
203
|
+
if (server != null)
|
204
|
+
try{ server.close(); } catch (IOException ex) {};
|
205
|
+
break;
|
206
|
+
}
|
207
|
+
|
208
|
+
try {
|
209
|
+
sn.configureBlocking(false);
|
210
|
+
} catch (IOException e) {
|
211
|
+
e.printStackTrace();
|
212
|
+
continue;
|
213
|
+
}
|
214
|
+
|
215
|
+
b = createBinding();
|
216
|
+
EventableSocketChannel ec = new EventableSocketChannel (sn, b, mySelector, callback);
|
217
|
+
ec.setServerMode();
|
218
|
+
Connections.put (b, ec);
|
219
|
+
NewConnections.add (b);
|
220
|
+
|
221
|
+
callback.trigger(((Long)k.attachment()).longValue(), EventCode.EM_CONNECTION_ACCEPTED, null, b);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
void isReadable (SelectionKey k) {
|
226
|
+
EventableChannel<?> ec = (EventableChannel<?>) k.attachment();
|
227
|
+
if (!ec.read()) {
|
228
|
+
UnboundConnections.add (ec.getBinding());
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
void isWritable (SelectionKey k) {
|
233
|
+
EventableChannel<?> ec = (EventableChannel<?>) k.attachment();
|
234
|
+
if (!ec.write()) {
|
235
|
+
UnboundConnections.add (ec.getBinding());
|
236
|
+
}
|
237
|
+
}
|
238
|
+
|
239
|
+
void isConnectable (SelectionKey k) {
|
240
|
+
EventableSocketChannel ec = (EventableSocketChannel) k.attachment();
|
241
|
+
if (!ec.finishConnecting()) {
|
242
|
+
UnboundConnections.add (ec.getBinding());
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
void close() {
|
247
|
+
try {
|
248
|
+
if (mySelector != null)
|
249
|
+
mySelector.close();
|
250
|
+
} catch (IOException e) {}
|
251
|
+
mySelector = null;
|
252
|
+
|
253
|
+
// run down open connections and sockets.
|
254
|
+
for (ServerSocketChannel c : Acceptors.values()) {
|
255
|
+
try {
|
256
|
+
c.close();
|
257
|
+
} catch (IOException e) {}
|
258
|
+
}
|
259
|
+
|
260
|
+
// 29Sep09: We create an ArrayList of the existing connections, then iterate over
|
261
|
+
// that to call unbind on them. This is because an unbind can trigger a reconnect,
|
262
|
+
// which will add to the Connections HashMap, causing a ConcurrentModificationException.
|
263
|
+
// XXX: The correct behavior here would be to latch the various reactor methods to return
|
264
|
+
// immediately if the reactor is shutting down.
|
265
|
+
ArrayList<EventableChannel<?>> conns = new ArrayList<EventableChannel<?>>();
|
266
|
+
for (EventableChannel<?> ec : Connections.values()) {
|
267
|
+
if (ec != null) {
|
268
|
+
conns.add (ec);
|
269
|
+
}
|
270
|
+
}
|
271
|
+
Connections.clear();
|
272
|
+
|
273
|
+
for (EventableChannel<?> ec : conns) {
|
274
|
+
callback.trigger(ec.getBinding(), EventCode.EM_CONNECTION_UNBOUND, null, (long) 0);
|
275
|
+
ec.close();
|
276
|
+
|
277
|
+
EventableSocketChannel sc = (EventableSocketChannel) ec;
|
278
|
+
if (sc != null && sc.isAttached())
|
279
|
+
DetachedConnections.add (sc);
|
280
|
+
}
|
281
|
+
|
282
|
+
for (EventableSocketChannel ec : DetachedConnections) {
|
283
|
+
ec.cleanup();
|
284
|
+
}
|
285
|
+
DetachedConnections.clear();
|
286
|
+
}
|
287
|
+
|
288
|
+
void runLoopbreaks() {
|
289
|
+
if (loopBreaker.getAndSet(false)) {
|
290
|
+
callback.trigger((long) 0, EventCode.EM_LOOPBREAK_SIGNAL, null, (long) 0);
|
291
|
+
}
|
292
|
+
}
|
293
|
+
|
294
|
+
public void stop() {
|
295
|
+
bRunReactor = false;
|
296
|
+
signalLoopbreak();
|
297
|
+
}
|
298
|
+
|
299
|
+
void runTimers() {
|
300
|
+
long now = new Date().getTime();
|
301
|
+
while (!Timers.isEmpty()) {
|
302
|
+
long k = Timers.firstKey();
|
303
|
+
if (k > now)
|
304
|
+
break;
|
305
|
+
|
306
|
+
ArrayList<Long> callbacks = Timers.get(k);
|
307
|
+
Timers.remove(k);
|
308
|
+
|
309
|
+
// Fire all timers at this timestamp
|
310
|
+
for (long timerCallback : callbacks) {
|
311
|
+
callback.trigger((long) 0, EventCode.EM_TIMER_FIRED, null, timerCallback);
|
312
|
+
}
|
313
|
+
}
|
314
|
+
}
|
315
|
+
|
316
|
+
public long installOneshotTimer (int milliseconds) {
|
317
|
+
long s = createBinding();
|
318
|
+
long deadline = new Date().getTime() + milliseconds;
|
319
|
+
|
320
|
+
if (Timers.containsKey(deadline)) {
|
321
|
+
Timers.get(deadline).add(s);
|
322
|
+
} else {
|
323
|
+
ArrayList<Long> callbacks = new ArrayList<Long>();
|
324
|
+
callbacks.add(s);
|
325
|
+
Timers.put(deadline, callbacks);
|
326
|
+
}
|
327
|
+
|
328
|
+
return s;
|
329
|
+
}
|
330
|
+
|
331
|
+
public long startTcpServer (SocketAddress sa) throws EmReactorException {
|
332
|
+
try {
|
333
|
+
ServerSocketChannel server = ServerSocketChannel.open();
|
334
|
+
server.configureBlocking(false);
|
335
|
+
server.socket().bind (sa);
|
336
|
+
long s = createBinding();
|
337
|
+
Acceptors.put(s, server);
|
338
|
+
server.register(mySelector, SelectionKey.OP_ACCEPT, s);
|
339
|
+
return s;
|
340
|
+
} catch (IOException e) {
|
341
|
+
throw new EmReactorException ("unable to open socket acceptor: " + e.toString());
|
342
|
+
}
|
343
|
+
}
|
344
|
+
|
345
|
+
public long startTcpServer (String address, int port) throws EmReactorException {
|
346
|
+
return startTcpServer (new InetSocketAddress (address, port));
|
347
|
+
}
|
348
|
+
|
349
|
+
public void stopTcpServer (long signature) throws IOException {
|
350
|
+
ServerSocketChannel server = Acceptors.remove(signature);
|
351
|
+
if (server != null)
|
352
|
+
server.close();
|
353
|
+
else
|
354
|
+
throw new RuntimeException ("failed to close unknown acceptor");
|
355
|
+
}
|
356
|
+
|
357
|
+
public long openUdpSocket (InetSocketAddress address) throws IOException {
|
358
|
+
// TODO, don't throw an exception out of here.
|
359
|
+
DatagramChannel dg = DatagramChannel.open();
|
360
|
+
dg.configureBlocking(false);
|
361
|
+
dg.socket().bind(address);
|
362
|
+
long b = createBinding();
|
363
|
+
EventableChannel<?> ec = new EventableDatagramChannel (dg, b, mySelector, callback);
|
364
|
+
dg.register(mySelector, SelectionKey.OP_READ, ec);
|
365
|
+
Connections.put(b, ec);
|
366
|
+
return b;
|
367
|
+
}
|
368
|
+
|
369
|
+
public long openUdpSocket (String address, int port) throws IOException {
|
370
|
+
return openUdpSocket (new InetSocketAddress (address, port));
|
371
|
+
}
|
372
|
+
|
373
|
+
public void sendData (long sig, ByteBuffer bb) throws IOException {
|
374
|
+
Connections.get(sig).scheduleOutboundData( bb );
|
375
|
+
}
|
376
|
+
|
377
|
+
public void sendData (long sig, byte[] data) throws IOException {
|
378
|
+
sendData (sig, ByteBuffer.wrap(data));
|
379
|
+
}
|
380
|
+
|
381
|
+
public void setCommInactivityTimeout (long sig, long mills) {
|
382
|
+
Connections.get(sig).setCommInactivityTimeout (mills);
|
383
|
+
}
|
384
|
+
|
385
|
+
public void sendDatagram (long sig, byte[] data, int length, String recipAddress, int recipPort) {
|
386
|
+
sendDatagram (sig, ByteBuffer.wrap(data), recipAddress, recipPort);
|
387
|
+
}
|
388
|
+
|
389
|
+
public void sendDatagram (long sig, ByteBuffer bb, String recipAddress, int recipPort) {
|
390
|
+
(Connections.get(sig)).scheduleOutboundDatagram( bb, recipAddress, recipPort);
|
391
|
+
}
|
392
|
+
|
393
|
+
public long connectTcpServer (String address, int port) {
|
394
|
+
return connectTcpServer(null, 0, address, port);
|
395
|
+
}
|
396
|
+
|
397
|
+
public long connectTcpServer (String bindAddr, int bindPort, String address, int port) {
|
398
|
+
long b = createBinding();
|
399
|
+
|
400
|
+
try {
|
401
|
+
SocketChannel sc = SocketChannel.open();
|
402
|
+
sc.configureBlocking(false);
|
403
|
+
if (bindAddr != null)
|
404
|
+
sc.socket().bind(new InetSocketAddress (bindAddr, bindPort));
|
405
|
+
|
406
|
+
EventableSocketChannel ec = new EventableSocketChannel (sc, b, mySelector, callback);
|
407
|
+
|
408
|
+
if (sc.connect (new InetSocketAddress (address, port))) {
|
409
|
+
// Connection returned immediately. Can happen with localhost connections.
|
410
|
+
// WARNING, this code is untested due to lack of available test conditions.
|
411
|
+
// Ought to be be able to come here from a localhost connection, but that
|
412
|
+
// doesn't happen on Linux. (Maybe on FreeBSD?)
|
413
|
+
// The reason for not handling this until we can test it is that we
|
414
|
+
// really need to return from this function WITHOUT triggering any EM events.
|
415
|
+
// That's because until the user code has seen the signature we generated here,
|
416
|
+
// it won't be able to properly dispatch them. The C++ EM deals with this
|
417
|
+
// by setting pending mode as a flag in ALL eventable descriptors and making
|
418
|
+
// the descriptor select for writable. Then, it can send UNBOUND and
|
419
|
+
// CONNECTION_COMPLETED on the next pass through the loop, because writable will
|
420
|
+
// fire.
|
421
|
+
throw new RuntimeException ("immediate-connect unimplemented");
|
422
|
+
}
|
423
|
+
else {
|
424
|
+
ec.setConnectPending();
|
425
|
+
Connections.put (b, ec);
|
426
|
+
NewConnections.add (b);
|
427
|
+
}
|
428
|
+
} catch (IOException e) {
|
429
|
+
// Can theoretically come here if a connect failure can be determined immediately.
|
430
|
+
// I don't know how to make that happen for testing purposes.
|
431
|
+
throw new RuntimeException ("immediate-connect unimplemented: " + e.toString());
|
432
|
+
}
|
433
|
+
return b;
|
434
|
+
}
|
435
|
+
|
436
|
+
public void closeConnection (long sig, boolean afterWriting) {
|
437
|
+
EventableChannel<?> ec = Connections.get(sig);
|
438
|
+
if (ec != null)
|
439
|
+
if (ec.scheduleClose (afterWriting))
|
440
|
+
UnboundConnections.add (sig);
|
441
|
+
}
|
442
|
+
|
443
|
+
long createBinding() {
|
444
|
+
return ++BindingIndex;
|
445
|
+
}
|
446
|
+
|
447
|
+
public void signalLoopbreak() {
|
448
|
+
loopBreaker.set(true);
|
449
|
+
if (mySelector != null)
|
450
|
+
mySelector.wakeup();
|
451
|
+
}
|
452
|
+
|
453
|
+
public void setTlsParms(long sig, KeyStore keyStore, boolean verifyPeer) {
|
454
|
+
((EventableSocketChannel) Connections.get(sig)).setTlsParms(keyStore, verifyPeer);
|
455
|
+
}
|
456
|
+
|
457
|
+
public void startTls (long sig) throws NoSuchAlgorithmException, KeyManagementException {
|
458
|
+
Connections.get(sig).startTls();
|
459
|
+
}
|
460
|
+
|
461
|
+
public void acceptSslPeer (long sig) {
|
462
|
+
EventableSocketChannel sc = (EventableSocketChannel) Connections.get(sig);
|
463
|
+
sc.acceptSslPeer();
|
464
|
+
}
|
465
|
+
|
466
|
+
public byte[] getPeerCert (long sig) {
|
467
|
+
EventableSocketChannel sc = (EventableSocketChannel) Connections.get(sig);
|
468
|
+
return sc.getPeerCert();
|
469
|
+
}
|
470
|
+
|
471
|
+
public void setTimerQuantum (int mills) {
|
472
|
+
if (mills < 5 || mills > 2500)
|
473
|
+
throw new RuntimeException ("attempt to set invalid timer-quantum value: "+mills);
|
474
|
+
timerQuantum = mills;
|
475
|
+
}
|
476
|
+
|
477
|
+
public Object[] getPeerName (long sig) {
|
478
|
+
return Connections.get(sig).getPeerName();
|
479
|
+
}
|
480
|
+
|
481
|
+
public Object[] getSockName (long sig) {
|
482
|
+
return Connections.get(sig).getSockName();
|
483
|
+
}
|
484
|
+
|
485
|
+
public long attachChannel (SocketChannel sc, boolean watch_mode) {
|
486
|
+
long b = createBinding();
|
487
|
+
|
488
|
+
EventableSocketChannel ec = new EventableSocketChannel (sc, b, mySelector, callback);
|
489
|
+
|
490
|
+
ec.setAttached();
|
491
|
+
if (watch_mode)
|
492
|
+
ec.setWatchOnly();
|
493
|
+
|
494
|
+
Connections.put (b, ec);
|
495
|
+
NewConnections.add (b);
|
496
|
+
|
497
|
+
return b;
|
498
|
+
}
|
499
|
+
|
500
|
+
public SocketChannel detachChannel (long sig) {
|
501
|
+
EventableSocketChannel ec = (EventableSocketChannel) Connections.get (sig);
|
502
|
+
if (ec != null) {
|
503
|
+
UnboundConnections.add (sig);
|
504
|
+
return ec.getChannel();
|
505
|
+
} else {
|
506
|
+
return null;
|
507
|
+
}
|
508
|
+
}
|
509
|
+
|
510
|
+
public void setNotifyReadable (long sig, boolean mode) {
|
511
|
+
((EventableSocketChannel) Connections.get(sig)).setNotifyReadable(mode);
|
512
|
+
}
|
513
|
+
|
514
|
+
public void setNotifyWritable (long sig, boolean mode) {
|
515
|
+
((EventableSocketChannel) Connections.get(sig)).setNotifyWritable(mode);
|
516
|
+
}
|
517
|
+
|
518
|
+
public boolean isNotifyReadable (long sig) {
|
519
|
+
return Connections.get(sig).isNotifyReadable();
|
520
|
+
}
|
521
|
+
|
522
|
+
public boolean isNotifyWritable (long sig) {
|
523
|
+
return Connections.get(sig).isNotifyWritable();
|
524
|
+
}
|
525
|
+
|
526
|
+
public int getConnectionCount() {
|
527
|
+
return Connections.size() + Acceptors.size();
|
528
|
+
}
|
588
529
|
}
|