Olá!
Estou com uma duvida quanto a implementação de um código. Estou tentado desenvolver uma aplicação que passe duas matrizes a um servidor e ele faça o calculo da matriz e retorne as duas matrizes enviadas e a matriz resultante da multiplicação. Entendi bem como faço a multiplicação normalmente, mas quando tento implementar em RMI, não consigo fazer o servidor 'exportar' o resultado. Li sobre Serializable, mas não entendi muita coisa.
Alguém pode dar uma olhada no meu dódigo e ver o que está 'faltando?'
public class ServidorMatriz implements IntMatriz{
public ServidorMatriz() throws RemoteException{
inicia();
}
public void inicia() {
try {
IntMatriz stub = (IntMatriz) UnicastRemoteObject.exportObject(this, 0);
// Liga o stub do objeto remoto no registro, e inicia o RMIRegistry
// na porta 3637
Registry registry = LocateRegistry.createRegistry(3637);
// D� um nome pra ele no registro
registry.bind("Matriz", 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 void CalculaMatriz(int m1[][],int m2[][]) throws RemoteException{
int row,column,aux,i;
int resl[][]= new int[3][3];
for (row=0;row < resl.length;row++){ // multiplicação das matrizes
for (column=0; column < resl[row].length;column++){
aux = 0;
for (i=0;i < m1[row].length;i++)
{
aux= aux + m1[row][i]*m2[i][column];
}
resl[row][column]=aux;
}
}
System.out.println("Matriz A");
for (row = 0 ; row < m1.length;row++){
for (column= 0 ; column < m1[row].length; column++)
{
System.out.printf("%d ", m1[row][column]);
}
System.out.println();
}
System.out.println();
System.out.println();
System.out.println("Matriz B");
for (row = 0 ; row < m2.length;row++){
for (column= 0 ; column < m2[row].length; column++){
System.out.printf("%d ", m2[row][column]);
}
System.out.println();
}
System.out.println();
System.out.println();
System.out.println("Mutiplicação da matriz 1 e 2: ");
for (row = 0 ; row < resl.length;row++){
for (column= 0 ; column < resl[row].length; column++){
System.out.printf("%d ", resl[row][column]);
}
System.out.println();
}
}
public static void main (String[] args) throws RemoteException{
new ServidorMatriz();
}
}
public class Cliente {
public Cliente() {
iniciar();
}
public void iniciar() {
String opcao;
int[][] a = null;
int[][] b = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
Registry registry = LocateRegistry.getRegistry("localhost", 3637);
IntMatriz intf = (IntMatriz) registry.lookup("Matriz");
System.out.println("Para enviar os dados entre com 'Envia");
System.out.println("Para sair, pressione a tecla 'Enter'");
System.out.println("Opção: ");
opcao = br.readLine();
while (!opcao.equals("")) {
if (opcao.equalsIgnoreCase("envia")) {
tente(intf, a, b);
} else {
System.out.println("Opcao Inválida!");
}
System.out.println("Opcao: ");
opcao = br.readLine();
}
System.out.println("Sair");
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
public void enviaDados(IntMatriz intf, double[][] m1, double[][] m2){
double a[][] = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
double b[][] = {{3.,2.,6},{9.,5.,8.},{1.,4.,2.}};
m1 = a;
m2 = b;
System.out.println(m1);
System.out.println(m2);
}*/
public void tente(IntMatriz intf, int a[][],int b[][]){
int mat1[][] = new int[3][3];
int mat2[][] = new int[3][3];
a = mat1;
b = mat2;
}
public static void main(String[] args) {
new Cliente();
}
}