com.ibm.tspaces.examples.handler
Class StaleTupleSpaceHandler

java.lang.Object
  |
  +--com.ibm.tspaces.server.handler.TSHandler
        |
        +--com.ibm.tspaces.examples.handler.StaleTupleSpaceHandler

public class StaleTupleSpaceHandler
extends com.ibm.tspaces.server.handler.TSHandler
implements java.lang.Runnable

This class is a handler for a "StaleTupleSpace" this is a space that will clean up tuples that are in there too long. Currenly this only means from creation time, ie reading them or performing some other operation will not update a tuple's timestamp. But of course, the handlers for those commands could be re-implemented here to update the timestamps. The handler only operates on Tuples of type StaleTuple and these tuples must always have a long in the first field that is a timestamp field. The handler will initialize this value on write and return the value to the user if they want to use it for something eg as a unique index ( assuming currentTimeMills returns unique values). Two commands are implemented: TupleSpace.WRITE -- stamps the correct time in the first field before using the basic write command. START -- start the daemon on the server (only call this once). This starts a seperate daemon thread on the server that usually sleeps, but occasionally wakes up to check for stale tuples which it will delete.

Author:
Peter Wyckoff, John Thomas
See Also:
TSFactory, TSFExtendable, TSHandler

Field Summary
static java.lang.String START
          the command to start this handler up as a daemon on the server that periodically deletes stale tuples
 
Constructor Summary
StaleTupleSpaceHandler()
          empty constructor called by the factory methods to create the new handler
 
Method Summary
 com.ibm.tspaces.server.handler.AccessAttribute[] attributes(java.lang.String cmdString, SuperTuple argTuple)
          What type of access control attributes are required to use the handler? If a zero length array is returned this means no attributes are needed.
 SuperTuple command(com.ibm.tspaces.server.TS ts, java.lang.String cmdString, SuperTuple argTuple, java.lang.String clientID_, TSResponse communicator_, java.lang.String user_)
          Implement one or more "commands" (usually one).
 boolean handles(java.lang.String commandString, SuperTuple argTuple)
          Do I handle this command on this argument? For now, always say yes because the factory will only call us on the command strings that we handle and we operate on all types of tuples When each handler only handles one command, can tighten this up.
static void main(java.lang.String[] argv)
          This is code to test the handler by simulating what a client application would do to load a special handler and then invode the the new commands.Normally this code would be part of the application that is going to to make use of the handler.
 void run()
          actually run as a background daemon and do the deletions of stale tuples.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

START

public static final java.lang.String START
the command to start this handler up as a daemon on the server that periodically deletes stale tuples
Constructor Detail

StaleTupleSpaceHandler

public StaleTupleSpaceHandler()
empty constructor called by the factory methods to create the new handler
Method Detail

handles

public final boolean handles(java.lang.String commandString,
                             SuperTuple argTuple)
Do I handle this command on this argument? For now, always say yes because the factory will only call us on the command strings that we handle and we operate on all types of tuples When each handler only handles one command, can tighten this up.
Overrides:
handles in class com.ibm.tspaces.server.handler.TSHandler
Parameters:
commandString - the command
argTuple - the agument to the command
Returns:
true if I am so inclined as to handle this command false else

attributes

public com.ibm.tspaces.server.handler.AccessAttribute[] attributes(java.lang.String cmdString,
                                                                   SuperTuple argTuple)
                                                            throws com.ibm.tspaces.server.handler.TSHandlerException
What type of access control attributes are required to use the handler? If a zero length array is returned this means no attributes are needed.
Overrides:
attributes in class com.ibm.tspaces.server.handler.TSHandler
Parameters:
commandStr - the command in question
argTuple - The argument for the command.
Returns:
an array of the attributes need for this command --- at least a zero length array is always returned except if this is not a command for this handler in which case null is returned, but that should never happen.

command

public SuperTuple command(com.ibm.tspaces.server.TS ts,
                          java.lang.String cmdString,
                          SuperTuple argTuple,
                          java.lang.String clientID_,
                          TSResponse communicator_,
                          java.lang.String user_)
                   throws com.ibm.tspaces.server.handler.TSHandlerException
Implement one or more "commands" (usually one).
Overrides:
command in class com.ibm.tspaces.server.handler.TSHandler
Parameters:
ts - The TupleSpace instance on which the command is executed.
cmdString - The cmd to be invoked
argTuple - The argument for the command.
clientID_ - The client Transaction ID
communicator_ - the io handle back to the client
user_ - the user issuing the command
Returns:
a "result" tuple that is the output of the command.
Throws:
com.ibm.tspaces.server.handler.TSHandlerException - if this handler cannot complete the command or there was some problem encountered while executing the command.

run

public void run()
actually run as a background daemon and do the deletions of stale tuples.
Specified by:
run in interface java.lang.Runnable

main

public static void main(java.lang.String[] argv)
This is code to test the handler by simulating what a client application would do to load a special handler and then invode the the new commands.Normally this code would be part of the application that is going to to make use of the handler.

The basic steps are:

Create a TupleSpace. Remember that this function must be performed by a user that has "admin" authority for the server

issue an AddFactory specifing a factory that supports downlable handlers. The distributed TSFExtendable can be used for this..

Create the Handler object.

Issue an addHandler command for each of the commands that this handler will support.

The TupleSpace and its new commands are now ready for use so this routine will write a StaleTuple to the Space and then issue the START command which will actually start a thread at the server which will delete tuples when they expire.

This routine then writes some more StaleTuples just to verify that it is really working.

For this relatively simple test, we hardcode all of the values that we need.