rockcairn logoRockCairn.com rockcairn logo
/************************************************************
* ComputeEngine.java
*
* Aaron Penner
* MidTerm
* Due 04/04/2000 11:59PM
*
* Remote Object Server provider
* Input:
* Output:
**************************************************************/

import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

public class ComputeEngine extends UnicastRemoteObject implements Compute{
public ComputeEngine() throws RemoteException{
super();
}

/* NOT IN THE REMOTE INTERFACE */
/*************************************************
* returns int ( -1 for no account)
* ( or position of account in vector )
**************************************************/
public int getAccountExist(int acc_num) {
int ret_val = -1;
if (_accounts.size() > 0){
for (int i = 0; i < _accounts.size(); i++){
AccountData ad = (AccountData) _accounts.elementAt(i);
if (ad.getAccountNumber() == acc_num){
ret_val = i;
break;
}
}
}
return ret_val;
}

/* NOT IN THE REMOTE INTERFACE */
/******************************************************
* returns int ( -1 for account aready exists)
* ( or position of added account in vector )
*******************************************************/
public int getAccountCreate(int acc_num) {
int ret_val = -1;
if(getAccountExist(acc_num) == -1){
AccountData acc_data = new AccountData(acc_num);
_accounts.add(acc_data);
ret_val = _accounts.size() - 1; /* vector (0 to n - 1), size (1 to n) */
}
return ret_val;
}

/* THE FOLLOWING ARE IN THE REMOTE INTERFACE */

/*********************************************************
* returns boolean ( false for failure to login to account)
* ( or true for access to account granted )
**********************************************************/
public boolean isAccountLogin(int acc_num, String name, String passwd){
boolean ret_val = false;
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
if ((ad.getAccountNumber() == acc_num) && (name.equals(ad.getAccountName())) &&
(passwd.equals(ad.getAccountPasswd()))){
ret_val = true;
System.out.println("Login is valid for: " + acc_num);
}
}
if (!ret_val){
System.out.println("Login is not valid for: " + acc_num);}

return ret_val;
}

/*********************************************************
* returns boolean ( false for failure to login to account)
* ( or true for access to account granted )
**********************************************************/
public boolean getAccountValid (int acc_num, boolean verify){
boolean ret_val = false;
/* because || short circuts */
if((verify && (getAccountExist(acc_num) != -1)) ||
(!verify && (getAccountCreate(acc_num) != -1))){
ret_val = true;
}
else{ ret_val = false;}

return ret_val;
}

/*******************************************************
* returns boolean ( false for failure to remove account)
* ( or true for account removed )
*******************************************************/
public boolean setAccountRemoval(int acc_num) {
boolean ret_val = false;
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
if (ad.getAccountNumber() == acc_num){
_accounts.remove(vec_pos);
ret_val = true;
}
}
if (!ret_val){
System.out.println("Account remove failed : " + acc_num);}
return ret_val;
}

/*******************************************************
* returns boolean ( false for failure to set attributes)
* ( or true for set acount attributes )
********************************************************/
public boolean setAccountAttb(int acc_num, String name, String passwd){
boolean ret_val = false;
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
ad.setAccountAttributes(name, passwd);
ret_val = true;
}

return ret_val;
}

/*******************************************************
* returns double ( -1.0 for attemp to over draw account)
* ( or the new balance on the account )
********************************************************/
public double addNewTrans(int acc_num, String date, String trans,
String desc, double amount){
double ret_val = -1.0;
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
ret_val = ad.addNewTrans(date, trans, desc, amount);
}
if (ret_val == -1.0){
System.out.println("Failure to add transaction to account : " + acc_num);}
return ret_val;
}



/*******************************************************
* returns int ( -1.0 for account checknum unknown)
* ( or the last posted check num for the account )
********************************************************/
public int getCheckNum(int acc_num) {
int ret_val = -1;
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
ret_val = ad.getChecknum();
}

return ret_val;
}

/*******************************************************
* returns boolean ( false check num update failure )
* ( or true for checknum updated )
********************************************************/
public boolean setCheckNum(int acc_num) {
boolean ret_val = false;
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
ad.setChecknum(ad.getChecknum() + 1);
ret_val = true;
}

return ret_val;
}

/*******************************************************
* returns Vector ( null for no account info)
* ( or model used for display in a table )
********************************************************/
public Vector getTableItems(int acc_num){
int vec_pos = getAccountExist(acc_num);
if (vec_pos != -1){
AccountData ad = (AccountData) _accounts.elementAt(vec_pos);
return (ad.getTableItems());
}
else {return (null);}
}


/***********************************************************
* Start the server and register with the RMI naming service
***********************************************************/
public static void main(String[] args) {
// if (System.getSecurityManager() == null) {
// System.setSecurityManager (new RMISecurityManager());
// }
try {
Compute engine = new ComputeEngine();
//Naming.rebind("rmi://localhost/Compute", engine);
Naming.rebind("rmi://ouray.cudenver.edu:9977/Compute", engine);

System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception: " + e.getMessage());
e.printStackTrace();
}
_accounts = new Vector();
}

/* private data */
private static Vector _accounts;
}