Java-RMI-Aufzugssystem: Quellcode mit Server und Tür
AscensorRMI
AscensorRMI: Package Server; import java.rmi.*; import java.rmi.server.UnicastRemoteObject; import interfaz.GestorAscensores;
public class extends AscensorRMI extends UnicastRemoteObject { private static final long serialVersionUID = 1L;
Gate Tür; private int Stock; private int state; private int id; int-Modell
public AscensorRMI(int num) throws RemoteException {
Tür = new Tuer();
Stock = 0;
id = num;
status = 0;
System.out.println("Elevator" + num + " erstellt");
}
public int getpiso() throws RemoteException { return Stock; }
public int getId() throws RemoteException { return id; }
public int getest() throws Exception { return status; }
public void los(int pisodest) throws RemoteException {
if (pisodest > Stock) { oben(pisodest); } else { unten(pisodest); }
}
private void down(int pisodest) {
Tür.close();
state = -1;
while (Stock > pisodest) {
System.out.println("Elevator" + id + " Ich bin auf der Etage " + Stock);
try { Thread.currentThread().sleep(1000); } catch (Exception e) { e.printStackTrace(); }
Stock--;
}
System.out.println("Elevator" + id + " Ich habe das Wort kommen" + Stock);
state = 0;
Tür.open();
}
private void raise(int pisodest) {
Tür.close();
status = 1;
while (Stock < pisodest) {
System.out.println("Elevator" + id + " Ich bin auf der Etage " + Stock);
try { Thread.currentThread().sleep(1000); } catch (Exception e) { e.printStackTrace(); }
Stock++;
}
System.out.println("Elevator" + id + " Ich habe das Wort kommen" + Stock);
state = 0;
Tür.open();
}
}
GestorAscensoresRMI
GestorAscensoresRMI: Package Server; import interfaz.GestorAscensores; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject;
public class GestorAscensoresRMI extends UnicastRemoteObject implements GestorAscensores { private static final long serialVersionUID = 1L; private AscensorRMI Aufzüge[] = new AscensorRMI[5];
public GestorAscensoresRMI() throws RemoteException {
for (int i = 0; i < 5; i++) Aufzüge[i] = new AscensorRMI(i);
}
public AscensorRMI[] getAscensores() { return Aufzüge; }
public int getest(int id) throws RemoteException { return Aufzüge[id].getest(); }
public int getId(int id) throws RemoteException { return Aufzüge[id].getId(); }
public int getpiso(int id) throws Exception { return Aufzüge[id].getpiso(); }
public void los(int pisodest, int id) throws RemoteException { Aufzüge[id].los(pisodest); }
}
Server
Server: Server-Paket; import java.rmi.*; java.util.Scanner;
import public class Server { GestorAscensoresRMI Manager;
public Server() throws Exception {
GestorAscensoresRMI manager = new GestorAscensoresRMI();
Naming.rebind("rmi://localhost/Manager", manager);
}
public void check() throws Exception {
int option;
do {
option = menu();
switch (option) {
case 1: enseñarAscensores(); break;
case 2: break;
default: System.out.println("Invalid Option");
}
} while (option != 2);
System.out.println("Sie haben das System verlassen");
}
private void enseñarAscensores() {
int i = 0;
try {
for (i = 0; i < 5; i++) {
System.out.println("Kein Aufzug -" + (manager.getAscensores()[i].getId() + " ist in Das floor: " + (manager.getAscensores()[i].getpiso() + " heißt es: " + (manager.getAscensores()[i].getest()));
}
} catch (RemoteException e) { e.printStackTrace(); }
}
public int menu() {
Scanner keyboard = new Scanner(System.in);
int option;
System.out.println("1.- Siehe Aufzüge");
System.out.println("2.- Exit");
System.out.println();
System.out.print("Wählen Sie Option: ");
option = keyboard.nextInt();
return option;
}
public static void main(String[] args) {
try {
java.rmi.registry.LocateRegistry.createRegistry(1099);
Server manager = new Server();
manager.check();
} catch (Exception e) { System.out.println(e.getMessage()); }
}
}
Door
Door: package Server; import java.io.*;
public class Gate implements Serializable { private static final long serialVersionUID = 1L; boolean state;
public Door() { state = true; }
public void open() {
if (!state) System.out.println("\tAbriendo Tür");
// Code, der Ausfall eines zweiten Versuchs verursacht
try { Thread.currentThread().sleep(1000); } catch (Exception e) { e.printStackTrace(); }
state = true;
}
public void close() {
if (state) System.out.println("\tCerrando Tür");
try { Thread.currentThread().sleep(1000); } catch (Exception e) { e.printStackTrace(); }
state = false;
}
}