|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.iperg.part.core.IpObject
public class IpObject
The IpObject
class is the super class of all PART
replicated objects.
PART objects can be replicated which means that several copies
of a particular PART object can exist simultaneously in different
PART processes. The process of copying a PART object from one
process to another is called replication
. The new
object copy which is created as a result of the replication is
called a replica
. The first instance of a particular
PART object is called the master
. Thus, a PART object
is represented by a master copy, and zero or more replicas.
PART objects can have properties in addition to ordinary class
variables. Properties are represented by a name and a string value.
Properties also has a group attribute, which is the name of the
group to which the property belongs. All properties with the same
group name are thus considered to be part of the same group. If no
group name is given when a property is created, it will implicitly
be made a member of the default group. The name of the default
group is given by IpObject.DEFAULT_PROP_GROUP
attribute.
The IpObject
class provides a number of methods
for manipulating properties.
When a PART object is replicated, a copy of the object is sent to another process and used to create a new replica. This includes copying all of the object's properties to the new process. The result is a new replica in which the property values are the same as in the "source" object. The values of the object's class variables are not copied, only its properties.
Properties can only be updated in the master copy. This means
that only the process holding the master copy of a PART object is
allowed to update its properties. Other processes holding a replica
of a PART object can subscribe to updates made to the master. This
is done using one of the subscribe
methods.
Subscriptions cause the platform to automatically synchronise the
values of a replica's properties with the corresponding values in
the master. By selecting the subscription rate, a process can
choose how often it wants this synchronisation to occur.
It is also possible to synchronise two replicas explicitly
using one of the synchronise
methods. When
synchronising the local replica with a remote replica, the values
of the properties of the two replicas will be set to the most
recent values held by any of the replicas. This means that both
replicas can be affected by calls to the synchronise
methods.
Field Summary | |
---|---|
static java.lang.String |
DEFAULT_PROP_GROUP
|
static int |
PROP_ADD
|
static int |
PROP_REMOVE
|
static int |
PROP_SET
|
Constructor Summary | |
---|---|
IpObject()
Create a new object. |
|
IpObject(IpIdentifier id)
Create a new object and set its identifier to the given id. |
Method Summary | |
---|---|
void |
addCommandHandler(IpCommandHandler h)
Add a handler that will be invoked to handle commands executed via exec . |
void |
addProperty(java.lang.String group,
java.lang.String name,
java.lang.String value)
Create and initialize a new object property. |
void |
addPropertyViaFloat(java.lang.String group,
java.lang.String name,
float value)
Add a new object property and set its value using a float. |
void |
addPropertyViaInt(java.lang.String group,
java.lang.String name,
int value)
Add a new property to the object and set its value using an integer. |
protected void |
createProperty(java.lang.String group,
java.lang.String name,
java.lang.String value)
A method that can be used to add properties to an object when the object is created. |
static java.lang.String |
exec(IpIdentifier procId,
IpIdentifier objId,
java.lang.String call)
Execute a command in an arbitrary PART object in an arbitrary PART process. |
java.lang.String |
exec(java.lang.String call)
Execute a command in the object. |
static java.util.Enumeration |
getAllObjects()
Return an enumeration of IpObject instances representing all replicated objects (masters and replicas alike) held by the local process. |
java.util.Enumeration |
getAllPropertyGroupNames()
Get an enumeration of String instances representing the names of all object property groups. |
java.util.Enumeration |
getAllPropertyNames(java.lang.String group)
Get an enumeration of String objects representing the names of all properties that "belongs to" the given group. |
IpIdentifier |
getId()
Get the unique identifier representing this object |
IpIdentifier |
getMasterHolderId()
Get the identifier of the process that holds the master copy of this replicated object. |
long |
getMostRecentUpdateTime()
Get time (as given by System.currentTimeMillis()) of the most recent update to any of the object's the properties. |
long |
getMostRecentUpdateTime(java.lang.String group,
java.lang.String name)
Get time (as given by System.currentTimeMillis()) of the most recent update to the property. |
static IpObject |
getObject(IpIdentifier id)
Get a PART object which has a given id. |
static java.lang.String |
getProperty(IpIdentifier procId,
IpIdentifier objId,
java.lang.String group,
java.lang.String name,
int timeout)
Get the property value of an object held by an arbitrary PART process. |
java.lang.String |
getProperty(java.lang.String group,
java.lang.String name)
Get the value of an object property in a given group. |
float |
getPropertyAsFloat(java.lang.String group,
java.lang.String name,
float valueIfNoSuchProp)
Get the value of a property in the given group as a float. |
int |
getPropertyAsInt(java.lang.String group,
java.lang.String name,
int valueIfNoSuchProp)
Get the value of an property in the given group as an integer. |
java.lang.String |
getScript()
Get the object's scripting URL |
int |
getSubscriptionInterval(java.lang.String group,
java.lang.String name)
Get the interval at which the local process subscribes to updates to the property with the given name and group. |
protected java.lang.String |
handleCommand(java.lang.String cmd,
java.lang.String[] params)
Should be overridden by sub-classes that wish to handle the invocation of a command. |
boolean |
isDiscoverable()
Check if an object is discoverable or not. |
boolean |
isMaster()
Check if this object instance is the master copy. |
protected void |
propertyUpdateResolve(int type,
java.lang.String group,
java.lang.String name,
java.lang.String value,
IpIdentifier sourceProcId)
Is called in the master copy of the object as a result of addProperty , removeProperty or
setProperty being called in a the master or a
replica. |
protected void |
read(IpInputStream stream)
Read the object's state from an input stream. |
void |
release()
Should be called to tell PART to remove any references to the object, for instance in order for the objnect to become deallocated. |
static void |
releaseAllObjects()
Should be called to tell PART to remove references to all replicated objects. |
void |
removeProperty(java.lang.String group,
java.lang.String name)
Remove an object property |
void |
replicate(IpIdentifier procId)
Replicate the object to a remote process identified by the given identifier. |
protected void |
replicaUpdateCb(IpObjectEvent event)
Is called when the object (master or replica) has been updated somehow. |
void |
setDiscoverable(boolean status)
Set the discoverable status of a PART object. |
void |
setProperty(java.lang.String group,
java.lang.String name,
java.lang.String value)
Set the value of an object property in given group. |
void |
setPropertyViaFloat(java.lang.String group,
java.lang.String name,
float value)
Set the value of a property in given group using a float value. |
void |
setPropertyViaInt(java.lang.String group,
java.lang.String name,
int value)
Set the value of a property in given group using an integer value. |
void |
setScript(java.lang.String scriptUrl)
Set the script associated to the object. |
void |
subscribe(java.lang.String group,
java.lang.String name,
int interval)
Subscribe to updates to a property in the given group. |
void |
subscribeAll(int interval)
Subscribe to all object properties using the same interval. |
void |
synchronise()
Synchronise the values of all object properties with the master object copy. |
void |
synchronise(IpIdentifier procId)
Synchronise the values of the replica with the replica (or master) copy held by a remote process. |
void |
synchroniseProperty(java.lang.String group,
java.lang.String name)
Synchronise the value of a property in the given group with the master object copy. |
void |
synchroniseProperty(java.lang.String group,
java.lang.String name,
IpIdentifier procId)
Synchronise the value of a property in the given group with the replica (or master) copy held by another remote process. |
java.lang.String |
toString()
Return a string representation of the PART object. |
void |
unsubscribe(java.lang.String group,
java.lang.String name)
Cancel the subscription of a property in the given group. |
void |
unsubscribeAll()
Will cause all local subscriptions to the object's properties to be cancelled. |
protected void |
write(IpOutputStream stream)
Write the state of the object into an output stream. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int PROP_ADD
public static final int PROP_REMOVE
public static final int PROP_SET
public static final java.lang.String DEFAULT_PROP_GROUP
Constructor Detail |
---|
public IpObject()
public IpObject(IpIdentifier id)
id
- The object identifierMethod Detail |
---|
public IpIdentifier getId()
public static IpObject getObject(IpIdentifier id)
id
- The object identifierpublic static java.util.Enumeration getAllObjects()
public IpIdentifier getMasterHolderId()
public void setDiscoverable(boolean status)
By default, objects are undiscoverable when created.
status
- true
makes the object discoverable,
false
un-discoverablepublic boolean isDiscoverable()
true
means that the object is discoverable, false
that
it's notpublic boolean isMaster()
true
if object is the master,
false
otherwise.protected void replicaUpdateCb(IpObjectEvent event)
event
- An event that describes the updatepublic void release()
After calling release, calls to
IpObject.getObject
using the object's identifier
will always return null.
public static void releaseAllObjects()
protected final void createProperty(java.lang.String group, java.lang.String name, java.lang.String value)
group
- The name of the property group, the default group
is assumed if value is null
name
- The property namevalue
- The property value
java.lang.IllegalArgumentException
- if name
and/or
value
parameters are nullpublic void addProperty(java.lang.String group, java.lang.String name, java.lang.String value) throws IpVetoException
The group is a string that can be used to group properties, i.e., to tell PART that they somehow are related.
group
- The property group, the default group is assumed
if value is null
name
- The property namevalue
- The property value
IpVetoException
- if the master object doesn't allow
the property to be added, or if an timeout occurs while
communicating with the master.
java.lang.IllegalArgumentException
- if name
and/or
value
parameters are nullpublic void removeProperty(java.lang.String group, java.lang.String name) throws IpVetoException, IpNoSuchPropertyException
group
- The property group, the default group is assumed
if value is null
name
- The property name
IpVetoException
- if the master copy doesn't permit
the property to be removed
IpNoSuchPropertyException
- if no such property exists
in the current objectpublic void addPropertyViaInt(java.lang.String group, java.lang.String name, int value) throws IpVetoException
name
- The property namevalue
- The property value in the form of an integergroup
- The name of the property group, the default group
is assumed if value is null
IpVetoException
- if the master object doesn't allow
the property to be added, or if an timeout occurs while
communicating with the master.public void addPropertyViaFloat(java.lang.String group, java.lang.String name, float value) throws IpVetoException
name
- The property namevalue
- The property value in the form of a floatgroup
- The name of the property group, the default group
is assumed if value is null
IpVetoException
- if the master object doesn't allow
the property to be added, or if an timeout occurs while
communicating with the master.public java.lang.String getProperty(java.lang.String group, java.lang.String name)
null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed.
group
- The property group, the default group is assumed
if value is null
name
- The property name
null
otherwisepublic static java.lang.String getProperty(IpIdentifier procId, IpIdentifier objId, java.lang.String group, java.lang.String name, int timeout) throws IpNoSuchPropertyException, IpTimeoutException, java.io.IOException
Since the value may be fetched from a remote process, a
timeout value has to be supplied. If the value can't be
retrieved within the time specified by the timeout parameter, a
IpTimeoutException
will be thrown. The timeout
value is only used if the process identifier parameter
identifies a remote process.
Note that the property value will always be fetched from the object copy held by the process identified by the process identifier parameter, even if the local process holds a copy of the object.
procId
- The identifier of the process holding the objectobjId
- The identifier of the object holding the propertygroup
- The property group, the default group is assumed
if value is null
name
- The property nametimeout
- Timeout in milliseconds, 0 means no timeout
IpNoSuchPropertyException
- If the process, object or
property doesn't exist
IpTimeoutException
- if a timeout occurs before the
property value has been retrieved
java.io.IOException
- if an io exception occurs while
communicating with the remote processpublic int getPropertyAsInt(java.lang.String group, java.lang.String name, int valueIfNoSuchProp)
null
, the default property group is assumed.
group
- The name of the property group, the default group
is assumed if value is null
name
- The property namevalueIfNoSuchProp
- Value to return if no property of the
given name exists.
valueIfNoSuchProp
parameter.public float getPropertyAsFloat(java.lang.String group, java.lang.String name, float valueIfNoSuchProp)
null
, the default property group is assumed.
group
- The name of the property group, the default group
is assumed if value is null
name
- The property namevalueIfNoSuchProp
- to return if no property of the
given name exists.
valueIfNoSuchProp
parameter.protected void propertyUpdateResolve(int type, java.lang.String group, java.lang.String name, java.lang.String value, IpIdentifier sourceProcId) throws IpVetoException
addProperty
, removeProperty
or
setProperty
being called in a the master or a
replica. Must determine if the update should be allowed.
Should be overridden in sub-classes that wish to inspect and
possibly veto property updates suggested by replicas.
type
- IpObject.PROP_ADD
,
IpObject.PROP_REMOVE
or
IpObject.PROP_SET
group
- The group to which the property belongsname
- The name of the property that is being updatedvalue
- The suggested new value of the propertysourceProcId
- The identifier of the process that is
trying to make the update
IpVetoException
- if the update not allowedpublic void setProperty(java.lang.String group, java.lang.String name, java.lang.String value) throws IpVetoException, IpNoSuchPropertyException
null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed.
group
- The name of the property group, the default group
is assumed if value is null
name
- The property namevalue
- The property value
java.lang.IllegalArgumentException
- if name
and/or
value
parameters are null
IpVetoException
- if the master object doesn't allow
the property to be updated, or if an timeout occurs while
communicating with the master.
IpNoSuchPropertyException
- if the given property
doesn't existpublic void setPropertyViaInt(java.lang.String group, java.lang.String name, int value) throws IpVetoException, IpNoSuchPropertyException
group
- The property group name, the default group
is assumed if value is null
name
- The property namevalue
- The property value in the form of an integer
IpVetoException
- if the local process doesn't have
permission to update the property value
IpNoSuchPropertyException
- if the given property
doesn't existpublic void setPropertyViaFloat(java.lang.String group, java.lang.String name, float value) throws IpVetoException, IpNoSuchPropertyException
group
- The name of the property group, the default group
is assumed if value is null
name
- The property namevalue
- The property value in the form of a float
IpVetoException
- if the local process doesn't have
permission to update the property value
IpNoSuchPropertyException
- if the given property
doesn't existpublic long getMostRecentUpdateTime()
System.currentTimeMillis()
) of
most recent update to any object property, or -1 if the object
doesn't have any propertiespublic long getMostRecentUpdateTime(java.lang.String group, java.lang.String name) throws IpNoSuchPropertyException
group
- The property group, the default group is assumed
if value is null
name
- The property name
System.currentTimeMillis()
) of
most recent property update
IpNoSuchPropertyException
- if the object has no
matching propertypublic int getSubscriptionInterval(java.lang.String group, java.lang.String name) throws IpNoSuchPropertyException
If the returned subscription interval is > 0, the value indicates the maximum rate (in milliseconds) at which updates to the property will be reported to the local process. A value of 0 indicates that updates will be reported immediately. A value of -1 is not subscribed by the local process.
group
- The property group, the default group is assumed
if value is null
name
- The property name
IpNoSuchPropertyException
- if the object has no
matching propertypublic void subscribe(java.lang.String group, java.lang.String name, int interval) throws IpNoSuchPropertyException
null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed.The interval parameter can be used to specify the maximum interval at which the process wants to receive update notifications. For instance, if the value is 1000, updates to the object property will be reported at a maximum rate of once per second, even if the value is updated more often than that.
The subscription
method can be called more
than once to update the subscription interval. The supplied
interval value will replace any interval set in previous calls
to subscribe
.
group
- The name of the property group, the default group
is assumed if value is null
name
- The name of the propertyinterval
- The maximum interval (in milliseconds) at which
the process wants to receive object update notifications. A
value of 0 means that the process wants be be notified as soon
as the property is updated.
IpNoSuchPropertyException
- if a property with the
given name doesn't existpublic void subscribeAll(int interval)
interval
- The subscription intervalpublic void unsubscribe(java.lang.String group, java.lang.String name) throws IpNoSuchPropertyException
null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed.
group
- The name of the property group, the default group
is assumed if value is null
name
- The name of the property for which the
active subscription should be cancelled.
IpNoSuchPropertyException
- if a property with the
given name doesn't existpublic void unsubscribeAll()
public void synchronise() throws java.io.IOException
Will block until the object has been synchronised or until an exception is thrown.
Note that the property values will be synchronised regardless of whether the local process subscribes to all properties or not.
If the local process holds the master copy of the object, the method has no effect.
java.io.IOException
- if an io exception occurs while
synchronising. If exception occurs, property values are not
guaranteed to have been synchronised.public void synchroniseProperty(java.lang.String group, java.lang.String name) throws java.io.IOException, IpNoSuchPropertyException
null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed.Will block until the property value has been synchronised or until an exception is thrown.
Note that the property value will be synchronised regardless of whether the local process subscribes to updates to the property or not.
If the local process holds the master copy of the object, the method has no effect.
group
- The name of the property group, the default group
is assumed if value is null
name
- The name of the property to synchronise
java.io.IOException
- if an io exception occurs while
synchronising. If exception occurs, the property value is not
guaranteed to have been synchronised.
IpNoSuchPropertyException
- if a property with the
given name doesn't existpublic void synchronise(IpIdentifier procId) throws java.io.IOException, IpNoSuchPropertyException
If the remote process doesn't hold a replica of the PART
object, an IOException
will be thrown.
Will block until the object has been synchronised or until an exception is thrown.
Note that the property values will be synchronised regardless of whether the local process subscribes to all properties or not.
If the local process holds the master copy of the object, the method has no effect.
procId
- The identifier of the remote process
java.io.IOException
- if an io exception occurs while
synchronising. If exception occurs, property values are not
guaranteed to have been synchronised.
IpNoSuchPropertyException
- if a property with the
given name doesn't existpublic void synchroniseProperty(java.lang.String group, java.lang.String name, IpIdentifier procId) throws java.io.IOException, IpNoSuchPropertyException
null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed. If the remote process doesn't hold a replica of the PART
object, an IOException
will be thrown.
Will block until the property has been synchronised or until an exception is thrown.
Note that the property value will be synchronised regardless of whether the local process subscribes to the property or not.
If the local process holds the master copy of the object, the method has no effect.
group
- The name of the property group, the default group
is assumed if value is null
name
- The name of the property to synchroniseprocId
- The identifier of the remote process
java.io.IOException
- if an io exception occurs while
synchronising. If exception occurs, property values are not
guaranteed to have been synchronised.
IpNoSuchPropertyException
- if a property with the
given name doesn't existpublic java.util.Enumeration getAllPropertyGroupNames()
Enumeration names = obj.getAllPropertyGroupNames(); while (names.hasMoreElements()) { System.out.println((String)names.nextElement()); }
null
but will be empty if no properties has been added to the object.public java.util.Enumeration getAllPropertyNames(java.lang.String group)
group1
:Enumeration names = obj.getAllPropertyNames("group1"); while (names.hasMoreElements()) { System.out.println((String)names.nextElement()); }
group
- The property group name. If value is null
, theIpObject.DEFAULT_PROP_GROUP
group is assumed.null
but will be empty if the given group doesn't exist.protected void write(IpOutputStream stream) throws java.io.IOException
protected void write(IpOutputStream stream) throws IOException { super.write(stream); // write class specific state information ... }
stream
- The output stream into which the object's state
should be written
java.io.IOException
- if an io exception occured while writing
the object to the streamprotected void read(IpInputStream stream) throws java.io.IOException
protected void read(IpInputStream stream) throws IOException { super.read(stream); // read class specific state information ... }
stream
- The input stream from which the object's state
should be read
java.io.IOException
- if the read failedpublic java.lang.String toString()
"id {group.prop=value, group.prop=value, ...}"
toString
in class java.lang.Object
public void replicate(IpIdentifier procId) throws java.io.IOException
IpObjectEvent
of type
IpObjectEvent.OBJ_NEW
to be triggered. If the
object already exist in the remote process, the replication
will be aborted, i.e., the remote object replica will not be
changed in any way.
procId
- The identifier of the remote process
java.io.IOException
- if an IO exception occurs while
replicating the objectpublic void addCommandHandler(IpCommandHandler h)
exec
. Command handlers can only be added on
the master copy of an object.
h
- The command handler.public void setScript(java.lang.String scriptUrl) throws IpVetoException
A script is given via a "URL" with the syntax
lang://script
, where lang
is the
scripting language, e.g., javascript
or
tcl
, and the script
is the actual
scripting code.
If the script is set successfully, the scripts init method
is invoked if defined. The name of the init procedure is
defined by IpScriptInterp.SCRIPT_INIT_PROC
. Note
that the script doesn't have to define this procedure.
scriptUrl
- A string in the form lang://script. May not be
null
.
IpVetoException
- if the update is refused, e.g., due
to the script being syntactically incorrectpublic java.lang.String getScript()
public java.lang.String exec(java.lang.String call) throws IpException
Commands are executed either by command handlers, or by
scripts. When a command is executed using
exec(call)
, the command is first handed to the
command handlers added via the addCommandHandler
method. The script is first handed to the handler that was
added first. If this handler does not handle the command, i.e.,
it returns null
, the command is handed to the next
handler, and so on. If a handler returns a value which is not
null
, the "iteration" is stopped and the the value
is returned from exec(call)
. If no command handler
handles the command, the object's own
handleCommand
method is called. If this method
doesn't handle the command, PART tries to execute a script
method with the same name and parameter as the given by the
call
parameter. If the script execution returns a
value, this value is made the return value of the call to
exec(call)
. If no script exists, or if the script
invocation fails, an IpException
exception is
thrown.
call
- A call string which must have the format 'cmd(arg1,
..., argn)'
IpException
- if the command execution failed, for
instance due to the command being unhandled.public static java.lang.String exec(IpIdentifier procId, IpIdentifier objId, java.lang.String call) throws IpException
Commands are executed either by command handlers, or by
scripts. When a command is executed using exec(procId,
objId, call)
, the command is first sent to the
(possibly) remote process identified by the procId
parameter. When the command has arrived at this process, the
object, identified by the objId
is located. Once
this is done, the call is then handed to the object's command
handlers, added via the addCommandHandler
method. The script is first handed to the handler that was
added first. If this handler does not handle the command, i.e.,
it returns null
, the command is handed to the next
handler, and so on. If a handler returns a value which is not
null
, the "iteration" is stopped and the the value
is sent back to the calling process and returned from
exec(procId, objId, call)
. If no command handler
handles the command, the object's own
handleCommand
method is called. If this method
doesn't handle the command, PART tries to execute a script
method with the same name and parameter as the given by the
call
parameter. If the script execution returns a
value, this value is sent back to the calling process and made
the return value of the call to exec(procId, objId,
call)
. If no script exists, or if the script invocation
fails, an IpException
exception is thrown at the
calling process.
If the process to which the call is sent discovers that
it's not holding the master copy of the object, the call will
be forwarded to the master process. The result of the execution
on the master process will then be sent back to the calling
process, i.e., the process caliing exec(procId, objId,
call)
.
procId
- The identifier of the process where the command
should be executed. May be the local or some remote process.objId
- The identifier of the object in which the command
will be executed.call
- A call string which must have the format 'cmd(arg1,
..., argn)'
IpException
- if the command execution failed, for
instance due to the command being unhandled.protected java.lang.String handleCommand(java.lang.String cmd, java.lang.String[] params) throws IpException
cmd
- The command nameparams
- The call parameters
IpException
- if the execution failed for some reason
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |