Java 2 Networking
Chapter 11 Updates

Last Updated: 4 July 1999

This page contains links to the latest updates of the code and text to Chapter 11: The Java Shared Data Toolkit. This is currently a work in progress. I'm updating the code and text as I go. This is the text, the code should be following shortly.

  Return to top

Changes to v1.5

Chapter 11 introduces the main core of the JSDT API and the way it does things. As such, there isn't too much that needs to change. As the change to JSDT from v1.4 to v1.5 is fairly minor, we'll go through the changes fairly quickly. The main things to cover are what has changed in the core API set.

Making the API more Consistent

As you may have noticed in the book, one of my biggest bug-bears was the inconsistent nature of the APIs for creating different objects. This has been fixed with v1.5.

The main change that you should notice is that creating a ByteArray has the same method signature as the other two objects. The old method that required you to pass in an array of bytes (byte[]) has been marked as deprecated. There was no need for that array of bytes so now you can remove that argument.

Convenience Methods

There are a number of common uses for shared objects. As was illustrated in the book, you might want to serialise objects and pass them along the stream. With the latest version, a number of convenience methods have been added to do this for you. The orginal methods that returned arrays of bytes have now been made deprecated. In their place, are new methods describing what each does. For example, in the Data class, you now have:
  byte[] getDataAsBytes();
  String getDataAsString();
  Object getDataAsObject();
As the method names suggest, these return data according to the various types. There are also corresponding setter methods as well. Now you just need to make sure that whatever variant is used on the sender is also used on the receivers.

Note that when you pass in an object, that object must be serializable. If not, then JSDT will silently fail (bummer, need to change this!).

Another annoyance from earlier versions was determining whether one of the shared objects already had managers associated with them. Now the Session has a bunch of methods that return a boolean indicating if the named object was already managed. Prior to these, the only way to tell was to create the object and then call the isManaged() method on that object. If you were denied permission to join, then you were pretty well stuffed. You can also check if the session is managed by using SessionFactory.sessionManaged().

URL Construction

Beginning with v1.5, a new representation of the URL has been created. Instead of concatenating parts of your string together, you now have the URLString class. This class now represents everything you need to know about the URL. Also, that is now passed as the arguments to all of the Session and Client methods that require URLs.

Registry Management

For the earlier versions of JSDT, registry management was pretty trivial. You could basically check to see if one existed, and ask it to start one. With v1.5 the registry is part of the way through a large makeover to include much better functionality.

Included in this first cut of new functionality is better control. New methods allow you to start a registry on something other than the default port. Of course, you can also check for a registry already there too. A new feature is the ability to forcibly stop the registry using the stop() method. Again, like the other methods, this looks for the connection type and optionally the port number.

In forthcoming versions of JSDT, it is expected that you will be able to add a registry manager along the same lines as the session and shared object managers. The idea of this is to allow authentication of people attempting to add or rebind objects within the registry that are not allowed.

For those applications that need to monitor everything at the session level, a new listener for changes in the registry may also be added.

  Return to top

Code Changes

All of the line numbers in this section refer to the original line numbers in the code given on the CD.

Download the updated code

Client

There is only one class to change here: JSDTBankClient.java.

The following fixes are used to change the old java.lang.String URL statements with the new URLString class.

Delete line 32 and replace it with

  private static final String SESSION_TYPE = "socket";
  private static final String SESSION_NAME = "McDuckBank";
Delete lines 80-86 and replace with the following code:
      URLString url =
        URLString.createSessionURL(host, port, SESSION_TYPE, SESSION_NAME);
To bring the code up to date with the new ByteArray construction:

Remove old line 106 (new byte[]) as it is no longer needed.
Line 108 is now replaced with

  balance = session.createByteArray(this, name, true);
To deal with the different ways of retrieving information from shared objects we need to make the following change:

Line 272 now changes the method name to

      msg = balance.getValueAsBytes();

Server

For the server, the changes are a little more distributed. Three classes need changing.

BalanceOutput.java

To bring the code up to date with the new ByteArray construction:

Remove old line 53 (new byte[]) as it is no longer needed.
Line 54 is now replaced with

  shared_array = session.createByteArray(this, name, true);

NetworkServer.java

The following fixes are used to change the old java.lang.String URL statements with the new URLString class.

Replace line 26 with the following two lines:

  private static final String SESSION_TYPE = "socket";
  private static final String SESSION_NAME = "McDuckBank";
then change line 71 to:
  URLString url = URLString.createSessionURL("localhost",
                                             port,
                                             SESSION_TYPE,
                                             SESSION_NAME);

TransactionListener.java

To deal with the different ways of retrieving information from shared objects we need to make the following change:

Change line 87 to

  byte[] msg = data.getDataAsBytes();

  Return to top

[ Homepage ][ J2 Networking ][ Linux ][ Books ][ Java ][ About ]