eventmachine 1.0.0.beta.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/.gitignore +16 -0
  2. data/Gemfile +1 -0
  3. data/README +81 -0
  4. data/Rakefile +11 -0
  5. data/docs/COPYING +60 -0
  6. data/docs/ChangeLog +211 -0
  7. data/docs/DEFERRABLES +246 -0
  8. data/docs/EPOLL +141 -0
  9. data/docs/GNU +281 -0
  10. data/docs/INSTALL +13 -0
  11. data/docs/KEYBOARD +42 -0
  12. data/docs/LEGAL +25 -0
  13. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -0
  14. data/docs/PURE_RUBY +75 -0
  15. data/docs/RELEASE_NOTES +94 -0
  16. data/docs/SMTP +4 -0
  17. data/docs/SPAWNED_PROCESSES +148 -0
  18. data/docs/TODO +8 -0
  19. data/eventmachine.gemspec +33 -0
  20. data/examples/ex_channel.rb +43 -0
  21. data/examples/ex_queue.rb +2 -0
  22. data/examples/ex_tick_loop_array.rb +15 -0
  23. data/examples/ex_tick_loop_counter.rb +32 -0
  24. data/examples/helper.rb +2 -0
  25. data/ext/binder.cpp +124 -0
  26. data/ext/binder.h +46 -0
  27. data/ext/cmain.cpp +838 -0
  28. data/ext/ed.cpp +1884 -0
  29. data/ext/ed.h +418 -0
  30. data/ext/em.cpp +2348 -0
  31. data/ext/em.h +228 -0
  32. data/ext/eventmachine.h +123 -0
  33. data/ext/extconf.rb +157 -0
  34. data/ext/fastfilereader/extconf.rb +85 -0
  35. data/ext/fastfilereader/mapper.cpp +214 -0
  36. data/ext/fastfilereader/mapper.h +59 -0
  37. data/ext/fastfilereader/rubymain.cpp +127 -0
  38. data/ext/kb.cpp +79 -0
  39. data/ext/page.cpp +107 -0
  40. data/ext/page.h +51 -0
  41. data/ext/pipe.cpp +347 -0
  42. data/ext/project.h +155 -0
  43. data/ext/rubymain.cpp +1200 -0
  44. data/ext/ssl.cpp +460 -0
  45. data/ext/ssl.h +94 -0
  46. data/java/.classpath +8 -0
  47. data/java/.project +17 -0
  48. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  49. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  50. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  51. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  52. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  53. data/lib/em/buftok.rb +138 -0
  54. data/lib/em/callback.rb +26 -0
  55. data/lib/em/channel.rb +57 -0
  56. data/lib/em/connection.rb +569 -0
  57. data/lib/em/deferrable.rb +206 -0
  58. data/lib/em/file_watch.rb +54 -0
  59. data/lib/em/future.rb +61 -0
  60. data/lib/em/iterator.rb +270 -0
  61. data/lib/em/messages.rb +66 -0
  62. data/lib/em/process_watch.rb +44 -0
  63. data/lib/em/processes.rb +119 -0
  64. data/lib/em/protocols.rb +36 -0
  65. data/lib/em/protocols/header_and_content.rb +138 -0
  66. data/lib/em/protocols/httpclient.rb +268 -0
  67. data/lib/em/protocols/httpclient2.rb +590 -0
  68. data/lib/em/protocols/line_and_text.rb +125 -0
  69. data/lib/em/protocols/line_protocol.rb +28 -0
  70. data/lib/em/protocols/linetext2.rb +161 -0
  71. data/lib/em/protocols/memcache.rb +323 -0
  72. data/lib/em/protocols/object_protocol.rb +45 -0
  73. data/lib/em/protocols/postgres3.rb +247 -0
  74. data/lib/em/protocols/saslauth.rb +175 -0
  75. data/lib/em/protocols/smtpclient.rb +357 -0
  76. data/lib/em/protocols/smtpserver.rb +640 -0
  77. data/lib/em/protocols/socks4.rb +66 -0
  78. data/lib/em/protocols/stomp.rb +200 -0
  79. data/lib/em/protocols/tcptest.rb +53 -0
  80. data/lib/em/pure_ruby.rb +1013 -0
  81. data/lib/em/queue.rb +62 -0
  82. data/lib/em/spawnable.rb +85 -0
  83. data/lib/em/streamer.rb +130 -0
  84. data/lib/em/tick_loop.rb +85 -0
  85. data/lib/em/timers.rb +57 -0
  86. data/lib/em/version.rb +3 -0
  87. data/lib/eventmachine.rb +1548 -0
  88. data/lib/jeventmachine.rb +258 -0
  89. data/lib/rubyeventmachine.rb +2 -0
  90. data/setup.rb +1585 -0
  91. data/tasks/cpp.rake_example +77 -0
  92. data/tasks/doc.rake +30 -0
  93. data/tasks/package.rake +85 -0
  94. data/tasks/test.rake +6 -0
  95. data/tests/client.crt +31 -0
  96. data/tests/client.key +51 -0
  97. data/tests/test_attach.rb +136 -0
  98. data/tests/test_basic.rb +249 -0
  99. data/tests/test_channel.rb +64 -0
  100. data/tests/test_connection_count.rb +35 -0
  101. data/tests/test_defer.rb +49 -0
  102. data/tests/test_deferrable.rb +35 -0
  103. data/tests/test_epoll.rb +160 -0
  104. data/tests/test_error_handler.rb +35 -0
  105. data/tests/test_errors.rb +82 -0
  106. data/tests/test_exc.rb +55 -0
  107. data/tests/test_file_watch.rb +49 -0
  108. data/tests/test_futures.rb +198 -0
  109. data/tests/test_get_sock_opt.rb +30 -0
  110. data/tests/test_handler_check.rb +37 -0
  111. data/tests/test_hc.rb +190 -0
  112. data/tests/test_httpclient.rb +227 -0
  113. data/tests/test_httpclient2.rb +154 -0
  114. data/tests/test_inactivity_timeout.rb +50 -0
  115. data/tests/test_kb.rb +60 -0
  116. data/tests/test_ltp.rb +190 -0
  117. data/tests/test_ltp2.rb +317 -0
  118. data/tests/test_next_tick.rb +133 -0
  119. data/tests/test_object_protocol.rb +37 -0
  120. data/tests/test_pause.rb +70 -0
  121. data/tests/test_pending_connect_timeout.rb +48 -0
  122. data/tests/test_process_watch.rb +50 -0
  123. data/tests/test_processes.rb +128 -0
  124. data/tests/test_proxy_connection.rb +144 -0
  125. data/tests/test_pure.rb +134 -0
  126. data/tests/test_queue.rb +44 -0
  127. data/tests/test_running.rb +42 -0
  128. data/tests/test_sasl.rb +72 -0
  129. data/tests/test_send_file.rb +251 -0
  130. data/tests/test_servers.rb +76 -0
  131. data/tests/test_smtpclient.rb +83 -0
  132. data/tests/test_smtpserver.rb +85 -0
  133. data/tests/test_spawn.rb +322 -0
  134. data/tests/test_ssl_args.rb +79 -0
  135. data/tests/test_ssl_methods.rb +50 -0
  136. data/tests/test_ssl_verify.rb +82 -0
  137. data/tests/test_tick_loop.rb +59 -0
  138. data/tests/test_timers.rb +160 -0
  139. data/tests/test_ud.rb +36 -0
  140. data/tests/testem.rb +31 -0
  141. metadata +240 -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,69 @@
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.io.IOException;
34
+ import java.nio.channels.ClosedChannelException;
35
+
36
+ public interface EventableChannel {
37
+
38
+ public void scheduleOutboundData (ByteBuffer bb);
39
+
40
+ public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort);
41
+
42
+ public boolean scheduleClose (boolean afterWriting);
43
+
44
+ public void startTls();
45
+
46
+ public long getBinding();
47
+
48
+ public void readInboundData (ByteBuffer dst) throws IOException;
49
+
50
+ public void register() throws ClosedChannelException;
51
+
52
+ /**
53
+ * This is called by the reactor after it finishes running.
54
+ * The idea is to free network resources.
55
+ */
56
+ public void close();
57
+
58
+ public boolean writeOutboundData() throws IOException;
59
+
60
+ public void setCommInactivityTimeout (long seconds);
61
+
62
+ public Object[] getPeerName();
63
+
64
+ public boolean isWatchOnly();
65
+
66
+ public boolean isNotifyReadable();
67
+ public boolean isNotifyWritable();
68
+
69
+ }
@@ -0,0 +1,189 @@
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
+ long binding;
54
+ Selector selector;
55
+ boolean bCloseScheduled;
56
+ LinkedList<Packet> outboundQ;
57
+ SocketAddress returnAddress;
58
+
59
+
60
+ public EventableDatagramChannel (DatagramChannel dc, long _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 boolean scheduleClose (boolean afterWriting) {
93
+ System.out.println ("NOT SCHEDULING CLOSE ON DATAGRAM");
94
+ return false;
95
+ }
96
+
97
+ public void startTls() {
98
+ throw new RuntimeException ("TLS is unimplemented on this Channel");
99
+ }
100
+
101
+ public long getBinding() {
102
+ return binding;
103
+ }
104
+
105
+ public void register() throws ClosedChannelException {
106
+ // TODO
107
+ }
108
+
109
+ /**
110
+ * Terminate with extreme prejudice. Don't assume there will be another pass through
111
+ * the reactor core.
112
+ */
113
+ public void close() {
114
+ try {
115
+ channel.close();
116
+ } catch (IOException e) {
117
+ }
118
+ }
119
+
120
+ public void readInboundData (ByteBuffer dst) {
121
+ returnAddress = null;
122
+ try {
123
+ // If there is no datagram available (we're nonblocking after all),
124
+ // then channel.receive returns null.
125
+ returnAddress = channel.receive(dst);
126
+ } catch (IOException e) {
127
+ // probably a no-op. The caller will see the empty (or even partial) buffer
128
+ // and presumably do the right thing.
129
+ }
130
+ }
131
+
132
+ public boolean writeOutboundData() {
133
+ while (!outboundQ.isEmpty()) {
134
+ Packet p = outboundQ.getFirst();
135
+ int written = 0;
136
+ try {
137
+ // With a datagram socket, it's ok to send an empty buffer.
138
+ written = channel.send(p.bb, p.recipient);
139
+ }
140
+ catch (IOException e) {
141
+ return false;
142
+ }
143
+
144
+ /* Did we consume the whole outbound buffer? If yes, pop it off and
145
+ * keep looping. If no, the outbound network buffers are full, so break
146
+ * out of here. There's a flaw that affects outbound buffers that are intentionally
147
+ * empty. We can tell whether they got sent or not. So we assume they were.
148
+ * TODO: As implemented, this ALWAYS discards packets if they were at least
149
+ * partially written. This matches the behavior of the C++ EM. My judgment
150
+ * is that this is less surprising than fragmenting the data and sending multiple
151
+ * packets would be. I could be wrong, so this is subject to change.
152
+ */
153
+
154
+ if ((written > 0) || (p.bb.remaining() == 0))
155
+ outboundQ.removeFirst();
156
+ else
157
+ break;
158
+ }
159
+
160
+ if (outboundQ.isEmpty()) {
161
+ try {
162
+ channel.register(selector, SelectionKey.OP_READ, this);
163
+ } catch (ClosedChannelException e) {}
164
+ }
165
+
166
+ // ALWAYS drain the outbound queue before triggering a connection close.
167
+ // If anyone wants to close immediately, they're responsible for clearing
168
+ // the outbound queue.
169
+ return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
170
+ }
171
+
172
+ public void setCommInactivityTimeout (long seconds) {
173
+ // TODO
174
+ System.out.println ("DATAGRAM: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
175
+ }
176
+
177
+ public Object[] getPeerName () {
178
+ if (returnAddress != null) {
179
+ InetSocketAddress inetAddr = (InetSocketAddress) returnAddress;
180
+ return new Object[]{ inetAddr.getPort(), inetAddr.getHostName() };
181
+ } else {
182
+ return null;
183
+ }
184
+ }
185
+
186
+ public boolean isWatchOnly() { return false; }
187
+ public boolean isNotifyReadable() { return false; }
188
+ public boolean isNotifyWritable() { return false; }
189
+ }
@@ -0,0 +1,364 @@
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 java.net.Socket;
44
+ import javax.net.ssl.*;
45
+ import javax.net.ssl.SSLEngineResult.*;
46
+ import java.lang.reflect.Field;
47
+
48
+ import java.security.*;
49
+
50
+ public class EventableSocketChannel implements EventableChannel {
51
+ Selector selector;
52
+ SelectionKey channelKey;
53
+ SocketChannel channel;
54
+
55
+ long binding;
56
+ LinkedList<ByteBuffer> outboundQ;
57
+
58
+ boolean bCloseScheduled;
59
+ boolean bConnectPending;
60
+ boolean bWatchOnly;
61
+ boolean bAttached;
62
+ boolean bNotifyReadable;
63
+ boolean bNotifyWritable;
64
+
65
+ SSLEngine sslEngine;
66
+ SSLContext sslContext;
67
+
68
+ public EventableSocketChannel (SocketChannel sc, long _binding, Selector sel) {
69
+ channel = sc;
70
+ binding = _binding;
71
+ selector = sel;
72
+ bCloseScheduled = false;
73
+ bConnectPending = false;
74
+ bWatchOnly = false;
75
+ bAttached = false;
76
+ bNotifyReadable = false;
77
+ bNotifyWritable = false;
78
+ outboundQ = new LinkedList<ByteBuffer>();
79
+ }
80
+
81
+ public long getBinding() {
82
+ return binding;
83
+ }
84
+
85
+ public SocketChannel getChannel() {
86
+ return channel;
87
+ }
88
+
89
+ public void register() throws ClosedChannelException {
90
+ if (channelKey == null) {
91
+ int events = currentEvents();
92
+ channelKey = channel.register(selector, events, this);
93
+ }
94
+ }
95
+
96
+ /**
97
+ * Terminate with extreme prejudice. Don't assume there will be another pass through
98
+ * the reactor core.
99
+ */
100
+ public void close() {
101
+ if (channelKey != null) {
102
+ channelKey.cancel();
103
+ channelKey = null;
104
+ }
105
+
106
+ if (bAttached) {
107
+ // attached channels are copies, so reset the file descriptor to prevent java from close()ing it
108
+ Field f;
109
+ FileDescriptor fd;
110
+
111
+ try {
112
+ /* do _NOT_ clobber fdVal here, it will break epoll/kqueue on jdk6!
113
+ * channelKey.cancel() above does not occur until the next call to select
114
+ * and if fdVal is gone, we will continue to get events for this fd.
115
+ *
116
+ * instead, remove fdVal in cleanup(), which is processed via DetachedConnections,
117
+ * after UnboundConnections but before NewConnections.
118
+ */
119
+
120
+ f = channel.getClass().getDeclaredField("fd");
121
+ f.setAccessible(true);
122
+ fd = (FileDescriptor) f.get(channel);
123
+
124
+ f = fd.getClass().getDeclaredField("fd");
125
+ f.setAccessible(true);
126
+ f.set(fd, -1);
127
+ } catch (java.lang.NoSuchFieldException e) {
128
+ e.printStackTrace();
129
+ } catch (java.lang.IllegalAccessException e) {
130
+ e.printStackTrace();
131
+ }
132
+
133
+ return;
134
+ }
135
+
136
+ try {
137
+ channel.close();
138
+ } catch (IOException e) {
139
+ }
140
+ }
141
+
142
+ public void cleanup() {
143
+ if (bAttached) {
144
+ Field f;
145
+ try {
146
+ f = channel.getClass().getDeclaredField("fdVal");
147
+ f.setAccessible(true);
148
+ f.set(channel, -1);
149
+ } catch (java.lang.NoSuchFieldException e) {
150
+ e.printStackTrace();
151
+ } catch (java.lang.IllegalAccessException e) {
152
+ e.printStackTrace();
153
+ }
154
+ }
155
+
156
+ channel = null;
157
+ }
158
+
159
+ public void scheduleOutboundData (ByteBuffer bb) {
160
+ if (!bCloseScheduled && bb.remaining() > 0) {
161
+ if (sslEngine != null) {
162
+ try {
163
+ ByteBuffer b = ByteBuffer.allocate(32*1024); // TODO, preallocate this buffer.
164
+ sslEngine.wrap(bb, b);
165
+ b.flip();
166
+ outboundQ.addLast(b);
167
+ } catch (SSLException e) {
168
+ throw new RuntimeException ("ssl error");
169
+ }
170
+ }
171
+ else {
172
+ outboundQ.addLast(bb);
173
+ }
174
+
175
+ updateEvents();
176
+ }
177
+ }
178
+
179
+ public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
180
+ throw new RuntimeException ("datagram sends not supported on this channel");
181
+ }
182
+
183
+ /**
184
+ * Called by the reactor when we have selected readable.
185
+ */
186
+ public void readInboundData (ByteBuffer bb) throws IOException {
187
+ if (channel.read(bb) == -1)
188
+ throw new IOException ("eof");
189
+ }
190
+
191
+ /**
192
+ * Called by the reactor when we have selected writable.
193
+ * Return false to indicate an error that should cause the connection to close.
194
+ * TODO, VERY IMPORTANT: we're here because we selected writable, but it's always
195
+ * possible to become unwritable between the poll and when we get here. The way
196
+ * this code is written, we're depending on a nonblocking write NOT TO CONSUME
197
+ * the whole outbound buffer in this case, rather than firing an exception.
198
+ * We should somehow verify that this is indeed Java's defined behavior.
199
+ * Also TODO, see if we can use gather I/O rather than one write at a time.
200
+ * Ought to be a big performance enhancer.
201
+ * @return
202
+ */
203
+ public boolean writeOutboundData() throws IOException {
204
+ while (!outboundQ.isEmpty()) {
205
+ ByteBuffer b = outboundQ.getFirst();
206
+ if (b.remaining() > 0)
207
+ channel.write(b);
208
+
209
+ // Did we consume the whole outbound buffer? If yes,
210
+ // pop it off and keep looping. If no, the outbound network
211
+ // buffers are full, so break out of here.
212
+ if (b.remaining() == 0)
213
+ outboundQ.removeFirst();
214
+ else
215
+ break;
216
+ }
217
+
218
+ if (outboundQ.isEmpty() && !bCloseScheduled) {
219
+ updateEvents();
220
+ }
221
+
222
+ // ALWAYS drain the outbound queue before triggering a connection close.
223
+ // If anyone wants to close immediately, they're responsible for clearing
224
+ // the outbound queue.
225
+ return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
226
+ }
227
+
228
+ public void setConnectPending() {
229
+ bConnectPending = true;
230
+ updateEvents();
231
+ }
232
+
233
+ /**
234
+ * Called by the reactor when we have selected connectable.
235
+ * Return false to indicate an error that should cause the connection to close.
236
+ */
237
+ public boolean finishConnecting() throws IOException {
238
+ channel.finishConnect();
239
+
240
+ bConnectPending = false;
241
+ updateEvents();
242
+ return true;
243
+ }
244
+
245
+ public boolean scheduleClose (boolean afterWriting) {
246
+ // TODO: What the hell happens here if bConnectPending is set?
247
+ if (!afterWriting)
248
+ outboundQ.clear();
249
+
250
+ if (outboundQ.isEmpty())
251
+ return true;
252
+ else {
253
+ updateEvents();
254
+ bCloseScheduled = true;
255
+ return false;
256
+ }
257
+ }
258
+
259
+ public void startTls() {
260
+ if (sslEngine == null) {
261
+ try {
262
+ sslContext = SSLContext.getInstance("TLS");
263
+ sslContext.init(null, null, null); // TODO, fill in the parameters.
264
+ sslEngine = sslContext.createSSLEngine(); // TODO, should use the parameterized version, to get Kerb stuff and session re-use.
265
+ sslEngine.setUseClientMode(false);
266
+ } catch (NoSuchAlgorithmException e) {
267
+ throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
268
+ } catch (KeyManagementException e) {
269
+ throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
270
+ }
271
+ }
272
+ System.out.println ("Starting TLS");
273
+ }
274
+
275
+ public ByteBuffer dispatchInboundData (ByteBuffer bb) throws SSLException {
276
+ if (sslEngine != null) {
277
+ if (true) throw new RuntimeException ("TLS currently unimplemented");
278
+ System.setProperty("javax.net.debug", "all");
279
+ ByteBuffer w = ByteBuffer.allocate(32*1024); // TODO, WRONG, preallocate this buffer.
280
+ SSLEngineResult res = sslEngine.unwrap(bb, w);
281
+ if (res.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
282
+ Runnable r;
283
+ while ((r = sslEngine.getDelegatedTask()) != null) {
284
+ r.run();
285
+ }
286
+ }
287
+ System.out.println (bb);
288
+ w.flip();
289
+ return w;
290
+ }
291
+ else
292
+ return bb;
293
+ }
294
+
295
+ public void setCommInactivityTimeout (long seconds) {
296
+ // TODO
297
+ System.out.println ("SOCKET: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
298
+ }
299
+
300
+ public Object[] getPeerName () {
301
+ Socket sock = channel.socket();
302
+ return new Object[]{ sock.getPort(), sock.getInetAddress().getHostAddress() };
303
+ }
304
+
305
+ public void setWatchOnly() {
306
+ bWatchOnly = true;
307
+ updateEvents();
308
+ }
309
+ public boolean isWatchOnly() { return bWatchOnly; }
310
+
311
+ public void setAttached() {
312
+ bAttached = true;
313
+ }
314
+ public boolean isAttached() { return bAttached; }
315
+
316
+ public void setNotifyReadable (boolean mode) {
317
+ bNotifyReadable = mode;
318
+ updateEvents();
319
+ }
320
+ public boolean isNotifyReadable() { return bNotifyReadable; }
321
+
322
+ public void setNotifyWritable (boolean mode) {
323
+ bNotifyWritable = mode;
324
+ updateEvents();
325
+ }
326
+ public boolean isNotifyWritable() { return bNotifyWritable; }
327
+
328
+ private void updateEvents() {
329
+ if (channelKey == null)
330
+ return;
331
+
332
+ int events = currentEvents();
333
+
334
+ if (channelKey.interestOps() != events) {
335
+ channelKey.interestOps(events);
336
+ }
337
+ }
338
+
339
+ private int currentEvents() {
340
+ int events = 0;
341
+
342
+ if (bWatchOnly)
343
+ {
344
+ if (bNotifyReadable)
345
+ events |= SelectionKey.OP_READ;
346
+
347
+ if (bNotifyWritable)
348
+ events |= SelectionKey.OP_WRITE;
349
+ }
350
+ else
351
+ {
352
+ if (bConnectPending)
353
+ events |= SelectionKey.OP_CONNECT;
354
+ else {
355
+ events |= SelectionKey.OP_READ;
356
+
357
+ if (!outboundQ.isEmpty())
358
+ events |= SelectionKey.OP_WRITE;
359
+ }
360
+ }
361
+
362
+ return events;
363
+ }
364
+ }