Comments
rock333 wrote: At the IaaS Cloud layer virtualisation is going to be essential to allow the self service attributes, all painful and slow to do with physical hardware. Moving up the stack to PaaS and SaaS the use of virtualisation may, as you say, be less required if you put lots of smarts into your software. A lot of software does not have those smarts and by utalising virtualisation of the layers below can manipulate existing software architectures to have more cloudy attributes through automation (eg run load balancers and deploy more servers automagically). Over time, as new investment in software at...
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 . . .
    Integrated Windows Authentication (IWA) provides a user-friendly interface for single sign-on. IWA uses ‘Simple and Protected GSSAPI Negotiation Mechanism’ (SPNEGO) to allow the initiators and acceptors to negotiate the underlying protocol to be used for authentication. In this article...
    Preternaturally quiet since a hedge fund offered to buy it two weeks ago and take it private, Novell stated on Wednesday that the open source Ingres database is available in the free SUSE Studio as part of the SUSE Appliance Program. Novell and Ingres are supposed to jointly support an...
    Cloud Computing Journal caught up with the CEO of a major new player in the fast-emerging Cloud ecosystem - a CEO who has taken an interesting and unusual decision. While signing up as the Platinum Plus Sponsor of the 5th International Cloud Expo, he and his company have decided to rem...
    Open-Xchange, a provider of business-class open source collaboration software, today announced enhancements that give users telephone and fax integrated with e-mail, contacts, calendar and task information. By combining Open-Xchange (hosted and on-premise editions) with Unified Commun...
    Home Energy monitoring products maker People Power has come out with an open source hardware and software application developer kit called SuRF that lets embedded systems developers build energy saving apps for household electronics and devices on top of its Open Source Home Area Netwo...
    Novell and Ingres Corporation on Wednesday announced the Ingres database is available within SUSE Studio as part of the SUSE Appliance Program. Both companies have entered into a cooperative agreement to make it easier and more cost-effective for independent software vendors (ISVs) and...
    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