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.
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
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.
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.