Passar parametros por RMI

2 respostas
muniquewassem

Oi!

Já estou ficando com vergonha de tanto que eu pergunto aqui, maaas eu realmente não consegui resolver isso sozinha. Tenho uma interface, um servidor e um cliente em uma aplicação pra calculo de matriz em RMI, mas ela não funciona de jeito nenhum e tenho impressão que estou fazendo(ou esquecendo de fazer) algo bem simples.
Eu to tentando passar duas matrizes m1 e m2 do cliente pro servidor.... Então o servidor deveria fazer a multiplicação e retornar a matriz resultante da multiplicação.

Servidor:
import Jama.Matrix;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Servidor implements IntMatriz{
    
    public Servidor() throws RemoteException{
        inicia();
    }
    
     public void inicia() {
        try {
            IntMatriz stub = (IntMatriz) UnicastRemoteObject.exportObject(this, 0);

            Registry registry = LocateRegistry.createRegistry(3637);

            registry.bind("Servidor", stub);
            System.out.println("Servidor iniciado...");
        } catch (RemoteException ex) {
            System.out.println(ex.getMessage());
        //} catch (AlreadyBoundException ex) {
            //System.out.println(ex.getMessage());
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public double[][] calculaMatriz(double a[][], double b[][]) throws RemoteException{
        int row, column, i;
        double aux;
        double c[][] = new double[a.length][b[0].length];
        for (row = 0; row < c.length; row++) // multiplicação das matrizes{
        {
            for (column = 0; column < c[row].length; column++) {
                aux = 0;
                for (i = 0; i < a[row].length; i++) {
                    try {
                        aux = aux + a[row][i] * b[i][column];
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                c[row][column] = aux;
            }
        }
        return c;
    }

    public static void main(String[] args) throws RemoteException {
        new Servidor();
    }
}
Cliente:
import Jama.Matrix;
import java.io.IOException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ClienteJama {
    public ClienteJama(){
       iniciar();
    }   
    
     public void iniciar() {
        try {
            Registry registry = LocateRegistry.getRegistry("localhost", 3637);
            IntMatriz intf = (IntMatriz) registry.lookup("Servidor");
            System.out.println("passou");
           
        } catch (RemoteException ex) {
            ex.printStackTrace();
        } catch (NotBoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void passaValores(IntMatriz intf, double[][] a, double[][] b) throws RemoteException{
        System.out.println("passou");
        double m1[][] = {{1, 3, 5},
            {6, 5, 8},
            {9, 6, 7}};

        double m2[][] = {{9, 4, 4},
            {5, 2, 0},
            {4, 2, 1}};
        
        double[][] resultante;
        Matrix mA = new Matrix(a);
        Matrix mB = new Matrix(b);
        System.out.println("MATRIZ A");
        mA.print(5, 1);
        System.out.println("MATRIZ B");
        mB.print(5, 1); 
        a = m1;
        b = m2;
        double[][] resposta = intf.calculaMatriz(a, b);
        System.out.println(resposta);
    }
    
    public static void main(String[] args) {
        new ClienteJama();
    }
}
Interface:
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IntMatriz extends Remote{
    //public double[][] calculaMatriz(double[][] m1, double[][] m2) throws RemoteException;
    public double[][] calculaMatriz(double a[][], double b[][]) throws RemoteException;
}

Tem alguma alma caridosa por ai disposta a me ajudar?

2 Respostas

danieldomingues86

Olá,

Veja se é isso que voce quer :

Cliente
package com.rmi.calmatriz;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class ClienteJama {

	public static void main(String[] args) throws RemoteException,NotBoundException {
		new ClienteJama();
	}

	public ClienteJama() throws RemoteException, NotBoundException {
		final Registry registry = LocateRegistry.getRegistry("localhost", 3637);
		final IntMatriz intf = (IntMatriz) registry.lookup("Servidor");

		final double m1[][] = { { 1, 3, 5 }, { 6, 5, 8 }, { 9, 6, 7 } };
		final double m2[][] = { { 9, 4, 4 }, { 5, 2, 0 }, { 4, 2, 1 } };

		double[][] resposta = intf.calculaMatriz(m1, m2);
		for (int i = 0; i < resposta.length; i++) {
			for (int j = 0; j < resposta.length; j++) {
				System.out.print(resposta[i][j] + "  ");
			}
			System.out.println();
		}
	}
}
Interface
package com.rmi.calmatriz;

import java.rmi.Remote;
import java.rmi.RemoteException;
  
public interface IntMatriz extends Remote {  
    public double[][] calculaMatriz(double a[][], double b[][]) throws RemoteException;  
}
Servidor
package com.rmi.calmatriz;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class Servidor implements IntMatriz {

	public static void main(String[] args) throws RemoteException {
		new Servidor().inicia();
	}

	public void inicia() {
		try {
			IntMatriz stub = (IntMatriz) UnicastRemoteObject.exportObject(this,0);
			Registry registry = LocateRegistry.createRegistry(3637);
			registry.bind("Servidor", stub);
			System.out.println("Servidor iniciado...");
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	@Override
	public double[][] calculaMatriz(double a[][], double b[][]) throws RemoteException {
		
		int row, column;
		double c[][] = new double[a.length][b[0].length];

		for (row = 0; row < c.length; row++) {
			for (column = 0; column < c[row].length; column++) {
				double aux = 0;
				for (int i = 0; i < a[row].length; i++) {
					aux = aux + a[row][i] * b[i][column];
				}
				c[row][column] = aux;
			}
		}
		return c;
	}
}
Abraços Daniel
muniquewassem

Rapaz, era isso mesmo!
Nem tinha me passado pela cabeça colocar no construtor!

Muito obrigado mesmo! Salvou um bando dos meus neurônios de morrerem fritos hoje!

Criado 19 de junho de 2011
Ultima resposta 20 de jun. de 2011
Respostas 2
Participantes 2