Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 18 Jun 2003 11:18:51 -0500
From:      "Joseph Gleason" <clash@tasam.com>
To:        <java@freebsd.org>
Subject:   NIO Selector creation
Message-ID:  <001101c335b5$4ebd3000$19cf000a@frolickingmoose>

next in thread | raw e-mail | index | archive | help
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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?001101c335b5$4ebd3000$19cf000a>