I’ve been talking about it for quite some time, and I think I got something that nicely wraps NIO into something workable.
You send and receive byte-arrays (!) not ByteBuffers. Because it is just a NIO-wrapper, you have full access to the SelectionKey instances, just in case you need more control.
The basic idea is this:
Network network = new Network(...);
SelectionKey server = network.createServer(port);
SelectionKey client = network.createClient(host, port);
// you can ignore these SelectionKeys if you wish
network.write(client, yourByteArray);
network.write(client, yourByteArray);
Event-handling is done via the listener pattern:
public interface ConnectionHandler
{
public void onConnected(Network net, SelectionKey key);
public void onExecute(Network net, SelectionKey key);
public void onReceivedTCP(Network net, SelectionKey key, byte[] data);
public void onReceivedUDP(Network net, SelectionKey key, byte[] data, InetSocketAddress source);
public void onSent(Network net, SelectionKey key, int byteCount);
public void onDisconnected(Network net, SelectionKey key, IOException cause);
}
http://213.247.55.3/~balk1242/html_stuff/ for a convenient listing of packages and classes, I added most of my utility classes, and the ‘relevant’ packages are near the bottom of the listing.
NIO Wrapper (implementation)
http://213.247.55.3/~balk1242/html_stuff/source/jawnae/net/Network.html
Examples:
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/EchoServer.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/EchoClient.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/PacketServer.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/PacketClient.html
I built a toy HTTP server with this wrapper, so I am reasonably confident about its stability.
Give it a whirl, tell me what you think!