The design we follow is similar to the Observer Pattern.
We have a central object that keeps track of what's happening.
It updates all or a group of objects when some other object changes state.
The update being a change in the quantity or state (or both) of the affected objects.
I say similar because the idea here is to illustrate the bi-directional communication between isolated remote objects.
That is, to not go for the "model-view" or the Observable interface but move towards a "business logic" oriented approach where each object is responsible for its actions.
That is why a registry is started on a client to give the client objects the ability to provide a remote reference to other clients directly if required.
We don't just need simple remote callbacks from the server but in addition to that, each object must be capable of providing a direct reference to itself.
Writing the Remote Interfaces
The interface for the server, conforming to the RMI definitions, contains methods that add and remove users and send the messages.
The interface for the server object is called PopupServerInterface and is stored in PopupServerInterface.java
public void addUser(PopupClientInterface userId)
throws RemoteException;
public void removeUser(PopupClientInterface userId)
throws RemoteException ;
public Vector getUsers() throws RemoteException ;
public void sendMessage(String message)
throws RemoteException;
The working can be understood with what I quite crudely call the "going fishing analogy."
Consider the client as a fisherman who goes fishing as soon as it's started.
The client throws a line, which is caught by the fish, (our PopupServer) that keeps it tightly pulled, refusing to let go, (not letting the garbage get collected) by keeping a reference to it.
The fisherman, being smart, carries more than one fishing rod and casts multiple lines to different fish.
Whenever the fish wants to send a message to any other fish, it just pulls on the line (sends a message by invoking a method on the reference stored).
The effects of this tugging ripple through the fisherman to all the other fish!!
Of course it is the fish' job to inform the fisherman when it's tired so that the fisherman can also free the line.
Let's go on to define the client interface, PopupClientInterface now.
It contains only one method that will be invoked by the server for showing the popup message, and is stored in the file PopupClientInterface.java.
public void showPopup(String msg) throws RemoteException;
public String getInfo() throws RemoteException;
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.