advertisement

Search Tips
Articles  |   Tutorials  |   Reviews  |   Dev Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source Code  |  

Browse DevX
DevX Updates - Sign Up Here


Partners & Affiliates












advertisement

Articles : Advanced Topics in Java :
Bi-Directional Communication in Distributed Remote Objects :

Contents
Introduction to Remote Method Invocation
The Problem Definition
Creating the Remote Interface
Implementing the Interfaces
The Server Starter
The Client Starter
Running the Application

ClientStarter - the Applet

The applet on the client, (ClientStarter) contains a text field and a button for entering the message to transmit to other clients. Another text field contains the IP listing of presently registered Clients on the server.

The applet init is like this:

public void init(){
  try{
    LocateRegistry.createRegistry(1099);
    client =new PopupClientImpl();
    Naming.rebind("/ClientPopup",client);
    System.out.println("Client bound");

    server =(PopupServerInterface)Naming.lookup("rmi://"
      +getCodeBase().getHost()+":5000/ServerPop");
    server.addUser((PopupClientInterface)client);
    System.out.println("Client registered on server");
      }catch(Exception e){
    System.out.println(e);
  }
}

This starts an instance of the rmiregistry on the client on the default port 1099 and binds an instance of the PopupClientImpl to it. It also queries the registry on the server from which it was loaded to locate the object that was registered with the name "ServerPop" (an instance of PopupServerImpl).

The applet then looks up the server and registers itself (an instance of PopupClientImpl) there, so that other clients know it has come up by invoking a remote method on the server.

Notice that all objects have been cast to their interfaces. Interfaces provide an implementation-free method for a client to access the servers published methods. That is, when deploying the client applications, the interface file is all that is needed to make the remote call.

In the start() method of the applet the significant piece of code is:

Vector v = server.getUsers();
for(int i=0;i<v.size();i++){
  String info =((PopupClientInterface)v.elementAt(i)).getInfo();
  userlist.append(info+ "\n");
}

This queries the already-located PopupServerImpl on the server for a listing of all the present users. The getUsers() method returns a vector containing all the client references (registered earlier onto the server in their individual init() methods). Each one of these registered client objects is then queried for some information (an IP address here) by getInfo() which is displayed in a text area.

We also add a button, and an anonymous inner class handles the button clicks. When the button is pressed, another remote method is invoked on the server object and the text in the textarea is sent as a message to all clients.

Button but = new Button("Send Message");

but.addActionListener(
  new ActionListener(){
    public void actionPerformed(ActionEvent e){
      try{
        server.sendMessage(ta.getText()); // Invoke remote method
      }catch(Exception excep){
        ta.setText(excep.toString());
      }
    }
  }
);
add(but); //Add the button with its event handler onto the applet.

The point worth noting here is that by invoking getInfo() on all the clients from within a client, the applets are not contacted directly. Rather it is the object reference on the server which is queried for its state information.


Fig 3 : Screen Shot

Compiling the sources

Compile ServerStarter.java and ClientStarter.java and create an html file with the applet tag in it.

<applet code="ClientStarter.class" codebase="." height=200 width=800>
</applet>

NEXT


Sameer Tyagi is a Software Engineer with several years of programming experience in iNet application development and has conducted multiple training workshops in Java. Besides holding an Engineering degree in Electronics he is a Sun Certified Java 1.1 Programmer.
Email: sameertyagi@usa.net

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

internet.com logo


JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.
Blackmail Applet
This applet prints out text that looks like letters cut out of a newspaper. You can specify the text, the width and height of the applet and the delay after each letter. Future versions will have more parameters.
JB User Poll
What is your primary tool for learning Java?
The JavaBoutique Top 15:
1. articles.rdf
2. Pool
3. applets.rdf
4.
5. Little_Wizard
6. PingPong
7. Birdbrain
8. Castles
9. PacMan
10. AScroll2
11. Viewer
12. JUpload
13. JavaMarquee
14. JTM
15. BasicCalendar
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Google IM Not Talking to Other Jabbers
Newly-launched Google Talk IM service doesn't connect to other open source Jabber servers and users -- at least not yet.

Printer Sets Good Example for Small Business Security
While surveys suggest that small businesses aren't prepared for ever-present security dangers, this Boston-area printer has maintained a pristine operation since a scare six years ago.

VoIP Gizmo Comes to Universities
As student gear up for back to school, a SIP-based initiative aims to VoIP-enable universities around the world for free calling.



Copyright 2002 INT Media Group, Incorporated. All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/