NIO Wrapper (TCP / UDP)

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!

looks lovely! - I’ve always hated nio for being too technical to use - this cleans it up quite a bit!

It does look great. Nice work Riven. What license is applied?

Kev

Really nice, thanks for sharing!
I’d be interested to know about the license too.

I don’t know about the license.
I don’t care about the license.

As long as you don’t sue me, or say you wrote it.

(Does that mean BSD?)

Edit

Oh, and the code is under construction, so… it will change (for the better) soon.

Just one question: I put TCP and UDP in the same class (Network)

// for TCP
network.write(selectionKey, byte[] data); // packets will be merged / split for you

// for UDP
network.write(selectionKey, byte[] data, InetSocketAddress target); // packets will be sent ‘as is’.

You receive them in The ConnectionHandler using:
handler.onReceivedTCP(selectionKey, byte[] data); // needs to be split/merged
handler.onReceivedUDP(selectionKey, byte[] data, InetSocketAddres source); // packets are received ‘as is’

It’s all nice and will, but it’s a bit messy.

Any suggestions? What needs to be worked on?

yup, basically.

Cleaned up TCP examples…!

Now using these classes

  • jawnae.net.Network
  • jawnae.net.NetworkHandler
  • jawnae.net.NetworkHandlerProvider
    —> jawnae.net.TCPHandler (implements NetworkHandler, removes boilerplate for TCP)
    —> jawnae.net.UDPHandler (implements NetworkHandler, removes boilerplate for UDP)
    source: http://213.247.55.3/~balk1242/html_stuff/

UDP examples:
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/NibbleServer.html
http://213.247.55.3/~balk1242/html_stuff/source/test/jawnae/net/NibbleClient.html

Nice. How about a zip that contains all the required files and examples/test for this? It’s a bit awkward to copy all you need to use it from that html client.

Fair enough…

The JARs are at the bottom of the listing now, containing both *.java and *.class files.

http://213.247.55.3/~balk1242/html_stuff/jars/base.jar
http://213.247.55.3/~balk1242/html_stuff/jars/components.jar
http://213.247.55.3/~balk1242/html_stuff/jars/jawnae.jar

Thank you very much. It’s much more convenient that way.

Needless to say, I’d love to get some feedback after you (all) played with it :slight_smile:

I am not trying to rain on your parade Riven.

Have any of you checked out: http://mina.apache.org/

On Mina’s Homepage:

[quote]Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
[/quote]
I haven’t put forth the time to check it out, but it seems like a pretty nice library.

Ofcourse. But MINA does stuff under the hood (resolving IPs on connecting) that
trigger SecurityExceptions when the default (applet) SecurityManager is installed.

Besides that, my API is extremely light weight.

Oh awesome. I think I might put this to use very soon.

Thanks for sharing this!

Some months ago I have been looking for a solid and easy to use NIO-based networking solution and I took a look at MINA, Grizzly and xsocket. I selected xsocket (http://xsocket.sourceforge.net/) and have been developing a multiplayer game server on it for the last months - it offers a straight-forward API and I’m quite happy with it… Have you checked this one? How does it compare to your project?

Many thanks again for sharing the knowledge!

@Mina and a little OT:
There is also the JBoss Netty project (which is by the same author as Mina, the origins are very Soap-Opera-ish).

Maybe we can split off this section into another topic?