Package org.apache.mina.core.polling
Class AbstractPollingIoProcessor<S extends AbstractIoSession>
- java.lang.Object
-
- org.apache.mina.core.polling.AbstractPollingIoProcessor<S>
-
- Type Parameters:
S
- the type of theIoSession
this processor can handle
- All Implemented Interfaces:
IoProcessor<S>
- Direct Known Subclasses:
AprIoProcessor
,NioProcessor
public abstract class AbstractPollingIoProcessor<S extends AbstractIoSession> extends Object implements IoProcessor<S>
An abstract implementation ofIoProcessor
which helps transport developers to write anIoProcessor
easily. This class is in charge of active polling a set ofIoSession
and trigger events when some I/O operation is possible.- Author:
- Apache MINA Project
-
-
Field Summary
Fields Modifier and Type Field Description protected AtomicBoolean
wakeupCalled
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractPollingIoProcessor(Executor executor)
Create anAbstractPollingIoProcessor
with the givenExecutor
for handling I/Os events.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
add(S session)
Adds the specifiedsession
to the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession
.protected abstract Iterator<S>
allSessions()
protected abstract int
allSessionsCount()
Get the number ofIoSession
polled by thisIoProcessor
protected abstract void
destroy(S session)
Destroy the underlying client socket handlevoid
dispose()
Releases any resources allocated by this processor.protected abstract void
doDispose()
Dispose the resources used by thisIoProcessor
for polling the client connections.void
flush(S session)
Flushes the internal write request queue of the specifiedsession
.protected abstract SessionState
getState(S session)
Get the state of a session (One of OPENING, OPEN, CLOSING)protected abstract void
init(S session)
Initialize the polling of a session.protected abstract boolean
isBrokenConnection()
Check that the select() has not exited immediately just because of a broken connection.boolean
isDisposed()
boolean
isDisposing()
protected abstract boolean
isInterestedInRead(S session)
Tells if this session is registered for readingprotected abstract boolean
isInterestedInWrite(S session)
Tells if this session is registered for writingprotected abstract boolean
isReadable(S session)
Tells if the session ready for readingprotected abstract boolean
isSelectorEmpty()
Say if the list ofIoSession
polled by thisIoProcessor
is emptyprotected abstract boolean
isWritable(S session)
Tells if the session ready for writingprotected abstract int
read(S session, IoBuffer buf)
protected abstract 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, registring all the sockets on it.void
remove(S session)
Removes and closes the specifiedsession
from the I/O processor so that the I/O processor closes the connection associated with thesession
and releases any other related resources.protected abstract int
select()
poll those sessions foreverprotected abstract int
select(long timeout)
poll those sessions for the given timeoutprotected abstract Iterator<S>
selectedSessions()
protected abstract void
setInterestedInRead(S session, boolean isInterested)
Set the session to be informed when a read event should be processedprotected abstract void
setInterestedInWrite(S session, boolean isInterested)
Set the session to be informed when a write event should be processedprotected abstract int
transferFile(S 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 usualwrite(AbstractIoSession, IoBuffer, int)
call.void
updateTrafficControl(S session)
Controls the traffic of the specifiedsession
depending of theIoSession.isReadSuspended()
andIoSession.isWriteSuspended()
flagsvoid
updateTrafficMask(S session)
Updates the traffic mask for a given sessionprotected abstract void
wakeup()
Interrupt theselect(long)
call.protected abstract int
write(S session, IoBuffer buf, int length)
Write a sequence of bytes to aIoSession
, means to be called when a session was found ready for writing.void
write(S session, WriteRequest writeRequest)
Writes the WriteRequest for the specifiedsession
.
-
-
-
Field Detail
-
wakeupCalled
protected AtomicBoolean wakeupCalled
-
-
Constructor Detail
-
AbstractPollingIoProcessor
protected AbstractPollingIoProcessor(Executor executor)
Create anAbstractPollingIoProcessor
with the givenExecutor
for handling I/Os events.- Parameters:
executor
- theExecutor
for handling I/O events
-
-
Method Detail
-
isDisposing
public final boolean isDisposing()
- Specified by:
isDisposing
in interfaceIoProcessor<S extends AbstractIoSession>
- Returns:
- true if and if only
IoProcessor.dispose()
method has been called. Please note that this method will return true even after all the related resources are released.
-
isDisposed
public final boolean isDisposed()
- Specified by:
isDisposed
in interfaceIoProcessor<S extends AbstractIoSession>
- Returns:
- true if and if only all resources of this processor have been disposed.
-
dispose
public final void dispose()
Releases any resources allocated by this processor. Please note that the resources might not be released as long as there are any sessions managed by this processor. Most implementations will close all sessions immediately and release the related resources.- Specified by:
dispose
in interfaceIoProcessor<S extends AbstractIoSession>
-
doDispose
protected abstract void doDispose() throws Exception
Dispose the resources used by thisIoProcessor
for polling the client connections. The implementing class doDispose method will be called.- Throws:
Exception
- if some low level IO error occurs
-
select
protected abstract int select(long timeout) throws Exception
poll those sessions for the given timeout- 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 abstract int select() throws Exception
poll those sessions forever- Returns:
- The number of session ready for read or for write
- Throws:
Exception
- if some low level IO error occurs
-
isSelectorEmpty
protected abstract boolean isSelectorEmpty()
Say if the list ofIoSession
polled by thisIoProcessor
is empty- Returns:
- true if at least a session is managed by this
IoProcessor
-
wakeup
protected abstract void wakeup()
Interrupt theselect(long)
call.
-
allSessionsCount
protected abstract int allSessionsCount()
Get the number ofIoSession
polled by thisIoProcessor
- Returns:
- the number of sessions attached to this
IoProcessor
-
getState
protected abstract SessionState getState(S session)
Get the state of a session (One of OPENING, OPEN, CLOSING)- Parameters:
session
- theIoSession
to inspect- Returns:
- the state of the session
-
isWritable
protected abstract boolean isWritable(S session)
Tells if the session ready for writing- Parameters:
session
- the queried session- Returns:
- true is ready, false if not ready
-
isReadable
protected abstract boolean isReadable(S session)
Tells if the session ready for reading- Parameters:
session
- the queried session- Returns:
- true is ready, false if not ready
-
setInterestedInWrite
protected abstract void setInterestedInWrite(S session, boolean isInterested) throws Exception
Set the session to be informed when a write event should be processed- 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
-
setInterestedInRead
protected abstract void setInterestedInRead(S session, boolean isInterested) throws Exception
Set the session to be informed when a read event should be processed- 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
-
isInterestedInRead
protected abstract boolean isInterestedInRead(S session)
Tells if this session is registered for reading- Parameters:
session
- the queried session- Returns:
- true is registered for reading
-
isInterestedInWrite
protected abstract boolean isInterestedInWrite(S session)
Tells if this session is registered for writing- Parameters:
session
- the queried session- Returns:
- true is registered for writing
-
init
protected abstract void init(S session) throws Exception
Initialize the polling of a session. Add it to the polling process.
-
destroy
protected abstract void destroy(S session) throws Exception
Destroy the underlying client socket handle
-
read
protected abstract int read(S session, IoBuffer buf) throws Exception
Reads a sequence of bytes from aIoSession
into the givenIoBuffer
. Is called when the session was found ready for reading.- 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 abstract int write(S session, IoBuffer buf, int length) throws IOException
Write a sequence of bytes to aIoSession
, means to be called when a session was found ready for writing.- 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 abstract int transferFile(S session, FileRegion region, int length) throws Exception
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 usualwrite(AbstractIoSession, IoBuffer, int)
call.- 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
-
add
public final void add(S session)
Adds the specifiedsession
to the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession
.- Specified by:
add
in interfaceIoProcessor<S extends AbstractIoSession>
- Parameters:
session
- The added session
-
remove
public final void remove(S session)
Removes and closes the specifiedsession
from the I/O processor so that the I/O processor closes the connection associated with thesession
and releases any other related resources.- Specified by:
remove
in interfaceIoProcessor<S extends AbstractIoSession>
- Parameters:
session
- The session to be removed
-
write
public void write(S session, WriteRequest writeRequest)
Writes the WriteRequest for the specifiedsession
.- Specified by:
write
in interfaceIoProcessor<S extends AbstractIoSession>
- Parameters:
session
- The session we want the message to be writtenwriteRequest
- the WriteRequest to write
-
flush
public final void flush(S session)
Flushes the internal write request queue of the specifiedsession
.- Specified by:
flush
in interfaceIoProcessor<S extends AbstractIoSession>
- Parameters:
session
- The session we want the message to be written
-
updateTrafficMask
public final void updateTrafficMask(S session)
Updates the traffic mask for a given session- Parameters:
session
- the session to update
-
registerNewSelector
protected abstract 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, registring all the sockets on it.- Throws:
IOException
- If we got an exception
-
isBrokenConnection
protected abstract 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.- Returns:
- true if a connection has been brutally closed.
- Throws:
IOException
- If we got an exception
-
updateTrafficControl
public void updateTrafficControl(S session)
Controls the traffic of the specifiedsession
depending of theIoSession.isReadSuspended()
andIoSession.isWriteSuspended()
flags- Specified by:
updateTrafficControl
in interfaceIoProcessor<S extends AbstractIoSession>
- Parameters:
session
- The session to be updated
-
-