In an exercise for the last chapter, you used RMI to write an application for a prototype opinion poll system. Modify that application so that each client provides the user interface for a respondent to cast a vote. In addition, the current tally of the poll will be displayed on the client screen whenever a new vote has been cast (either by this client or some other). Test your programs until you are satisfied that they are working correctly. Turn in listings of your programs, including all interface files. A demonstration of the programs in the laboratory is recommended.

What will be an ideal response?

The Server Interface is as follows:
```
public interface VoteServerInterface extends Remote {

public void castVote(char yesOrNo )
throws java.rmi.RemoteException;

public void registerForCallback(
CallbackClientInterface callbackClientObject
) throws java.rmi.RemoteException;

// This remote method allows an object client to
// cancel its registration for callback

public void unregisterForCallback(
CallbackClientInterface callbackClientObject)
throws java.rmi.RemoteException;
}
```
The client interface, the Client, and the server are the same as the same as in the Callback sample.
The remote object implementation provides the method for casting a vote. It also maintains the counts of the votes. And, whenever a vote is received, the callbacks are made to return the tallies.

The server object implementation is as follows:
```
import java.rmi.*;
import java.rmi.server.*;
import java.util.Vector;

/**
* This class implements the remote interface
* CallbackServerInterface.
* @author M. L. Liu
*/

public class VoteServerImpl extends UnicastRemoteObject
implements VoteServerInterface {

private Vector clientList;
private int yesCount, noCount;


public VoteServerImpl() throws RemoteException {
super( );
clientList = new Vector();
yesCount = 0;
noCount = 0;
}

public void castVote(char vote )
throws java.rmi.RemoteException {
if (vote == 'Y')
yesCount++;
else
noCount++;
doCallbacks( );
}

public synchronized void registerForCallback(
CallbackClientInterface callbackClientObject)
throws java.rmi.RemoteException{
// store the callback object into the vector
if (!(clientList.contains(callbackClientObject))) {
clientList.addElement(callbackClientObject);
System.out.println("Registered new client ");
doCallbacks();
} // end if
}

// This remote method allows an object client to
// cancel its registration for callback
// @param id is an ID for the client; to be used by
// the server to uniquely identify the registered client.
public synchronized void unregisterForCallback(
CallbackClientInterface callbackClientObject)
throws java.rmi.RemoteException{
if (clientList.removeElement(callbackClientObject)) {
System.out.println("Unregistered client ");
} else {
System.out.println(
"unregister: clientwasn't registered.");
}
}

public void doCallbacks( ) throws java.rmi.RemoteException
{
// make callback to each registered client
System.out.println(
"**************************************\n"
+ "Callbacks initiated ---");
for (int i = 0; i < clientList.size(); i++)
{
System.out.println("doing "+ i +"-th callback\n");
// convert the vector object to a callback object
VoteClientInterface nextClient =
(VoteClientInterface)clientList.elementAt(i);
// invoke the callback method
nextClient.notifyMe(β€œYes = β€œ + yesCount + β€œ; No = β€œ + noCount);
}// end for
System.out.println("********************************\n" +
"Server completed callbacks ---");
} // doCallbacks

}// end CallbackServerImpl class
```

Computer Science & Information Technology

You might also like to view...

Which of the following is the normal Excel file format?

A. .excl B. .xlsx C. .xcl D. .exl

Computer Science & Information Technology

In the accompanying figure, Item 3 points to a ____.

A. centered paragraph B. justified paragraph C. right-aligned paragraph D. left-aligned paragraph

Computer Science & Information Technology