users@grizzly.java.net

Re: [ANN] Getting notified when client or server close the connection.

From: Survivant 00 <survivant00_at_gmail.com>
Date: Tue, 25 Nov 2008 11:00:51 -0500

c'est ca.. The code wasn't compiling so I assume it was the interface that
wasn't commited.

wrote a little entry on my blog
http://weblogs.java.net/blog/survivant/archive/2008/11/grizzly_how_to.html


and you should have add a little unit test :)


2008/11/25 Jeanfrancois Arcand <Jeanfrancois.Arcand_at_sun.com>

> Salut,
>
> Survivant 00 wrote:
>
>> The code below looks OK to me. You don't get the notified? Strange as I
>> tested this yesterday with telnet and it was working...let me work on a unit
>> test.
>>
>> thanks.
>>
>>
>> I replace SelectionKeyHandler skh = new DefaultSelectionKeyHandler();
>>
>> by
>> DefaultSelectionKeyHandler skh = new DefaultSelectionKeyHandler();
>>
>
> I see (c'est a cause de la maudite tempete!)...cut&paste typo. I shouldn't
> write code in email! Must have been:
>
> BaseSelectionKeyHandler selectionKeyHandler = new
> DefaultSelectionKeyHandler();
>
> Thanks!!!!
>
> -- Jeanfrancois
>
>
>
>> but if I do that.. it's mean that your sample doesn't compile.
>>
>> and if I replace
>> controler.setSelectionKeyHandler(skh);
>> by
>> tcpSelectorHandler.setSelectionKeyHandler(skh);
>>
>> I'll have to change to that
>>
>> BaseSelectionKeyHandler selectionKeyHandler = new
>> BaseSelectionKeyHandler();
>> // to be notify when a client close the connection
>> selectionKeyHandler.setConnectionCloseHandler(new
>> ConnectionCloseHandler() {
>>
>> public void locallyClosed(SelectionKey key) {
>> s_logger.debug(key + " is being locally cancelled");
>> }
>>
>> public void remotlyClosed(SelectionKey key) {
>> s_logger.debug(key + " is being remotly cancelled
>> (connection closed)");
>> }
>> });
>>
>> tcpSelectorHandler.setSelectionKeyHandler(selectionKeyHandler);
>> f_controller.addSelectorHandler(tcpSelectorHandler);
>>
>>
>>
>> and that WORK.
>> here the output when I close the telnet connection.
>>
>> DEBUG [25/11/08 10:48:21]
>> (com.gfbn.quotegw.connection.handler.parser.QuoteQueryProtocolParser) -
>> isExpectingMoreData
>> DEBUG [25/11/08 10:48:21]
>> (com.gfbn.quotegw.connection.handler.GrizzlyConnectionListener) -
>> sun.nio.ch.SelectionKeyImpl_at_fdb00d is being remotly cancelled (connection
>> closed)
>> DEBUG [25/11/08 10:48:21]
>> (com.gfbn.quotegw.connection.handler.parser.QuoteQueryProtocolParser) -
>> startBuffer
>> DEBUG [25/11/08 10:48:21]
>> (com.gfbn.quotegw.connection.handler.parser.QuoteQueryProtocolParser) -
>> hasMoreBytesToParse
>> DEBUG [25/11/08 10:48:21]
>> (com.gfbn.quotegw.connection.handler.parser.QuoteQueryProtocolParser) -
>> releaseBuffer
>> DEBUG [25/11/08 10:48:21]
>> (com.gfbn.quotegw.connection.handler.GrizzlyConnectionListener) -
>> sun.nio.ch.SelectionKeyImpl_at_fdb00d is being locally cancelled
>>
>>
>>
>>
>> 2008/11/25 Jeanfrancois Arcand <Jeanfrancois.Arcand_at_sun.com <mailto:
>> Jeanfrancois.Arcand_at_sun.com>>
>>
>> Salut,
>>
>>
>> Survivant 00 wrote:
>>
>> JF, I try directly your code.. first you did a typo.. second
>> line : you wrote shk.. it's skh :)
>>
>> but if I check at the commit logs :
>>
>> Sending grizzly
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/BaseSelectionKeyHandler.java
>>
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/SSLConnectorHandler.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/TCPConnectorHandler.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/UDPConnectorHandler.java
>>
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/async/AbstractAsyncQueueReader.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/async/AbstractAsyncQueueWriter.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/async/TCPAsyncQueueReader.java
>>
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/async/TCPAsyncQueueWriter.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/async/UDPAsyncQueueWriter.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/filter/ReadFilter.java
>>
>> Adding
>> grizzly/src/main/java/com/sun/grizzly/util/ConnectionCloseHandler.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/util/InputReader.java
>> Sending
>> grizzly/src/main/java/com/sun/grizzly/util/OutputWriter.java
>>
>>
>> you didn't commit Handler or SelectionKeyHandler. I don't have
>> the method : skh.setConnectionCloseHandler in the interface.
>>
>>
>> Handler and SelectionKeyHandler hasn't changed. You need to use an
>> instance of BaseSelectionKeyHandler like DefaultSelectionKeyHandler.
>>
>>
>>
>>
>>
>> and if I look into the code the method setSelectionKeyHandler is
>> deprecated.
>>
>> f_controller.setSelectionKeyHandler(skh);
>>
>>
>> Yes you can use instead
>> SelectorHandler.setSelectionKeyHandler()...but I was too lazy and
>> used the Controller instead.
>>
>> The code below looks OK to me. You don't get the notified? Strange
>> as I tested this yesterday with telnet and it was working...let me
>> work on a unit test.
>>
>> A+
>>
>> -- Jeanfrancois
>>
>>
>>
>>
>>
>> here my init method
>>
>>
>> /**
>> * Init
>> */
>> @SuppressWarnings("unchecked")
>> public void init(){
>> if(s_logger.isDebugEnabled()){
>> s_logger.debug("listening for incomming TCP
>> Connections on port : " + f_port);
>> }
>> try {
>> f_controller = new Controller();
>> TCPSelectorHandler tcpSelectorHandler = new
>> TCPSelectorHandler();
>> tcpSelectorHandler.setPort(f_port);
>> Pipeline pipeline = new DefaultPipeline();
>> pipeline.setMaxThreads(5);
>> f_controller.setPipeline(pipeline);
>>
>> tcpSelectorHandler.setSelectionKeyHandler(new
>> BaseSelectionKeyHandler());
>>
>> f_controller.addSelectorHandler(tcpSelectorHandler);
>> SelectionKeyHandler skh = new
>> DefaultSelectionKeyHandler();
>> skh.setConnectionCloseHandler(new
>> ConnectionCloseHandler() {
>>
>> public void locallyClosed(SelectionKey key) {
>> s_logger.debug(key + " is being locally
>> cancelled");
>> }
>>
>> public void remotlyClosed(SelectionKey key) {
>> s_logger.debug(key + " is being remotly
>> cancelled (connection closed)");
>> }
>> });
>>
>> f_controller.setSelectionKeyHandler(skh);
>> QuoteQueryProtocolFilter
>> protocolParser = new QuoteQueryProtocolFilter();
>> QuoteQueryManagerFilter quoteManagerFilter = new
>> QuoteQueryManagerFilter(f_quoteManager);
>> final ProtocolChain protocolChain = new
>> DefaultProtocolChain();
>> protocolChain.addFilter(protocolParser);
>> protocolChain.addFilter(quoteManagerFilter);
>> ((DefaultProtocolChain)
>> protocolChain).setContinuousExecution(true);
>>
>>
>> ProtocolChainInstanceHandler pciHandler = new
>> DefaultProtocolChainInstanceHandler() {
>>
>> public boolean offer(ProtocolChain protocolChain) {
>> return false;
>>
>> }
>>
>> public ProtocolChain poll() {
>>
>> return protocolChain;
>> }
>> };
>>
>> f_controller.setProtocolChainInstanceHandler(pciHandler);
>> try {
>> f_controller.start();
>> } catch (IOException e) {
>> e.printStackTrace();
>> }
>> } catch (Exception e) {
>> f_quoteManager.exit(-10);
>> }
>> }
>>
>>
>> 2008/11/25 Survivant 00 <survivant00_at_gmail.com
>> <mailto:survivant00_at_gmail.com> <mailto:survivant00_at_gmail.com
>>
>> <mailto:survivant00_at_gmail.com>>>
>>
>>
>> I reopen the bug, but not sure if I did it correctly.
>> here what I have.
>>
>>
>> tcpSelectorHandler.setSelectionKeyHandler(new
>> BaseSelectionKeyHandler(){
>>
>>
>> public void locallyClosed(SelectionKey key) {
>> s_logger.debug(key + " is being locally
>> cancelled");
>>
>> }
>>
>> public void remotlyClosed(SelectionKey key) {
>> s_logger.debug(key + " is being remotly
>> cancelled (connection closed)");
>> }
>> });
>>
>> f_controller.addSelectorHandler(tcpSelectorHandler);
>>
>> if I open a telnet connection and close it, I won't be notify.
>> but I didn't use ConnectionCloseHandler.
>> Can I mix the 2 handlers ?
>>
>>
>>
>>
>> 2008/11/24 Jeanfrancois Arcand <Jeanfrancois.Arcand_at_sun.com
>> <mailto:Jeanfrancois.Arcand_at_sun.com>
>> <mailto:Jeanfrancois.Arcand_at_sun.com
>> <mailto:Jeanfrancois.Arcand_at_sun.com>>>
>>
>>
>> Salut,
>>
>> I've added a new Handler for managing local and remote
>> connection close. The new ConnectionCloseHandler[1] can be
>> configured on any class that extends our
>> BaseSelectionKeyHandler
>> (like DefaultSelectionKeyHandler):
>>
>> SelectionKeyHandler skh = new DefaultSelectionKeyHandler();
>> shk.setConnectionCloseHandler(new ConnectionCloseHandler() {
>>
>> public void locallyClosed(SelectionKey key) {
>> logger.info <http://logger.info>
>> <http://logger.info>(key + " is being locally
>>
>> cancelled");
>> }
>>
>> public void remotlyClosed(SelectionKey key) {
>> logger.fine(key + " is being remotly cancelled
>> (connection closed)");
>> }
>> });
>>
>> controller.setSelectionKeyHandler(skh);
>>
>> Those two methods gets invoked when any of the following
>> condition happens:
>>
>> 1. Any IOException occurs on I/O operations (read/write)
>> 2. When the read() or write() operation return -1
>>
>> Please try it :-)
>>
>> A+
>>
>> -- Jeanfrancois
>>
>> [1]
>>
>> https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/util/ConnectionCloseHandler.html
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> users-unsubscribe_at_grizzly.dev.java.net
>> <mailto:users-unsubscribe_at_grizzly.dev.java.net>
>> <mailto:users-unsubscribe_at_grizzly.dev.java.net
>> <mailto:users-unsubscribe_at_grizzly.dev.java.net>>
>>
>> For additional commands, e-mail:
>> users-help_at_grizzly.dev.java.net
>> <mailto:users-help_at_grizzly.dev.java.net>
>> <mailto:users-help_at_grizzly.dev.java.net
>> <mailto:users-help_at_grizzly.dev.java.net>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>> <mailto:users-unsubscribe_at_grizzly.dev.java.net>
>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>> <mailto:users-help_at_grizzly.dev.java.net>
>>
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>