eventmachine-eventmachine 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +169 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +183 -0
- data/docs/DEFERRABLES +138 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +15 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
- data/docs/PURE_RUBY +77 -0
- data/docs/README +74 -0
- data/docs/RELEASE_NOTES +96 -0
- data/docs/SMTP +9 -0
- data/docs/SPAWNED_PROCESSES +93 -0
- data/docs/TODO +10 -0
- data/ext/binder.cpp +126 -0
- data/ext/binder.h +48 -0
- data/ext/cmain.cpp +530 -0
- data/ext/cplusplus.cpp +172 -0
- data/ext/ed.cpp +1473 -0
- data/ext/ed.h +361 -0
- data/ext/em.cpp +1895 -0
- data/ext/em.h +170 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +90 -0
- data/ext/eventmachine_cpp.h +94 -0
- data/ext/extconf.rb +150 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +368 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +327 -0
- data/ext/project.h +119 -0
- data/ext/rubymain.cpp +683 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +408 -0
- data/ext/ssl.h +86 -0
- data/java/src/com/rubyeventmachine/Application.java +196 -0
- data/java/src/com/rubyeventmachine/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
- data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
- data/lib/em/deferrable.rb +208 -0
- data/lib/em/eventable.rb +39 -0
- data/lib/em/future.rb +62 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/processes.rb +68 -0
- data/lib/em/spawnable.rb +88 -0
- data/lib/em/streamer.rb +112 -0
- data/lib/eventmachine.rb +1763 -0
- data/lib/eventmachine_version.rb +31 -0
- data/lib/evma.rb +32 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/jeventmachine.rb +137 -0
- data/lib/pr_eventmachine.rb +1011 -0
- data/lib/protocols/buftok.rb +127 -0
- data/lib/protocols/header_and_content.rb +129 -0
- data/lib/protocols/httpcli2.rb +794 -0
- data/lib/protocols/httpclient.rb +270 -0
- data/lib/protocols/line_and_text.rb +122 -0
- data/lib/protocols/linetext2.rb +163 -0
- data/lib/protocols/postgres.rb +261 -0
- data/lib/protocols/saslauth.rb +179 -0
- data/lib/protocols/smtpclient.rb +308 -0
- data/lib/protocols/smtpserver.rb +556 -0
- data/lib/protocols/stomp.rb +130 -0
- data/lib/protocols/tcptest.rb +57 -0
- data/tasks/cpp.rake +77 -0
- data/tasks/project.rake +78 -0
- data/tasks/tests.rake +192 -0
- data/tests/test_attach.rb +66 -0
- data/tests/test_basic.rb +231 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +161 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_eventables.rb +78 -0
- data/tests/test_exc.rb +58 -0
- data/tests/test_futures.rb +214 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +215 -0
- data/tests/test_httpclient2.rb +133 -0
- data/tests/test_kb.rb +61 -0
- data/tests/test_ltp.rb +192 -0
- data/tests/test_ltp2.rb +320 -0
- data/tests/test_next_tick.rb +102 -0
- data/tests/test_processes.rb +56 -0
- data/tests/test_pure.rb +129 -0
- data/tests/test_running.rb +47 -0
- data/tests/test_sasl.rb +74 -0
- data/tests/test_send_file.rb +245 -0
- data/tests/test_servers.rb +80 -0
- data/tests/test_smtpclient.rb +81 -0
- data/tests/test_smtpserver.rb +93 -0
- data/tests/test_spawn.rb +329 -0
- data/tests/test_ssl_args.rb +68 -0
- data/tests/test_timers.rb +146 -0
- data/tests/test_ud.rb +43 -0
- data/tests/testem.rb +31 -0
- metadata +197 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
package com.rubyeventmachine;
|
30
|
+
|
31
|
+
/**
|
32
|
+
* @author francis
|
33
|
+
*
|
34
|
+
*/
|
35
|
+
public class EmReactorException extends Exception {
|
36
|
+
static final long serialVersionUID = 0;
|
37
|
+
public EmReactorException (String msg) {
|
38
|
+
super (msg);
|
39
|
+
}
|
40
|
+
}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
package com.rubyeventmachine;
|
31
|
+
|
32
|
+
import java.nio.ByteBuffer;
|
33
|
+
|
34
|
+
public interface EventableChannel {
|
35
|
+
|
36
|
+
public void scheduleOutboundData (ByteBuffer bb);
|
37
|
+
|
38
|
+
public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort);
|
39
|
+
|
40
|
+
public void scheduleClose (boolean afterWriting);
|
41
|
+
|
42
|
+
public void startTls();
|
43
|
+
|
44
|
+
public String getBinding();
|
45
|
+
|
46
|
+
public void readInboundData (ByteBuffer dst);
|
47
|
+
|
48
|
+
/**
|
49
|
+
* This is called by the reactor after it finishes running.
|
50
|
+
* The idea is to free network resources.
|
51
|
+
*/
|
52
|
+
public void close();
|
53
|
+
|
54
|
+
public boolean writeOutboundData();
|
55
|
+
|
56
|
+
public void setCommInactivityTimeout (long seconds);
|
57
|
+
}
|
@@ -0,0 +1,171 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
package com.rubyeventmachine;
|
31
|
+
|
32
|
+
import java.nio.ByteBuffer;
|
33
|
+
import java.nio.channels.ClosedChannelException;
|
34
|
+
import java.nio.channels.SelectionKey;
|
35
|
+
import java.nio.channels.Selector;
|
36
|
+
import java.nio.channels.DatagramChannel;
|
37
|
+
import java.util.LinkedList;
|
38
|
+
import java.io.*;
|
39
|
+
import java.net.*;
|
40
|
+
|
41
|
+
public class EventableDatagramChannel implements EventableChannel {
|
42
|
+
|
43
|
+
class Packet {
|
44
|
+
public ByteBuffer bb;
|
45
|
+
public SocketAddress recipient;
|
46
|
+
public Packet (ByteBuffer _bb, SocketAddress _recipient) {
|
47
|
+
bb = _bb;
|
48
|
+
recipient = _recipient;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
DatagramChannel channel;
|
53
|
+
String binding;
|
54
|
+
Selector selector;
|
55
|
+
boolean bCloseScheduled;
|
56
|
+
LinkedList<Packet> outboundQ;
|
57
|
+
SocketAddress returnAddress;
|
58
|
+
|
59
|
+
|
60
|
+
public EventableDatagramChannel (DatagramChannel dc, String _binding, Selector sel) throws ClosedChannelException {
|
61
|
+
channel = dc;
|
62
|
+
binding = _binding;
|
63
|
+
selector = sel;
|
64
|
+
bCloseScheduled = false;
|
65
|
+
outboundQ = new LinkedList<Packet>();
|
66
|
+
|
67
|
+
dc.register(selector, SelectionKey.OP_READ, this);
|
68
|
+
}
|
69
|
+
|
70
|
+
public void scheduleOutboundData (ByteBuffer bb) {
|
71
|
+
try {
|
72
|
+
if ((!bCloseScheduled) && (bb.remaining() > 0)) {
|
73
|
+
outboundQ.addLast(new Packet(bb, returnAddress));
|
74
|
+
channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this);
|
75
|
+
}
|
76
|
+
} catch (ClosedChannelException e) {
|
77
|
+
throw new RuntimeException ("no outbound data");
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
|
82
|
+
try {
|
83
|
+
if ((!bCloseScheduled) && (bb.remaining() > 0)) {
|
84
|
+
outboundQ.addLast(new Packet (bb, new InetSocketAddress (recipAddress, recipPort)));
|
85
|
+
channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this);
|
86
|
+
}
|
87
|
+
} catch (ClosedChannelException e) {
|
88
|
+
throw new RuntimeException ("no outbound data");
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
public void scheduleClose (boolean afterWriting) {
|
93
|
+
System.out.println ("NOT SCHEDULING CLOSE ON DATAGRAM");
|
94
|
+
}
|
95
|
+
|
96
|
+
public void startTls() {
|
97
|
+
throw new RuntimeException ("TLS is unimplemented on this Channel");
|
98
|
+
}
|
99
|
+
|
100
|
+
public String getBinding() {
|
101
|
+
return binding;
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Terminate with extreme prejudice. Don't assume there will be another pass through
|
106
|
+
* the reactor core.
|
107
|
+
*/
|
108
|
+
public void close() {
|
109
|
+
try {
|
110
|
+
channel.close();
|
111
|
+
} catch (IOException e) {
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
public void readInboundData (ByteBuffer dst) {
|
116
|
+
returnAddress = null;
|
117
|
+
try {
|
118
|
+
// If there is no datagram available (we're nonblocking after all),
|
119
|
+
// then channel.receive returns null.
|
120
|
+
returnAddress = channel.receive(dst);
|
121
|
+
} catch (IOException e) {
|
122
|
+
// probably a no-op. The caller will see the empty (or even partial) buffer
|
123
|
+
// and presumably do the right thing.
|
124
|
+
}
|
125
|
+
}
|
126
|
+
|
127
|
+
public boolean writeOutboundData() {
|
128
|
+
while (!outboundQ.isEmpty()) {
|
129
|
+
Packet p = outboundQ.getFirst();
|
130
|
+
int written = 0;
|
131
|
+
try {
|
132
|
+
// With a datagram socket, it's ok to send an empty buffer.
|
133
|
+
written = channel.send(p.bb, p.recipient);
|
134
|
+
}
|
135
|
+
catch (IOException e) {
|
136
|
+
return false;
|
137
|
+
}
|
138
|
+
|
139
|
+
/* Did we consume the whole outbound buffer? If yes, pop it off and
|
140
|
+
* keep looping. If no, the outbound network buffers are full, so break
|
141
|
+
* out of here. There's a flaw that affects outbound buffers that are intentionally
|
142
|
+
* empty. We can tell whether they got sent or not. So we assume they were.
|
143
|
+
* TODO: As implemented, this ALWAYS discards packets if they were at least
|
144
|
+
* partially written. This matches the behavior of the C++ EM. My judgment
|
145
|
+
* is that this is less surprising than fragmenting the data and sending multiple
|
146
|
+
* packets would be. I could be wrong, so this is subject to change.
|
147
|
+
*/
|
148
|
+
|
149
|
+
if ((written > 0) || (p.bb.remaining() == 0))
|
150
|
+
outboundQ.removeFirst();
|
151
|
+
else
|
152
|
+
break;
|
153
|
+
}
|
154
|
+
|
155
|
+
if (outboundQ.isEmpty()) {
|
156
|
+
try {
|
157
|
+
channel.register(selector, SelectionKey.OP_READ, this);
|
158
|
+
} catch (ClosedChannelException e) {}
|
159
|
+
}
|
160
|
+
|
161
|
+
// ALWAYS drain the outbound queue before triggering a connection close.
|
162
|
+
// If anyone wants to close immediately, they're responsible for clearing
|
163
|
+
// the outbound queue.
|
164
|
+
return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
|
165
|
+
}
|
166
|
+
|
167
|
+
public void setCommInactivityTimeout (long seconds) {
|
168
|
+
// TODO
|
169
|
+
System.out.println ("DATAGRAM: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
|
170
|
+
}
|
171
|
+
}
|
@@ -0,0 +1,244 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
/**
|
30
|
+
*
|
31
|
+
*/
|
32
|
+
package com.rubyeventmachine;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* @author francis
|
36
|
+
*
|
37
|
+
*/
|
38
|
+
|
39
|
+
import java.nio.channels.*;
|
40
|
+
import java.nio.*;
|
41
|
+
import java.util.*;
|
42
|
+
import java.io.*;
|
43
|
+
import javax.net.ssl.*;
|
44
|
+
import javax.net.ssl.SSLEngineResult.*;
|
45
|
+
|
46
|
+
import java.security.*;
|
47
|
+
|
48
|
+
public class EventableSocketChannel implements EventableChannel {
|
49
|
+
|
50
|
+
// TODO, must refactor this to permit channels that aren't sockets.
|
51
|
+
SocketChannel channel;
|
52
|
+
String binding;
|
53
|
+
Selector selector;
|
54
|
+
LinkedList<ByteBuffer> outboundQ;
|
55
|
+
boolean bCloseScheduled;
|
56
|
+
boolean bConnectPending;
|
57
|
+
|
58
|
+
SSLEngine sslEngine;
|
59
|
+
|
60
|
+
|
61
|
+
SSLContext sslContext;
|
62
|
+
|
63
|
+
|
64
|
+
public EventableSocketChannel (SocketChannel sc, String _binding, Selector sel) throws ClosedChannelException {
|
65
|
+
channel = sc;
|
66
|
+
binding = _binding;
|
67
|
+
selector = sel;
|
68
|
+
bCloseScheduled = false;
|
69
|
+
bConnectPending = false;
|
70
|
+
outboundQ = new LinkedList<ByteBuffer>();
|
71
|
+
|
72
|
+
sc.register(selector, SelectionKey.OP_READ, this);
|
73
|
+
}
|
74
|
+
|
75
|
+
public String getBinding() {
|
76
|
+
return binding;
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Terminate with extreme prejudice. Don't assume there will be another pass through
|
81
|
+
* the reactor core.
|
82
|
+
*/
|
83
|
+
public void close() {
|
84
|
+
try {
|
85
|
+
channel.close();
|
86
|
+
} catch (IOException e) {
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
public void scheduleOutboundData (ByteBuffer bb) {
|
91
|
+
try {
|
92
|
+
if ((!bCloseScheduled) && (bb.remaining() > 0)) {
|
93
|
+
if (sslEngine != null) {
|
94
|
+
ByteBuffer b = ByteBuffer.allocate(32*1024); // TODO, preallocate this buffer.
|
95
|
+
sslEngine.wrap(bb, b);
|
96
|
+
b.flip();
|
97
|
+
outboundQ.addLast(b);
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
outboundQ.addLast(bb);
|
101
|
+
}
|
102
|
+
channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ | (bConnectPending ? SelectionKey.OP_CONNECT : 0), this);
|
103
|
+
}
|
104
|
+
} catch (ClosedChannelException e) {
|
105
|
+
throw new RuntimeException ("no outbound data");
|
106
|
+
} catch (SSLException e) {
|
107
|
+
throw new RuntimeException ("no outbound data");
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
|
112
|
+
throw new RuntimeException ("datagram sends not supported on this channel");
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Called by the reactor when we have selected readable.
|
117
|
+
*/
|
118
|
+
public void readInboundData (ByteBuffer bb) {
|
119
|
+
try {
|
120
|
+
channel.read(bb);
|
121
|
+
} catch (IOException e) {
|
122
|
+
throw new RuntimeException ("i/o error");
|
123
|
+
}
|
124
|
+
}
|
125
|
+
/**
|
126
|
+
* Called by the reactor when we have selected writable.
|
127
|
+
* Return false to indicate an error that should cause the connection to close.
|
128
|
+
* We can get here with an empty outbound buffer if bCloseScheduled is true.
|
129
|
+
* TODO, VERY IMPORTANT: we're here because we selected writable, but it's always
|
130
|
+
* possible to become unwritable between the poll and when we get here. The way
|
131
|
+
* this code is written, we're depending on a nonblocking write NOT TO CONSUME
|
132
|
+
* the whole outbound buffer in this case, rather than firing an exception.
|
133
|
+
* We should somehow verify that this is indeed Java's defined behavior.
|
134
|
+
* Also TODO, see if we can use gather I/O rather than one write at a time.
|
135
|
+
* Ought to be a big performance enhancer.
|
136
|
+
* @return
|
137
|
+
*/
|
138
|
+
public boolean writeOutboundData(){
|
139
|
+
while (!outboundQ.isEmpty()) {
|
140
|
+
ByteBuffer b = outboundQ.getFirst();
|
141
|
+
try {
|
142
|
+
if (b.remaining() > 0)
|
143
|
+
channel.write(b);
|
144
|
+
}
|
145
|
+
catch (IOException e) {
|
146
|
+
return false;
|
147
|
+
}
|
148
|
+
|
149
|
+
// Did we consume the whole outbound buffer? If yes,
|
150
|
+
// pop it off and keep looping. If no, the outbound network
|
151
|
+
// buffers are full, so break out of here.
|
152
|
+
if (b.remaining() == 0)
|
153
|
+
outboundQ.removeFirst();
|
154
|
+
else
|
155
|
+
break;
|
156
|
+
}
|
157
|
+
|
158
|
+
if (outboundQ.isEmpty()) {
|
159
|
+
try {
|
160
|
+
channel.register(selector, SelectionKey.OP_READ, this);
|
161
|
+
} catch (ClosedChannelException e) {
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
// ALWAYS drain the outbound queue before triggering a connection close.
|
166
|
+
// If anyone wants to close immediately, they're responsible for clearing
|
167
|
+
// the outbound queue.
|
168
|
+
return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
|
169
|
+
}
|
170
|
+
|
171
|
+
public void setConnectPending() throws ClosedChannelException {
|
172
|
+
channel.register(selector, SelectionKey.OP_CONNECT, this);
|
173
|
+
bConnectPending = true;
|
174
|
+
}
|
175
|
+
|
176
|
+
/**
|
177
|
+
* Called by the reactor when we have selected connectable.
|
178
|
+
* Return false to indicate an error that should cause the connection to close.
|
179
|
+
* @throws ClosedChannelException
|
180
|
+
*/
|
181
|
+
public boolean finishConnecting() throws ClosedChannelException {
|
182
|
+
try {
|
183
|
+
channel.finishConnect();
|
184
|
+
}
|
185
|
+
catch (IOException e) {
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
bConnectPending = false;
|
189
|
+
channel.register(selector, SelectionKey.OP_READ | (outboundQ.isEmpty() ? 0 : SelectionKey.OP_WRITE), this);
|
190
|
+
return true;
|
191
|
+
}
|
192
|
+
|
193
|
+
public void scheduleClose (boolean afterWriting) {
|
194
|
+
// TODO: What the hell happens here if bConnectPending is set?
|
195
|
+
if (!afterWriting)
|
196
|
+
outboundQ.clear();
|
197
|
+
try {
|
198
|
+
channel.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE, this);
|
199
|
+
} catch (ClosedChannelException e) {
|
200
|
+
throw new RuntimeException ("unable to schedule close"); // TODO, get rid of this.
|
201
|
+
}
|
202
|
+
bCloseScheduled = true;
|
203
|
+
}
|
204
|
+
public void startTls() {
|
205
|
+
if (sslEngine == null) {
|
206
|
+
try {
|
207
|
+
sslContext = SSLContext.getInstance("TLS");
|
208
|
+
sslContext.init(null, null, null); // TODO, fill in the parameters.
|
209
|
+
sslEngine = sslContext.createSSLEngine(); // TODO, should use the parameterized version, to get Kerb stuff and session re-use.
|
210
|
+
sslEngine.setUseClientMode(false);
|
211
|
+
} catch (NoSuchAlgorithmException e) {
|
212
|
+
throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
|
213
|
+
} catch (KeyManagementException e) {
|
214
|
+
throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
|
215
|
+
}
|
216
|
+
}
|
217
|
+
System.out.println ("Starting TLS");
|
218
|
+
}
|
219
|
+
|
220
|
+
public ByteBuffer dispatchInboundData (ByteBuffer bb) throws SSLException {
|
221
|
+
if (sslEngine != null) {
|
222
|
+
if (true) throw new RuntimeException ("TLS currently unimplemented");
|
223
|
+
System.setProperty("javax.net.debug", "all");
|
224
|
+
ByteBuffer w = ByteBuffer.allocate(32*1024); // TODO, WRONG, preallocate this buffer.
|
225
|
+
SSLEngineResult res = sslEngine.unwrap(bb, w);
|
226
|
+
if (res.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
|
227
|
+
Runnable r;
|
228
|
+
while ((r = sslEngine.getDelegatedTask()) != null) {
|
229
|
+
r.run();
|
230
|
+
}
|
231
|
+
}
|
232
|
+
System.out.println (bb);
|
233
|
+
w.flip();
|
234
|
+
return w;
|
235
|
+
}
|
236
|
+
else
|
237
|
+
return bb;
|
238
|
+
}
|
239
|
+
|
240
|
+
public void setCommInactivityTimeout (long seconds) {
|
241
|
+
// TODO
|
242
|
+
System.out.println ("SOCKET: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
|
243
|
+
}
|
244
|
+
}
|