dev@grizzly.java.net

Re: Some problems creating client

From: Hubert Iwaniuk <neotyk_at_kungfoo.pl>
Date: Mon, 27 Apr 2009 23:55:13 +0200

Hi,

Thanks it did help.
Now I'm having problem with ReadFilter trying to read from closed channel.
Any thoughts?

Cheers,
   Hubert


On Mon, Apr 27, 2009 at 11:41 PM, Jeanfrancois Arcand <
Jeanfrancois.Arcand_at_sun.com> wrote:

> Salut,
>
> Hubert Iwaniuk wrote:
>
>> Hi All,
>>
>> I'm having hard time getting client calls to run ProtocolFilters on read
>> responses.
>> I've attached full file that I run against trunk.
>>
>> Please help I'm getting hope less here,
>> Hubert
>>
>>
>> ------------------------------------------------------------------------
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
>> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>> package pl.kungfoo.grizzly.http.client;
>>
>> import com.sun.grizzly.*;
>> import com.sun.grizzly.util.WorkerThreadImpl;
>> import com.sun.grizzly.filter.ReadFilter;
>> import com.sun.grizzly.filter.LogFilter;
>>
>> import java.net.InetSocketAddress;
>> import java.io.IOException;
>> import java.nio.ByteBuffer;
>> import java.nio.channels.SelectionKey;
>> import java.util.concurrent.CountDownLatch;
>> import java.util.logging.Level;
>>
>> /**
>> * Hello world!
>> */
>> public class App {
>> public static void main(String[] args) throws IOException {
>>
>> Controller controller = new Controller();
>> ProtocolChain protocolChain =
>> controller.getProtocolChainInstanceHandler().poll();
>> protocolChain.addFilter(new ReadFilter());
>> protocolChain.addFilter(new LogFilter());
>>
>
> Move the above 3 lines >>>
>
>
> ProtocolChainInstanceHandler pciHandler =
>> new ProtocolChainInstanceHandler() {
>>
>> final private ProtocolChain protocolChain = new
>> DefaultProtocolChain();
>>
>> public ProtocolChain poll() {
>> return protocolChain;
>> }
>>
>> public boolean offer(ProtocolChain instance) {
>> return true;
>> }
>> };
>> controller.setProtocolChainInstanceHandler(pciHandler);
>>
>
> >>> Here.
>
> I think it should works after.
>
> Thanks!
>
> -- Jeanfrancois
>
>
>
>> TCPSelectorHandler selectorHandler = new TCPSelectorHandler(true);
>> selectorHandler.setSelectionKeyHandler(new
>> DefaultSelectionKeyHandler());
>> controller.addSelectorHandler(selectorHandler);
>>
>> startController(controller);
>>
>> final TCPConnectorHandler tcpConnectorHandler =
>> (TCPConnectorHandler) controller.acquireConnectorHandler(
>> Controller.Protocol.TCP);
>>
>> final CountDownLatch anotherLatch = new CountDownLatch(1);
>> CallbackHandler<Context> callbackHandler = new
>> CallbackHandler<Context>() {
>> public void onConnect(IOEvent<Context> ioEvent) {
>> System.out.println("onConnect");
>> SelectionKey selectionKey =
>> ioEvent.attachment().getSelectionKey();
>> try {
>> tcpConnectorHandler.finishConnect(selectionKey);
>> System.out.println("Done finishconnect");
>> } catch (IOException e) {
>> System.out.println("onConnect: " + e.getMessage());
>> e.printStackTrace(System.out);
>> }
>> anotherLatch.countDown();
>>
>> ioEvent.attachment().getSelectorHandler().register(selectionKey,
>> SelectionKey.OP_READ);
>> }
>>
>> public void onRead(IOEvent<Context> ioEvent) {
>> Context context = ioEvent.attachment();
>> try {
>> context.getProtocolChain().execute(context);
>> } catch (Exception e) {
>> e.printStackTrace();
>> }
>> }
>>
>> /** {_at_inheritDoc} */
>> public void onWrite(IOEvent ioEvent) {
>> }
>> };
>> tcpConnectorHandler
>> .connect(new InetSocketAddress("localhost", 8080), new
>> DefaultCallbackHandler(tcpConnectorHandler));
>>
>> /* try {
>> anotherLatch.await();
>> } catch (InterruptedException e) {
>> e.printStackTrace();
>> }*/
>> byte[] bytes = "GET \"/index.html\" HTTP/1.0\n\n".getBytes();
>> long written = tcpConnectorHandler.write(ByteBuffer.wrap(bytes),
>> true);
>> if (written != bytes.length) {
>> System.out.println("Written " + written + ", supposed to: " +
>> bytes.length);
>> }
>> ByteBuffer buffer = ByteBuffer.allocateDirect(2048);
>> long l = tcpConnectorHandler.read(buffer, true);
>> System.out.println(l);
>> System.out.println(buffer.toString());
>> tcpConnectorHandler.close();
>> controller.stop();
>> }
>>
>> private static void startController(Controller controller) {
>> final CountDownLatch latch = new CountDownLatch(1);
>> controller.addStateListener(new ControllerStateListenerAdapter() {
>> @Override
>> public void onReady() {
>> System.out.println("Ready.");
>> latch.countDown();
>> }
>>
>> @Override
>> public void onException(Throwable e) {
>> if (latch.getCount() > 0) {
>> Controller.logger().log(Level.SEVERE, "Exception during
>> " +
>> "starting the controller", e);
>> latch.countDown();
>> } else {
>> Controller.logger().log(Level.SEVERE, "Exception during
>> " +
>> "controller processing", e);
>> }
>> }
>> });
>>
>> new WorkerThreadImpl("ControllerWorker", controller).start();
>>
>> try {
>> latch.await();
>> System.out.println("Controller started.");
>> } catch (InterruptedException ex) {
>> }
>>
>> if (!controller.isStarted()) {
>> throw new IllegalStateException("Controller is not started!");
>> }
>> }
>> }
>>
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>
>