Changeset 425
- Timestamp:
- 07/17/07 06:07:01 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
experiments/jruby-1/src/com/rubyeventmachine/EM.java
r424 r425 35 35 import java.net.*; 36 36 import java.util.concurrent.atomic.*; 37 import java x.net.ssl.*;37 import java.security.*; 38 38 39 39 public class EM { … … 98 98 runTimers(); 99 99 if (!bRunReactor) break; 100 mySelector.select( 1000); // TODO, timer quantum needs to be configurable.100 mySelector.select(200); // TODO, timer quantum needs to be configurable. 101 101 102 102 Iterator<SelectionKey> it = mySelector.selectedKeys().iterator(); … … 120 120 if (k.isReadable()) { 121 121 SocketChannel sn = (SocketChannel) k.channel(); 122 ByteBuffer bb = ByteBuffer.allocate(16 * 1024); 122 ByteBuffer bb = ByteBuffer.allocate(16 * 1024); // TODO. Preallocate and reuse the buffer. 123 123 int r = sn.read(bb); 124 124 if (r > 0) { 125 125 bb.flip(); 126 bb = ((EventableChannel)k.attachment()).dispatchInboundData (bb); 126 127 eventCallback (((EventableChannel)k.attachment()).getBinding(), EM_CONNECTION_READ, bb); 127 128 } 128 129 else { 130 // TODO. Figure out if a socket that selects readable can ever return 0 bytes 131 // without it being indicative of an error condition. If Java is like C, the answer is no. 129 132 String b = ((EventableChannel)k.attachment()).getBinding(); 130 133 eventCallback (b, EM_CONNECTION_UNBOUND, EmptyByteBuffer); … … 186 189 while (!Timers.isEmpty()) { 187 190 long k = Timers.firstKey(); 191 //System.out.println (k - now); 188 192 if (k > now) 189 193 break; … … 196 200 BindingIndex++; 197 201 String s = createBinding(); 202 //System.out.println (new Date().getTime()+ milliseconds); 198 203 Timers.put(new Date().getTime() + milliseconds, s); 199 204 return s; … … 260 265 mySelector.wakeup(); 261 266 } 267 268 public void startTls (String sig) throws NoSuchAlgorithmException, KeyManagementException { 269 Connections.get(sig).startTls(); 270 } 262 271 } experiments/jruby-1/src/com/rubyeventmachine/EMTest.java
r421 r425 15 15 16 16 class ShortTimer extends EM { 17 /*18 public void xeventCallback (String a1, int a2, String a3, int a4) {19 System.out.println ("Short Callback "+a1+" "+a2+" "+a3+" "+a4);20 this.stop();21 }*/22 17 public void eventCallback (String sig, int eventCode, ByteBuffer data) { 23 18 System.out.println ("Short Callback "+sig+" "+eventCode+" "+data); … … 49 44 em.run(); 50 45 } 46 47 51 48 } experiments/jruby-1/src/com/rubyeventmachine/EventableChannel.java
r416 r425 13 13 import java.util.*; 14 14 import java.io.*; 15 import javax.net.ssl.*; 16 import javax.net.ssl.SSLEngineResult.*; 17 18 import java.security.*; 15 19 16 20 public class EventableChannel { … … 23 27 boolean bCloseScheduled; 24 28 29 SSLEngine sslEngine; 30 31 32 SSLContext sslContext; 33 34 25 35 public EventableChannel (SocketChannel sc, String binding, Selector sel) throws ClosedChannelException { 26 36 myChannel = sc; … … 36 46 return myBinding; 37 47 } 38 public void scheduleOutboundData (ByteBuffer bb) throws ClosedChannelException {48 public void scheduleOutboundData (ByteBuffer bb) throws ClosedChannelException, SSLException { 39 49 if ((!bCloseScheduled) && (bb.remaining() > 0)) { 40 OutboundQ.addLast(bb); 50 if (sslEngine != null) { 51 ByteBuffer b = ByteBuffer.allocate(32*1024); // TODO, preallocate this buffer. 52 sslEngine.wrap(bb, b); 53 b.flip(); 54 OutboundQ.addLast(b); 55 } 56 else { 57 OutboundQ.addLast(bb); 58 } 41 59 myChannel.register(mySelector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this); 42 60 } … … 99 117 bCloseScheduled = true; 100 118 } 119 public void startTls() throws NoSuchAlgorithmException, KeyManagementException { 120 if (sslEngine == null) { 121 sslContext = SSLContext.getInstance("TLS"); 122 sslContext.init(null, null, null); // TODO, fill in the parameters. 123 sslEngine = sslContext.createSSLEngine(); // TODO, should use the parameterized version, to get Kerb stuff and session re-use. 124 sslEngine.setUseClientMode(false); 125 } 126 System.out.println ("Starting TLS"); 127 } 128 129 public ByteBuffer dispatchInboundData (ByteBuffer bb) throws SSLException { 130 if (sslEngine != null) { 131 if (true) throw new RuntimeException ("TLS currently unimplemented"); 132 System.setProperty("javax.net.debug", "all"); 133 ByteBuffer w = ByteBuffer.allocate(32*1024); // TODO, WRONG, preallocate this buffer. 134 SSLEngineResult res = sslEngine.unwrap(bb, w); 135 if (res.getHandshakeStatus() == HandshakeStatus.NEED_TASK) { 136 Runnable r; 137 while ((r = sslEngine.getDelegatedTask()) != null) { 138 r.run(); 139 } 140 } 141 System.out.println (bb); 142 w.flip(); 143 return w; 144 } 145 else 146 return bb; 147 } 101 148 }