Title: RE: [Juglist] java.nio can't be used to implement high-performance SSL server?

> -----Original Message-----
> From: Christopher L Merrill [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, November 11, 2003 11:26 AM
> To: Research Triangle Java User's Group mailing list.
> Subject: Re: [Juglist] java.nio can't be used to implement
> high-performance SSL server?
>
>
> Nascif Abousalh-Neto wrote:
> > 1) It looks like Sun will indeed solve this oversight on 1.5 (they
> > seem
> > to be taking a lot of heat for this problem);
>
> I am hopeful...I found a JBD entry concerning this issue:
>    http://developer.java.sun.com/developer/bugParade/bugs/4495742.html
> It says it's 'fixed'...but I can not tell what the solution
> is...<sigh>
>
> > 2) You can "wrap" an SSL socket in two PipeChannels. Yes
> each one will
> > still consume two threads, but they are now Selectable, and
> you can take
> > advantage of that in your design and have an easier
> migration path in
> > the next JDK release.
>
> Interesting idea.  Looks like it should work (in theory)...have you
> actually implemented this?   Or do you know anyone who has?

Attached. Ron Hitchens (author of the O'Reilly book "Java NIO", which I ***highly*** recommend) was
kind enough to send me this code sample some weeks ago.

Regards,
        Nascif

>
> --
> --------------------------------------------------------------
> -----------
> Chris Merrill                      |  http://webperformanceinc.com
> Web Performance Inc.               | 
> http://webperformancemonitoring.net
>
> Website Load Testing,
> Stress Testing, and Performance Monitoring Software
> --------------------------------------------------------------
> -----------
>
>
> _______________________________________________
> Juglist mailing list
> [EMAIL PROTECTED]
> http://trijug.org/mailman/listinfo/juglist_tri> jug.org
>

 

package com.ronsoft.books.nio.channels;

import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
import java.nio.channels.WritableByteChannel;
import java.nio.channels.SelectableChannel;
import java.io.InputStream;
import java.io.IOException;

/**
 * Class which encapsulates System.in as a selectable channel.
 * Instantiate this class, call start() on it to run the background
 * draining thread, then call getStdinChannel() to get a SelectableChannel
 * object which can be used with a Selector object.
 *
 * @author Ron Hitchens ([EMAIL PROTECTED])
 * created: Jan 2003
 */
public class SystemInPipe
{
        Pipe pipe;
        CopyThread copyThread;

        public SystemInPipe (InputStream in)
                throws IOException
        {
                pipe = Pipe.open();

                copyThread = new CopyThread (in, pipe.sink());
        }

        public SystemInPipe()
                throws IOException
        {
                this (System.in);
        }

        public void start()
        {
                copyThread.start();
        }

        public SelectableChannel getStdinChannel()
                throws IOException
        {
                SelectableChannel channel = pipe.source();

                channel.configureBlocking (false);

                return (channel);
        }

        protected void finalize()
        {
                copyThread.shutdown();
        }

        // ---------------------------------------------------

        public static class CopyThread extends Thread
        {
                boolean keepRunning = true;
                byte [] bytes = new byte [128];
                ByteBuffer buffer = ByteBuffer.wrap (bytes);
                InputStream in;
                WritableByteChannel out;

                CopyThread (InputStream in, WritableByteChannel out)
                {
                        this.in = in;
                        this.out = out;
                        this.setDaemon (true);
                }

                public void shutdown()
                {
                        keepRunning = false;
                        this.interrupt();
                }

                public void run()
                {
                        // this could be improved

                        try {
                                while (keepRunning) {
                                        int count = in.read (bytes);

                                        if (count < 0) {
                                                break;
                                        }

                                        buffer.clear().limit (count);

                                        out.write (buffer);
                                }

                                out.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }
}
_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to