users@grizzly.java.net

Re: DefaultProtocolChainInstanceHandler

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Mon, 11 Feb 2008 15:43:17 -0500

Salut,

Oleksiy Stashok wrote:
> Hello,
>
> IMHO to use correctly ProtocolChainInstanceHandler.offer(ProtocolChain)
> - we need to add one more method to ProtocolChain API:
> ProtocolChainInstanceHandler getInstanceHandler();
>
> as at time, when ProtocolChain could be released - we don't know from
> which ProtocolChainInstanceHandler the ProtocolChain was polled from.
> Once method above will be added to the ProtocolChain interface - we can
> easily implement ProtocolChain releasing in Controller's returnContext()
> method:
>
> public void returnContext(Context ctx){
> / ProtocolChain protocolChain = ctx.getProtocolChain();
> protocolChain.getInstanceHandler().offer(protocolChain);
> / contexts.offer(ctx);
> }
>
> What do you think?

An alternative is to change the Controller and delegate the
ProtocolChain configuration to the Context (see patch). That way no API
changes :-)

What do you think?

-- Jeanfrancois





Index: src/main/java/com/sun/grizzly/DefaultProtocolChain.java
===================================================================
--- src/main/java/com/sun/grizzly/DefaultProtocolChain.java (revision 784)
+++ src/main/java/com/sun/grizzly/DefaultProtocolChain.java (working copy)
@@ -78,7 +78,6 @@
                 int currentPosition = executeProtocolFilter(ctx);
                 reinvokeChain = postExecuteProtocolFilter(currentPosition, ctx);
             }
- ctx.recycle();
         }
         controller.returnContext(ctx);
     }
Index: src/main/java/com/sun/grizzly/Controller.java
===================================================================
--- src/main/java/com/sun/grizzly/Controller.java (revision 784)
+++ src/main/java/com/sun/grizzly/Controller.java (working copy)
@@ -360,11 +360,7 @@
                         }
 
                         if (delegateToWorkerThread){
- pciHandler = selectorHandler
- .getProtocolChainInstanceHandler();
- protocolChain = (pciHandler != null ?
- pciHandler.poll(): instanceHandler.poll());
- Context context = pollContext(key,protocolChain);
+ Context context = pollContext(key);
                             context.setSelectorHandler(selectorHandler);
                             context.setPipeline(selectorHandler.pipeline());
                             context.setAsyncQueueReader(
@@ -469,33 +465,20 @@
                     "with known SelectorHandler");
         }
     }
+
     
-
     /**
      * Get an instance of a <code>Context</code>
      * @param key <code>SelectionKey</code>
      * @return <code>Context</code>
- */
- public Context pollContext(SelectionKey key){
- return pollContext(key,instanceHandler.poll());
- }
-
- /**
- * Get an instance of a <code>Context</code>
- * @param key <code>SelectionKey</code>
- * @param protocolChain The ProtocolChain used to execute the
- * returned Context.
- * @return <code>Context</code>
      */
- protected Context pollContext(SelectionKey key,
- ProtocolChain protocolChain){
+ public Context pollContext(SelectionKey key){
         Context ctx = contexts.poll();
         if (ctx == null){
             ctx = new Context();
         }
         ctx.setController(this);
         ctx.setSelectionKey(key);
- ctx.setProtocolChain(protocolChain);
         return ctx;
     }
     
@@ -504,6 +487,14 @@
      * @param ctx - the <code>Context</code>
      */
     public void returnContext(Context ctx){
+ ProtocolChainInstanceHandler pciHandler =
+ ctx.getSelectorHandler().getProtocolChainInstanceHandler();
+ if (pciHandler == null){
+ pciHandler = instanceHandler;
+ }
+ instanceHandler.offer(ctx.getProtocolChain());
+
+ ctx.recycle();
         contexts.offer(ctx);
     }
     
Index: src/main/java/com/sun/grizzly/Context.java
===================================================================
--- src/main/java/com/sun/grizzly/Context.java (revision 784)
+++ src/main/java/com/sun/grizzly/Context.java (working copy)
@@ -402,7 +402,14 @@
      * be executed in separate thread, false - in current thread.
      * @throws com.sun.grizzly.PipelineFullException
      */
- public void execute(ContextTask contextTask, boolean runInSeparateThread) throws PipelineFullException {
+ public void execute(ContextTask contextTask, boolean runInSeparateThread) throws PipelineFullException {
+ if (protocolChain == null){
+ ProtocolChainInstanceHandler pciHandler = selectorHandler
+ .getProtocolChainInstanceHandler();
+ protocolChain = (pciHandler != null ?
+ pciHandler.poll(): controller.getProtocolChainInstanceHandler().poll());
+ }
+
         if (contextTask != null) {
             contextTask.setContext(this);
             if (runInSeparateThread) {
Index: src/main/java/com/sun/grizzly/TCPSelectorHandler.java
===================================================================
--- src/main/java/com/sun/grizzly/TCPSelectorHandler.java (revision 784)
+++ src/main/java/com/sun/grizzly/TCPSelectorHandler.java (working copy)
@@ -1071,11 +1071,12 @@
             instanceHandler.poll() :
             serverContext.getController().getProtocolChainInstanceHandler().poll();
         
- final Context context = serverContext.getController().pollContext(key, protocolChain);
+ final Context context = serverContext.getController().pollContext(key);
         context.setSelectionKey(key);
         context.setSelectorHandler(this);
         context.setAsyncQueueReader(asyncQueueReader);
         context.setAsyncQueueWriter(asyncQueueWriter);
+ context.setProtocolChain(protocolChain);
         return context;
     }