NIO Selector creation

NIO Selector creation

Post by Joseph Gleaso » Fri, 20 Jun 2003 01:18:50



This is a multi-part message in MIME format.

------=_NextPart_000_000E_01C3358B.656CEEF0
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I am having trouble using NIO under FreeBSD.  I am wondering if it is
because of something I am doing wrong or a problem with the port.

I am using 4.8-STABLE (May 21) and jdk-1.4.1p3_3

I have this line:
HTTPSelector =
java.nio.channels.spi.SelectorProvider.provider().openSelector();

This works with jdk 1.4.1 under WinXP.

Under FreeBSD I get:
root@tesla# java cc.glsn.test.HTTPKick fireduck.com /index.html 80 10 100
Exception in thread "main" java.lang.UnsatisfiedLinkError: init
        at sun.nio.ch.DevPollArrayWrapper.init(Native Method)
        at
sun.nio.ch.DevPollArrayWrapper.<init>(DevPollArrayWrapper.java:59)
        at
sun.nio.ch.DevPollSelectorImpl.<init>(DevPollSelectorImpl.java:54)
        at
sun.nio.ch.DevPollSelectorProvider.openSelector(DevPollSelectorProvider.java
:18)
        at cc.glsn.test.HTTPKick.<init>(HTTPKick.java:63)
        at cc.glsn.test.HTTPKick.main(HTTPKick.java:38)

I am just learning how to use NIO, so I certainly could be doing something
wrong.

My code is attached if anyone wants to look at it, but I suspect the problem
can be replicated by others with just that one line.  I swear my code is
usally neater than this...I am just messing around to learn NIO.

My thanks to anyone who might be able to help me with me problem.

--Joe Gleason

------=_NextPart_000_000E_01C3358B.656CEEF0
Content-Type: application/octet-stream;
        name="HTTPKick.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
        filename="HTTPKick.java"

// $gleason: test/HTTPKick.java,v 1.2 2003/06/18 16:13:00 clash Exp $

package cc.glsn.test;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.SelectionKey;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Set;
import java.util.Iterator;

/** Attempt at learning how to use NIO by creating a HTTP load testing
utility. */
class HTTPKick
{

    String Host;
    String Path;
    int Port;
    int Size;
    int Count;
    int DoneOK;
    int DoneBad;

    Selector HTTPSelector;

    boolean ConnsOpen;

    public static void main(String Args[]) throws Exception
    {
        HTTPKick HK=3Dnew HTTPKick(Args);

    }

    HTTPKick(String Args[]) throws Exception
    {

        DoneOK=3D0;
        DoneBad=3D0;

        Host=3DArgs[0];
        Path=3DArgs[1];
        Port=3Dnew Integer(Args[2]).intValue();

        Size=3Dnew Integer(Args[3]).intValue();
        Count=3Dnew Integer(Args[4]).intValue();

        String Request=3Dnew String("GET " + Path + " HTTP/1.1\r\nHost: =
" + Host + "\r\nUser-Agent: kick cc.glsn.HTTPKick/0.5\r\nConnection: =
close\r\n\r\n");

        ConnsOpen=3Dfalse;

        HTTPSelector =3D =
java.nio.channels.spi.SelectorProvider.provider().openSelector();

        Connector C=3Dnew Connector();
        C.start();

        long D=3D200;

        while ((!ConnsOpen) || (HTTPSelector.keys().size() > 0))
        {

            while (HTTPSelector.select(D) > 0)
            {
                Set SelectedKeys=3DHTTPSelector.selectedKeys();

                Iterator I=3DSelectedKeys.iterator();
                while (I.hasNext())
                {
                    SelectionKey SK=3D(SelectionKey)I.next();
                    I.remove();

                    SocketChannel Chan=3D(SocketChannel)SK.channel();

                    HTTPSession HS=3D(HTTPSession)SK.attachment();
                    //System.out.print(HS.Name);

                    int Ops=3DSK.readyOps();
                    //System.out.print(" " + Ops);
                    if (Ops >=3DSK.OP_ACCEPT)
                    {
                        //System.out.print(" + ACCEPT" );
                        Ops=3DOps-SK.OP_ACCEPT;
                    }
                    if (Ops >=3DSK.OP_CONNECT)
                    {
                        //System.out.print(" + CONNECT" );
                        Ops=3DOps-SK.OP_CONNECT;

                        SK.interestOps(SK.OP_WRITE);

                        try
                        {

                            Chan.finishConnect();
                        }
                        catch(Exception e)
                        {
                            System.out.println(HS.Name + " Connection =
failed");
                            e.printStackTrace();
                            DoneBad++;
                        }
                    }
                    if (Ops >=3DSK.OP_WRITE)
                    {
                        //System.out.print(" + WRITE" );
                        Ops=3DOps-SK.OP_WRITE;

                        ByteBuffer BB=3DByteBuffer.allocate(10000);

                        //ByteBuffer =
BB=3DByteBuffer.wrap(Request.getBytes());
                        BB.put(Request.getBytes(),0,Request.length());
                        BB.flip();
                        //System.out.println(" r=3D" + BB.remaining() =
+".");
                        Chan.write(BB);

                        SK.interestOps(SK.OP_READ);

                        //I.remove();

                        //PrintWriter PS=3Dnew =
PrintWriter(Chan.socket().getOutputStream(),true);
                        //String S=3Dnew String("GET =
/fireduck/index.html");
                        //PS.println(S);

                    }
                    if (Ops >=3DSK.OP_READ)
                    {
                        //System.out.print(" + READ" );
                        Ops=3DOps-SK.OP_READ;

                        //Chan.socket().shutdownOutput();
                        Chan.read(HS.Read);

                        //System.out.print(SK.interestOps());
                        //System.out.print(" R=3D" + =
HS.Read.remaining());

                        //System.out.print(" " + =
Chan.socket().isConnected());
                        //System.out.print(" " + =
Chan.socket().isInputShutdown());
                        //System.out.print(" " + =
Chan.socket().isOutputShutdown());
                        //System.out.print(" " + =
Chan.isConnectionPending());
                        //SK.interestOps(SelectionKey.OP_READ + =
SelectionKey.OP_CONNECT + SelectionKey.OP_WRITE);

                        if (HS.Read.remaining()=3D=3D0)
                        {
                            Chan.socket().close();
                            SK.cancel();
                            System.out.println(HS.Name + " done.");
                            DoneOK++;
                        }

                    }

                    //System.out.println();

                }

            }

        }

        System.out.println("Total: " + (DoneOK + DoneBad));

        System.out.println("OK: " + DoneOK);
        System.out.println("BAD: " + DoneBad);

    }

    class HTTPSession
    {

        HTTPSession()
        {
            Read=3DByteBuffer.allocate(Size);

        }
        public String Name;
        ByteBuffer Read;

    }

    class Connector extends Thread
    {
        Connector()
        {

        }

        public void run()
        {
            System.out.println("Connecting");
            try
            {
                SocketAddress SA=3Dnew InetSocketAddress(Host,Port);

                for (int i=3D0; i<Count; i++)
                {

                    SocketChannel SC=3DSocketChannel.open();
                    SC.configureBlocking(false);
                    SC.connect(SA);

                    HTTPSession HS=3Dnew HTTPSession();
                    HS.Name=3Dnew String("Connection." + i);

                    SC.register(HTTPSelector,SelectionKey.OP_READ + =
SelectionKey.OP_CONNECT + SelectionKey.OP_WRITE,HS);

                }

            }
            catch(Exception e)
            { e.printStackTrace(); }

            System.out.println("Done making connections");

            ConnsOpen=3Dtrue;

        }

    }

}

------=_NextPart_000_000E_01C3358B.656CEEF0
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
freebsd-j...@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-java
To unsubscribe, send any mail to "freebsd-java-unsubscr...@freebsd.org"

------=_NextPart_000_000E_01C3358B.656CEEF0--

 
 
 

NIO Selector creation

Post by Antony T Curt » Fri, 20 Jun 2003 06:44:20


=2D----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Quote:> I am having trouble using NIO under FreeBSD.  I am wondering if it is
> because of something I am doing wrong or a problem with the port.

NIO is broken on FreeBSD... I did hack together a fix which allowed OpenNMS=
 to=20
work (which requires NIO)=20

Unfortunately, the box which I hacked the code on has been recycled and I=20
didn't keep the work... :(

I hope to have some time to redo it properly, like use kqueue (which is new=
 to=20
me so I'll have to do some more playing)

=2D --=20
Antony T Curtis BSc     Unix Analyst Programmer
http://homepage.ntlworld.com/antony.t.curtis/
=2D----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQE+8N0iql7dp2cddmIRArJOAJ9wzsanuCefIASG9mUySvlagP1f6wCgmKX4
vU9MlTvaJLH2Vx+4/RPyiz0=3D
=3Dg/QY
=2D----END PGP SIGNATURE-----

_______________________________________________

http://lists.freebsd.org/mailman/listinfo/freebsd-java


 
 
 

NIO Selector creation

Post by Alexey Zelk » Fri, 20 Jun 2003 07:22:24


hi,


> > I am having trouble using NIO under FreeBSD.  I am wondering if it is
> > because of something I am doing wrong or a problem with the port.

> NIO is broken on FreeBSD... I did hack together a fix which allowed OpenNMS to
> work (which requires NIO)

It is _NOT_ broken.  There're some issues, but nothing prevent you from using
NIO on FreeBSD.

According to exactly this problem -- it is _NOT_ FreeBSD specific problem.
And is fixed already in main repository.

Quote:> Unfortunately, the box which I hacked the code on has been recycled and I
> didn't keep the work... :(

> I hope to have some time to redo it properly, like use kqueue (which is new to
> me so I'll have to do some more playing)

_______________________________________________

http://lists.freebsd.org/mailman/listinfo/freebsd-java