Class NioProcessor
- java.lang.Object
-
- org.apache.mina.core.polling.AbstractPollingIoProcessor<NioSession>
-
- org.apache.mina.transport.socket.nio.NioProcessor
-
- All Implemented Interfaces:
IoProcessor<NioSession>
public final class NioProcessor extends AbstractPollingIoProcessor<NioSession>
A processor for incoming and outgoing data get and written on a TCP socket.- Author:
- Apache MINA Project
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
NioProcessor.IoSessionIterator<NioSession>
An encapsulating iterator around theSelector.selectedKeys()
or theSelector.keys()
iterator;
-
Field Summary
-
Fields inherited from class org.apache.mina.core.polling.AbstractPollingIoProcessor
wakeupCalled
-
-
Constructor Summary
Constructors Constructor Description NioProcessor(Executor executor)
Creates a new instance of NioProcessor.NioProcessor(Executor executor, SelectorProvider selectorProvider)
Creates a new instance of NioProcessor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Iterator<NioSession>
allSessions()
protected int
allSessionsCount()
Get the number ofIoSession
polled by thisIoProcessor
protected void
destroy(NioSession session)
Destroy the underlying client socket handleprotected void
doDispose()
Dispose the resources used by thisIoProcessor
for polling the client connections.protected SessionState
getState(NioSession session)
Get the state of a session (One of OPENING, OPEN, CLOSING)protected void
init(NioSession session)
Initialize the polling of a session.protected boolean
isBrokenConnection()
Check that the select() has not exited immediately just because of a broken connection.protected boolean
isInterestedInRead(NioSession session)
Tells if this session is registered for readingprotected boolean
isInterestedInWrite(NioSession session)
Tells if this session is registered for writingprotected boolean
isReadable(NioSession session)
Tells if the session ready for readingprotected boolean
isSelectorEmpty()
Say if the list ofIoSession
polled by thisIoProcessor
is emptyprotected boolean
isWritable(NioSession session)
Tells if the session ready for writingprotected int
read(NioSession session, IoBuffer buf)
protected void
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.protected int
select()
poll those sessions foreverprotected int
select(long timeout)
poll those sessions for the given timeoutprotected Iterator<NioSession>
selectedSessions()
Get anIterator
for the list ofIoSession
found selected by the last call ofAbstractPollingIoProcessor.select(long)
protected void
setInterestedInRead(NioSession session, boolean isInterested)
Set the session to be informed when a read event should be processedprotected void
setInterestedInWrite(NioSession session, boolean isInterested)
Set the session to be informed when a write event should be processedprotected int
transferFile(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 aUnsupportedOperationException
so the file will be send using usualAbstractPollingIoProcessor.write(AbstractIoSession, IoBuffer, int)
call.protected void
wakeup()
Interrupt theAbstractPollingIoProcessor.select(long)
call.protected int
write(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 org.apache.mina.core.polling.AbstractPollingIoProcessor
add, dispose, flush, isDisposed, isDisposing, remove, updateTrafficControl, updateTrafficMask, write
-
-
-
-
Constructor Detail
-
NioProcessor
public NioProcessor(Executor executor)
Creates a new instance of NioProcessor.- Parameters:
executor
- The executor to use
-
NioProcessor
public NioProcessor(Executor executor, SelectorProvider selectorProvider)
Creates a new instance of NioProcessor.- Parameters:
executor
- The executor to useselectorProvider
- The Selector provider to use
-
-
Method Detail
-
doDispose
protected void doDispose() throws Exception
Description copied from class:AbstractPollingIoProcessor
Dispose the resources used by thisIoProcessor
for polling the client connections. The implementing class doDispose method will be called.- Specified by:
doDispose
in classAbstractPollingIoProcessor<NioSession>
- Throws:
Exception
- if some low level IO error occurs
-
select
protected int select(long timeout) throws Exception
Description copied from class:AbstractPollingIoProcessor
poll those sessions for the given timeout- Specified by:
select
in 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
protected int select() throws Exception
Description copied from class:AbstractPollingIoProcessor
poll those sessions forever- Specified by:
select
in 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:AbstractPollingIoProcessor
Say if the list ofIoSession
polled by thisIoProcessor
is empty- Specified by:
isSelectorEmpty
in classAbstractPollingIoProcessor<NioSession>
- Returns:
- true if at least a session is managed by this
IoProcessor
-
wakeup
protected void wakeup()
Description copied from class:AbstractPollingIoProcessor
Interrupt theAbstractPollingIoProcessor.select(long)
call.- Specified by:
wakeup
in classAbstractPollingIoProcessor<NioSession>
-
allSessions
protected Iterator<NioSession> allSessions()
Description copied from class:AbstractPollingIoProcessor
- Specified by:
allSessions
in classAbstractPollingIoProcessor<NioSession>
- Returns:
Iterator
ofIoSession
-
allSessionsCount
protected int allSessionsCount()
Description copied from class:AbstractPollingIoProcessor
Get the number ofIoSession
polled by thisIoProcessor
- Specified by:
allSessionsCount
in classAbstractPollingIoProcessor<NioSession>
- Returns:
- the number of sessions attached to this
IoProcessor
-
selectedSessions
protected Iterator<NioSession> selectedSessions()
Description copied from class:AbstractPollingIoProcessor
Get anIterator
for the list ofIoSession
found selected by the last call ofAbstractPollingIoProcessor.select(long)
- Specified by:
selectedSessions
in classAbstractPollingIoProcessor<NioSession>
- Returns:
Iterator
ofIoSession
read for I/Os operation
-
init
protected void init(NioSession session) throws Exception
Description copied from class:AbstractPollingIoProcessor
Initialize the polling of a session. Add it to the polling process.- Specified by:
init
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- theIoSession
to add to the polling- Throws:
Exception
- any exception thrown by the underlying system calls
-
destroy
protected void destroy(NioSession session) throws Exception
Description copied from class:AbstractPollingIoProcessor
Destroy the underlying client socket handle- Specified by:
destroy
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- theIoSession
- Throws:
Exception
- any exception thrown by the underlying system calls
-
registerNewSelector
protected void registerNewSelector() throws IOException
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:
registerNewSelector
in classAbstractPollingIoProcessor<NioSession>
- Throws:
IOException
- If we got an exception
-
isBrokenConnection
protected boolean isBrokenConnection() throws IOException
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:
isBrokenConnection
in classAbstractPollingIoProcessor<NioSession>
- Returns:
- true if a connection has been brutally closed.
- Throws:
IOException
- If we got an exception
-
getState
protected SessionState getState(NioSession session)
Get the state of a session (One of OPENING, OPEN, CLOSING)- Specified by:
getState
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- theIoSession
to inspect- Returns:
- the state of the session
-
isReadable
protected boolean isReadable(NioSession session)
Description copied from class:AbstractPollingIoProcessor
Tells if the session ready for reading- Specified by:
isReadable
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- the queried session- Returns:
- true is ready, false if not ready
-
isWritable
protected boolean isWritable(NioSession session)
Description copied from class:AbstractPollingIoProcessor
Tells if the session ready for writing- Specified by:
isWritable
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- the queried session- Returns:
- true is ready, false if not ready
-
isInterestedInRead
protected boolean isInterestedInRead(NioSession session)
Description copied from class:AbstractPollingIoProcessor
Tells if this session is registered for reading- Specified by:
isInterestedInRead
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- the queried session- Returns:
- true is registered for reading
-
isInterestedInWrite
protected boolean isInterestedInWrite(NioSession session)
Description copied from class:AbstractPollingIoProcessor
Tells if this session is registered for writing- Specified by:
isInterestedInWrite
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- the queried session- Returns:
- true is registered for writing
-
setInterestedInRead
protected void setInterestedInRead(NioSession session, boolean isInterested) throws Exception
Set the session to be informed when a read event should be processed- Specified by:
setInterestedInRead
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- the session for which we want to be interested in read eventsisInterested
- true for registering, false for removing- Throws:
Exception
- If there was a problem while registering the session
-
setInterestedInWrite
protected void setInterestedInWrite(NioSession session, boolean isInterested) throws Exception
Set the session to be informed when a write event should be processed- Specified by:
setInterestedInWrite
in classAbstractPollingIoProcessor<NioSession>
- Parameters:
session
- the session for which we want to be interested in write eventsisInterested
- true for registering, false for removing- Throws:
Exception
- If there was a problem while registering the session
-
read
protected int read(NioSession session, IoBuffer buf) throws Exception
Description copied from class:AbstractPollingIoProcessor
Reads a sequence of bytes from aIoSession
into the givenIoBuffer
. Is called when the session was found ready for reading.- Specified by:
read
in 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
protected int write(NioSession session, IoBuffer buf, int length) throws IOException
Description copied from class:AbstractPollingIoProcessor
Write a sequence of bytes to aIoSession
, means to be called when a session was found ready for writing.- Specified by:
write
in 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
protected int transferFile(NioSession session, FileRegion region, int length) throws Exception
Description copied from class:AbstractPollingIoProcessor
Write a part of a file to aIoSession
, if the underlying API isn't supporting system calls like sendfile(), you can throw aUnsupportedOperationException
so the file will be send using usualAbstractPollingIoProcessor.write(AbstractIoSession, IoBuffer, int)
call.- Specified by:
transferFile
in 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
-
-