Remote Control System for Elevators and Calculators
Remote Control: RMI Implementation
Package: customer java.rmi
Import java.rmi.*;
Import java.util.Scanner;
Import interfaz.GestorAscensores;
Import java.lang.Math
Public class Remote {
GestorAscensores manager;
Scanner keyboard = new Scanner(System.in);
Public Remotecontrol() throws Exception {
handle = ((GestorAscensores) Naming.lookup(“rmi://localhost/Manager“));
}
Public void check() throws Exception {
Scanner keyboard = new Scanner(System.in);
int option, pisodest, id;
do {
choice = menu();
switch (option) {
case 1:
case 2:
System.out.print(“Flat”);
pisodest = teclado.nextInt();
id = discoverrCercano(pisodest);
System.out.println(“before”);
manager.ir(pisodest, id);
break;
case 3:
break;
default:
System.out.println(“Invalid option”);
}
} while (option != 3);
}
Private int encontrarCercano(int pisoDest) {
int i = 0, id = -1;
boolean found = false;
int dist = 100, dist1;
try {
do {
for (i = 0; i < 5; i++) {
dist1 = pisoDest – (manager.getpiso(i));
if (dist1 < 0) {
dist1 *= -1;
}
if ((dist > dist1) && (manager.getest(i) == 0)) {
dist = dist1;
id = manager.getId(i);
System.out.println(“id copied” + id);
found = true;
}
}
} while (found == false);
} catch (Exception e) {
System.out.println();
}
System.out.println(“The closest to the plant” + pisoDest + “is the elevator” + id);
return id;
}
Public int menu() {
int option;
System.out.println(“1.-Call Elevator”);
System.out.println(“2.-floor display”);
System.out.println(“3.-Out”);
System.out.println();
System.out.print(“Choose option:”);
option = keyboard.nextInt();
return option;
}
Public static void main(String[] args) {
try {
manager = new Remotecontrol();
Remotecontrol.controlar();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
Manager Elevators (interface)
Package: interface
Import java.rmi.Remote;
Import java.rmi.RemoteException;
Import servidor.AscensorRMI
Public interface Manager extends Remote {
Public Lifts getpiso(int id) throws RemoteException;
Public int getest(int id) throws RemoteException;
Public int getId(int id) throws RemoteException;
Public AscensorRMI[] getAscensores() throws RemoteException;
void go(int pisodest, int id) throws RemoteException;
}
(END loader lifts RMI) CORBA STARTS IN THE BACK?
Remote Control: CORBA Implementation
Package: client
Import interface.*;
Import java.util.Scanner;
Import org.omg.CORBA.ORB;
Import org.omg.CosNaming.BindingIteratorHolder;
Import org.omg.CosNaming.BindingListHolder;
Import org.omg.CosNaming.NamingContextExt;
Import org.omg.CosNaming.NamingContextExtHelper
Public class Remotecontrol {
Calculator calc;
Dispenser disp;
Scanner keyboard = new Scanner(System.in);
NamingContextExt ncRef;
Public Remotecontrol(String[] args) throws Exception {
ORB orb = ORB.init(args, null);
try {
org.omg.CORBA.Object objRef = orb.resolve_initial_references(“NameService”);
ncRef = NamingContextExtHelper.narrow(objRef);
} catch (Exception e) {
System.out.println(“can not find the reference”);
}
}
Private Calculator elegirCalculadora() {
int idCalcu = 0;
System.out.println(“\nChoose a calculator (0-4):”);
idCalcu = keyboard.nextInt();
keyboard.nextLine();
return disp.getCalculadora(idCalcu);
}
Public void control() throws Exception {
Scanner keyboard = new Scanner(System.in);
int option, number1, number2;
try {
disp = DispensadorHelper.narrow(ncRef.resolve_str(“dispenser”));
} catch (Exception e) {
System.out.println(“error in getting dispenser”);
}
do {
calc = elegirCalculadora();
option = menu();
switch (option) {
case 1:
System.out.print(“Number:”);
number1 = keyboard.nextInt();
System.out.printf(“The factorial of the number = %d is %d \n”, number1, calc.getFactorial(number1));
break;
case 2:
System.out.print(“Number”);
number1 = teclado.nextInt();
System.out.print(“Number to raise:”);
number2 = teclado.nextInt();
System.out.printf(“The exponential of %d ^ %d is %d \n”, number1, number2, calc.getExponencial(number1, number2));
break;
case 3:
break;
default:
System.out.println(“Invalid option”);
}
} while (option != 3);
}
public int menu() {
Scanner keyboard = new Scanner(System.in);
int option;
System.out.println(“1.-Calculate the factorial of a number”);
System.out.println(“2.-Calculate the exponential of a number”);
System.out.println(“3.-Exit”);
System.out.println();
System.out.print(“Choose option:”);
option = teclado.nextInt();
return option;
}
Public static void main(String[] args) {
try {
manager = new Remotecontrol(args);
gestor.controlar();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}