aula07tRMi

clienteRMi.java

import java.rmi.*;
public class clienteRMi
{
    public static void main(String args[])
    {
        System.out.println ("Looking for light bulb service");
        try {
            String registry = "localhost";
            if (args.length >=1){ registry = args[0]; }
            String registration = "rmi://" + registry + "/registryRMInterfaceRemotaImplements";

            Remote remoteService = Naming.lookup ( registration ); //cria a ligação TCP

            // CAST TO A RMILIGHTBULB INTERFACE
            registryRMInterfaceRemota bulbService = (registryRMInterfaceRemota) remoteService; //fazemos o cast para aceder aos métodos da interface

            bulbService.on();
            System.out.println ("Bulb state : " + bulbService.isOn() );
            bulbService.off();
            System.out.println ("Bulb state : " + bulbService.isOn() );
        }catch (NotBoundException e){
            System.out.println ("No light bulb service available!");
        }catch (RemoteException e){
            System.out.println ("RMI Error - " + e);
        }catch (Exception e){
            System.out.println ("Error - " + e);
        }
    }
}

registryRMInterfaceRemota.java

//Interfaces Remotas
public interface registryRMInterfaceRemota extends java.rmi.Remote
{
    public void on() throws java.rmi.RemoteException;
    public void off() throws java.rmi.RemoteException;
    public boolean isOn() throws java.rmi.RemoteException;
}

registryRMInterfaceRemoteImplements.java


//implements
//public class registryRMInterfaceRemoteImplements extends java.rmi.server.UnicastRemoteObject implements registryRMInterfaceRemota
public class registryRMInterfaceRemoteImplements extends java.rmi.server.UnicastRemoteObject implements registryRMInterfaceRemota
{
    private boolean lightOn;
    // A CONSTRUCTOR MUST BE PROVIDED FOR THE REMOTE OBJECT
    public registryRMInterfaceRemoteImplements() throws java.rmi.RemoteException
    {
        setBulb(false);
    }
    // REMOTELY ACCESSIBLE "ON" METHOD - TURNS ON THE LIGHT
    public void on() throws java.rmi.RemoteException
    {
        setBulb(true);
    }
    public void off() throws java.rmi.RemoteException
    {
        setBulb (false);
    }
    public boolean isOn() throws java.rmi.RemoteException
    {
        return getBulb();
    }
    public void setBulb (boolean value)
    {
        lightOn = value;
    }
    public boolean getBulb ()
    {
        return lightOn;
    }
}

RMIServer.java

import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.*;

public class RMIServer
{
    public static void main(String args[])
    {
        System.out.println ("Loading RMI service");


        System.out.println ("Loading RMI service");
        try {
            //v1
            // LOAD THE SERVICE
            registryRMInterfaceRemoteImplements bulbService = new registryRMInterfaceRemoteImplements();

            // EXAMINE THE SERVICE TO SEE WHERE IT IS STORED
            RemoteRef location = bulbService.getRef();
            System.out.println (location.remoteToString());
//v2
            Registry r1;
            try {
                r1 = LocateRegistry.createRegistry(Registry.REGISTRY_PORT); //HOST LOCAL
            }catch (RemoteException e) {
                System.out.println("jรก existe" + e);
                e.printStackTrace();
                r1 = LocateRegistry.getRegistry();
            }
            r1.rebind("registryRMInterfaceRemotaImplements", bulbService);

            Registry r2 = LocateRegistry.getRegistry(Registry.REGISTRY_PORT);
            r2.rebind("registryRMInterfaceRemotaImplements", new registryRMInterfaceRemoteImplements());
/*
//v1
            // CHECK TO SEE IF A REGISTRY WAS SPECIFIED
            String registry = "localhost";
            if (args.length >= 1){ registry = args[0]; }

            // REGISTRATION FORMAT: //REGISTRY_HOSTNAME[:PORT]/SERVICE_NAME
            String registration = "rmi://" + registry + "/RMILightBulb";

            // REGISTER WITH SERVICE SO THAT CLIENTS CAN FIND US
            // AN ALREADY REGISTERED SERVICE WILL BE REPLACED
            Naming.rebind( registration, bulbService );
            */

        }catch (RemoteException e){
            System.err.println ("Remote Error - " + e);
        }catch (Exception e){
            System.err.println ("Error - " + e);
        }
    }
}

Tags : ,

0 thoughts on “aula07tRMi”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.