users@grizzly.java.net

Re: Simple input manipulation

From: Wayne Gemmell <wayne_at_flashmedia.co.za>
Date: Tue, 20 May 2008 14:39:23 +0200

On Tuesday 20 May 2008 14:03:26 Oleksiy Stashok wrote:
> can you provide complete source of your Filter? As here I don't see  
> how you get the buffer :)
K, I've put it below. Its really just Jeanfrancois's echo server in 20 lines
that I'm hacking with.

I access this service with the command "echo hello |nc dude 8013"
>
> > I'm doing this bit by bit and I have a wierd problem with reading  
> > the buffer
> > into a string.
> > Heres a code snippet.
> > <code>
> > SelectableChannel channel = ctx.getSelectionKey().channel();
> >            try {
> >
> >                CharBuffer cbuf = buffer.asCharBuffer();
> >                while(cbuf.hasRemaining())
> >                    System.out.print(cbuf.get());
>
> Does this print what you need?
This prints 3 squares, 1 on each line in debug (Netbeans) and 3 on the same
line in normal run time.
>
> >               cbuf.flip();
> >                String tmp = cbuf.toString();
> >                System.out.println(tmp);
>
> And blank squares are printed just here?
This prints the other 3 squares in the same line.

>
> >               OutputWriter.flushChannel(channel, buffer);
This performs as expected. hello is echoes on the console.

> >                channel.close();
> >            }
> > </code>
> >
>
> Not sure. Think CharBuffer.toString() should work properly.

<code>
private static void startListener() throws IOException {
        Controller controller = new Controller();
        TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler();
        tcpSelectorHandler.setPort(PORT);
        controller.addSelectorHandler(tcpSelectorHandler);
         System.out.println("Adding chains");
         
         
        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);
        //ProtocolChain protocolChain = new DefaultProtocolChain();
        ProtocolChain protocolChain = pciHandler.poll();
        protocolChain.addFilter(new ReadFilter());
        protocolChain.addFilter(new InputFilter());
        
        //protocolChain.addFilter(new EchoFilter());
        System.out.println("Done");
        controller.start();
    }

public class InputFilter implements ProtocolFilter {

    //public ProtocolParser newProtocolParser() {
    // return new MyProtocolParser());
    //}
    
    public boolean execute(Context ctx) throws IOException {
        final WorkerThread workerThread =
((WorkerThread)Thread.currentThread());
        ByteBuffer buffer = workerThread.getByteBuffer();
        buffer.flip();
        if (buffer.hasRemaining()) {
            // Depending on protocol perform echo
            SelectableChannel channel = ctx.getSelectionKey().channel();
            try {
                
                CharBuffer cbuf = buffer.asCharBuffer();
                while(cbuf.hasRemaining())
                    System.out.print(cbuf.get());
                cbuf.flip();
                String tmp = cbuf.toString();
                System.out.println(tmp);
                OutputWriter.flushChannel(channel, buffer);
                    
            } catch (IOException ex) {
                // Store incoming data in byte[]
                byte[] data = new byte[buffer.remaining()];
                int position = buffer.position();
                buffer.get(data);
                buffer.position(position);
                throw ex;
            }
            channel.close();
        }
        
        buffer.clear();
        
        return false;
    }

    public boolean postExecute(Context ctx) throws IOException {
        return false;
    }

}
</code>
-- 
Regards
Wayne