org.hebe.mps
Class Server

java.lang.Object
  |
  +--org.hebe.mps.Server
All Implemented Interfaces:
java.lang.Runnable

public class Server
extends java.lang.Object
implements java.lang.Runnable

Implements all the code needed to get an implementation of an MPS interface running as an MPS server. Pass in your desired objectName and an implementation of the correct interface to the automatically-generated subclasses of this class. It will spawn a thread that listens for connections to the server object.

Note that the implementation object is managed by subclasses of this class, and that this class knows nothing about the implementation of the MPS interface at all.

Author:
Tony Garnock-Jones
See Also:
Proxy

Inner Class Summary
private  class Server.ConnectionThread
          This class implements the body of the thread that is run for each client that connects to an MPS interface server.
 
Field Summary
private  java.net.InetAddress address
          Address of the interface our server socket is bound to
private static java.util.Hashtable allServers
          Map used to associate an MPSIDL interface instance with an instance of class Server.
private  java.lang.String boundName
          Machine-readable address of this object
private  java.util.Vector methodTable
          Look-up-table mapping method number to method implementation.
private  boolean multiThreaded
          Set to true if we are to serve access to our implementation object in a multithreaded way; set to false to serialize all accesses
private  java.lang.String objectName
          Human-readable name for this object
private  int portNumber
          TCP Port number we're listening on
private  java.net.ServerSocket serverSocket
          Server socket that clients connect to us on.
 
Constructor Summary
Server(java.lang.String objectName)
          Create a new server named objectName.
Server(java.lang.String objectName, java.net.InetAddress address, int portNumber)
          Create a new server named objectName.
Server(java.lang.String objectName, int portNumber)
          Create a new server named objectName.
 
Method Summary
 void dispatch(int index, InputStream in, OutputStream out)
          Dispatches to a method on this MPS interface server.
 java.lang.String getBoundName()
          Return the canonical name of this server.
 boolean getMultiThreaded()
          Returns true if this server is running multithreaded.
 void registerMethod(int index, java.lang.String name, Thunk thunk)
          Register a method-handler with the server.
protected static void registerServer(java.lang.Object key, Server svr)
          Registers an instance of Server against the MPSIDL interface object that it is serving.
 void run()
          The main body of the server thread.
static Server serverFor(java.lang.Object key)
          Retrieve the Server associated with a particular MPSIDL interface instance.
 void setMultiThreaded(boolean m)
          Determines whether multiple threads (one thread per client connection, remember) may access methods on the implementation object simultaneously.
private  void setup()
          Creates and starts the thread, once all the instance variables have been set up.
private  void setup(java.lang.String objectName, java.net.InetAddress address, int portNumber)
          Performs all the steps needed to get the server started.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

allServers

private static java.util.Hashtable allServers
Map used to associate an MPSIDL interface instance with an instance of class Server.
See Also:
OutputStream.writeReference(java.lang.Object)

objectName

private java.lang.String objectName
Human-readable name for this object

boundName

private java.lang.String boundName
Machine-readable address of this object

serverSocket

private java.net.ServerSocket serverSocket
Server socket that clients connect to us on.

address

private java.net.InetAddress address
Address of the interface our server socket is bound to

portNumber

private int portNumber
TCP Port number we're listening on

multiThreaded

private boolean multiThreaded
Set to true if we are to serve access to our implementation object in a multithreaded way; set to false to serialize all accesses
See Also:
setMultiThreaded(boolean)

methodTable

private java.util.Vector methodTable
Look-up-table mapping method number to method implementation.
See Also:
Thunk, dispatch(int, org.hebe.mps.InputStream, org.hebe.mps.OutputStream)
Constructor Detail

Server

public Server(java.lang.String objectName)
       throws MPSException
Create a new server named objectName. Bind to INADDR_ANY, on any unused port number.
Parameters:
objectName - human-readable name for this server
Throws:
MPSException - if anything goes wrong setting up the server

Server

public Server(java.lang.String objectName,
              int portNumber)
       throws MPSException
Create a new server named objectName. Bind to INADDR_ANY, on the passed-in port number.
Parameters:
objectName - human-readable name for this server
portNumber - the port to use to accept connections on
Throws:
MPSException - if anything goes wrong setting up the server

Server

public Server(java.lang.String objectName,
              java.net.InetAddress address,
              int portNumber)
       throws MPSException
Create a new server named objectName. Bind to a specific interface, on the passed-in port number.
Parameters:
objectName - human-readable name for this server
address - the InetAddress of the interface to bind to
portNumber - the port to use to accept connections on
Throws:
MPSException - if anything goes wrong setting up the server
Method Detail

registerServer

protected static void registerServer(java.lang.Object key,
                                     Server svr)
Registers an instance of Server against the MPSIDL interface object that it is serving. The MPSIDL compiler emits calls to this method when compiling MPSIDL specifications down to java skeletons.
Parameters:
key - the MPSIDL interface instance being served
svr - the server object we are registering

serverFor

public static Server serverFor(java.lang.Object key)
Retrieve the Server associated with a particular MPSIDL interface instance.
Parameters:
key - the MPSIDL interface instance we're interested in
Returns:
an instance of Server, or null if key is not being served

getBoundName

public java.lang.String getBoundName()
Return the canonical name of this server.
Returns:
the string representation of the canonical name

setup

private void setup(java.lang.String objectName,
                   java.net.InetAddress address,
                   int portNumber)
            throws MPSException
Performs all the steps needed to get the server started.
Parameters:
objectName - human-readable name for this server
address - the InetAddress of the interface to bind to - null for INADDR_ANY
portNumber - the port to use to accept connections on - 0 for any unused
Throws:
MPSException - if anything goes wrong setting up the server

setup

private void setup()
            throws MPSException
Creates and starts the thread, once all the instance variables have been set up.
Throws:
MPSException - if the specified local socket address is already taken

getMultiThreaded

public boolean getMultiThreaded()
Returns true if this server is running multithreaded.
Returns:
the current multithreadedness of this server

setMultiThreaded

public void setMultiThreaded(boolean m)
Determines whether multiple threads (one thread per client connection, remember) may access methods on the implementation object simultaneously. If not, all dispatches to the implementation object go through a synchronized lock.
Parameters:
m - set to true to enable parallel access, or to false to enforce serial access

dispatch

public void dispatch(int index,
                     InputStream in,
                     OutputStream out)
              throws MPSException
Dispatches to a method on this MPS interface server.
Parameters:
index - the index of the method to call
in - the inputstream used by the method to read in parameters
out - the outputstream used by the method to return results
Throws:
MPSException - if the method causes it to be raised, or if the method index is out-of-range

registerMethod

public void registerMethod(int index,
                           java.lang.String name,
                           Thunk thunk)
Register a method-handler with the server.
Parameters:
index - the method-index to use to register under
name - the name of the method - not currently used, but useful for debugging
thunk - the object to call when the method is invoked

run

public void run()
The main body of the server thread.
Specified by:
run in interface java.lang.Runnable