Distributed Systems
Lecture -5-
Created by :
Eng. Ghadeer Al Hasan
RMI
Remote Method Invocation
Intro 1
Today we havetwo simple examples
- 1st : a programallows the userto log in to the server
- 2nd : a programallows the userto viewbooks fromthe server and query them
1st Example : LogInRMI
Server 3
public interface loginFacade extends Remote{
public String login(String username, String password) throws RemoteException;
}
public class LoginServer extends UnicastRemoteObject implements loginFacade{
private TreeMap clients = new TreeMap<String,String>();
public LoginServer() throws RemoteException{
super();
}
@Override
public String login(String username, String password) throws RemoteException {
Init();
String response = search(username, password);
return response;
}
Server… 4
private void Init() {
clients.put("user1", "1234");
clients.put("user2","1234");
}
private String search(String username,String password){
String response ="";
Set set = clients.entrySet();
Iterator it = set.iterator();
boolean flag = false;
while(it.hasNext()){
response ="";
Map.Entry me = (Map.Entry) it.next();
String user = me.getKey().toString();
String pass = me.getValue().toString();
if(username.equalsIgnoreCase(user)){
flag = true;
if(password.equalsIgnoreCase(pass)){
response ="LOGIN_SUCCESFUL";
}else{
response = "PASSWORD_INCORRECT";
}
break;
}
}
if(!flag){
response = "USER_NOT_EXISTS";
}
return response;
}
Server… 5
public static void main(String[] args){
try{
Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
LoginServer obj = new LoginServer();
reg.rebind("rmi://localhost/service", obj);
System.out.println("Server Running...");
}catch(RemoteException ex){
System.out.println(ex);
}
}
Client 6
public class Client {
public static void main(String[] args) {
try{
Registry reg = LocateRegistry.getRegistry("localhost", 1099);
loginFacade server = (loginFacade) reg.lookup("rmi://localhost/service");
Scanner scan = new Scanner(System.in);
System.out.println("Enter username : ");
String username = scan.nextLine();
System.out.println("Enter password : ");
String password = scan.nextLine();
String response = server.login(username, password);
System.out.println("Message From Server : " + response);
}catch(RemoteException | NotBoundException ex){
System.out.println(ex);
}
}
}
Run 7
2nd Example : BookRMI
9
public class Book implements Serializable{
private static final long serialVersionUID = 1190476516911661470L;
private String title;
private String code;
private double cost;
public Book(String code) {
this.code = code;
}
public Book(String title, String isbn, double cost) {…}
public String getTitle() {…}
public String getCode() {…}
public double getCost() {…}
public String toString() {
return "> " + this.title + " ($" + this.cost + ")";
}
}
Server
10
public interface BookInterface extends Remote {
Book findBook(Book b) throws RemoteException;
List<Book> allBooks() throws RemoteException;
}
public class BookStore extends UnicastRemoteObject implements BookInterface {
private static final long serialVersionUID = 1L;
private List<Book> bookList;
protected BookStore(List<Book> list) throws RemoteException {
super();
this.bookList = list;
}
Server…
11
@Override
public Book findBook(Book book) throws RemoteException {
Predicate<Book> predicate = x -> x.getCode().equals(book.getCode());
return bookList.stream().filter(predicate).findFirst().get();
}
@Override
public List<Book> allBooks() throws RemoteException {
return bookList;
}
private static List<Book> initializeList() {
List<Book> list = new ArrayList<>();
list.add(new Book("Head First Java, 2nd Edition", "A1", 31.41));
list.add(new Book("Java In A Nutshell", "A2", 10.90));
list.add(new Book("Java: The Complete Reference", "B1", 40.18));
list.add(new Book("Head First Servlets and JSP", "B2", 35.41));
list.add(new Book("Java Puzzlers: Traps, Pitfalls", "C1", 39.99));
return list;
}
Server…
12
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
BookStore store = new BookStore(initializeList());
reg.rebind("rmi://localhost/service", store);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.getMessage());
}
}
Server…
13
public class Client {
private static BookInterface server;
public static void main(String[] args) {
try {
Registry reg = LocateRegistry.getRegistry("localhost", 1099);
server = (BookInterface) reg.lookup("rmi://localhost/service");
boolean findmore;
do {
String[] options = {"Show All", "Find a book", "Exit"};
int choice = JOptionPane.
showOptionDialog(null, "Choose an action", "Option dialog",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, options, options[0]);
Client
14
switch (choice) {
case 0:
List<Book> list = server.allBooks();
StringBuilder message = new StringBuilder();
list.forEach(x -> {
message.append(x.toString() + "n");
});
JOptionPane.showMessageDialog(null, new String(message));
break;
Client…
case 1:
String code= JOptionPane
.showInputDialog("Type the code of the book you want to find.");
try {
Book response = server.findBook(new Book(code));
JOptionPane.showMessageDialog(null, "Title: "
+ response.getTitle() + "n" + "Cost: $"
+ response.getCost(),
response.getCode(), JOptionPane.INFORMATION_MESSAGE);
} catch (NoSuchElementException ex) {
JOptionPane.showMessageDialog(null, "Not found");
}
break;
default:
System.exit(0);
break;
}
findmore = (JOptionPane.showConfirmDialog(null, "Do you want to exit?",
"Exit",JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION);
} while (findmore);
} catch (RemoteException | NotBoundException ex) {
System.out.println(ex);
}
}
}
15Run
References 16
- YouTube link
https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ
- GitHub
https://github.com/Ghadeerof
End Lecture

#5 (Remote Method Invocation)

  • 1.
    Distributed Systems Lecture -5- Createdby : Eng. Ghadeer Al Hasan RMI Remote Method Invocation
  • 2.
    Intro 1 Today wehavetwo simple examples - 1st : a programallows the userto log in to the server - 2nd : a programallows the userto viewbooks fromthe server and query them
  • 3.
    1st Example :LogInRMI
  • 4.
    Server 3 public interfaceloginFacade extends Remote{ public String login(String username, String password) throws RemoteException; } public class LoginServer extends UnicastRemoteObject implements loginFacade{ private TreeMap clients = new TreeMap<String,String>(); public LoginServer() throws RemoteException{ super(); } @Override public String login(String username, String password) throws RemoteException { Init(); String response = search(username, password); return response; }
  • 5.
    Server… 4 private voidInit() { clients.put("user1", "1234"); clients.put("user2","1234"); } private String search(String username,String password){ String response =""; Set set = clients.entrySet(); Iterator it = set.iterator(); boolean flag = false; while(it.hasNext()){ response =""; Map.Entry me = (Map.Entry) it.next(); String user = me.getKey().toString(); String pass = me.getValue().toString(); if(username.equalsIgnoreCase(user)){ flag = true; if(password.equalsIgnoreCase(pass)){ response ="LOGIN_SUCCESFUL"; }else{ response = "PASSWORD_INCORRECT"; } break; } } if(!flag){ response = "USER_NOT_EXISTS"; } return response; }
  • 6.
    Server… 5 public staticvoid main(String[] args){ try{ Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); LoginServer obj = new LoginServer(); reg.rebind("rmi://localhost/service", obj); System.out.println("Server Running..."); }catch(RemoteException ex){ System.out.println(ex); } }
  • 7.
    Client 6 public classClient { public static void main(String[] args) { try{ Registry reg = LocateRegistry.getRegistry("localhost", 1099); loginFacade server = (loginFacade) reg.lookup("rmi://localhost/service"); Scanner scan = new Scanner(System.in); System.out.println("Enter username : "); String username = scan.nextLine(); System.out.println("Enter password : "); String password = scan.nextLine(); String response = server.login(username, password); System.out.println("Message From Server : " + response); }catch(RemoteException | NotBoundException ex){ System.out.println(ex); } } }
  • 8.
  • 9.
  • 10.
    9 public class Bookimplements Serializable{ private static final long serialVersionUID = 1190476516911661470L; private String title; private String code; private double cost; public Book(String code) { this.code = code; } public Book(String title, String isbn, double cost) {…} public String getTitle() {…} public String getCode() {…} public double getCost() {…} public String toString() { return "> " + this.title + " ($" + this.cost + ")"; } } Server
  • 11.
    10 public interface BookInterfaceextends Remote { Book findBook(Book b) throws RemoteException; List<Book> allBooks() throws RemoteException; } public class BookStore extends UnicastRemoteObject implements BookInterface { private static final long serialVersionUID = 1L; private List<Book> bookList; protected BookStore(List<Book> list) throws RemoteException { super(); this.bookList = list; } Server…
  • 12.
    11 @Override public Book findBook(Bookbook) throws RemoteException { Predicate<Book> predicate = x -> x.getCode().equals(book.getCode()); return bookList.stream().filter(predicate).findFirst().get(); } @Override public List<Book> allBooks() throws RemoteException { return bookList; } private static List<Book> initializeList() { List<Book> list = new ArrayList<>(); list.add(new Book("Head First Java, 2nd Edition", "A1", 31.41)); list.add(new Book("Java In A Nutshell", "A2", 10.90)); list.add(new Book("Java: The Complete Reference", "B1", 40.18)); list.add(new Book("Head First Servlets and JSP", "B2", 35.41)); list.add(new Book("Java Puzzlers: Traps, Pitfalls", "C1", 39.99)); return list; } Server…
  • 13.
    12 public static voidmain(String[] args) { try { Registry reg = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); BookStore store = new BookStore(initializeList()); reg.rebind("rmi://localhost/service", store); System.err.println("Server ready"); } catch (Exception e) { System.err.println("Server exception: " + e.getMessage()); } } Server…
  • 14.
    13 public class Client{ private static BookInterface server; public static void main(String[] args) { try { Registry reg = LocateRegistry.getRegistry("localhost", 1099); server = (BookInterface) reg.lookup("rmi://localhost/service"); boolean findmore; do { String[] options = {"Show All", "Find a book", "Exit"}; int choice = JOptionPane. showOptionDialog(null, "Choose an action", "Option dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); Client
  • 15.
    14 switch (choice) { case0: List<Book> list = server.allBooks(); StringBuilder message = new StringBuilder(); list.forEach(x -> { message.append(x.toString() + "n"); }); JOptionPane.showMessageDialog(null, new String(message)); break; Client… case 1: String code= JOptionPane .showInputDialog("Type the code of the book you want to find."); try { Book response = server.findBook(new Book(code)); JOptionPane.showMessageDialog(null, "Title: " + response.getTitle() + "n" + "Cost: $" + response.getCost(), response.getCode(), JOptionPane.INFORMATION_MESSAGE); } catch (NoSuchElementException ex) { JOptionPane.showMessageDialog(null, "Not found"); } break; default: System.exit(0); break; } findmore = (JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit",JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION); } while (findmore); } catch (RemoteException | NotBoundException ex) { System.out.println(ex); } } }
  • 16.
  • 17.
    References 16 - YouTubelink https://www.youtube.com/playlist?list=PLtDIUAtyP4lhV7CsYfLuIx26UeG4J-ujZ - GitHub https://github.com/Ghadeerof
  • 18.