|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.grizzly.Controller
public class Controller
Main entry point when using the Grizzly Framework. A Controller is composed of Handlers, ProtocolChain and ExecutorService. All of those components are configurable by client using the Grizzly Framework.
A ProtocolChain implement the "Chain of Responsibility" pattern (for more info, take a look at the classic "Gang of Four" design patterns book). Towards that end, the Chain API models a computation as a series of "protocol filter" that can be combined into a "protocol chain".
An Handler is a interface that can be implemented by implemented by client of the Grizzly Framework to used to help handling NIO operations. The Grizzly Framework define three Handlers:
(1) SelectorHandler: A SelectorHandler handles all java.nio.channels.Selector
operations. One or more instance of a Selector are
handled by SelectorHandler. The logic for processing of
SelectionKey interest (OP_ACCEPT,OP_READ, etc.) is usually
defined using an instance of SelectorHandler.
(2) SelectionKeyHandler: A SelectionKeyHandler is used to handle the life
life cycle of a SelectionKey. Operations like canceling,
registering or closing are handled by SelectionKeyHandler.
(3) ProtocolChainInstanceHandler: An ProtocolChainInstanceHandler is where one or several ProtocolChain
are created and cached. An ProtocolChainInstanceHandler decide if
a stateless or statefull ProtocolChain needs to be created.
By default, the Grizzly Framework bundles implementation for TCP and UPD transport. The TCPSelectorHandler is instanciated by default. As an example, supporting the HTTP protocol should only consist of adding the appropriate ProtocolFilter like:
Controller sel = new Controller();
sel.setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler(){
public ProtocolChain poll() {
ProtocolChain protocolChain = protocolChains.poll();
if (protocolChain == null){
protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(new ReadFilter());
protocolChain.addFilter(new HTTPParserFilter());
}
return protocolChain;
}
});
In the example above, a pool of ProtocolChain will be created, and all instance of ProtocolChain will have their instance of ProtocolFilter. Hence the above implementation can be called statefull. A stateless implementation would instead consist of sharing the ProtocolFilter among ProtocolChain:
final Controller sel = new Controller();
final ReadFilter readFilter = new ReadFilter();
final LogFilter logFilter = new LogFilter();
sel.setProtocolChainInstanceHandler(new DefaultProtocolChainInstanceHandler(){
public ProtocolChain poll() {
ProtocolChain protocolChain = protocolChains.poll();
if (protocolChain == null){
protocolChain = new DefaultProtocolChain();
protocolChain.addFilter(readFilter);
protocolChain.addFilter(logFilter);
}
return protocolChain;
}
});
| Nested Class Summary | |
|---|---|
static class |
Controller.Protocol
|
| Field Summary | |
|---|---|
protected Map<String,Object> |
attributes
Attributes, associated with the Controller instance |
protected ConnectorHandlerPool |
connectorHandlerPool
The ConnectorHandlerPool, which is responsible for creating/caching ConnectorHandler instances. |
protected ProtocolChainInstanceHandler |
instanceHandler
The ProtocolChainInstanceHandler used by this instance. |
protected static Logger |
logger
Default Logger. |
protected ComplexSelectorHandler |
multiReadThreadSelectorHandler
The SelectorHandler, which will manage connection accept, if readThreadsCount > 0 and spread connection processing between different read threads |
protected ReadController[] |
readThreadControllers
The array of Controllers to be used for reading |
protected int |
readThreadsCount
The number of read threads |
protected AtomicInteger |
readySelectorHandlerCounter
Internal countdown counter of SelectorHandlers, which
are ready to process |
protected SelectionKeyHandler |
selectionKeyHandler
The SelectionKey Handler used by this instance. |
protected LinkedTransferQueue<SelectorHandler> |
selectorHandlers
The set of SelectorHandlers used by this instance. |
protected StateHolder<State> |
stateHolder
Current Controller state |
protected Collection<ControllerStateListener> |
stateListeners
Collection of Controller state listeners, which
will are notified on Controller state change. |
protected AtomicInteger |
stoppedSelectorHandlerCounter
Internal countdown counter of SelectorHandlers, which stopped |
protected ExecutorService |
threadPool
Default Thread Pool (called ExecutorService).If not set, and instance of the DefaultThreadPool will be created. |
| Constructor Summary | |
|---|---|
Controller()
Controller constructor |
|
| Method Summary | |
|---|---|
ConnectorHandler |
acquireConnectorHandler(Controller.Protocol protocol)
Return an instance of a ConnectorHandler based on the
Protocol requested. |
void |
addSelectorHandler(SelectorHandler selectorHandler)
Add a SelectorHandler |
void |
addStateListener(ControllerStateListener stateListener)
Add controller state listener |
void |
cancelKey(SelectionKey key)
Deprecated. |
void |
configureContext(Context context,
SelectorHandler selectorHandler)
Configure the Context |
void |
copyTo(Copyable copy)
Copy this Controller state to another instance of a Controller. |
protected void |
doSelect(SelectorHandler selectorHandler)
This method handle the processing of all Selector's interest op (OP_ACCEPT,OP_READ,OP_WRITE,OP_CONNECT) by delegating to its Handler. |
Object |
getAttribute(String key)
Return an object based on a key. |
Map<String,Object> |
getAttributes()
Return a Map of attribute name/value pairs. |
ConnectorHandlerPool |
getConnectorHandlerPool()
Return the ConnectorHandlerPool used. |
static Controller |
getHandlerController(Handler handler)
Return the Controller which is handling the Handler |
ProtocolChainInstanceHandler |
getProtocolChainInstanceHandler()
Return the ProtocolChainInstanceHandler |
int |
getReadThreadsCount()
Return the number of Reader threads count. |
SelectionKeyHandler |
getSelectionKeyHandler()
Deprecated. Return the SelectionKeyHandler
Method is deprecated. Use SelectorHandler.getSelectionKeyHandler() instead |
SelectorHandler |
getSelectorHandler(Controller.Protocol protocol)
Return the SelectorHandler associated with the protocol. |
SelectorHandler |
getSelectorHandler(Selector selector)
Return the SelectorHandler associated
with the Selector. |
LinkedTransferQueue |
getSelectorHandlers()
Return the list SelectorHandler |
StateHolder<State> |
getStateHolder()
Gets this Controller's StateHolder |
ExecutorService |
getThreadPool()
Return the ExecutorService (Thread Pool) used by this Controller. |
boolean |
isHandleReadWriteConcurrently()
true if OP_ERAD and OP_WRITE can be handled concurrently. |
boolean |
isStarted()
Is this Controller started? |
static Logger |
logger()
Return the current Logger used by this Controller. |
protected void |
notifyException(Throwable e)
Notify exception occured |
void |
notifyReady()
Notify controller is ready |
void |
notifyStarted()
Notify controller started |
void |
notifyStopped()
Notify controller stopped |
void |
pause()
Pause this Controller and associated SelectorHandlers |
Context |
pollContext(SelectionKey key)
Get an instance of a Context |
NIOContext |
pollContext(SelectionKey key,
Context.OpType opType)
Get an instance of a NIOContext |
void |
registerKey(SelectionKey key)
Register a SelectionKey. |
void |
registerKey(SelectionKey key,
int ops)
Register a SelectionKey on the first SelectorHandler that was added using the addSelectorHandler(). |
void |
registerKey(SelectionKey key,
int ops,
Controller.Protocol protocol)
Register a SelectionKey. |
void |
releaseConnectorHandler(ConnectorHandler connectorHandler)
Return a ConnectorHandler to the pool of ConnectorHandler. |
Object |
removeAttribute(String key)
Remove a key/value object. |
void |
removeSelectorHandler(SelectorHandler selectorHandler)
Shuts down SelectorHandler and removes it from this
Controller list |
void |
removeStateListener(ControllerStateListener stateListener)
Remove controller state listener |
void |
resume()
Resume this Controller and associated SelectorHandlers |
void |
returnContext(Context ctx)
Return a Context to its pool if it is not shared. |
void |
run()
Execute this Controller. |
void |
setAttribute(String key,
Object value)
Set a key/value object. |
void |
setAttributes(Map<String,Object> attributes)
Set a Map of attribute name/value pairs. |
void |
setConnectorHandlerPool(ConnectorHandlerPool connectorHandlerPool)
Set the ConnectorHandlerPool used. |
void |
setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)
true if OP_ERAD and OP_WRITE can be handled concurrently. |
static void |
setLogger(Logger l)
Set the Logger single instance to use. |
void |
setProtocolChainInstanceHandler(ProtocolChainInstanceHandler instanceHandler)
Set the ProtocolChainInstanceHandler to use for
creating instance of ProtocolChain. |
void |
setReadThreadsCount(int readThreadsCount)
Set the number of Reader threads count. |
void |
setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
Deprecated. Set the SelectionKeyHandler to use for managing the life
cycle of SelectionKey.
Method is deprecated. Use SelectorHandler.setSelectionKeyHandler() instead |
void |
setSelectorHandler(SelectorHandler selectorHandler)
Set the first SelectorHandler |
void |
setThreadPool(ExecutorService threadPool)
Set the ExecutorService (Thread Pool). |
void |
start()
Start the Controller. |
void |
stop()
Stop the Controller by canceling all the registered keys. |
void |
stop(boolean isAsync)
Stop the Controller by canceling all the registered keys. |
protected void |
waitUntilSeletorHandlersStop()
Method waits until all initialized SelectorHandlers will
not get stopped |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected ProtocolChainInstanceHandler instanceHandler
protected SelectionKeyHandler selectionKeyHandler
protected ComplexSelectorHandler multiReadThreadSelectorHandler
protected ConnectorHandlerPool connectorHandlerPool
protected LinkedTransferQueue<SelectorHandler> selectorHandlers
SelectorHandlers used by this instance. If not set, the instance
of the TCPSelectorHandler will be added by default.
protected StateHolder<State> stateHolder
Controller state
protected int readThreadsCount
protected ReadController[] readThreadControllers
Controllers to be used for reading
protected static Logger logger
protected ExecutorService threadPool
protected final Collection<ControllerStateListener> stateListeners
Controller state listeners, which
will are notified on Controller state change.
protected AtomicInteger readySelectorHandlerCounter
SelectorHandlers, which
are ready to process
protected AtomicInteger stoppedSelectorHandlerCounter
SelectorHandlers, which stopped
protected Map<String,Object> attributes
Controller instance
| Constructor Detail |
|---|
public Controller()
| Method Detail |
|---|
protected void doSelect(SelectorHandler selectorHandler)
selectorHandler - - the SelectorHandlerpublic void registerKey(SelectionKey key)
key - SelectionKey to register
public void registerKey(SelectionKey key,
int ops)
key - SelectionKey to registerops - - the interest op to register
public void registerKey(SelectionKey key,
int ops,
Controller.Protocol protocol)
key - SelectionKey to registerops - - the interest op to registerprotocol - specified protocol SelectorHandler key should be registered onpublic void cancelKey(SelectionKey key)
key - SelectionKey to cancelpublic Context pollContext(SelectionKey key)
Context
key - SelectionKey
Context
public NIOContext pollContext(SelectionKey key,
Context.OpType opType)
NIOContext
key - SelectionKeyopType - the current SelectionKey op.
Context
public void configureContext(Context context,
SelectorHandler selectorHandler)
Context
context - selectorHandler - public void returnContext(Context ctx)
Context to its pool if it is not shared.
ctx - - the Contextpublic static Logger logger()
Logger used by this Controller.
public static void setLogger(Logger l)
public void setProtocolChainInstanceHandler(ProtocolChainInstanceHandler instanceHandler)
ProtocolChainInstanceHandler to use for
creating instance of ProtocolChain.
public ProtocolChainInstanceHandler getProtocolChainInstanceHandler()
ProtocolChainInstanceHandler
public void setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
SelectionKeyHandler to use for managing the life
cycle of SelectionKey.
Method is deprecated. Use SelectorHandler.setSelectionKeyHandler() instead
public SelectionKeyHandler getSelectionKeyHandler()
SelectionKeyHandler
Method is deprecated. Use SelectorHandler.getSelectionKeyHandler() instead
public void addSelectorHandler(SelectorHandler selectorHandler)
SelectorHandler
selectorHandler - - the SelectorHandlerpublic void setSelectorHandler(SelectorHandler selectorHandler)
SelectorHandler
selectorHandler - - the SelectorHandlerpublic SelectorHandler getSelectorHandler(Controller.Protocol protocol)
SelectorHandler associated with the protocol.
protocol - - the Controller.Protocol
SelectorHandlerpublic SelectorHandler getSelectorHandler(Selector selector)
SelectorHandler associated
with the Selector.
selector - - the Selector
SelectorHandlerpublic LinkedTransferQueue getSelectorHandlers()
SelectorHandler
ConcurrentLinkedQueuepublic void removeSelectorHandler(SelectorHandler selectorHandler)
SelectorHandler and removes it from this
Controller list
{@link - SelectorHandler} to removepublic ExecutorService getThreadPool()
ExecutorService (Thread Pool) used by this Controller.
public void setThreadPool(ExecutorService threadPool)
ExecutorService (Thread Pool).
public int getReadThreadsCount()
public void setReadThreadsCount(int readThreadsCount)
public ConnectorHandlerPool getConnectorHandlerPool()
ConnectorHandlerPool used.
public void setConnectorHandlerPool(ConnectorHandlerPool connectorHandlerPool)
ConnectorHandlerPool used.
public void run()
run in interface Runnablepublic void copyTo(Copyable copy)
copyTo in interface Copyablepublic void addStateListener(ControllerStateListener stateListener)
public void removeStateListener(ControllerStateListener stateListener)
public void notifyStarted()
public void notifyReady()
public void notifyStopped()
protected void notifyException(Throwable e)
public void start()
throws IOException
start in interface LifecycleIOException
public void stop()
throws IOException
stop in interface LifecycleIOException
public void stop(boolean isAsync)
throws IOException
isAsync, - true if controller should be stopped asynchrounously and control
returned immediately. If false - control will be returned
after Controller will be completely stoped.
IOException
public void pause()
throws IOException
Controller and associated SelectorHandlers
pause in interface LifecycleIOException
public void resume()
throws IOException
Controller and associated SelectorHandlers
resume in interface LifecycleIOExceptionpublic StateHolder<State> getStateHolder()
Controller's StateHolder
getStateHolder in interface SupportStateHolder<State>StateHolderpublic boolean isStarted()
boolean true / falsepublic ConnectorHandler acquireConnectorHandler(Controller.Protocol protocol)
ConnectorHandler based on the
Protocol requested.
acquireConnectorHandler in interface ConnectorHandlerPoolprotocol - a Controller.Protocol
ConnectorHandlerpublic void releaseConnectorHandler(ConnectorHandler connectorHandler)
ConnectorHandler to the pool of ConnectorHandler.
Any reference to the returned must not be re-used as that instance
can always be acquired again, causing unexpected results.
releaseConnectorHandler in interface ConnectorHandlerPoolconnectorHandler - - a ConnectorHandlerpublic boolean isHandleReadWriteConcurrently()
public void setHandleReadWriteConcurrently(boolean handleReadWriteConcurrently)
protected void waitUntilSeletorHandlersStop()
SelectorHandlers will
not get stopped
public Object removeAttribute(String key)
removeAttribute in interface AttributeHolderkey - - name of an attribute
public void setAttribute(String key,
Object value)
setAttribute in interface AttributeHolderkey - - name of an attributevalue - - value of named attributepublic Object getAttribute(String key)
getAttribute in interface AttributeHolderkey - - name of an attribute
public void setAttributes(Map<String,Object> attributes)
Map of attribute name/value pairs.
Old AttributeHolder values will not be available.
Later changes of this Map will lead to changes to the current
AttributeHolder.
setAttributes in interface AttributeHolderattributes - - map of name/value pairspublic Map<String,Object> getAttributes()
Map of attribute name/value pairs.
Updates, performed on the returned Map will be reflected in
this AttributeHolder
getAttributes in interface AttributeHolderMap of attribute name/value pairspublic static Controller getHandlerController(Handler handler)
Handler
handler - The handler (like SelectorHandler)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||