Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Direct Applet to Applet Communication with RMI
Direct Applet to Applet Communication with RMI

It's widely known that an applet isn't allowed to create a network connection to a computer that's not the one from which the applet itself was loaded. This has led to the idea that two applets aren't allowed to communicate directly with each other unless they're located on the same host. This article provides a brief overview of Java's Remote Method Invocation (RMI), describes a small conferencing program and shows how direct applet-to-applet communication can be established with RMI.

RMI's Overview
Three approaches for programming distributed systems can be identified. The first one consists of using a low-level API such as the java.net package. The second one uses middleware such as OMG's CORBA. The third one consists of using a high level API such as RMI.

In a nutshell, the advantages of using RMI over other approaches are:

  • Transparency: From the programmer's perspective, remote objects and methods are somewhat like local objects and methods; the only differences are that remote classes must implement the java.rmi.Remote interface and remote methods must throw the java.rmi.RemoteException exception.
  • Integration: As RMI is a standard component of Java, it supports distributed programming in a seamless manner with the other aspects of the language.
  • Automatic object serialization: Java provides the facility to transfer data not only among hosts, but also classes and objects.
  • Automatic class loading: If an object is of a class that is not available on the given host, the class is dynamically loaded at runtime.
  • Distributed garbage collection: As objects are moved among hosts, RMI keeps track of which remote objects are no longer referenced by clients and deletes them automatically.

    The ability to pass objects among hosts makes it extremely easy to program applications in which some classes are both clients and servers. For example, a method of a remote class R1 can be called with a parameter being a remote class R2. The remote class R1 will then be able to call any of the methods of the remote class R2.

    A Conferencing Program
    In this section I'll walk you through the complete code of a small conferencing program and show how it takes advantage of the facilities provided by RMI to achieve direct applet-to-applet communication. The program consists of two basic remote objects (implementing the "java.rmi.Remote" interface):

  • A server that accepts requests from clients who wish to participate in a discussion, and requests to identify which clients are already active
  • An applet that participates both as a client of the server and as the entity notified when other applets are ready to communicate (thus the applet is also a server)

    To explain briefly, each applet registers its reference in the server, which broadcasts these references back to all other applets. All applets now have a reference to all other applets, which enables them to communicate directly without having to access the server. (To prove this, the system allows an applet to shut down the server.) Each applet displays the list of other available applets, allows the user of the system to send messages to other users and displays incoming messages.

    Defining the Remote Interfaces
    The first step in writing an applet or an application using RMI is to define the remote interfaces to the remote objects in the application. A remote interface extends the interface java.rmi.Remote and declares all the methods that may be invoked remotely. The two remote interfaces of the conferencing program are the Talker and TalkerServer interfaces (see Listings 1 and 2). The Talker interface is used by the applet to receive notifications from the server and messages from other applets while the TalkerServer is used by the server to receive commands from the applet. The Talker interface is implemented by the TalkerImpl class and the TalkerServer interface is implemented by the TalkerServerImpl class.

    Creating the Remote Server
    The next step is to define the TalkerServerImpl class that extends java.rmi.UnicastRemoteObject. UnicastRemoteObject provides support for point-to-point object references using TCP streams. The TalkerServerImpl class (see Listing 3) has a constructor, a main method and an implementation for the methods declared in the TalkerServer interface. The main method starts the remote server process, installs a security manager, creates a registry on port 2006, creates a TalkerServer remote object and finally makes this object available via the registry. The implementation of all the methods is trivial. Register adds a client to a private vector and notifies every registered client that a new member has joined the group. UnRegister notifies every client that one of them has left the session and removes it from the vector. Lookup returns a copy of the vector, and shutdown terminates the server. Although these methods are remote, they differ from local methods only in throwing RemoteException.

    Implementing the Talker Interface
    TalkerImpl (see Listing 4) implements the Talker interface. The constructor saves references of the applet and the current user's name. The applet is used to update the user interface; the name is used by other users of the system. Remote users call the getName method to identify the current user and call the recvMsg method to send him messages. The add and remove methods are called by the server to update the system's list of users.

    Creating the User Interface
    The user interface (see Listing 5) consists of the following components displayed in an applet (Figure 1):

  • A TextField to enter the name of the user
  • A List to show the names of remote users
  • A TextArea to display the sent messages
  • A TextArea to display the received message
  • Three buttons to start participating in the system, stop participating and shut down the server, respectively

    Although the code of the applet is longer than the code of the other classes, it is fairly straightforward. The init method initializes a connection with the server and contains the code necessary to lay down the components within the applet. An ActionListener is added to each button. StartListener initializes the list of persons already connected and registers the current user in the server; StopListener unregisters the user from the server. ShutdownListener allows the user to shut down the server to prove that once connections among users are established, the server is not needed anymore. A KeyListener is also added to the TextArea handling the messages to be sent. The initList, addTalker and removeTalker methods update the list of users. The addToMsg method displays an incoming message.

    Installing the System
    This system is based on an applet accessing a server and other applets with RMI. Therefore, it will work only with a browser supporting all of the features of JDK1.1. For this experiment I used HotJava running on several PCs while the server was running on a UNIX machine. The system is easy to configure; the only requirement is that the server must be located on the same host as the HTTP daemon. The HTML file is shown here:

    <HTML>
    <TITLE>Talk
    <APPLET code="TalkerApplet.class" width=550 height=450>
    </APPLET>
    </HTML>

    Conclusion
    In this article, I have shown how to take advantage of the facilities provided by RMI to pass remote objects in remote calls to allow both bidirectional communication between the applets and the server and among the applets themselves. This short example also demonstrates that direct applet-to-applet communication is, in fact, possible.

    About Pascal Ledru
    Pascal Ledru is a software engineer specializing in networking applications at Netran Interactive Commerce. He is also working on his Ph.D. in computer science at the University of Alabama in Huntsville.

  • In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1

    Enterprise Open Source Magazine Latest Stories . . .
    With Cloud Expo 2012 New York (10th Cloud Expo) just four months away, what better time to start introducing you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference... We have technical and st...
    AMD said late Tuesday that its chief sales officer Emilio Ghilardi had left the company and that CEO and president Rory Read is going to do his job while a replacement is sought. AMD didn’t say why Ghilardi left but it’s assumed Read wants his own people. Read is relatively new to th...
    During the lifespan of M3 (Monitis Monitor Manager) there has always been something lacking – timers. M3 execution procedure was outlined in this previous article. The execution mentioned in the latter was a one-time-execution, whereas server monitoring requires periodic invocati...
    Red Hat is putting its bought-in Gluster scale-out NAS storage technology, acquired in October, on the Amazon cloud. It’s styled Red Hat Virtual Storage Appliance for Amazon Web Services and other clouds are supposed to follow in short order.
    A new episode of the screencast series is now available at the OpenNebula YouTube Channel. This screencast demonstrates the new easily-customizable self-service portal for cloud consumers. Its aim is to offer a simplified access to shared infrastructure for non-IT end users. The scree...
    C12G Labs has just announced an update release of OpenNebulaPro, the enterprise edition of the OpenNebula Toolkit. OpenNebula 3.2, released two weeks ago, brings important benefits to cloud providers with a new easily-customizable self-service portal for cloud consumers, and builders w...
    Subscribe to the World's Most Powerful Newsletters
    Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


    SYS-CON Featured Whitepapers
    ADS BY GOOGLE