D:\DEV\PROJECTS\Harbor_ATM_DEMO_SHIP\src\harbor_atm_demo\CD_UNIT.java
/*
 * CD_UNIT.java
 */

package harbor_atm_demo;
import kewlstuff.atm.demo.interfaces.*;
import kewlstuff.harbor.client.Vessel; //Added ref to library
import javax.swing.JOptionPane;


/**
 *
 * @author JK
 * Classic Coherent Diffusion Again...
 * Using the classic pattern of starting the 'server', passing the
 * CD_Unit in the UI, and letting the UI callback into required
 * server methods... see tutorials for more information
 *
 * Some additional notes
 * When running standalone we used the harbor_client_lib to get at
 * the digital signature functionality inherent in that lib.
 * And we used it on both client and server side.
 * In the SHIP this library is also the vessel so the same library
 * does both. 
 * On the server side we do not have to put it in the repository at all
 * because the client is part of the servers inherent functionality as well.
 * So.. the functionality of this library is essentially free, as it should be
 * because this functionality is also used in Harbors security, and one is
 * really just tapping into that.
 * One can use their own signature libs... its POJO, but the application will
 * pick up the overhead of that library, on client and server.
 *
 * This demo also uses the new special folder MUST_BE_SEC
 * This means that even if one modified the client, and removed certifiedSecurity
 * it will not then communicate insecurely... the server will reject the
 * client. ie in this example the "BANK" forces secure comms to be used.
 *
 * Also demo's a few other tricks, like client side property config files, logs, 
 * detecting comms failures etc.
 */
public class CD_UNIT implements I_CD_Unit{
    I_BankInstructionProcessor i_BankInstructionProcessor = null;
    private Vessel vessel = null; //Added
    
    /** Creates a new instance of CD_UNIT */
    public CD_UNIT() {
        //Tell this code where our application server is
        String harborUrl = "http://localhost:8080/harbor/service"; 

        //Allows one to change the url without re-compiling this example
        //Nice for testing on other machines... the getenv Java function has issues
        Vessel.loadUserConfig(); //loads <user location>/harbor/user/config.txt
        String harborTestUrl = Vessel.getUserProperty("HARBOR_SITE_URL");
        if(harborTestUrl != null){
                Vessel.setLocalAdminLogMessage("URL environment variable set to " + harborTestUrl);
                harborUrl = harborTestUrl;
                JOptionPane.showMessageDialog(null,harborUrl, "URL Setting in config file", JOptionPane.INFORMATION_MESSAGE);
        }

        vessel = new Vessel(harborUrl); //get a vessel from the harbor 
        vessel.enableProgressDisplay(true); //for internet apps
        
        //On a system that passes money around, this is essential... SSL80
        boolean fSuccess = vessel.certifiedSecurity("Company B"); //Secure comms
    }
    
    public void startATM(){
        
        if(!vessel.isCommsSecure()){
            JOptionPane.showMessageDialog(null, "A secure line could not be established", "Cannot Continue", JOptionPane.WARNING_MESSAGE);
            return;
        } 
        
        i_BankInstructionProcessor = (I_BankInstructionProcessor)vessel.loadRemoteClassInst(I_BankInstructionProcessor.class,"kewlstuff.atm.demo.server.BankInstructionProcessor");
        
        Class uiApp = vessel.getRemoteClass("kewlstuff.atm.demo.UI.ATM_Terminal"); 
        if(uiApp != null){ 
            I_UI atmTerminal = (I_UI)vessel.newInst(uiApp);
            atmTerminal.start(this);
        }
    }
    
    public void closeDownServer(){
        if(i_BankInstructionProcessor != null) vessel.releaseRemoteClassInst(i_BankInstructionProcessor);//Release remote class
    }
    
    public byte[] processRemoteInstruction(byte[] bInstruction, byte[] userCertificate, byte[] auxData){
        if(i_BankInstructionProcessor == null) return null;
        
        try {//This is a simple way to detect communication failures
            return i_BankInstructionProcessor.processRemoteInstruction(bInstruction,userCertificate,auxData);
        } catch(Exception e){
            JOptionPane.showMessageDialog(null, "Please check the internet connection and retry", "Communications Failed", JOptionPane.WARNING_MESSAGE);        
        }
   
         return null;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new CD_UNIT().startATM();
    }
    
}