| 45 | | public String getBinding() { |
|---|
| 46 | | return myBinding; |
|---|
| 47 | | } |
|---|
| 48 | | public void scheduleOutboundData (ByteBuffer bb) throws ClosedChannelException, SSLException { |
|---|
| 49 | | if ((!bCloseScheduled) && (bb.remaining() > 0)) { |
|---|
| 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 | | } |
|---|
| 59 | | myChannel.register(mySelector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this); |
|---|
| 60 | | } |
|---|
| 61 | | } |
|---|
| | 15 | public String getBinding(); |
|---|
| | 16 | |
|---|
| | 17 | public void readInboundData (ByteBuffer dst); |
|---|
| 69 | | public boolean writeOutboundData() throws ClosedChannelException { |
|---|
| 70 | | while (!OutboundQ.isEmpty()) { |
|---|
| 71 | | ByteBuffer b = OutboundQ.getFirst(); |
|---|
| 72 | | try { |
|---|
| 73 | | myChannel.write(b); |
|---|
| 74 | | } |
|---|
| 75 | | catch (IOException e) { |
|---|
| 76 | | return false; |
|---|
| 77 | | } |
|---|
| 78 | | if (b.remaining() == 0) |
|---|
| 79 | | OutboundQ.removeFirst(); |
|---|
| 80 | | else |
|---|
| 81 | | break; |
|---|
| 82 | | } |
|---|
| 83 | | |
|---|
| 84 | | if (OutboundQ.isEmpty()) |
|---|
| 85 | | myChannel.register(mySelector, SelectionKey.OP_READ, this); |
|---|
| 86 | | |
|---|
| 87 | | // ALWAYS drain the outbound queue before triggering a connection close. |
|---|
| 88 | | // If anyone wants to close immediately, they're responsible for clearing |
|---|
| 89 | | // the outbound queue. |
|---|
| 90 | | return (bCloseScheduled && OutboundQ.isEmpty()) ? false : true; |
|---|
| 91 | | } |
|---|
| | 23 | public void close(); |
|---|
| 93 | | public void setConnectPending() throws ClosedChannelException { |
|---|
| 94 | | myChannel.register(mySelector, SelectionKey.OP_CONNECT, this); |
|---|
| 95 | | } |
|---|
| 96 | | |
|---|
| 97 | | /** |
|---|
| 98 | | * Called by the reactor when we have selected connectable. |
|---|
| 99 | | * Return false to indicate an error that should cause the connection to close. |
|---|
| 100 | | * @throws ClosedChannelException |
|---|
| 101 | | */ |
|---|
| 102 | | public boolean finishConnecting() throws ClosedChannelException { |
|---|
| 103 | | try { |
|---|
| 104 | | myChannel.finishConnect(); |
|---|
| 105 | | } |
|---|
| 106 | | catch (IOException e) { |
|---|
| 107 | | return false; |
|---|
| 108 | | } |
|---|
| 109 | | myChannel.register(mySelector, SelectionKey.OP_READ, this); |
|---|
| 110 | | return true; |
|---|
| 111 | | } |
|---|
| 112 | | |
|---|
| 113 | | public void scheduleClose (boolean afterWriting) throws ClosedChannelException { |
|---|
| 114 | | if (!afterWriting) |
|---|
| 115 | | OutboundQ.clear(); |
|---|
| 116 | | myChannel.register(mySelector, SelectionKey.OP_READ|SelectionKey.OP_WRITE, this); |
|---|
| 117 | | bCloseScheduled = true; |
|---|
| 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 | | } |
|---|
| | 25 | public boolean writeOutboundData(); |
|---|