Implementación de Sistema de Control de Ascensores con Java RMI

Informática y Telecomunicaciones5,92 KB

A continuación se presenta el código fuente completo y corregido para desarrollar un sistema distribuido de control de ascensosores utilizando la tecnología Java RMI (Remote Method Invocation).

Clase AscensorRMI


package servidor;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import interfaz.GestorAscensores;

public class AscensorRMI extends UnicastRemoteObject {
    private static final long serialVersionUID = 1L;
    Puerta puerta;
    private int piso;
    private int estado;
    private int id;
    int modelo;

    public AscensorRMI(int num) throws RemoteException {
        puerta = new Puerta();
        piso = 0;
        id = num;
        estado = 0;
        System.out.println("Ascensor " + num + " creado");
    }

    public int getpiso() throws RemoteException {
        return piso;
    }

    public int getId() throws RemoteException {
        return id;
    }

    public int getEstado() throws RemoteException {
        return estado;
    }

    synchronized public void ir(int pisodest) throws RemoteException {
        if (pisodest > piso) {
            subir(pisodest);
        } else {
            bajar(pisodest);
        }
    }

    private void bajar(int pisodest) {
        puerta.cerrar();
        estado = -1;
        while (piso > pisodest) {
            System.out.println("Ascensor " + id + ", estoy en piso " + piso);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            piso--;
        }
        System.out.println("Ascensor " + id + ", he llegado al piso " + piso);
        estado = 0;
        puerta.abrir();
    }

    private void subir(int pisodest) {
        puerta.cerrar();
        estado = 1;
        while (piso < pisodest) {
            System.out.println("Ascensor " + id + ", Estoy en piso " + piso);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            piso++;
        }
        System.out.println("Ascensor " + id + ", he llegado al piso " + piso);
        estado = 0;
        puerta.abrir();
    }
}

Clase GestorAscensoresRMI


package servidor;

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 ascensores[] = new AscensorRMI[5];

    public GestorAscensoresRMI() throws RemoteException {
        int i = 0;
        for (i = 0; i < 5; i++) {
            ascensores[i] = new AscensorRMI(i);
        }
    }

    public AscensorRMI[] getAscensores() {
        return ascensores;
    }

    public int getEstado(int id) throws RemoteException {
        return ascensores[id].getEstado();
    }

    public int getId(int id) throws RemoteException {
        return ascensores[id].getId();
    }

    public int getpiso(int id) throws RemoteException {
        return ascensores[id].getpiso();
    }

    public void ir(int pisodest, int id) throws RemoteException {
        (ascensores[id]).ir(pisodest);
    }
}

Clase Servidor


package servidor;

import java.rmi.*;
import java.util.Scanner;

public class Servidor {
    GestorAscensoresRMI gestor;

    public Servidor() throws Exception {
        gestor = new GestorAscensoresRMI();
        Naming.rebind("rmi://localhost/Gestor", gestor);
    }

    public void controlar() throws Exception {
        int opcion;
        do {
            opcion = menu();
            switch (opcion) {
                case 1:
                    enseñarAscensores();
                    break;
                case 2:
                    break;
                default:
                    System.out.println("Opción no válida");
            }
        } while (opcion != 2);
        System.out.println("Has salido del sistema");
    }

    private void enseñarAscensores() {
        int i = 0;
        try {
            for (i = 0; i < 5; i++) {
                System.out.println("Ascensor nº-" + (gestor.getAscensores())[i].getId() + ", está en el " + "piso:" + (gestor.getAscensores())[i].getpiso() + " estado:" + (gestor.getAscensores())[i].getEstado());
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public int menu() {
        Scanner teclado = new Scanner(System.in);
        int opcion;
        System.out.println("1.- Ver ascensores");
        System.out.println("2.- Salir");
        System.out.println();
        System.out.print("Elija opción:  ");
        opcion = teclado.nextInt();
        return opcion;
    }

    public static void main(String[] args) {
        try {
            java.rmi.registry.LocateRegistry.createRegistry(1099);
            Servidor gestor = new Servidor();
            gestor.controlar();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Clase Puerta


package servidor;

import java.io.*;

public class Puerta implements Serializable {
    private static final long serialVersionUID = 1L;
    boolean estado;

    public Puerta() {
        estado = true;
    }

    public void abrir() {
        if (!estado) {
            System.out.println("\tAbriendo puerta");
        }
        // código para provocar un retraso de un segundo
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        estado = true;
    }

    public void cerrar() {
        if (estado) {
            System.out.println("\tCerrando puerta");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        estado = false;
    }
}

Descargar

PDF Word HTML