001package edu.pdx.cs410J.rmi;
002
003import java.net.*;
004import java.rmi.*;
005import java.rmi.server.*;
006
007/**
008 * Instances of this remote class reside in a server JVM and perform
009 * the work of solving an equation of the form <code>Ax = b</code>.
010 */
011public class EquationSolverImpl extends UnicastRemoteObject
012  implements EquationSolver {
013
014  /**
015   * Creates a new <code>EquationSolverImpl</code>.  Invokes
016   * <code>UnicastRemoteObject's</code> constructor to register this
017   * object with the RMI runtime.
018   */
019  public EquationSolverImpl() throws RemoteException {
020    super();
021  }
022  
023  /**
024   * Use Cholesky's algorithm to solve a linear system of equations of
025   * the <code>Ax = b</code>.
026   *
027   * @param matrix
028   *        The <code>A</code> in <code>Ax = b</code>
029   * @param constants
030   *        The <code>b</code> in <code>Ax = b</code>
031   *
032   * @throws IllegalArgumentException
033   *         The number of rows and columns in the matrix are not the
034   *         same 
035   * @throws RemoteException
036   *         Something went wrong while communicating with the server
037   */
038  public double[] solve(double[][] matrix, double[] constants) 
039    throws RemoteException {
040
041      return GaussianElimination.solve(matrix, constants);
042  }
043
044  /**
045   * Main program that creates a new <code>EquationSolverImpl</code>
046   * and binds it into the RMI registry under a given name.
047   */
048  public static void main(String[] args) {
049    String host = args[0];
050    int port = Integer.parseInt(args[1]);
051
052    // Install an RMISecurityManager, if there is not a
053    // SecurityManager already installed
054    if (System.getSecurityManager() == null) {
055      System.setSecurityManager(new RMISecurityManager());
056    }
057
058    String name = "//" + host + ":" + port + "/EquationSolver";
059
060    try {
061      Remote solver = new EquationSolverImpl();
062      Naming.rebind(name, solver);
063
064    } catch (RemoteException ex) {
065      ex.printStackTrace(System.err);
066
067    } catch (MalformedURLException ex) {
068      ex.printStackTrace(System.err);
069    }
070  }
071
072}