Class NioProcessor
java.lang.Object
org.apache.mina.core.polling.AbstractPollingIoProcessor<NioSession>
org.apache.mina.transport.socket.nio.NioProcessor
- All Implemented Interfaces:
IoProcessor<NioSession>
A processor for incoming and outgoing data get and written on a TCP socket.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classAn encapsulating iterator around theSelector.selectedKeys()or theSelector.keys()iterator; -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected SelectorThe selector associated with this processorprotected ReadWriteLockA lock used to protect concurent access to the selectorprotected SelectorProviderFields inherited from class AbstractPollingIoProcessor
wakeupCalled -
Constructor Summary
ConstructorsConstructorDescriptionNioProcessor(Executor executor) Creates a new instance of NioProcessor.NioProcessor(Executor executor, SelectorProvider selectorProvider) Creates a new instance of NioProcessor. -
Method Summary
Modifier and TypeMethodDescriptionprotected Iterator<NioSession> protected intGet the number ofIoSessionpolled by thisIoProcessorprotected voiddestroy(NioSession session) Destroy the underlying client socket handleprotected voidDispose the resources used by thisIoProcessorfor polling the client connections.protected SessionStategetState(NioSession session) Get the state of a session (One of OPENING, OPEN, CLOSING)protected voidinit(NioSession session) Initialize the polling of a session.protected booleanCheck that the select() has not exited immediately just because of a broken connection.protected booleanisInterestedInRead(NioSession session) Tells if this session is registered for readingprotected booleanisInterestedInWrite(NioSession session) Tells if this session is registered for writingprotected booleanisReadable(NioSession session) Tells if the session ready for readingprotected booleanSay if the list ofIoSessionpolled by thisIoProcessoris emptyprotected booleanisWritable(NioSession session) Tells if the session ready for writingprotected intread(NioSession session, IoBuffer buf) protected voidIn the case we are using the java select() method, this method is used to trash the buggy selector and create a new one, registering all the sockets on it.protected intselect()poll those sessions foreverprotected intselect(long timeout) poll those sessions for the given timeoutprotected Iterator<NioSession> Get anIteratorfor the list ofIoSessionfound selected by the last call ofAbstractPollingIoProcessor.select(long)protected voidsetInterestedInRead(NioSession session, boolean isInterested) Set the session to be informed when a read event should be processedprotected voidsetInterestedInWrite(NioSession session, boolean isInterested) Set the session to be informed when a write event should be processedprotected inttransferFile(NioSession session, FileRegion region, int length) Write a part of a file to aIoSession, if the underlying API isn't supporting system calls like sendfile(), you can throw aUnsupportedOperationExceptionso the file will be send using usualAbstractPollingIoProcessor.write(AbstractIoSession, IoBuffer, int)call.protected voidwakeup()Interrupt theAbstractPollingIoProcessor.select(long)call.protected intwrite(NioSession session, IoBuffer buf, int length) Write a sequence of bytes to aIoSession, means to be called when a session was found ready for writing.Methods inherited from class AbstractPollingIoProcessor
add, dispose, flush, isDisposed, isDisposing, remove, updateTrafficControl, updateTrafficMask, write
-
Field Details
-
selector
The selector associated with this processor -
selectorLock
A lock used to protect concurent access to the selector -
selectorProvider
-
-
Constructor Details
-
NioProcessor
Creates a new instance of NioProcessor.- Parameters:
executor- The executor to use
-
NioProcessor
Creates a new instance of NioProcessor.- Parameters:
executor- The executor to useselectorProvider- The Selector provider to use
-
-
Method Details
-
doDispose
Description copied from class:AbstractPollingIoProcessorDispose the resources used by thisIoProcessorfor polling the client connections. The implementing class doDispose method will be called.- Specified by:
doDisposein classAbstractPollingIoProcessor<NioSession>- Throws:
Exception- if some low level IO error occurs
-
select
Description copied from class:AbstractPollingIoProcessorpoll those sessions for the given timeout- Specified by:
selectin classAbstractPollingIoProcessor<NioSession>- Parameters:
timeout- milliseconds before the call timeout if no event appear- Returns:
- The number of session ready for read or for write
- Throws:
Exception- if some low level IO error occurs
-
select
Description copied from class:AbstractPollingIoProcessorpoll those sessions forever- Specified by:
selectin classAbstractPollingIoProcessor<NioSession>- Returns:
- The number of session ready for read or for write
- Throws:
Exception- if some low level IO error occurs
-
isSelectorEmpty
protected boolean isSelectorEmpty()Description copied from class:AbstractPollingIoProcessorSay if the list ofIoSessionpolled by thisIoProcessoris empty- Specified by:
isSelectorEmptyin classAbstractPollingIoProcessor<NioSession>- Returns:
trueif at least a session is managed by thisIoProcessor
-
wakeup
protected void wakeup()Description copied from class:AbstractPollingIoProcessorInterrupt theAbstractPollingIoProcessor.select(long)call.- Specified by:
wakeupin classAbstractPollingIoProcessor<NioSession>
-
allSessions
Description copied from class:AbstractPollingIoProcessor- Specified by:
allSessionsin classAbstractPollingIoProcessor<NioSession>- Returns:
IteratorofIoSession
-
allSessionsCount
protected int allSessionsCount()Description copied from class:AbstractPollingIoProcessorGet the number ofIoSessionpolled by thisIoProcessor- Specified by:
allSessionsCountin classAbstractPollingIoProcessor<NioSession>- Returns:
- the number of sessions attached to this
IoProcessor
-
selectedSessions
Description copied from class:AbstractPollingIoProcessorGet anIteratorfor the list ofIoSessionfound selected by the last call ofAbstractPollingIoProcessor.select(long)- Specified by:
selectedSessionsin classAbstractPollingIoProcessor<NioSession>- Returns:
IteratorofIoSessionread for I/Os operation
-
init
Description copied from class:AbstractPollingIoProcessorInitialize the polling of a session. Add it to the polling process.- Specified by:
initin classAbstractPollingIoProcessor<NioSession>- Parameters:
session- theIoSessionto add to the polling- Throws:
Exception- any exception thrown by the underlying system calls
-
destroy
Description copied from class:AbstractPollingIoProcessorDestroy the underlying client socket handle- Specified by:
destroyin classAbstractPollingIoProcessor<NioSession>- Parameters:
session- theIoSession- Throws:
Exception- any exception thrown by the underlying system calls
-
registerNewSelector
In the case we are using the java select() method, this method is used to trash the buggy selector and create a new one, registering all the sockets on it.- Specified by:
registerNewSelectorin classAbstractPollingIoProcessor<NioSession>- Throws:
IOException- If we got an exception
-
isBrokenConnection
Check that the select() has not exited immediately just because of a broken connection. In this case, this is a standard case, and we just have to loop.- Specified by:
isBrokenConnectionin classAbstractPollingIoProcessor<NioSession>- Returns:
trueif a connection has been brutally closed.- Throws:
IOException- If we got an exception
-
getState
Get the state of a session (One of OPENING, OPEN, CLOSING)- Specified by:
getStatein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- theIoSessionto inspect- Returns:
- the state of the session
-
isReadable
Description copied from class:AbstractPollingIoProcessorTells if the session ready for reading- Specified by:
isReadablein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the queried session- Returns:
trueis ready,falseif not ready
-
isWritable
Description copied from class:AbstractPollingIoProcessorTells if the session ready for writing- Specified by:
isWritablein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the queried session- Returns:
trueis ready,falseif not ready
-
isInterestedInRead
Description copied from class:AbstractPollingIoProcessorTells if this session is registered for reading- Specified by:
isInterestedInReadin classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the queried session- Returns:
trueis registered for reading
-
isInterestedInWrite
Description copied from class:AbstractPollingIoProcessorTells if this session is registered for writing- Specified by:
isInterestedInWritein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the queried session- Returns:
trueis registered for writing
-
setInterestedInRead
Set the session to be informed when a read event should be processed- Specified by:
setInterestedInReadin classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the session for which we want to be interested in read eventsisInterested-truefor registering,falsefor removing- Throws:
Exception- If there was a problem while registering the session
-
setInterestedInWrite
Set the session to be informed when a write event should be processed- Specified by:
setInterestedInWritein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the session for which we want to be interested in write eventsisInterested-truefor registering,falsefor removing- Throws:
Exception- If there was a problem while registering the session
-
read
Description copied from class:AbstractPollingIoProcessorReads a sequence of bytes from aIoSessioninto the givenIoBuffer. Is called when the session was found ready for reading.- Specified by:
readin classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the session to readbuf- the buffer to fill- Returns:
- the number of bytes read
- Throws:
Exception- any exception thrown by the underlying system calls
-
write
Description copied from class:AbstractPollingIoProcessorWrite a sequence of bytes to aIoSession, means to be called when a session was found ready for writing.- Specified by:
writein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the session to writebuf- the buffer to writelength- the number of bytes to write can be superior to the number of bytes remaining in the buffer- Returns:
- the number of byte written
- Throws:
IOException- any exception thrown by the underlying system calls
-
transferFile
Description copied from class:AbstractPollingIoProcessorWrite a part of a file to aIoSession, if the underlying API isn't supporting system calls like sendfile(), you can throw aUnsupportedOperationExceptionso the file will be send using usualAbstractPollingIoProcessor.write(AbstractIoSession, IoBuffer, int)call.- Specified by:
transferFilein classAbstractPollingIoProcessor<NioSession>- Parameters:
session- the session to writeregion- the file region to writelength- the length of the portion to send- Returns:
- the number of written bytes
- Throws:
Exception- any exception thrown by the underlying system calls
-