stella.parameter
Class ParameterStub

java.lang.Object
  extended by util.PropertyContainer
      extended by util.PropertyResources
          extended by util.PropertyBundles
              extended by stella.parameter.AbstractParameter
                  extended by stella.parameter.ParameterStub
All Implemented Interfaces:
Cloneable, Runnable, Remote, Parameter, CancelRunnable, ExitCleaning, Initializable, LocalizedSupplying, PropertySupplying, Reloadable, ResourceSupplying, RmiListener
Direct Known Subclasses:
FitsParameterStub, TriggerStub, ValueStub

public class ParameterStub
extends AbstractParameter
implements CancelRunnable, RmiListener, Reloadable

The stub to a remote parameter. To allow local access to all paramters, a remote parameter is served by this stub. On construct this stub starts a thread within it tries to register to its remote counterpart. If binding is impossible this stub tries to re-establish contact after a waiting period defined in the properties. Only after a certain number of unsuccessful tries, the run method exits. Note that due to the data event listening schema it is not feasable to register to the remote sensor whenever data is needed.

Note that this globals get() method blocks until this registering thread dies to ensure proper functionality.


Nested Class Summary
static class ParameterStub.Finger
          A class that allows access to parameter skeletons.
 
Nested classes/interfaces inherited from class util.PropertyResources
PropertyResources.URLResource
 
Field Summary
private  Thread bind
          A reference to the registering thread.
private  boolean cancel
          The cancel flag for the binding thread.
private static boolean DEFBLOCK
          The default blocking behaviour.
private static int DEFINITTRY
          The default initial tries number (1000).
private static int DEFRETRY
          The default retries number (10).
private static long DEFSERVERDELAY
          The default delay on server exit rebinds.
private static long DEFWAIT
          The default waiting time-out between registering retries (1000ms).
private  long delay
          A delay for the binding thread to execute.
private  Format format
          A formatter for the remopte object received.
static String KEY_BLOCK
          If true, get() blocks on retries.
static String KEY_FORMATCLASS
          The key to the class name of the format to use.
static String KEY_FORMATPATTERN
          The key to the format string constructor to use.
static String KEY_INITTRY
          The key linked to the number of initial registering tries.
static String KEY_REMOTENAME
          The key pointing to the name of the remote reference to bind to.
static String KEY_RETRY
          The key linked to the number of registering retries.
static String KEY_SERVERDELAY
          The key linked to a delay for binding on server exit.
static String KEY_WAIT
          The key linked to the time-out between registering retries.
private  int retry
          The current number of retries.
protected  RemoteParameter sensor
          The remote instance of the sensor global.
 
Fields inherited from class stella.parameter.AbstractParameter
KEY_FORMAT, KEY_NAME, KEY_PRESERVE, UNKNOWN
 
Fields inherited from class util.PropertyBundles
KEY_LOCALECOUNTRY, KEY_LOCALELANGUAGE, KEY_RESOURCEBUNDLES
 
Fields inherited from class util.PropertyResources
KEY_NOINITONCREATE, localurl, locate, POSTFIX_DIR, POSTFIX_EXT, POSTFIX_FILE, POSTFIX_LIST, POSTFIX_URL, urlset
 
Fields inherited from class util.PropertyContainer
KEY_LISTSEPARATOR, KEY_MAPKEYVALUECHAR, KEY_MAPSEPARATOR
 
Fields inherited from interface util.PropertySupplying
CONFIG, KEY_CLASS
 
Fields inherited from interface util.ResourceSupplying
KEY_URLRESOURCES, KEY_URLUSECONFIG, KEY_URLUSECURRENT, KEY_URLUSEHOME
 
Constructor Summary
ParameterStub(Map<String,String> prop)
          Constructs a new paramter stub.
ParameterStub(String remotename)
          For convenience, we support a constructor with only the remote name.
 
Method Summary
private  void bindToRemote(int retries, long timeout)
          Tries to bind to the remote paramter.
 void cancel()
          Cancels the binding thread.
 void disconnect(RmiCaster name)
          Called by the remote parameter if it will stop serve.
 void exit()
          If we preserve our state, we do this at exit.
 Object get()
          Gets the value of the remotely wrapped global.
 Thread getAsociatedThread()
          Returns the binding thread.
 String getFormatted()
          Tries to format the remote object received according to its formatter.
 void init()
          Initializes the parameter stub by binding it to the remote reference.
 boolean isConnected()
          Returns true if sensor is non-null.
 long maxTimeToCancel()
          Returns the maximum time required for canceling the binding thread.
protected  void rebind()
          Daughter access after initial binding.
 double rescanned(String key, String old, String newval)
          If the binding name, KEY_REMOTENAME cahnged, we deregister if we are connected and restart the binding process.
 void run()
          Tries to register to a remote sensor global.
 Object set(Object val)
          Setting a sensor value is not allowed.
 
Methods inherited from class stella.parameter.AbstractParameter
createParameter, delivers, getFormatted, getName, getPreserveFile, getString, getString, register, setName, toString
 
Methods inherited from class util.PropertyBundles
clone, getLocalized, getLocalized, getLocalizedString, getLocalizedString, loadResource
 
Methods inherited from class util.PropertyResources
createFrom, createFrom, createFrom, getApplet, getAsResources, getLocalClassLoader, getPropertiesToKey, getPropertiesToKey, getResource, getResourceAsStream, getResourceFromKey, getResources, keyCreate, keyCreate, reload, setApplet
 
Methods inherited from class util.PropertyContainer
augment, augment, augment, defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsEnums, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, isNew, parseObject, reload, removeProperty, setObject, setProperties, setProperty, stringProperties
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface util.Reloadable
reload
 
Methods inherited from interface util.PropertySupplying
defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, parseObject, removeProperty, setObject, setProperty, stringProperties
 
Methods inherited from interface util.ResourceSupplying
getResource, getResourceAsStream, getResources
 

Field Detail

KEY_REMOTENAME

public static final String KEY_REMOTENAME
The key pointing to the name of the remote reference to bind to.

See Also:
Constant Field Values

KEY_BLOCK

public static final String KEY_BLOCK
If true, get() blocks on retries.

See Also:
Constant Field Values

KEY_WAIT

public static final String KEY_WAIT
The key linked to the time-out between registering retries.

See Also:
Constant Field Values

KEY_RETRY

public static final String KEY_RETRY
The key linked to the number of registering retries.

See Also:
Constant Field Values

KEY_INITTRY

public static final String KEY_INITTRY
The key linked to the number of initial registering tries.

See Also:
Constant Field Values

KEY_SERVERDELAY

public static final String KEY_SERVERDELAY
The key linked to a delay for binding on server exit.

See Also:
Constant Field Values

KEY_FORMATCLASS

public static final String KEY_FORMATCLASS
The key to the class name of the format to use.

See Also:
Constant Field Values

KEY_FORMATPATTERN

public static final String KEY_FORMATPATTERN
The key to the format string constructor to use.

See Also:
Constant Field Values

DEFBLOCK

private static final boolean DEFBLOCK
The default blocking behaviour.

See Also:
Constant Field Values

DEFWAIT

private static final long DEFWAIT
The default waiting time-out between registering retries (1000ms).

See Also:
Constant Field Values

DEFRETRY

private static final int DEFRETRY
The default retries number (10).

See Also:
Constant Field Values

DEFSERVERDELAY

private static final long DEFSERVERDELAY
The default delay on server exit rebinds.

See Also:
Constant Field Values

DEFINITTRY

private static final int DEFINITTRY
The default initial tries number (1000).

See Also:
Constant Field Values

sensor

protected RemoteParameter sensor
The remote instance of the sensor global.


bind

private Thread bind
A reference to the registering thread.


retry

private int retry
The current number of retries.


delay

private long delay
A delay for the binding thread to execute.


cancel

private boolean cancel
The cancel flag for the binding thread.


format

private Format format
A formatter for the remopte object received.

Constructor Detail

ParameterStub

public ParameterStub(String remotename)
              throws RemoteException
For convenience, we support a constructor with only the remote name. This constructor also initiates the binding process with a call to init().

Throws:
RemoteException

ParameterStub

public ParameterStub(Map<String,String> prop)
              throws RemoteException
Constructs a new paramter stub. This means the parameter tries to register at a RemoteParameter. If the remote counterpart is not yet bound, a retry loop, defined with KEY_RETRY and KEY_WAIT is started during which the parameter waits for the server to come up.

Throws:
RemoteException
Method Detail

init

public void init()
Initializes the parameter stub by binding it to the remote reference.

Specified by:
init in interface Initializable
Overrides:
init in class AbstractParameter

exit

public void exit()
Description copied from class: AbstractParameter
If we preserve our state, we do this at exit.

Specified by:
exit in interface ExitCleaning
Overrides:
exit in class AbstractParameter

bindToRemote

private void bindToRemote(int retries,
                          long timeout)
Tries to bind to the remote paramter. Does this with a thread that re-initiates registry lookup if unsuccessful.


run

public void run()
Tries to register to a remote sensor global. The #KEY_REMOTE is used to locate the registry on the remote machine. Then, the remote registry is querried for a remote object with the name given by the KEY_REMOTENAME, augmented with the naming extension defined in RemoteParameter#NAMING_EXTENSION. If a remote reference is obtained and is convertable into a remote global instance this thread dies. Otherwise a retires cycle (see KEY_RETRY for number of retires ) with a sleeping period defined by KEY_WAIT is entered.

Note that querrying this global is only permitted if this thread has died, i.e. if registering was successful.

Specified by:
run in interface Runnable

rescanned

public double rescanned(String key,
                        String old,
                        String newval)
If the binding name, KEY_REMOTENAME cahnged, we deregister if we are connected and restart the binding process.

Specified by:
rescanned in interface Reloadable
Overrides:
rescanned in class AbstractParameter

isConnected

public boolean isConnected()
                    throws RemoteException
Returns true if sensor is non-null.

Specified by:
isConnected in interface RmiListener
Throws:
RemoteException

disconnect

public void disconnect(RmiCaster name)
                throws RemoteException
Called by the remote parameter if it will stop serve. There is always only a single server, thus we ignore the name.

Specified by:
disconnect in interface RmiListener
Throws:
RemoteException

cancel

public void cancel()
Cancels the binding thread. Used on exit, if rebinding is in process. Also deletes itself from the server parameter.

Specified by:
cancel in interface CancelRunnable

maxTimeToCancel

public long maxTimeToCancel()
Returns the maximum time required for canceling the binding thread.

Specified by:
maxTimeToCancel in interface CancelRunnable

getAsociatedThread

public Thread getAsociatedThread()
Returns the binding thread. May be null.

Specified by:
getAsociatedThread in interface CancelRunnable

get

public Object get()
Gets the value of the remotely wrapped global. If the registering thread is still running this method blocks until it is finished. If the remote skeleton wraps a sensor the get method is likely to block, too, until data is available. If the reference to the remote parameter has become invalid between initial binding and current data retrieval, binding is again initiated. Only if the binding cannot be completed successfully, null is returned.

Specified by:
get in interface Parameter

rebind

protected void rebind()
Daughter access after initial binding.


set

public Object set(Object val)
Setting a sensor value is not allowed.

Specified by:
set in interface Parameter
Returns:
The previous value of this parameter.

getFormatted

public String getFormatted()
Tries to format the remote object received according to its formatter. For the special case that the object received is a Long and the formatter specified is a DateFormat, then a new Date object is constructed from the long value received and formatted accordingly. If no format is specified, the toString method is used.

Specified by:
getFormatted in interface Parameter
Overrides:
getFormatted in class AbstractParameter