eventmachine-with-ipv6 1.0.0.beta.4.ipv6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/FORK.md +47 -0
  4. data/GNU +281 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +60 -0
  7. data/README.md +109 -0
  8. data/Rakefile +20 -0
  9. data/docs/DocumentationGuidesIndex.md +27 -0
  10. data/docs/GettingStarted.md +521 -0
  11. data/docs/old/ChangeLog +211 -0
  12. data/docs/old/DEFERRABLES +246 -0
  13. data/docs/old/EPOLL +141 -0
  14. data/docs/old/INSTALL +13 -0
  15. data/docs/old/KEYBOARD +42 -0
  16. data/docs/old/LEGAL +25 -0
  17. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  18. data/docs/old/PURE_RUBY +75 -0
  19. data/docs/old/RELEASE_NOTES +94 -0
  20. data/docs/old/SMTP +4 -0
  21. data/docs/old/SPAWNED_PROCESSES +148 -0
  22. data/docs/old/TODO +8 -0
  23. data/eventmachine.gemspec +50 -0
  24. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  25. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  26. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  27. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  28. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  29. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  30. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  31. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  32. data/examples/old/ex_channel.rb +43 -0
  33. data/examples/old/ex_queue.rb +2 -0
  34. data/examples/old/ex_tick_loop_array.rb +15 -0
  35. data/examples/old/ex_tick_loop_counter.rb +32 -0
  36. data/examples/old/helper.rb +2 -0
  37. data/ext/binder.cpp +124 -0
  38. data/ext/binder.h +46 -0
  39. data/ext/cmain.cpp +858 -0
  40. data/ext/ed.cpp +1992 -0
  41. data/ext/ed.h +423 -0
  42. data/ext/em.cpp +2358 -0
  43. data/ext/em.h +245 -0
  44. data/ext/eventmachine.h +127 -0
  45. data/ext/extconf.rb +166 -0
  46. data/ext/fastfilereader/extconf.rb +94 -0
  47. data/ext/fastfilereader/mapper.cpp +214 -0
  48. data/ext/fastfilereader/mapper.h +59 -0
  49. data/ext/fastfilereader/rubymain.cpp +127 -0
  50. data/ext/kb.cpp +79 -0
  51. data/ext/page.cpp +107 -0
  52. data/ext/page.h +51 -0
  53. data/ext/pipe.cpp +347 -0
  54. data/ext/project.h +155 -0
  55. data/ext/rubymain.cpp +1280 -0
  56. data/ext/ssl.cpp +468 -0
  57. data/ext/ssl.h +94 -0
  58. data/java/.classpath +8 -0
  59. data/java/.project +17 -0
  60. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  61. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  62. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  63. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  64. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  65. data/lib/em/buftok.rb +110 -0
  66. data/lib/em/callback.rb +58 -0
  67. data/lib/em/channel.rb +64 -0
  68. data/lib/em/completion.rb +304 -0
  69. data/lib/em/connection.rb +728 -0
  70. data/lib/em/deferrable.rb +210 -0
  71. data/lib/em/deferrable/pool.rb +2 -0
  72. data/lib/em/file_watch.rb +73 -0
  73. data/lib/em/future.rb +61 -0
  74. data/lib/em/iterator.rb +270 -0
  75. data/lib/em/messages.rb +66 -0
  76. data/lib/em/pool.rb +151 -0
  77. data/lib/em/process_watch.rb +45 -0
  78. data/lib/em/processes.rb +123 -0
  79. data/lib/em/protocols.rb +36 -0
  80. data/lib/em/protocols/header_and_content.rb +138 -0
  81. data/lib/em/protocols/httpclient.rb +279 -0
  82. data/lib/em/protocols/httpclient2.rb +600 -0
  83. data/lib/em/protocols/line_and_text.rb +125 -0
  84. data/lib/em/protocols/line_protocol.rb +29 -0
  85. data/lib/em/protocols/linetext2.rb +161 -0
  86. data/lib/em/protocols/memcache.rb +331 -0
  87. data/lib/em/protocols/object_protocol.rb +46 -0
  88. data/lib/em/protocols/postgres3.rb +246 -0
  89. data/lib/em/protocols/saslauth.rb +175 -0
  90. data/lib/em/protocols/smtpclient.rb +365 -0
  91. data/lib/em/protocols/smtpserver.rb +640 -0
  92. data/lib/em/protocols/socks4.rb +66 -0
  93. data/lib/em/protocols/stomp.rb +202 -0
  94. data/lib/em/protocols/tcptest.rb +54 -0
  95. data/lib/em/pure_ruby.rb +1017 -0
  96. data/lib/em/queue.rb +71 -0
  97. data/lib/em/resolver.rb +195 -0
  98. data/lib/em/spawnable.rb +84 -0
  99. data/lib/em/streamer.rb +118 -0
  100. data/lib/em/threaded_resource.rb +90 -0
  101. data/lib/em/tick_loop.rb +85 -0
  102. data/lib/em/timers.rb +61 -0
  103. data/lib/em/version.rb +3 -0
  104. data/lib/eventmachine.rb +1517 -0
  105. data/lib/jeventmachine.rb +279 -0
  106. data/rakelib/cpp.rake_example +77 -0
  107. data/rakelib/package.rake +98 -0
  108. data/rakelib/test.rake +8 -0
  109. data/tests/client.crt +31 -0
  110. data/tests/client.key +51 -0
  111. data/tests/em_test_helper.rb +64 -0
  112. data/tests/test_attach.rb +126 -0
  113. data/tests/test_basic.rb +294 -0
  114. data/tests/test_channel.rb +62 -0
  115. data/tests/test_completion.rb +177 -0
  116. data/tests/test_connection_count.rb +33 -0
  117. data/tests/test_defer.rb +18 -0
  118. data/tests/test_deferrable.rb +35 -0
  119. data/tests/test_epoll.rb +134 -0
  120. data/tests/test_error_handler.rb +38 -0
  121. data/tests/test_exc.rb +28 -0
  122. data/tests/test_file_watch.rb +65 -0
  123. data/tests/test_futures.rb +170 -0
  124. data/tests/test_get_sock_opt.rb +37 -0
  125. data/tests/test_handler_check.rb +35 -0
  126. data/tests/test_hc.rb +155 -0
  127. data/tests/test_httpclient.rb +190 -0
  128. data/tests/test_httpclient2.rb +128 -0
  129. data/tests/test_inactivity_timeout.rb +54 -0
  130. data/tests/test_ipv4.rb +128 -0
  131. data/tests/test_ipv6.rb +135 -0
  132. data/tests/test_kb.rb +34 -0
  133. data/tests/test_ltp.rb +138 -0
  134. data/tests/test_ltp2.rb +288 -0
  135. data/tests/test_next_tick.rb +104 -0
  136. data/tests/test_object_protocol.rb +36 -0
  137. data/tests/test_pause.rb +78 -0
  138. data/tests/test_pending_connect_timeout.rb +52 -0
  139. data/tests/test_pool.rb +194 -0
  140. data/tests/test_process_watch.rb +48 -0
  141. data/tests/test_processes.rb +133 -0
  142. data/tests/test_proxy_connection.rb +168 -0
  143. data/tests/test_pure.rb +88 -0
  144. data/tests/test_queue.rb +50 -0
  145. data/tests/test_resolver.rb +55 -0
  146. data/tests/test_running.rb +14 -0
  147. data/tests/test_sasl.rb +47 -0
  148. data/tests/test_send_file.rb +217 -0
  149. data/tests/test_servers.rb +33 -0
  150. data/tests/test_set_sock_opt.rb +41 -0
  151. data/tests/test_shutdown_hooks.rb +23 -0
  152. data/tests/test_smtpclient.rb +55 -0
  153. data/tests/test_smtpserver.rb +57 -0
  154. data/tests/test_spawn.rb +293 -0
  155. data/tests/test_ssl_args.rb +78 -0
  156. data/tests/test_ssl_methods.rb +48 -0
  157. data/tests/test_ssl_verify.rb +82 -0
  158. data/tests/test_threaded_resource.rb +53 -0
  159. data/tests/test_tick_loop.rb +59 -0
  160. data/tests/test_timers.rb +123 -0
  161. data/tests/test_ud.rb +8 -0
  162. data/tests/test_udp46.rb +54 -0
  163. data/tests/test_unbind_reason.rb +48 -0
  164. metadata +319 -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
+ }