galaxy.server
Class MainServer

java.lang.Object
  |
  +--galaxy.server.MainServer
All Implemented Interfaces:
java.lang.Runnable

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

This is the base class for all Galaxy Java server applications. MainServer is responsible for listening for connections and spawning Servers to service requests for those connections. MainServer can also be configured to connect to a specified set of Hubs upon startup rather than listening for connections.

You may add a MainServerListener to receive events generated by this class.

After constructing the main server, make calls to set options, then call init() to perform one time initialization. Options include the port number to listen on, the name of the server class for handling connections, and a list of Hub host/port pairs to which this server will connect (if this latter option is set, the listener port setting for this server is ignored). After this you may call start() to start listening for connections or connect this server to the specified Hub(s).

In the case where this server is listening for connections, when a new connection is received, a server is created to handle the connection. The name of the server class may be specified using setServerClassName before the main server is started. It defaults to galaxy.server.Server The name is used to dynamically load the server class, and instantiate an instance to handle the connection. This class must be a subclass of Server.

In the case where this server is configured to connect to a Hub or set of Hubs, the connections are established, and a server is created for each connection to handle messages from the associated Hub.

This class is intended to be very generic. Subclasses are used to perform more specfic tasks.

See Also:
Server, MainServerListener

Field Summary
protected  ServerArgParser argParser
          Argument parser.
static int DEFAULT_HUB_CONTACT_POLICY
          Default policy when contacting Hubs is:
static int DEFAULT_VERBOSITY_LEVEL
          Default verbosity level (all logging on).
protected static int GAL_BROKER_LISTENER
          If this bit is set in this servers connection flag, the server listens for connections from broker clients.
protected static int GAL_CONNECTION_LISTENER
          If this bit is set in this servers connection flag, the server listens for connections from Hubs.
protected static int GAL_HUB_CLIENT
          If this bit is set in this servers connection flag, the server contacts Hubs when establishing connections.
static int GAL_HUB_CLIENT_CONNECT_FAILURE_MASK
           
static int GAL_HUB_CLIENT_CONNECT_FAILURE_NOOP
          Indicates server should only make one attempt to contact Hub.
static int GAL_HUB_CLIENT_CONNECT_FAILURE_RETRY
          Indicates server should keep trying to connect to Hub until successful.
static int GAL_HUB_CLIENT_CONNECT_FAILURE_SHUTDOWN
          Indicates server should shutdown if it can not establish initial connection to Hub.
static int GAL_HUB_CLIENT_DISCONNECT_MASK
           
static int GAL_HUB_CLIENT_DISCONNECT_NOOP
          Indicates server should do nothing if connection to Hub is lost.
static int GAL_HUB_CLIENT_DISCONNECT_RETRY
          Indicates server should try to reconnect to Hub if connection to Hub is lost.
static int GAL_HUB_CLIENT_DISCONNECT_SHUTDOWN
          Indicates server should shutdown if connection to Hub is lost.
protected static int GAL_SERVER_TYPE_MASK
           
protected  java.lang.String name
          This server's name.
protected  java.lang.String serverClassName
          The name of the Server class to construct to handle a new socket connection.
 
Constructor Summary
MainServer()
          Default constructor.
MainServer(java.lang.String name)
          Constructor.
MainServer(java.lang.String name, java.lang.String[] args)
          This constructor initializes itself based on the command line arguments.
MainServer(java.lang.String name, java.lang.String[] args, java.lang.String helpText)
          This constructor initializes itself based on the command line arguments.
 
Method Summary
 void addMainServerListener(MainServerListener listener)
          Add a MainServerListener.
protected  void cleanup()
          This cleanup method is called when this server is preparing to stop.
 void contactHub(java.lang.String host, int port, java.lang.String sessionId, int hubContactPolicy)
          Adds the specified Hub info to the list of Hubs to contact.
protected  void error(java.lang.String msg)
           
protected  void error(java.lang.String msg, java.lang.Exception ex)
           
protected  void fatal(java.lang.String msg)
           
protected  void fatal(java.lang.String msg, java.lang.Exception ex)
           
 int getHubContactPolicy()
          Gets the current policy for contacting Hubs.
 int getMaxConnections()
           
 java.lang.String getName()
          Get this server's name.
 int getPort()
          Returns the the current listener port (or the default) of this server.
 boolean getRequirePort()
          Deprecated. As of Galaxy Communicator 3.0. Use isRequiredPort.
 java.lang.String getServerClassName()
           
 java.lang.String getSessionId()
           
static int getVerbosityLevel()
          Returns the current logging verbosity level.
 void init()
          Call this function to initialize the main server.
protected  void initServer(Server server)
          Called by the framework after createServer().
 boolean isBrokerListener()
           
 boolean isHubClient()
           
 boolean isHubListener()
           
 boolean isRequiredPort()
           
 boolean isRunning()
           
protected  boolean listenerHasPort()
          Tests if the listener thread started by this server has acquired a port.
protected  void log(java.lang.String msg)
           
protected  void registerOutBroker(DataOutBroker broker)
          Registers an outgoing broker that is managed by this server.
 void removeMainServerListener(MainServerListener listener)
           
 void run()
          This server's main loop.
 void setActsAsBrokerListener(boolean actsAsBrokerListener)
           
 void setActsAsHubClient(boolean actsAsHubClient)
           
 void setActsAsHubListener(boolean actsAsHubListener)
           
 void setHubContactInfoCollection(java.util.List newHubContactInfoCollection)
           
 void setHubContactPolicy(int hubContactPolicy)
          Sets the Hub contact policy (used if this server is a Hub client).
 void setMaxConnections(int maxConnections)
           
 void setName(java.lang.String name)
          Set this server's name.
 void setPort(int port)
          Sets the listener port used by this server.
 void setRequirePort(boolean requirePort)
          Sets the flag that indicates if the current listener port is required (i.e., if true, the server should exit if it cannot obtain the port).
 void setServerClassName(java.lang.String serverClassName)
           
 void setSessionId(java.lang.String sessionId)
           
 void setValidate(boolean validate)
           
static void setVerbosityLevel(int verbosityLevel)
          Sets the current logging verbosity level.
 void start()
          Starts this server's thread if it is not already running.
protected  boolean startBrokerListenerThread()
          Starts this server's listener thread if it is not already running.
 void stop()
          Marks this server's thread as being stopped.
protected  boolean unregisterOutBroker(DataOutBroker broker)
          Unregisters an outgoing broker is managed by this server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serverClassName

protected volatile java.lang.String serverClassName
The name of the Server class to construct to handle a new socket connection.

name

protected java.lang.String name
This server's name.

argParser

protected ServerArgParser argParser
Argument parser.

GAL_CONNECTION_LISTENER

protected static final int GAL_CONNECTION_LISTENER
If this bit is set in this servers connection flag, the server listens for connections from Hubs.

GAL_BROKER_LISTENER

protected static final int GAL_BROKER_LISTENER
If this bit is set in this servers connection flag, the server listens for connections from broker clients.

GAL_HUB_CLIENT

protected static final int GAL_HUB_CLIENT
If this bit is set in this servers connection flag, the server contacts Hubs when establishing connections.

GAL_SERVER_TYPE_MASK

protected static final int GAL_SERVER_TYPE_MASK

GAL_HUB_CLIENT_CONNECT_FAILURE_RETRY

public static final int GAL_HUB_CLIENT_CONNECT_FAILURE_RETRY
Indicates server should keep trying to connect to Hub until successful.

GAL_HUB_CLIENT_CONNECT_FAILURE_SHUTDOWN

public static final int GAL_HUB_CLIENT_CONNECT_FAILURE_SHUTDOWN
Indicates server should shutdown if it can not establish initial connection to Hub.

GAL_HUB_CLIENT_CONNECT_FAILURE_NOOP

public static final int GAL_HUB_CLIENT_CONNECT_FAILURE_NOOP
Indicates server should only make one attempt to contact Hub.

GAL_HUB_CLIENT_CONNECT_FAILURE_MASK

public static final int GAL_HUB_CLIENT_CONNECT_FAILURE_MASK

GAL_HUB_CLIENT_DISCONNECT_RETRY

public static final int GAL_HUB_CLIENT_DISCONNECT_RETRY
Indicates server should try to reconnect to Hub if connection to Hub is lost.

GAL_HUB_CLIENT_DISCONNECT_SHUTDOWN

public static final int GAL_HUB_CLIENT_DISCONNECT_SHUTDOWN
Indicates server should shutdown if connection to Hub is lost.

GAL_HUB_CLIENT_DISCONNECT_NOOP

public static final int GAL_HUB_CLIENT_DISCONNECT_NOOP
Indicates server should do nothing if connection to Hub is lost.

GAL_HUB_CLIENT_DISCONNECT_MASK

public static final int GAL_HUB_CLIENT_DISCONNECT_MASK

DEFAULT_HUB_CONTACT_POLICY

public static final int DEFAULT_HUB_CONTACT_POLICY
Default policy when contacting Hubs is:

a) When trying to establish connection to Hub, keep trying until successful.

b) When connection to Hub is lost, try to reconnect.


DEFAULT_VERBOSITY_LEVEL

public static final int DEFAULT_VERBOSITY_LEVEL
Default verbosity level (all logging on).
Constructor Detail

MainServer

public MainServer()
Default constructor. Note that this does not start the server.

MainServer

public MainServer(java.lang.String name)
Constructor. Note that this does not start the server.
Parameters:
name - the server's name

MainServer

public MainServer(java.lang.String name,
                  java.lang.String[] args)
This constructor initializes itself based on the command line arguments. Currently, only these command line arguments are handled by the server:
-port
-assert
-maxconns
-contact_hub
-session_id
-server_locations_file
-validate
-verbosity
Note that this constructor does not start the server.
Parameters:
name - the server's name
args - the command line arguments

MainServer

public MainServer(java.lang.String name,
                  java.lang.String[] args,
                  java.lang.String helpText)
This constructor initializes itself based on the command line arguments. Currently, only these command line arguments are handled by the server:
-port
-assert
-maxconns
-contact_hub
-session_id
-server_locations_file
-validate
-verbosity
Note that this constructor does not start the server.
Parameters:
name - the server's name
args - the command line arguments
helpText - this text is prepended to the message that is displayed when the -help command line argument is used
Method Detail

isRunning

public boolean isRunning()

setActsAsHubListener

public void setActsAsHubListener(boolean actsAsHubListener)

isHubListener

public boolean isHubListener()

setActsAsBrokerListener

public void setActsAsBrokerListener(boolean actsAsBrokerListener)

isBrokerListener

public boolean isBrokerListener()

setActsAsHubClient

public void setActsAsHubClient(boolean actsAsHubClient)

isHubClient

public boolean isHubClient()

setValidate

public void setValidate(boolean validate)

setSessionId

public void setSessionId(java.lang.String sessionId)

getSessionId

public java.lang.String getSessionId()

setPort

public void setPort(int port)
Sets the listener port used by this server. If setRequirePort has been previously called, calling setPort will not change the listener port used by this server. setRequirePort would need to be called again to set the required port to be the new port.
Parameters:
port - this server's listener port

getPort

public int getPort()
Returns the the current listener port (or the default) of this server.

setRequirePort

public void setRequirePort(boolean requirePort)
Sets the flag that indicates if the current listener port is required (i.e., if true, the server should exit if it cannot obtain the port).
Parameters:
requirePort - true if (the current port is required), false otherwise

getRequirePort

public boolean getRequirePort()
Deprecated. As of Galaxy Communicator 3.0. Use isRequiredPort.


isRequiredPort

public boolean isRequiredPort()

setServerClassName

public void setServerClassName(java.lang.String serverClassName)

getServerClassName

public java.lang.String getServerClassName()

setHubContactInfoCollection

public void setHubContactInfoCollection(java.util.List newHubContactInfoCollection)

setMaxConnections

public void setMaxConnections(int maxConnections)

getMaxConnections

public int getMaxConnections()

setName

public void setName(java.lang.String name)
Set this server's name.
Parameters:
name - the server's name. A value of null is ignored and the current name is retained.

getName

public java.lang.String getName()
Get this server's name.
Returns:
the server's name

setHubContactPolicy

public void setHubContactPolicy(int hubContactPolicy)
Sets the Hub contact policy (used if this server is a Hub client). If an invalid policy is specified, no change is made.
Parameters:
hubContactPolicy - the policy. Valid values include MainServer.DEFAULT_HUB_CONTACT_POLICY, and any or'ed combination of one of a) MainServer.GAL_HUB_CLIENT_CONNECT_FAILURE_RETRY, MainServer.GAL_HUB_CLIENT_CONNECT_FAILURE_SHUTDOWN, or MainServer.GAL_HUB_CLIENT_CONNECT_FAILURE_NOOP, and one of b) MainServer.GAL_HUB_CLIENT_DISCONNECT_RETRY, MainServer.GAL_HUB_CLIENT_DISCONNECT_SHUTDOWN, and MainServer.GAL_HUB_CLIENT_DISCONNECT_NOOP.

getHubContactPolicy

public int getHubContactPolicy()
Gets the current policy for contacting Hubs.
Returns:
Hub contact policy flag

setVerbosityLevel

public static void setVerbosityLevel(int verbosityLevel)
Sets the current logging verbosity level.
Parameters:
verbosityLevel - the verbosity level. At the moment, the only valid values are0 (no logging) and 1 (all logging on). If an invalid value is specified, the current verbosity level is not changed.

getVerbosityLevel

public static int getVerbosityLevel()
Returns the current logging verbosity level.
Returns:
the verbosity level. At the moment, the only valid values are 0 (no logging) and 1 (all logging on).

contactHub

public void contactHub(java.lang.String host,
                       int port,
                       java.lang.String sessionId,
                       int hubContactPolicy)
Adds the specified Hub info to the list of Hubs to contact.
Parameters:
host - name of the Hub's host machine
port - listener port of the Hub
sessionId - session id to use for the connection
hubContactPolicy - Hub contact policy flag (if -1, current policy of this MainServer is always used).

listenerHasPort

protected boolean listenerHasPort()
Tests if the listener thread started by this server has acquired a port.
Returns:
true if the listener has a port, false otherwise (including when this server does not have a listener thread)

startBrokerListenerThread

protected boolean startBrokerListenerThread()
Starts this server's listener thread if it is not already running. The server thread must be running in order for the listener thread to run. There is only on listener thread listening for both Hub and brokers connections, so if the "Hub listener" is running, no other thread is started.
Returns:
false if the server is not running

init

public void init()
          throws java.lang.Exception
Call this function to initialize the main server. It is called after setting various parameters, such as the port number and dialogue file name. It must be called exactly once. Upon return, you may call start() and stop().

start

public void start()
Starts this server's thread if it is not already running.

cleanup

protected void cleanup()
This cleanup method is called when this server is preparing to stop. This is called after the "listener" and/or "client" threads have stopped and all Server objects (and their outgoing brokers) created by this server have stopped. Override this method to provide your own customized cleanup routine.

stop

public void stop()
Marks this server's thread as being stopped. The thread is not actually stopped until this condition is tested in the thread's main loop.

run

public void run()
This server's main loop. The server starts the listener and/or client thread(s) as needed and then waits to be stopped. Once it is interrupted, it stops the listener and/or client thread, stops all server threads it started, and stops all outgoing brokers that it is managing.
Specified by:
run in interface java.lang.Runnable

initServer

protected void initServer(Server server)
                   throws java.lang.Exception
Called by the framework after createServer(). The method may be redefined by subclasses to perform specialized server initialization.

registerOutBroker

protected void registerOutBroker(DataOutBroker broker)
Registers an outgoing broker that is managed by this server.
Parameters:
broker - the broker

unregisterOutBroker

protected boolean unregisterOutBroker(DataOutBroker broker)
Unregisters an outgoing broker is managed by this server.
Parameters:
broker - the broker
Returns:
true if the broker was found and unregistered, false if the broker was not found

addMainServerListener

public void addMainServerListener(MainServerListener listener)
Add a MainServerListener. Multiple listeners are permitted.

removeMainServerListener

public void removeMainServerListener(MainServerListener listener)

log

protected void log(java.lang.String msg)

error

protected void error(java.lang.String msg)

error

protected void error(java.lang.String msg,
                     java.lang.Exception ex)

fatal

protected void fatal(java.lang.String msg)

fatal

protected void fatal(java.lang.String msg,
                     java.lang.Exception ex)