scorej.external.socket
Class SignalServer

java.lang.Object
  |
  +--scorej.external.CommProtocol
        |
        +--scorej.external.socket.SignalServer
All Implemented Interfaces:
java.lang.Runnable

public class SignalServer
extends CommProtocol
implements java.lang.Runnable

This is the Cap Socket implementation of the CommProtocol. It is initiated when SignalServer.startServer is called by startProtocol(). The port parameter tells what port to open the ServerSocket on. The SignalServer will then spawn a Thread that listens for connections. Any external node that connects has a CapConnection created. Under this scheme, every node must open a socket connection to all other nodes it wants to talk to. This leads to n^2 sockets for a fully connected network. This Protocol is meant to be compatible with the old Lisp OMAR Caps. Nodes that connect can also send a registration message, just as the old Caps in the Lisp versions of Omar. A node can register as either an External Cap, Application Cap, or Gui Cap, or as none of the above. Registering as a specific Cap type will cause a filter to be automatically created with the subject set to "EXTERNAL", "APPLICATION", or "GUI". Signals sent with the old sendSignalExternal, sendSignalApplication, or sendSignalGui will have the subject fields set appropriately. Registering as none-of-the-above means that you need to specify your own filters if you want to get any messages. There are 2 special filter messages you can send over the CapConnection. One is a DistOmarEvent_Client with a type of DISTOMAREVENT_CLIENT_KIND_FILTER. The argument of this event should be a Vector, consisting of three Vectors, which create a Filter object. See Filter.java for details. The second message type is also a DistOmarEvent_Client, with a type of DISTOMAREVENT_CLIENT_KIND_CLEAR_FILTER. Sending this message will clear all filters. Note, that when a CapConnection has no filters up, it will get no messages.

See Also:
Filter

Field Summary
 
Fields inherited from class scorej.external.CommProtocol
_props
 
Constructor Summary
SignalServer()
          Default constructor.
 
Method Summary
 boolean acceptConnection(CapConnection cc)
          Placeholder function.
 void changeFilters(java.util.Vector newFilters)
          Changes the signal filters associated with the local node to the new set specified in the arguments.
 void changeFilters(java.util.Vector newFilters, Node n)
          Changes the signal filters associated with the node (local or remote) to the new set specified in the arguments.
 void closeAllConnections()
          Close all remote connections to the SignalServer, by calling closeConnection on all the CapConnection objects.
 void closeProtocol()
          Extends CommProtocol.closeProtocol.
 CapConnection connectToNode(java.lang.String remoteHost, int remotePort)
          Connect to a remote SignalServer and register as the default CapConnection.TYPE_VALUE type.
 CapConnection connectToNode(java.lang.String remoteHost, int remotePort, int type)
          Connect to a remote SignalServer and register with the specified type.
 CapConnection connectToNode(java.lang.String remoteHost, int remotePort, java.lang.String name)
          Connect to a remote SignalServer and register as the default CapConnection.TYPE_VALUE type.
 CapConnection connectToNode(java.lang.String remoteHost, int remotePort, java.lang.String remoteName, int type)
          Connect to a remote SignalServer and register with the specified type.
 java.util.Vector findAllAgents(AgentID ignore)
          Returns all registered agents that match the specified template.
 AgentID findSingleAgent(AgentID template)
          Tries to find a registered agent based on the specified template.
 java.util.Properties getDefaultProperties()
          Implements CommProtocol.getDefaultProperties.
 java.lang.String getHost()
          Getter for name of host machine running the SignalServer.
 Node getLocalNode()
          Gets a reference to the local Node object.
 java.util.Vector getNodes()
          Return a vector of Nodes.
 int getPort()
          Getter for the listen port of the Signal Server.
 void initializeProtocol(java.util.Properties props)
          Implements CommProtocol.initializeProtocol(Properties).
 void registerAgent(AgentID ignore)
          Registers an agent.
 void registerFilter(Filter filter)
          Register a new filter for this node by sending an ADD_FILTER message to all connected Score nodes.
 void registerFilter(Filter filter, Node node)
          Register a new filter for a node.
protected  void reporter(int level, java.lang.String message)
          Wrapper around ScorejLog.log.
 void run()
          Main socket accept thread code.
 void sendSignal(Node node, Signal signal)
          Send a signal to a remote Node.
 void shutdownHandler()
          Shutdown handler will be called when the JVM exits, such when after pressing Ctrl-C on a java process running in the foreground in a console window.
 void signalEventApplication(Node node, Signal signal)
          Deprecated.  
 void signalEventExternal(Node node, Signal signal)
          Deprecated.  
 void signalEventGui(Node node, com.bbn.omar.guiclient.DistOmarEvent_Gui guiEvt)
          Deprecated.  
 void startProtocol()
          Implements CommProtocol.startProtocol(Properties).
 boolean unregisterAgent(AgentID ignore)
          De-registers a previously registered agent.
 void unregisterAllAgents()
          De-registers all registered agents.
 void update()
          Upload any modifications to this node's attributes to the network.
 void waitForConnection(int conns)
          Blocks until the specified number of client connections have been established to this SignalServer.
 
Methods inherited from class scorej.external.CommProtocol
getProperties, isEnabledRegisterAgent, isEnabledRegisterFilter, setEnabledRegisterAgent, setEnabledRegisterFilter, setProperties
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SignalServer

public SignalServer()
Default constructor. Sets listen port to 9000.

Method Detail

update

public void update()
Description copied from class: CommProtocol
Upload any modifications to this node's attributes to the network.

Specified by:
update in class CommProtocol

initializeProtocol

public void initializeProtocol(java.util.Properties props)
Implements CommProtocol.initializeProtocol(Properties). Initializes the SignalServer protocol with the protocol properties object, containing the listen port as a single property.

Specified by:
initializeProtocol in class CommProtocol
Parameters:
props - protocol properties, can be null or empty.

startProtocol

public void startProtocol()
Implements CommProtocol.startProtocol(Properties). Starts the signal server in a separate non-scorej thread that is not registered with the ScoreJ clock. Signal Server begins listening to client connections once this method is called.

Specified by:
startProtocol in class CommProtocol
Throws:
java.lang.RuntimeException - if this method is called before calling the initializeProtocol(java.util.Properties) method.

getDefaultProperties

public java.util.Properties getDefaultProperties()
Implements CommProtocol.getDefaultProperties.

Specified by:
getDefaultProperties in class CommProtocol
Returns:
a java.util.property object contaning the default properties.

getPort

public int getPort()
Getter for the listen port of the Signal Server.

Returns:
listen port, -1 if signal server has yet to start listening to client connections.

getHost

public java.lang.String getHost()
Getter for name of host machine running the SignalServer.

Returns:
String hostname of the ServerSocket, null if the ServerSocket hasn't been created.

reporter

protected void reporter(int level,
                        java.lang.String message)
Wrapper around ScorejLog.log. Logs the specified message at the specified log level.


sendSignal

public void sendSignal(Node node,
                       Signal signal)
Send a signal to a remote Node. Required by the CommProtocol. If the subject of the Signal object is one of the three special types, "EXTERNAL", "APPLICATION", or "GUI", convert the Signal into a DistOmarEvent of the appropriate type. External and Application signals are converted with the signal.convertToVectorSometimes function. Gui "signals" are special, and assumed to be a one element object array, with that one object a DistOmarEvent_Gui. Any other format for a Gui "Signal" is an error. If the "Signal" is does not have one of these three Subjects, create a DistOmarEvent_External out of it, since everything going over the socket must be a DistOmarEvent.

Overrides:
sendSignal in class CommProtocol
Parameters:
node - The node the signal is sent to.
signal - The siganl object to be sent.

signalEventExternal

public void signalEventExternal(Node node,
                                Signal signal)
Deprecated.  

Send a signal with subject set to EXTERNAL.


signalEventApplication

public void signalEventApplication(Node node,
                                   Signal signal)
Deprecated.  

Send a signal with subject set to APPLICATION.


signalEventGui

public void signalEventGui(Node node,
                           com.bbn.omar.guiclient.DistOmarEvent_Gui guiEvt)
Deprecated.  

Does NOT take a generic Signal. Gui events are special, and must be DistOmarEvent_Gui's. Send the gui event to the specified node.


run

public void run()
Main socket accept thread code. Continually accepts connections and creates CapConnections for them. Only exits if the Socket goes down. Should not be called from user code.

Specified by:
run in interface java.lang.Runnable

getNodes

public java.util.Vector getNodes()
Return a vector of Nodes. The vector is every CapConnection that is active. Required by abstract superclass CommProtocol.

Specified by:
getNodes in class CommProtocol
Returns:
a vector a CapConnection objects.

getLocalNode

public Node getLocalNode()
Gets a reference to the local Node object. In the case of sockets, this is presently set to null. In theory, this method should return a CapConnection object that represents a connection established to the SignalServer from the same JVM.

Specified by:
getLocalNode in class CommProtocol
Returns:
a name of reference to the local Node object.

acceptConnection

public boolean acceptConnection(CapConnection cc)
Placeholder function. Could be used to let the SignalServer decide whether or not to accept a connection. Currently, all connections are accepted, so this just returns true.


waitForConnection

public void waitForConnection(int conns)
Blocks until the specified number of client connections have been established to this SignalServer. Call this method on a SignalServer refernce to wait for a certain number of remote nodes to connect.

Parameters:
conns - the number of remote connections that this method should wait for.

connectToNode

public CapConnection connectToNode(java.lang.String remoteHost,
                                   int remotePort)
Connect to a remote SignalServer and register as the default CapConnection.TYPE_VALUE type.

Parameters:
remoteHost - the host machine that the remote SignalServer is is running on.
remotePort - The port that the remote SignalServer is running on.
Returns:
the newly created CapConnection if connection succeeds, null otherwise.

connectToNode

public CapConnection connectToNode(java.lang.String remoteHost,
                                   int remotePort,
                                   java.lang.String name)
Connect to a remote SignalServer and register as the default CapConnection.TYPE_VALUE type.

Parameters:
remoteHost - the host machine that the remote SignalServer is is running on.
remotePort - The port that the remote SignalServer is running on.
Returns:
the newly created CapConnection if connection succeeds, null otherwise.

connectToNode

public CapConnection connectToNode(java.lang.String remoteHost,
                                   int remotePort,
                                   int type)
Connect to a remote SignalServer and register with the specified type.

Parameters:
remoteHost - the host machine that the remote SignaServer is is running on.
remotePort - The port that the remote SignalServer is running on.
type - The registration type: One of the several registration types defined in CapConnection.
Returns:
newly created CapConnection if connection succeeds, null otherwise.

connectToNode

public CapConnection connectToNode(java.lang.String remoteHost,
                                   int remotePort,
                                   java.lang.String remoteName,
                                   int type)
Connect to a remote SignalServer and register with the specified type.

Parameters:
remoteHost - the host machine that the remote SignaServer is is running on.
remotePort - The port that the remote SignalServer is running on.
remoteName - A String name for the remote Node
type - The registration type: One of the several registration types defined in CapConnection.
Returns:
newly created CapConnection if connection succeeds, null otherwise.

closeAllConnections

public void closeAllConnections()
Close all remote connections to the SignalServer, by calling closeConnection on all the CapConnection objects.


closeProtocol

public void closeProtocol()
Extends CommProtocol.closeProtocol. Closes all remote connections, and closes the server socket, thereby causing this SignalServer to stop listening to any new connections.

Specified by:
closeProtocol in class CommProtocol

registerAgent

public void registerAgent(AgentID ignore)
Registers an agent. Not yet implemented.

Specified by:
registerAgent in class CommProtocol

unregisterAgent

public boolean unregisterAgent(AgentID ignore)
De-registers a previously registered agent. Not yet implemented.

Specified by:
unregisterAgent in class CommProtocol

unregisterAllAgents

public void unregisterAllAgents()
De-registers all registered agents. Not yet implemented.

Specified by:
unregisterAllAgents in class CommProtocol

findSingleAgent

public AgentID findSingleAgent(AgentID template)
Tries to find a registered agent based on the specified template. Not yet implemented.

Specified by:
findSingleAgent in class CommProtocol
Returns:
AgentID matching the template

findAllAgents

public java.util.Vector findAllAgents(AgentID ignore)
Returns all registered agents that match the specified template. Not yet implemented.

Specified by:
findAllAgents in class CommProtocol
Returns:
Vector of AgentIDs that match the template

registerFilter

public void registerFilter(Filter filter)
Register a new filter for this node by sending an ADD_FILTER message to all connected Score nodes. Required by abstract superclass CommProtocol.

Specified by:
registerFilter in class CommProtocol
Parameters:
filter - The Filter object.

registerFilter

public void registerFilter(Filter filter,
                           Node node)
Register a new filter for a node. If the node is the local node, send all remote nodes an ADD_FILTER message with the new filter If the node is a remote node, give it a new filter with this node. This can be used to give a filter to a remote node, possibly without the remote node aware of it.

Specified by:
registerFilter in class CommProtocol
Parameters:
filter - The Filter object.
node - The Node object to associate the filter with. In this case the node argument should be of type CapConnection.

changeFilters

public void changeFilters(java.util.Vector newFilters)
Changes the signal filters associated with the local node to the new set specified in the arguments. Broadcast the changes to all remote nodes. The broadcast is done by first broadcasting a CLEAR_FILTERS node control signal, followed by N ADD_FILTER signals.

Specified by:
changeFilters in class CommProtocol
Parameters:
newFilters - A Vector of new Filter Objects.

changeFilters

public void changeFilters(java.util.Vector newFilters,
                          Node n)
Changes the signal filters associated with the node (local or remote) to the new set specified in the arguments. This can be used to change the filters of a remote node, possibly without the remote node aware of the change.

Specified by:
changeFilters in class CommProtocol
Parameters:
newFilters - A Vector of new Filter Objects.
n - Reference to the Node object.

shutdownHandler

public void shutdownHandler()
Shutdown handler will be called when the JVM exits, such when after pressing Ctrl-C on a java process running in the foreground in a console window.

Overrides:
shutdownHandler in class CommProtocol