Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Networking our whiteboard with Java 1.1

Find out how easy it is to network our whiteboard with servers, sockets, and RMI

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Our world grows smaller with each passing day. Corporate alliances span the globe, and, as businesses move into increasingly remote locations, a growing number of working stiffs commute via telnet. In the past, coordinating the activities of such globally de-centralized communities was a challenge to say the least, but with the advent of the Internet and the opportunities it's given rise to, all that has changed. With utilities like our whiteboard (fully ramped-up with the networking portion of the app), interaction among users in different cities (or even different countries) is a snap.

We developed the graphical aspects of the whiteboard last month (shown below), so now we're ready to add the networking features to the application. We are going to accomplish this by networking the ObservableList class. As we discussed last month, this class creates a list of the displayed elements of the whiteboard. If you need a refresher, take this opportunity to reread part 1.

The graphical whiteboard

Our whiteboard tools manipulate the whiteboard by altering the list of elements. When changes are made to the list, all interested listeners are notified. Listeners, such as the display area, can then automatically update themselves to reflect these changes.

To enable networking, we will simply replace ObservableList with a distributed ObservableList, which ensures changes are propagated over the Internet. The distributed list presents exactly the same API as the original local list and so requires no changes to (or knowledge of) any parts of the whiteboard.

In the course of this article, we will look at two different approaches to networking -- socket-based and RMI-based -- and examine the pros and cons of each approach.

The new list class

Our first step is to implement a new list class called an IDList. This class is essentially a combination of the traditional Hashtable and Vector classes; it is an ordered list (Vector) of identifier-element pairs (Hashtable). As objects are added to the IDList, they are automatically assigned an associated identifier. The list can then be manipulated by addressing the elements by their identifiers (instead of the traditional mode of addressing the elements by their index number, which can change if any element is removed or inserted).

The IDList class

When we network the IDList class, the element identifiers will be distributed to all connected whiteboards. The whiteboards can then communicate coherently about elements of the list through the use of their identifiers.

Class IDList
The interface that class IDList presents is fairly rudimentary; however, it is sufficient to serve our purposes. Let's quickly review the methods available to this class:

  • public Object addElement (Object element)

    This method adds an element to the list, assigns it a VM-unique identifier (based on the VM hashcode of the element) and returns this identifier.

    The effects of replaceElement()



  • public Object replaceElement (Object oldID, Object element)

    This method replaces the old element identified by oldID with the new element element. The new element is added to the end of the list and the identifier that it is allocated is returned. If the specified old element was not present in the list, this method returns null without adding the new element.

    • Digg
    • Reddit
    • SlashDot
    • Stumble
    • del.icio.us
    • Technorati
    • dzone
    Comments (1)
    Login
    Forgot your account info?

    weffeBy Anonymous on August 29, 2009, 4:14 pmewfef ew f fe fe

    Reply | Read entire comment

    View all comments

    Add comment
    Anonymous comments subject to approval. Register here for member benefits.
    Have a JavaWorld account? Log in here. Register now for a free account.
    Resources
    • Download this article and the complete source code a gzipped tar file http://www.javaworld.com/javaworld/jw-12-1997/step/jw-12-step.tar.gz
    • Download this article and the complete source code a zip file http://www.javaworld.com/javaworld/jw-12-1997/step/jw-12-step.zip
    • Read my article "Stepping through a site navigator applet" (JavaWorld - January 1997), which provides a primer on sockets http://www.javaworld.com/javaworld/jw-01-1997/jw-01-chat.html
    • Object serialization specification http://www.javasoft.com/products/jdk/rmi/doc/serial-spec/serialTOC.doc.html
    • Learn more about RMI and Java distributed computing http://www.javasoft.com/features/1997/nov/rmi.html
    • JavaSoft's RMI Web page http://www.javasoft.com/products/jdk/rmi/
    • Previous Step by Step articles