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
[-- Attachment #1 --]
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
[-- Attachment #2 --]
// $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=new HTTPKick(Args);
}
HTTPKick(String Args[]) throws Exception
{
DoneOK=0;
DoneBad=0;
Host=Args[0];
Path=Args[1];
Port=new Integer(Args[2]).intValue();
Size=new Integer(Args[3]).intValue();
Count=new Integer(Args[4]).intValue();
String Request=new 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=false;
HTTPSelector = java.nio.channels.spi.SelectorProvider.provider().openSelector();
Connector C=new Connector();
C.start();
long D=200;
while ((!ConnsOpen) || (HTTPSelector.keys().size() > 0))
{
while (HTTPSelector.select(D) > 0)
{
Set SelectedKeys=HTTPSelector.selectedKeys();
Iterator I=SelectedKeys.iterator();
while (I.hasNext())
{
SelectionKey SK=(SelectionKey)I.next();
I.remove();
SocketChannel Chan=(SocketChannel)SK.channel();
HTTPSession HS=(HTTPSession)SK.attachment();
//System.out.print(HS.Name);
int Ops=SK.readyOps();
//System.out.print(" " + Ops);
if (Ops >=SK.OP_ACCEPT)
{
//System.out.print(" + ACCEPT" );
Ops=Ops-SK.OP_ACCEPT;
}
if (Ops >=SK.OP_CONNECT)
{
//System.out.print(" + CONNECT" );
Ops=Ops-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 >=SK.OP_WRITE)
{
//System.out.print(" + WRITE" );
Ops=Ops-SK.OP_WRITE;
ByteBuffer BB=ByteBuffer.allocate(10000);
//ByteBuffer BB=ByteBuffer.wrap(Request.getBytes());
BB.put(Request.getBytes(),0,Request.length());
BB.flip();
//System.out.println(" r=" + BB.remaining() +".");
Chan.write(BB);
SK.interestOps(SK.OP_READ);
//I.remove();
//PrintWriter PS=new PrintWriter(Chan.socket().getOutputStream(),true);
//String S=new String("GET /fireduck/index.html");
//PS.println(S);
}
if (Ops >=SK.OP_READ)
{
//System.out.print(" + READ" );
Ops=Ops-SK.OP_READ;
//Chan.socket().shutdownOutput();
Chan.read(HS.Read);
//System.out.print(SK.interestOps());
//System.out.print(" R=" + 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()==0)
{
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=ByteBuffer.allocate(Size);
}
public String Name;
ByteBuffer Read;
}
class Connector extends Thread
{
Connector()
{
}
public void run()
{
System.out.println("Connecting");
try
{
SocketAddress SA=new InetSocketAddress(Host,Port);
for (int i=0; i<Count; i++)
{
SocketChannel SC=SocketChannel.open();
SC.configureBlocking(false);
SC.connect(SA);
HTTPSession HS=new HTTPSession();
HS.Name=new 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=true;
}
}
}
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?001101c335b5$4ebd3000$19cf000a>
