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

Implementing the Interfaces

The implementation for the remote object is also straightforward. It contains a private vector that stores references to all PopupClient objects. The accessor methods and queries the state of this instance variable.

Vector v= new Vector();

public void
	addUser(PopupClientInterface userId)
 	throws RemoteException{
	v.addElement(userId);
}

public void
	removeUser(PopupClientInterface userId)
 	throws RemoteException {
	v.removeElement(userId);
 }

 public Vector getUsers() throws RemoteException{
	v.trimToSize();
	return v;
 }

 public void sendMessage(String message)
 	throws RemoteException {
	v.trimToSize();
	for ( int i=0; i <v.size();i++){
		PopupClientInterface
			client=(PopupClientInterface)v.elementAt(i);
		client.showPopup(message);
	}
 }

The send message method is simple, yet holds the key to the entire concept with many elementary Java concepts embedded in a few lines of code. Each object stored in the vector has its own individual state that is encapsulated from us.

We just alter the state by sending it a message and somehow, magically, that change of state is reflected all the way through to the client. This is a perfect example of how Java provides an abstraction over the actual, rather complicated, semantics of programming.

This is easy to understand if we keep in mind that all remote objects are passed by reference not by copy. All we did here was alter the state of that reference by sending it a message through a method call.

Just like the first lesson of Java:

 ObjectType ref= new ObjectType();
 //Create an object and assign it to a ref
 ref.methodCall();
 // alter the state and do something useful by calling the objects
 // method.

Only here we never created an object but looked it up from a remote clients registry and cast it to an interface before storing it in a vector !

The Client object implementation is even more straightforward. The showPopup method just contains some AWT code that makes a new frame, adds a label to it with the message and attaches a listener to detect the closing of the frame.

 public void showPopup(String s) throws RemoteException{
	final Frame f= new Frame();
	this.s=s;
	f.setSize(200,100);
	f.setTitle(s);
	f.add( new Label(s));

 // This adds the anonymous inner class
 // to handle the closing of the frame.

	f.addWindowListener(new WindowAdapter()){
		public void windowClosing(WindowEvent e){
			f.dispose();
		}
	}

  	f.setVisible(true);
 }

The other method returns some information about the current client--an IP address in our case. This will help other clients in knowing who's online and will also help if you want to scale this example and make the clients send a message to another specific client rather than all of them.

 public String getInfo() throws RemoteException{
	try{
		return InetAddress.getLocalHost().getHostAddress();
	}
	catch(Exception e){ return e.toString();}
	}
 }

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. Little_Wizard
2. Birdbrain
3. applets.rdf
4. articles.rdf
5. Pool
6. Lovemeter
7. quickserv
8. ZTBanner
9. linx
10.
11. AScroll2
12. longball
13. Viewer
14. DS_Backlight
15. Castles
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/