You are on page 1of 4

INSAT 2009-2010

TP 2
Développement d’une application répartie Communicant
par RMI

I. Solution

A. Interface Banque
public interface Banque extends java.rmi.Remote
{
public void creer_compte(String id, double somme) throws
java.rmi.RemoteException;
public void ajouter(String id, double somme) throws
java.rmi.RemoteException;
public void retirer(String id, double somme) throws
java.rmi.RemoteException;
public Position position(String id) throws java.rmi.RemoteException;
public void enregistrerNotification(String id, BanqueNotification b,
double minimum) throws java.rmi.RemoteException;
public void enleverNotification(String id) throws
java.rmi.RemoteException;
}

B. Interface BanqueNotification
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface BanqueNotification extends Remote
{
public void notification(double valeur, double mini) throws RemoteException;
}

C. Classe Position
import java.io.Serializable;
import java.util.Date;
public class Position implements Serializable
{
private double solde;
private Date derniereOperation;
public Position(double solde) {
this.solde = solde; this.derniereOperation = new Date();
}
public Date getDerniereOperation() {
return derniereOperation;
}
public void setDerniereOperation(Date derniereOperation) {
this.derniereOperation = derniereOperation;
}
1
public double getSolde() {
return solde;
}
public void setSolde(double solde) {

GL2 | TP Applications Réparties


INSAT 2009-2010

this.solde = solde;
}
public String toString() {
return "solde = "+solde+" date dernière op. "+derniereOperation;
}
}

D. Classe Compte
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
class Compte
{
private Position position;
private BanqueNotification notif;
private double notifMinimum;
public Compte(double somme) {
this.position = new Position(somme);
}
public Position getPosition() {
return position;
}
public void ajouter(double somme) {
position.setSolde(position.getSolde()+somme);
position.setDerniereOperation(new Date());
}
public void retirer(double somme) {
position.setSolde(position.getSolde()-somme);
position.setDerniereOperation(new Date());
if (notif != null && position.getSolde() < notifMinimum) {
try{
notif.notification(position.getSolde(), notifMinimum);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
public void setNotification(BanqueNotification notif, double mini) {
this.notif = notif;
notifMinimum = mini;
}
}

E. Classe BanqueImpl
public class BanqueImpl extends UnicastRemoteObject implements Banque
{
Hashtable comptes;
public BanqueImpl() throws RemoteException {
super();
comptes = new Hashtable(); 2
}
public void creer_compte(String id, double somme) throws RemoteException
{
comptes.put(id, new Compte(somme));

GL2 | TP Applications Réparties


INSAT 2009-2010

}
public void ajouter(String id, double somme) throws RemoteException {
Compte cpt = (Compte)comptes.get(id);
cpt.ajouter(somme);
}
public void retirer(String id, double somme) throws RemoteException {
Compte cpt = (Compte)comptes.get(id);
cpt.retirer(somme);
}
public Position position(String id) throws RemoteException {
Compte cpt = (Compte)comptes.get(id);
return cpt.getPosition();
}
public void enregistrerNotification(String id, BanqueNotification b,
double mini) throws RemoteException {
Compte cpt = (Compte)comptes.get(id);
cpt.setNotification(b, mini);
}
public void enleverNotification(String id) throws RemoteException {
Compte cpt = (Compte)comptes.get(id);
cpt.setNotification(null, 0);
}
public static void main(String[] args) throws RemoteException,
MalformedURLException {
System.setSecurityManager(new RMISecurityManager());
Naming.rebind("MaBanque", new BanqueImpl());
System.out.println("MaBanque est enregistrée");
}
}

F. Classe BanqueNotificationImpl
import java.rmi.RemoteException;
import java.rmi.server.Unreferenced;
public class BanqueNotificationImpl implements BanqueNotification,
Unreferenced
{
private String id;
public BanqueNotificationImpl(String id) {
this.id = id;
}
public void notification(double valeur, double mini) throws
RemoteException {
System.out.println("Votre compte "+id+" est inférieur au mini : "+mini+"
solde : "+valeur);
}
public void unreferenced() {
System.out.println("La notification pour "+id+" n'est plus utilisée");
}
}

G. Classe BanqueClient
3
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class BanqueClient

GL2 | TP Applications Réparties


INSAT 2009-2010

{
public static void main(String[] args) throws MalformedURLException,
RemoteException, NotBoundException {
Banque proxy = (Banque) Naming.lookup("MaBanque");
proxy.creer_compte("Bob", 100);
BanqueNotificationImpl bni = new BanqueNotificationImpl("Bob");
UnicastRemoteObject.exportObject(bni);
proxy.enregistrerNotification("Bob", bni, 0);
proxy.ajouter("Bob", 100);
System.out.println("Position du compte de Bob : +proxy.position("Bob"));
proxy.retirer("Bob", 300);
proxy.retirer("Bob", 100);
System.out.println("Position du compte de Bob : +proxy.position("Bob"));
bni = null;
proxy.enleverNotification("Bob");
}
}

GL2 | TP Applications Réparties