My Launchpad‎ > ‎

Dats Network Library

Dats Network Library v1.2.9 (LGPL)

Requirements : You need at least Java Runtime Environment (JRE) 5 installed.

A very useful network library I wrote to take care of a lot of finicky details with getting applications to network together.

This caters for extremely lightweight peer-to-peer and server-client requirements. The message transport system used could probably use some work, but it is extremely light on network resources since each object has to encode itself into a binary representation of itself.

The library also features p2p automatic network discovery (using simple multicasting), so that you can find clients on the network easily.

Quick Instructions and Usage :

Download the binary distribution, unpack somewhere and double-click on the datsnet-1.2.9.jar. This will open up a little LANChat test application. Copy the binary distribution to another machine, repeat. You should now be able to chat to each other across the network!

One thing to note here - The lib folder is only used for the LANChat test client and are not datsnet-x.x.x.jar library dependencies!

Now, take a look at LANChat.java, Message.java and User.java. That's the entire example application! From a quick glance, you will notice that there is really not much network specific code happening here.


Let's take a closer look at Message.java and User.java, both these classes implement the TransportableObject interface. To send an object through the network using the datsnet library, you need to implement TransportableObject.

TransportableObject requires you to fill out 3 methods, namely :

getMaxVersion() - This is for message version control, it lets the framework and other connecting clients know which version of the message you are receiving you can handle.

writeObject(DataOutputStream out, int version) - This is a request from the framework to compile your object into the provided DataOutputStream, using the version number requested (backward compatibility with older clients).

readObject(DataInputStream in, int version) - This is a request from the framework to reconstruct your object from the provided DataInputStream, using the version number requested.


A word of warning: Be sure to read exactly the same amount of bytes as what you are writing in your writeObject() method. Reading even one byte out might result in the network framework not working properly.

That's all there is to constructing your network messages, with that done - let's look at what it takes to make a connection and actually have the clients talk to each other.
Setting up the P2PConnector:

All the setup happens in the LANChat constructor :
	public LANChat() {
1: conn = new P2PConnector("za.dats.lanchat", 10290, 10291, true);

2: conn.registerTransportable(User.class, 1);
3: conn.registerTransportable(Message.class, 2);

4: conn.addClientListener(this);
5: conn.addReceivedListener(this);
}
At line 1: The first argument to P2PConnector provides the framework with a signature with which to validate that messages are coming from clients who are meant to send to you, the second is the communications port (which can differ from client to client), the third is the broadcast port (which has to be identical for each client) and the last is simply a flag to say that you do want to broadcast to find other clients.

Line 2 and 3: Here you register your TransportableObject classes with the framework, assigning each class with a unique number that has to be constant throughout the lifespan of your application.

Line 4 and 5: Just registering to receive message and client events.

You'll notice that at the end of the start() method, conn.start() is called - this physically starts up the framework.
The event listener interfaces:

And the last thing you need to know about, TransportableReceivedListener and (to a lesser extent) AddressClientListener.

With TransportableReceivedListener you have a single method - receivedTransportable(AddressClient client, TransportableObject obj), you simply check the class of obj for the message type, cast it and get the meat of your messages. The client obviously tells you which client sent this message to you, so that you can conn.sendMessage(client, msg) back to them whenever you're ready.

The AddressClientListener just tells you when clients have connected and disconnected. I would advise using a message handshake for proper logging on and off, but this is a good fall-back for when connections get interrupted and so forth.


And that's it! You're set to go networking! You may be wondering why I haven't used a simple Serializable scheme in for messaging instead of using TransportableObject.
1) I wanted to know exactly what was being sent and received,
2) I needed to have the packets as small as possible,
3) Serializable objects are notorious for there version incompatibilities, I needed a more stable solution.

Another two classes you may want to look at instead of the P2PConnector is the ClientConnector and ServerConnector. They work in almost identical way as the P2PConnector, but are geared toward client/server communication instead.
ċ
LANChat.java
(5k)
Deon Moolman,
Mar 1, 2010, 8:49 PM
ċ
Message.java
(1k)
Deon Moolman,
Mar 1, 2010, 8:49 PM
ċ
User.java
(1k)
Deon Moolman,
Mar 1, 2010, 8:49 PM
ċ
datsnet-1.2.9-bin.zip
(274k)
Deon Moolman,
Mar 1, 2010, 8:48 PM
ċ
datsnet-1.2.9-src.zip
(257k)
Deon Moolman,
Mar 1, 2010, 8:48 PM
Comments