perfect.. just to be sure.
in my try/catch. I was doing this
catch (IOException ex){
ex.printStackTrace();
selectorHandler.getSelectionKeyHandler().cancel(key);
}
I see the stacktrace when I kill the server when the client is still
connected.. but that's it.
oh wait.. ah ah.. the line : sun.nio.ch.SelectionKeyImpl_at_1546e25 is being
locally cancelled was within the stacktrace.
cool.. that's works well.
so should I use cancel or close in my exception ?
java.io.IOException: Une connexion existante a dû être fermée par l'hôte
distant
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(Unknown Source)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.read(Unknown Source)
at sun.nio.ch.SocketChannelImpl.read(Unknown Source)
at test.quotegw.client.TCPClientTest$3.onRead(TCPClientTest.java:106)
at
com.sun.grizzly.CallbackHandlerContextTask.doCall(CallbackHandlerContextTask.java:76)
at
com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:56)sun.nio.ch.SelectionKeyImpl_at_1546e25is
being locally cancelled
at
com.sun.grizzly.util.WorkerThreadImpl.processTask(WorkerThreadImpl.java:335)
at com.sun.grizzly.util.WorkerThreadImpl.run(WorkerThreadImpl.java:194)
2008/11/26 Jeanfrancois Arcand <Jeanfrancois.Arcand_at_sun.com>
> Salut,
>
> My understanding is the client should also works but since you might
> execute the read inside the CallbackHandler.onRead(..), it is up to you to
> notify the listener. When you do:
>
> try {
>>> if(key.isValid() && key.isReadable()){
>>> int count = socketChannel.read(response);
>>> if(count>0){
>>> response.flip();
>>> byte[] b =
>>> new byte[(int)count];
>>> response.get(b);
>>>
>>> response.clear();
>>>
>>> System.out.println(new String(b));
>>> }
>>>
>>> selectorHandler.register(key, SelectionKey.OP_READ);
>>> }
>>>
>>
> here you need to do if count == -1 or if an IOException is thrown:
>
>
> ((BaseSelectionKeyHandler)ctx.getSelectorHandler()).getSelectionKeyHandler().close(key);
>
> A+
>
> -- Jeanfrancois
>
>
> Survivant 00 wrote:
>
>> can I be notify on the client side too ?
>>
>> I try, but I'm never notify on the client side. I need that for this
>> reason :
>>
>> I,m sending requests to the server, and sometime the server will close my
>> connection and it's will be fine, but in my client I would like to be notify
>> because it will mean something in the flow of requests that I'll send.
>>
>> here the code that I have for my client
>>
>> public class TCPClientTest {
>> private static final Logger s_logger =
>> LoggerFactory.getLogger(TCPClientTest.class);
>> private String host = "localhost";
>> private int port = 7803;
>>
>> private TCPConnectorHandler connector_handler;
>> private Controller controller;
>> private TCPSelectorHandler tcp_selector_handler;
>>
>> private ByteBuffer buf = ByteBufferFactory.allocateView(1000, false);
>> private ByteBuffer response = ByteBufferFactory.allocateView(1000,
>> false);
>>
>> public void init() {
>> final CountDownLatch started = new CountDownLatch(1);
>>
>> controller = new Controller();
>> tcp_selector_handler = new TCPSelectorHandler(true);
>> controller.addSelectorHandler(tcp_selector_handler);
>>
>> controller.addStateListener(new ControllerStateListenerAdapter() {
>>
>> public void onException(Throwable e) {
>> System.out.println("Grizzly controller exception:" +
>> e.getMessage());
>> }
>>
>> public void onReady() {
>> System.out.println("Ready!");
>> started.countDown();
>> }
>>
>> });
>> 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)");
>> }
>> });
>>
>> tcp_selector_handler.setSelectionKeyHandler(selectionKeyHandler);
>>
>> new Thread(controller).start();
>> try {
>> started.await();
>> } catch (Exception e) {
>> System.out.println("Timeout in wait" + e.getMessage());
>> }
>>
>> connector_handler = (TCPConnectorHandler)
>> controller.acquireConnectorHandler(Controller.Protocol.TCP);
>> }
>>
>> public void connect(){
>> try {
>> connector_handler.connect(new InetSocketAddress(host, port),
>> new CallbackHandler<Context>() {
>> public void onConnect(IOEvent<Context> e) {
>> SelectionKey k = e.attachment().getSelectionKey();
>> System.out.println("Callbackhandler: OnConnect...");
>> try {
>> connector_handler.finishConnect(k);
>> } catch (Exception ex) {
>> System.out.println("exception in CallbackHandler:"
>> + ex.getMessage());
>> }
>> e.attachment().getSelectorHandler().register(k,
>> SelectionKey.OP_READ);
>> }
>>
>> public void onRead(IOEvent<Context> ioEvent) {
>> SelectionKey key =
>> ioEvent.attachment().getSelectionKey();
>> SelectorHandler selectorHandler =
>> ioEvent.attachment().getSelectorHandler();
>> SocketChannel socketChannel =
>> (SocketChannel)key.channel();
>> try {
>> if(key.isValid() && key.isReadable()){
>> int count = socketChannel.read(response);
>> if(count>0){
>> response.flip();
>> byte[] b =
>> new byte[(int)count];
>> response.get(b);
>>
>> response.clear();
>>
>> System.out.println(new String(b));
>> }
>>
>> selectorHandler.register(key, SelectionKey.OP_READ);
>> }
>> } catch (IOException ex){
>> ex.printStackTrace();
>>
>> selectorHandler.getSelectionKeyHandler().cancel(key);
>> }
>> }
>>
>> public void onWrite(IOEvent<Context> e) {
>> System.out.println("onWrite");
>> }
>>
>> });
>> } catch (Exception e) {
>> System.out.println("Exception in execute..." + e);
>> e.printStackTrace(System.out);
>> }
>> }
>> public void send(String quote) throws Exception {
>> byte[] msg = quote.getBytes();
>> buf = ByteBufferFactory.allocateView(msg.length, false);
>> buf.put(msg);
>> buf.flip();
>> connector_handler.write(buf, true);
>> buf.clear();
>> }
>> public void close(){
>> try {
>> if(connector_handler!=null){
>> connector_handler.close();
>> }
>> if(controller!=null){
>> controller.stop();
>> }
>> } catch (IOException e) {
>> e.printStackTrace();
>> }
>> }
>> public static void main(String[] args) {
>> TCPClientTest client = new TCPClientTest();
>>
>> try {
>> client.init();
>> client.connect();
>> client.send("query");
>>
>> Thread.sleep(5000);
>> client.close();
>> Thread.sleep(300);
>> } catch(Exception e){
>> e.printStackTrace();
>> } finally {
>> try {client.close();}catch(Exception e){}
>> }
>> }
>>
>> }
>>
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>
>