State file

The state file indicates the state of an endpoint. It is downloaded by clients right after downloading the definition file and afterwards, if it is notified (most commonly through a change of the EPStateVersion service attribute; see DNS-SD/mDNS discovery for more information). The state file contains a list of (key, value) pairs that specify state variables. State variables can be bound to parameters (so they can be used to update the user interface) and can inform the client about which methods are enabled/disabled.

Example

An endpoint can use state variables to limit the methods that are enabled at any point in time. A traffic light, for example, has three states ('red','green','orange'). From the 'red' state, it is only possible to go to the 'orange' state, etcetera. A state variable could be used to indicate whether a method that jumps to the 'red' state is enabled (red_isenabled). The definition file will specify bind-enabled=“red_isenabled” to indicate that the method is only enabled if the red_isenabled variable is true. As soon as some other client changes the state of the traffic light, the other clients are notified of a state update, download the state file and get a different value for red_isenabled.

In fabrics

Fabric scripts can change the state variables through the state variable:

state.red_isenabled = true;

State variables that do not exist yet will be created automatically; they can also be specified up front using the <variable> tag. This is preferable, because then the state variables already exist before any method is called:

<variable id="red_isenabled" type="bool" value="true" />

In EPFramework

Users of the EPFramework library can subclass from EPEndpointServer<T> to implement an endpoint. To use state variables, declare a class variable of type Any (tj::shared::Any). Then, in void OnCreated(), use BindVariable(L”name_of_variable”, &variable) to bind the variable to a name. At any point in time, you can assign a new value to the variable; EPEndpointServer will marshal updates automatically to the state file after each method call finishes.

Format

The format of the state file is simple: a UTF-8 encoded XML file having a root element <state> containing zero or more <var> elements. Each <var> element must have an attribute 'id' that indicates a unique name for the variable (unique to this particular endpoint), a 'type' attribute that indicates the type of the variable (see value types for a list of possible values) and a 'value' attribute that specifies the current value.

Example

<?xml version="1.0" ?>
<state>
  <var id="red_isenabled" type="bool" value="true" />
  <var id="green_isenabled" type="bool" value="false" />
</state>
 
state_file.txt · Last modified: 2010/01/23 11:45 by tommy
 
© Pixelspark, 2009-2012