0% found this document useful (0 votes)
30 views20 pages

Suggested Solutions To Some Programming Problems

The document discusses revising a Java RMI example to allow clients to add city information to the server and query information added by other clients. It then discusses further revising the solution to allow the server to notify other clients of new city information using callbacks.

Uploaded by

haymondyon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views20 pages

Suggested Solutions To Some Programming Problems

The document discusses revising a Java RMI example to allow clients to add city information to the server and query information added by other clients. It then discusses further revising the solution to allow the server to notify other clients of new city information using callbacks.

Uploaded by

haymondyon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Tutorial 2

CE4013/CZ4013/SC4051 Distributed Systems


1
Question 1
 Revise the Java RMI example discussed in the
lecture to allow the clients to add information
about cities to the server and query the
information added by other clients to the server.

2
Java RMI
 Overview – steps for developing Java RMI
 Design remote interfaces (identify remote objects, their
roles, what do they take in, what do they produce)
 Implementation
 Servant: do the actual work – implement the methods

in the remote interface


 Server: do the easy job – create remote objects and

register them in RMIregistry (binder)


 Client: look up remote objects and access them

 Compile remote interface (generating proxies, skeletons


etc.), compile source codes
 Start server, followed by clients
3
Suggested Solution to Question 1
 Step 1: Design the remote interface
There is no need to pass the city name as a parameter
import java.rmi.*;since each city is now represented by a remote object
public interface City2 extends Remote {
String getName() throws RemoteException;
int getPopulation() throws RemoteException;
int getTemperature() throws RemoteException;
}
public interface CityFactory extends Remote {
void createCity(String name, int pop, int temp) throws
RemoteException;
City2 lookupCity(String name) throws RemoteException;
} 4
Suggested Solution to Question 1
 Step 2: Design City2 servant class
import java.rmi.*;
import java.rmi.server.*;
public class City2Impl extends UnicastRemoteObject
implements City2 {
private String cityName;
private int cityPopulation;
private int cityTemperature;

public City2Impl(String name, int pop, int temp) throws


RemoteException {
super(); cityName = name;
cityPopulation = pop; cityTemperature = temp; }
5
Suggested Solution to Question 1
public String getName() throws RemoteException {
return cityName;
}

public int getPopulation() throws RemoteException {


return cityPopulation;
}

public int getTemperature() throws RemoteException {


return cityTemperature;
}
}
6
Suggested Solution to Question 1
 Step 3: Design CityFactory servant class to allow
creation of City2 servants (which are remote objects
by themselves)
import java.rmi.*;
import java.rmi.server.*;
import java.util.Vector;

public class CityFactoryImpl extends UnicastRemoteObject


implements CityFactory {
private Vector cityList;

public CityFactoryImpl() throws RemoteException {


super();
cityList = new Vector(); } 7
Suggested Solution to Question 1
public void createCity(String name, int pop, int temp)
throws RemoteException {
City2 aCity = new City2Impl(name, pop, temp);
cityList.addElement(aCity); }

public City2 lookupCity(String name)


throws RemoteException {
City2 aCity;
for (int i = 0; i < cityList.size(); i++) {
aCity = (City2) cityList.elementAt(i);
if (aCity.getName() == name)
return aCity; }
return null; } }
8
Suggested Solution to Question 1
 Step 4: Design CityFactory server class
import java.rmi.*;
import java.rmi.server.*;

public class CityFactoryServer {


public static void main(String args[]) {
try {
CityFactoryImpl aFactory = new CityFactoryImpl();
Naming.rebind("rmi://…/CityFactory", aFactory);
} catch (Exception e) { … }
}
}
9
Suggested Solution to Question 1
 Step 5: Design the client class
public class CityFactoryClient {
public static void main(String args[]) {
try {
CityFactory aFactory =
(CityFactory) Naming.lookup("rmi://…/CityFactory");
aFactory.createCity("Ottawa", 2, 30);
aFactory.createCity("Toronto", 10, 20);
City2 aCity = aFactory.lookupCity("Ottawa");
System.out.println(aCity.getPopulation());
System.out.println(aCity.getTemperature());
} catch (Exception e) { … } }
} 10
Question 2
 Revise your solution to Question 1 to allow the
server to inform the other clients of the new
city information added by a client using the
callback approach.

11
Java RMI
 In the previous example, the client invokes the
methods at the server to retrieve information, he
can do it from time to time to check whether there
is any update to the information (polling)
 Is there any alternative?
 Callback: the server informs the client when the
information is updated
 Polling vs. Callback
 Ask your mom every 5 minutes “Is dinner ready?”
(polling)
 Please let me know when dinner is ready (callback)
12
Java RMI
 How to implement callback?
 Client creates a remote object that implements an interface
containing a method for server to call (callback object)
public interface Callback extends Remote {
void cbMethod(…) throws RemoteException; };
 Server provides an operation for interested clients to

inform it of the remote object references of their callback


objects and records them in a list
void register(Callback cbObject) throws RemoteException;
void deregister(Callback cbObject) throws RemoteException;
 When an event of interest occurs, server invokes the

method in callback objects


13
Suggested Solution to Question 2
 Step 1: Design the remote interface
Callback interface at the client side:
public interface Callback extends Remote {
void cityAdded(City2 aCity) throws RemoteException; }

14
Suggested Solution to Question 2
Interfaces at the server side:
public interface City2 extends Remote {
String getName() throws RemoteException;
int getPopulation() throws RemoteException;
int getTemperature() throws RemoteException; }
public interface CityFactory extends Remote {
void createCity(String name, int pop, int temp) throws
RemoteException;
City2 lookupCity(String name) throws RemoteException;
void register(Callback cb) throws RemoteException;
void deregister(Callback cb) throws RemoteException; }
15
Suggested Solution to Question 2
 Step 2: Design CityFactory servant class (City2
servant class and CityFactory server class remain
unchanged)
public class CityFactoryImpl extends UnicastRemoteObject
implements CityFactory {
private Vector cityList;
private Vector cbList;

public CityFactoryImpl() throws RemoteException {


super();
cityList = new Vector();
cbList = new Vector();
} 16
Suggested Solution to Question 2
public void createCity(String name, int pop, int temp)
throws RemoteException {
City2 aCity = new City2Impl(name, pop, temp);
cityList.addElement(aCity);
for (int i = 0; i < cbList.size(); i++) {
Callback cb = (Callback) cbList.elementAt(i);
cb.cityAdded(aCity); }
}

17
Suggested Solution to Question 2
public City2 lookupCity(String name)
throws RemoteException { … }
public void register(Callback cb) throws RemoteException {
if (!(cbList.contains(cb)))
cbList.addElement(cb); }

public void deregister(Callback cb) throws RemoteException {


if (cbList.contains(cb))
cbList.removeElement(cb); }
}

18
Suggested Solution to Question 2
 Step 3: Design Callback servant class
import java.rmi.*;
import java.rmi.server.*;
public class CallbackImpl extends UnicastRemoteObject
implements Callback {

public CallbackImpl() throws RemoteException {


super(); }

void cityAdded (City2 aCity) throws RemoteException {


System.out.println("New city added: " + aCity.getName()
+ aCity.getPopulation() + aCity.getTemperature());
}
} 19
Suggested Solution to Question 2
 Step 4: Design the client class (client creates a
callback object and registers it with the server)
public class City2Client {
public static void main(String args[]) {
try {
Callback cb = new CallbackImpl();
CityFactory aFactory =
(CityFactory) Naming.lookup("rmi://…/CityFactory");
aFactory.register(cb);
aFactory.createCity("Ottawa", 2, 30);
……
} catch (Exception e) { … } }
} 20

You might also like