package javatech.all20.server.impl;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javatech.all20.server.*;
//-----------------------------------------------------------------------
// CLASS: Server -
/************************************************************************
* Implementation of Chapter 20's ServerInterface.
************************************************************************/
public class Server extends UnicastRemoteObject implements ServerInterface
{
private String fID;
private ServerFactory fFactory;
private SimData fSimData;
private SimulationThread fSimulationThread;
private boolean fInitialized;
//-------------------------------------------------------------------
// CONSTRUCTOR -
/********************************************************************
* Constructs a Server object that implements the
* ServerInterface.
*
* @param myfac the ServerFactory that calls us
* @param id ID string
*
* @throws RemoteException if any RMI error occurs or if the server
* cannot be created for some reason
*******************************************************************/
public Server (ServerFactory myfac, String id) throws RemoteException {
System.err.println ("Server ctor: thread count = " + Thread.activeCount());
fFactory = myfac;
fID = id;
// Create a SimData to hold the input and output data.
fSimData = new SimData ();
// Create a SimulationThread, providing it a reference to the SimData.
fSimulationThread = new SimulationThread (fSimData);
} // Server constructor
// Used internally in this package only, so package (default) access scope.
String getID () { return fID; }
//-------------------------------------------------------------------
// METHOD: initialize -
/********************************************************************
* Initializes the server by accepting the supplied project name. In
* this trivial example any project name at all is acceptable. In a
* real case
*
* @param projectname the name for this project
*
*
* @return true if the server was succefully initialized, false
* otherwise
* @throws RemoteException if any error occurs
*******************************************************************/
public boolean initialize (String initparam) throws RemoteException {
System.err.println ("server is initializing for initparam '" + initparam + "'");
fInitialized = true;
return true;
} // initialize
//-------------------------------------------------------------------
// METHOD: initializeSimulation -
/********************************************************************
* Initializes the simulation with initial input data.
*
* @param indata array of initialization data
*
* @throws RemoteException if any error occurs
*******************************************************************/
public void initializeSimulation (float[] indata) throws RemoteException {
fSimulationThread.initialize (indata);
} // initializeSimulation
//-------------------------------------------------------------------
// METHOD: start -
/********************************************************************
* Starts the simulation running. This method returns asynchronously
* while the simulation continues to run in another thread. Further
* interaction with the simulation occurs through the retrieveData
* method.
*
* @throws RemoteException if any error occurs
*******************************************************************/
public void start () throws RemoteException {
fSimulationThread.start ();
} // start
//-------------------------------------------------------------------
// METHOD: retrieveData -
/********************************************************************
* Retrieves the most recent data set from the running simulation and
* supplies a new set of input data.
*
* @param indata array containing new input data set
*
* @return array containing the current data set
*
* @throws RemoteException if any error occurs
*******************************************************************/
public float[] retrieveData (float[] indata) throws RemoteException {
return fSimData.getResults (indata);
} // retrieveData
} // Server