The server demo program communicates with the client, runs the
simulation, and provides simulation results to the client. The code
listings are given for the following classes:
The directory path for the package for the Server
class goes as
javatech
|
|__ all20
|
|__
server
|
|__
impl
Server.java
|
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
|
The directory path for the package for the ServerInterface
and FactoryInterface codes goes as
javatech
|
|__ all20
|
|__
server
|
package
javatech.all20.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ServerInterface extends Remote
{
public boolean initialize (String initparam) throws
RemoteException;
public void initializeSimulation (float[] indata) throws
RemoteException;
public void start () throws
RemoteException;
public float[] retrieveData (float[] indata) throws
RemoteException;
} // ServerInterface
|
FactoryInterface.java |
package
javatech.all20.server;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface FactoryInterface extends Remote
{
final static String FACTORY_NAME = "all20ServerFactory";
public ServerInterface getInstance (String id)
throws RemoteException;
} // FactoryInterface
package javatech.all20.server.impl;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import javatech.all20.server.FactoryInterface;
import javatech.all20.server.ServerInterface;
//-----------------------------------------------------------------------
// CLASS: ServerFactory -
/************************************************************************
* Implementation of the FactoryInterface
************************************************************************/
public class ServerFactory
extends UnicastRemoteObject
implements FactoryInterface
{
private static int fServerIndex__ = 0;
/** Default constructor. Does nothing, but needed
since it is required
* to throw RemoteException. **/
public ServerFactory () throws RemoteException
{
} // ServerFactory
//-------------------------------------------------------------------
// METHOD: getInstance -
/********************************************************************
* Returns a ServerInterface instance
-- i.e., an object that
* implements ServerInterface
*
* @param uid ID string
*
* @return an
object that implements ServerInterface
*
* @throws RemoteException if
any RMI error occurs
*******************************************************************/
public ServerInterface getInstance (String id)
throws RemoteException {
System.err.println ("\nServerFactory:
making a new Server instance");
fServerIndex__ ++;
String server_id = id + "-" + fServerIndex__;
ServerInterface server = new Server
(this, server_id);
return server;
} // getInstance
//-------------------------------------------------------------------
// METHOD: main -
/********************************************************************
* Starts the rmiregistry and the ServerFactory.
The
* first command line argument, if present, will
be used as the port
* number. If not present, the default is hardocded
to be port 3000.
*******************************************************************/
public static void main (String[] args) {
// Get the port number -- either from
the server.port system property
// or use the default of 3000.
int port = Integer.getInteger ("server.port",
3000).intValue ();
// Start up the rmiregistry on that
port number.
try {
java.rmi.registry.LocateRegistry.createRegistry
(port);
}
catch (RemoteException re) {
System.err.println ("Could
not create remiregistry: " + re);
}
// Create a ServerFactory and bind
it into the registry.
try {
ServerFactory factory
= new ServerFactory ();
Naming.rebind ("//localhost:"
+ port + "/" + FACTORY_NAME, factory);
System.err.println ("\nREADY
AND WAITING ON CLIENTS ON PORT " +
port + "...");
} // try
catch (MalformedURLException mue)
{
System.err.println (mue);
}
catch (RemoteException re) {
System.err.println (re);
System.err.println ("\nEXITING
BECAUSE OF FAILURE");
System.exit (1);
}
} // main
} // ServerFactory |
References & Web Resources
- Information on encryption and user authentication and authorization:
- JAAS
- Java Authentication and Authorization Service
- JCE
- Java Cryptography Extension
- JCA
- Java Cryptography Architecture
Most recent update: Oct. 18, 2005
|