java.lang.Object
javax.net.ssl.SSLEngine
- See Also:
- Source Code,
AccessControlContext , setUseClientMode(boolean) , getEnabledCipherSuites() , setEnabledCipherSuites(String []) , getSupportedCipherSuites() , closeInbound() , isInboundDone() , closeOutbound() , isOutboundDone() , run() , getDelegatedTask() , SSLEngineResult , SSLSession.getApplicationBufferSize() , SSLSession.getPacketBufferSize() , unwrap() , wrap() , SSLContext.createSSLEngine() , ByteBuffers , selectable non-blocking I/O , non-blocking I/O (polling) , Socket , getSession()
public abstract void beginHandshake()
throws SSLException - See Also:
SSLSession.invalidate() , IllegalStateException, SSLSocket#startHandshake()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1495]Selector used for I/O multiplexing By kapila on 2005/07/25 05:46:08 Rate
package com.ceylonit.cse.nio.io; import java.io.IOException; import java.nio.channels.CancelledKeyException; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; final public class SelectorThread implements Runnable { private Selector selector; private final Thread selectorThread; private boolean closeRequested = false; private final List pendingInvocations = new ArrayList ( 32 ) ; public SelectorThread ( ) throws IOException { // Selector for incoming time requests selector = Selector.open ( ) ; selectorThread = new Thread ( this, "selector" ) ; selectorThread.start ( ) ; } public void requestClose ( ) { closeRequested = true; // Nudges the selector. selector.wakeup ( ) ; } public void addChannelInterestNow ( SelectableChannel channel, int interest ) throws IOException { if ( Thread.currentThread ( ) != selectorThread ) { System.out.println ( "Thread.currentThread ( ) " + Thread.currentThread ( ) ) ; System.out.println ( "selectorThread ) " + selectorThread ) ; throw new IOException ( "Method can only be called from selector thread-1" ) ; } SelectionKey sk = channel.keyFor ( selector ) ; changeKeyInterest ( sk, sk.interestOps ( ) | interest ) ; } public void addChannelInterestLater ( final SelectableChannel channel, final int interest, final CallbackErrorHandler errorHandler ) { // Add a new runnable to the list of tasks to be executed in the selector thread invokeLater ( new Runnable ( ) { public void run ( ) { try { addChannelInterestNow ( channel, interest ) ; } catch ( IOException e ) { errorHandler.handleError ( e ) ; } } } ) ; } public void removeChannelInterestNow ( SelectableChannel channel, int interest ) throws IOException { if ( Thread.currentThread ( ) != selectorThread ) { throw new IOException ( "Method can only be called from selector thread" ) ; } SelectionKey sk = channel.keyFor ( selector ) ; changeKeyInterest ( sk, sk.interestOps ( ) & ~interest ) ; } public void removeChannelInterestLater ( final SelectableChannel channel, final int interest, final CallbackErrorHandler errorHandler ) { invokeLater ( new Runnable ( ) { public void run ( ) { try { removeChannelInterestNow ( channel, interest ) ; } catch ( IOException e ) { errorHandler.handleError ( e ) ; } } } ) ; } private void changeKeyInterest ( SelectionKey sk, int newInterest ) throws IOException { try { sk.interestOps ( newInterest ) ; } catch ( CancelledKeyException cke ) { IOException ioe = new IOException ( "Failed to change channel interest." ) ; ioe.initCause ( cke ) ; throw ioe; } } public void registerChannelLater ( final SelectableChannel channel, final int selectionKeys, final SelectorHandler handlerInfo, final CallbackErrorHandler errorHandler ) { invokeLater ( new Runnable ( ) { public void run ( ) { try { registerChannelNow ( channel, selectionKeys, handlerInfo ) ; } catch ( IOException e ) { errorHandler.handleError ( e ) ; } } } ) ; } public void registerChannelNow ( SelectableChannel channel, int selectionKeys, SelectorHandler handlerInfo ) throws IOException { if ( Thread.currentThread ( ) != selectorThread ) { throw new IOException ( "Method can only be called from selector thread" ) ; } if ( !channel.isOpen ( ) ) { throw new IOException ( "Channel is not open." ) ; } try { if ( channel.isRegistered ( ) ) { SelectionKey sk = channel.keyFor ( selector ) ; assert sk != null : "Channel is already registered with other selector"; sk.interestOps ( selectionKeys ) ; Object previousAttach = sk.attach ( handlerInfo ) ; assert previousAttach != null; } else { channel.configureBlocking ( false ) ; channel.register ( selector, selectionKeys, handlerInfo ) ; } } catch ( Exception e ) { IOException ioe = new IOException ( "Error registering channel." ) ; ioe.initCause ( e ) ; throw ioe; } } public void invokeLater ( Runnable run ) { synchronized ( pendingInvocations ) { pendingInvocations.add ( run ) ; } selector.wakeup ( ) ; } public void invokeAndWait ( final Runnable task ) throws InterruptedException { if ( Thread.currentThread ( ) == selectorThread ) { // We are in the selector's thread. No need to schedule // execution task.run ( ) ; } else { // Used to deliver the notification that the task is executed final Object latch = new Object ( ) ; synchronized ( latch ) { // Uses the invokeLater method with a newly created task this.invokeLater ( new Runnable ( ) { public void run ( ) { task.run ( ) ; // Notifies latch.notify ( ) ; } } ) ; // Wait for the task to complete. latch.wait ( ) ; } // Ok, we are done, the task was executed. Proceed. } } private void doInvocations ( ) { synchronized ( pendingInvocations ) { //System.out.println ( "pendingInvocations"+pendingInvocations.size ( ) ) ; for ( int i = 0; i < pendingInvocations.size ( ) ; i++ ) { Runnable task = ( Runnable ) pendingInvocations.get ( i ) ; task.run ( ) ; } pendingInvocations.clear ( ) ; } } public void run ( ) { // Here's where everything happens. The select method will // return when any operations registered above have occurred, the // thread has been interrupted, etc. while ( true ) { // Execute all the pending tasks. doInvocations ( ) ; // Time to terminate? if ( closeRequested ) { return; } int selectedKeys = 0; try { selectedKeys = selector.select ( ) ; } catch ( IOException ioe ) { // Select should never throw an exception under normal // operation. If this happens, print the error and try to // continue working. ioe.printStackTrace ( ) ; continue; } if ( selectedKeys == 0 ) { // Go back to the beginning of the loop continue; } // Someone is ready for IO, get the ready keys Iterator it = selector.selectedKeys ( ) .iterator ( ) ; // Walk through the collection of ready keys and dispatch // any active event. while ( it.hasNext ( ) ) { SelectionKey sk = ( SelectionKey ) it.next ( ) ; it.remove ( ) ; try { // Obtain the interest of the key int readyOps = sk.readyOps ( ) ; // Disable the interest for the operation that is ready. // This prevents the same event from being raised multiple // times. sk.interestOps ( sk.interestOps ( ) & ~readyOps ) ; SelectorHandler handler = ( SelectorHandler ) sk.attachment ( ) ; // Some of the operations set in the selection key // might no longer be valid when the handler is executed. // So handlers should take precautions against this // possibility. // Check what are the interests that are active and // dispatch the event to the appropriate method. if ( sk.isAcceptable ( ) ) { // A connection is ready to be completed ( ( AcceptSelectorHandler ) handler ) .handleAccept ( ) ; } else if ( sk.isConnectable ( ) ) { // A connection is ready to be accepted ( ( ConnectorSelectorHandler ) handler ) .handleConnect ( ) ; } else { ReadWriteSelectorHandler rwHandler = ( ReadWriteSelectorHandler ) handler; // Readable or writable if ( sk.isReadable ( ) ) { // It is possible to read rwHandler.handleRead ( ) ; } // Check if the key is still valid, since it might // have been invalidated in the read handler // ( for instance, the socket might have been closed ) if ( sk.isValid ( ) && sk.isWritable ( ) ) { // It is read to write rwHandler.handleWrite ( ) ; } } } catch ( Throwable t ) { // No exceptions should be thrown in the previous block! // So kill everything if one is detected. // Makes debugging easier. closeSelectorAndChannels ( ) ; t.printStackTrace ( ) ; return; } } } } private void closeSelectorAndChannels ( ) { Set keys = selector.keys ( ) ; for ( Iterator iter = keys.iterator ( ) ; iter.hasNext ( ) ; ) { SelectionKey key = ( SelectionKey ) iter.next ( ) ; try { key.channel ( ) .close ( ) ; } catch ( IOException e ) { // Ignore } } try { selector.close ( ) ; } catch ( IOException e ) { // Ignore } } }
public abstract void closeInbound()
throws SSLException - See Also:
isOutboundDone() , isInboundDone() , wrap() , closeOutbound()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void closeOutbound() - See Also:
isOutboundDone() , wrap(ByteBuffer, ByteBuffer)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract Runnable getDelegatedTask() - See Also:
run
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract String[] getEnabledCipherSuites() - See Also:
setEnabledCipherSuites(String []) , getSupportedCipherSuites()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract String[] getEnabledProtocols() - See Also:
setEnabledProtocols(String [])
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean getEnableSessionCreation() - See Also:
setEnableSessionCreation(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract SSLEngineResult.HandshakeStatus getHandshakeStatus() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean getNeedClientAuth() - See Also:
setUseClientMode(boolean) , getWantClientAuth() , setWantClientAuth(boolean) , setNeedClientAuth(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public String getPeerHost() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public int getPeerPort() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract SSLSession getSession() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract String[] getSupportedCipherSuites() - See Also:
setEnabledCipherSuites(String []) , getEnabledCipherSuites()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract String[] getSupportedProtocols() - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean getUseClientMode() - See Also:
setUseClientMode(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean getWantClientAuth() - See Also:
setUseClientMode(boolean) , setWantClientAuth(boolean) , getNeedClientAuth() , setNeedClientAuth(boolean)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean isInboundDone() - See Also:
closeInbound() , unwrap(ByteBuffer, ByteBuffer)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract boolean isOutboundDone() - See Also:
closeInbound() , closeOutbound() , wrap(ByteBuffer, ByteBuffer)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setEnabledCipherSuites(String[] suites) - See Also:
getEnabledCipherSuites() , getSupportedCipherSuites() , IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setEnabledProtocols(String[] protocols) - See Also:
getEnabledProtocols() , IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setEnableSessionCreation(boolean flag) - See Also:
getEnableSessionCreation()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setNeedClientAuth(boolean need) - See Also:
setUseClientMode(boolean) , getWantClientAuth() , setWantClientAuth(boolean) , getNeedClientAuth()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setUseClientMode(boolean mode) - See Also:
getUseClientMode() , IllegalArgumentException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract void setWantClientAuth(boolean want) - See Also:
setUseClientMode(boolean) , getNeedClientAuth() , setNeedClientAuth(boolean) , getWantClientAuth()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected SSLEngine() - See Also:
SSLSessionContext
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
protected SSLEngine(String peerHost,
int peerPort) - See Also:
SSLSessionContext
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public SSLEngineResult unwrap(ByteBuffer src,
ByteBuffer dst)
throws SSLException - See Also:
unwrap(ByteBuffer, ByteBuffer [], int, int) , IllegalStateException, IllegalArgumentException, ReadOnlyBufferException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public SSLEngineResult unwrap(ByteBuffer src,
ByteBuffer[] dsts)
throws SSLException - See Also:
unwrap(ByteBuffer, ByteBuffer [], int, int) , IllegalStateException, IllegalArgumentException, ReadOnlyBufferException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract SSLEngineResult unwrap(ByteBuffer src,
ByteBuffer[] dsts,
int offset,
int length)
throws SSLException - See Also:
ScatteringByteChannel.read(
ByteBuffer[], int, int) , IllegalStateException, IllegalArgumentException, ReadOnlyBufferException, IndexOutOfBoundsException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public SSLEngineResult wrap(ByteBuffer src,
ByteBuffer dst)
throws SSLException - See Also:
wrap(ByteBuffer [], int, int, ByteBuffer) , IllegalStateException, IllegalArgumentException, ReadOnlyBufferException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public abstract SSLEngineResult wrap(ByteBuffer[] srcs,
int offset,
int length,
ByteBuffer dst)
throws SSLException - See Also:
GatheringByteChannel.write(
ByteBuffer[], int, int) , IllegalStateException, IllegalArgumentException, ReadOnlyBufferException, IndexOutOfBoundsException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public SSLEngineResult wrap(ByteBuffer[] srcs,
ByteBuffer dst)
throws SSLException - See Also:
wrap(ByteBuffer [], int, int, ByteBuffer) , IllegalStateException, IllegalArgumentException, ReadOnlyBufferException
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
| Popular Tags |