context.arch.widget
Class Widget

java.lang.Object
  |
  +--context.arch.BaseObject
        |
        +--context.arch.widget.Widget
Direct Known Subclasses:
Server, WContactPresence, WDisplay, WPersonNamePresence2, WPersonPresence, WSoundLevel, WTemperature

public abstract class Widget
extends BaseObject

This class is the basic context widget, with attributes and methods that should apply to all context widgets.

See Also:
BaseObject

Field Summary
protected  Attributes attributes
           
protected  java.util.Hashtable attributeTypes
           
protected  Callbacks callbacks
           
static java.lang.String CLASS
          Tag for the class file being used by the widget
protected  long CurrentOffset
           
static int DEFAULT_PORT
          Default port for widgets to use
protected  java.lang.String id
           
static java.lang.String PUT_DATA
          Tag to indicate the widget should accept the given data
static java.lang.String PUT_DATA_REPLY
          Tag to indicate the reply to a PUT_DATA message
static java.lang.String QUERY
          Tag to indicate the widget should return the latest stored data
static java.lang.String QUERY_ATTRIBUTES
          Tag to indicate the widget should return its list of attributes
static java.lang.String QUERY_ATTRIBUTES_REPLY
          Tag to indicate the reply to a QUERY_ATTRIBUTES message
static java.lang.String QUERY_CALLBACKS
          Tag to indicate the widget should return its list of callbacks
static java.lang.String QUERY_CALLBACKS_REPLY
          Tag to indicate the reply to a QUERY_CALLBACKS message
static java.lang.String QUERY_REPLY
          Tag to indicate the reply to a QUERY message
static java.lang.String QUERY_SERVICES
          Tag to indicate the widget should return its list of services
static java.lang.String QUERY_SERVICES_REPLY
          Tag to indicate the reply to a QUERY_SERVICES message
static java.lang.String QUERY_VERSION
          Tag to indicate the widget should return its version number
static java.lang.String QUERY_VERSION_REPLY
          Tag to indicate the reply to a QUERY_VERSION message
protected  Services services
           
static java.lang.String SPACER
          Constant for the widget spacer
 StorageObject storage
          Object to keep track of storage
 Subscribers subscribers
          Object to handle subscriptions to context data
static java.lang.String TIMESTAMP
          Tag for the timestamp of widget data
static java.lang.String UPDATE
          Tag to indicate an update is being sent
static java.lang.String UPDATE_AND_QUERY
          Tag to indicate the widget should get the latest data from the generator and return them
static java.lang.String UPDATE_AND_QUERY_REPLY
          Tag to indicate the reply to an UPDATE_AND_QUERY message
static java.lang.String VERSION
          Tag for version number.
 java.lang.String VERSION_NUMBER
          Dummy version number.
 
Fields inherited from class context.arch.BaseObject
communications, EXIT_OK, handlers, ID, parser, PING, PING_REPLY, serviceHandlers
 
Constructor Summary
Widget(int port, java.lang.String id)
          Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services.
Widget(int port, java.lang.String id, boolean storageFlag)
          Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services.
Widget(java.lang.String id)
          Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services.
Widget(java.lang.String id, boolean storageFlag)
          Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services.
Widget(java.lang.String clientClass, java.lang.String serverClass, int serverPort, java.lang.String encoderClass, java.lang.String decoderClass, boolean storageFlag, java.lang.String id)
          Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks, and services and setting up the BaseObject info.
Widget(java.lang.String clientClass, java.lang.String serverClass, int serverPort, java.lang.String encoderClass, java.lang.String decoderClass, java.lang.String storageClass, java.lang.String id)
          Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks, and services and setting up the BaseObject info.
 
Method Summary
protected  DataObject addSubscriber(DataObject sub, java.lang.String error)
          This method adds a subscriber to this object.
protected  boolean canHandle(AttributeFunctions atts, Conditions conds)
          This method checks the list of attributes and conditions to ensure that the widget contains these attributes.
protected  boolean canHandle(AttributeNameValues atts)
          This method checks the list of attributes (in an AttributeNameValues object) to ensure that the widget contains these attributes.
protected  boolean canHandle(Attributes attributes)
          This method checks the list of attributes to ensure that the widget contains these attributes.
protected  boolean canHandle(Attributes atts, Conditions conds)
          This method checks the list of attributes and conditions to ensure that the widget contains these attributes.
protected  DataObject executeService(DataObject request, java.lang.String error)
          This method attempts to execute a widget service.
protected  java.lang.String getAttributeType(java.lang.String name)
          Returns the attribute value with the given name
protected  java.lang.Long getCurrentTime()
          This method returns the current time to use as a timestamp
protected  void getNewOffset()
          This method creates a thread that retrieves a global time clock and determines the offset between the local clock and the global clock.
protected  long getNewOffsetNoDelay()
          This method retrieves the offset between the local clock and a global clock with no delay.
protected  boolean isAttribute(java.lang.String name)
          Checks if the given attribute is an attribute of this widget
protected  boolean isCallback(java.lang.String name)
          Checks if the given callback is an callback of this widget
 void notify(java.lang.String event, java.lang.Object data)
          This is an empty method that should be overridden by objects that subclass from this class.
protected  void processCallbackReply(DataObject result, Subscriber sub)
          This method should be overriden to process the results of subscription callbacks.
protected  DataObject putData(DataObject data, java.lang.String error)
          This method puts context data in a widget.
protected  DataObject queryAttributes(DataObject query, java.lang.String error)
          This method queries the attributes of a widget.
protected  DataObject queryCallbacks(DataObject query, java.lang.String error)
          This method queries the callbacks of a widget.
protected abstract  AttributeNameValues queryGenerator()
          This abstract method is called when the widget wants to get the latest generator info.
protected  DataObject queryServices(DataObject query, java.lang.String error)
          This method queries the services of a widget.
protected  DataObject queryWidget(DataObject query, boolean update, java.lang.String error)
          This method runs a query on a widget, asking for either it's latest acquired data (QUERY) or asking for the widget to acquire and return new data (UPDATE_AND_QUERY)
protected  DataObject removeSubscriber(DataObject sub, java.lang.String error)
          This method removes a subscriber to this object.
protected  DataObject retrieveData(DataObject data, java.lang.String error)
          This method retrieves data from the widget's storage.
 DataObject runUserMethod(DataObject data)
          This method is meant to handle any internal methods that the baseObject doesn't handle.
protected  DataObject runWidgetMethod(DataObject data, java.lang.String error)
          This is an empty method that should be overridden by objects that subclass from this class.
protected  void sendToSubscribers(java.lang.String callback, AttributeNameValues atts)
          This method should be called to send data to subscribers when a context widget's callback is triggered.
protected  void setAttribute(java.lang.String name, java.lang.String type)
          Sets an attribute
protected abstract  Attributes setAttributes()
          Sets the attributes for the widget
protected abstract  Callbacks setCallbacks()
          Sets the callbacks for the widget
protected abstract  Services setServices()
          Sets the services for the widget
protected  void store(AttributeNameValues data)
          This stub method stores the data in the given AttributeNameValues object
protected  void store(DataObject data)
          This stub method stores the data in the given DataObject
protected  void updateWidgetInformation()
          This method is called when a remote component sends an UPDATE_AND_QUERY message.
 
Methods inherited from class context.arch.BaseObject
askInterpreter, decodeData, encodeData, executeAsynchronousWidgetService, executeSynchronousWidgetService, getHostAddress, getId, getVersion, getVersion, getWidgetAttributes, getWidgetCallbacks, getWidgetServices, main, pingComponent, pollWidget, putDataInWidget, queryVersion, quit, retrieveDataFrom, retrieveDataFrom, retrieveDataFrom, retrieveDataFrom, retrieveDataFrom, retrieveDataFrom, retrieveDataFrom, returnPing, runComponentMethod, runMethod, serviceResult, setId, setVersion, subscribeTo, subscribeTo, subscribeTo, subscribeTo, unsubscribeFrom, updateAndPollWidget, userCallback, userRequest, userRequest, userRequest
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS

public static final java.lang.String CLASS
Tag for the class file being used by the widget

VERSION_NUMBER

public java.lang.String VERSION_NUMBER
Dummy version number. Subclasses should override this value.

DEFAULT_PORT

public static final int DEFAULT_PORT
Default port for widgets to use

VERSION

public static final java.lang.String VERSION
Tag for version number.

TIMESTAMP

public static final java.lang.String TIMESTAMP
Tag for the timestamp of widget data

QUERY

public static final java.lang.String QUERY
Tag to indicate the widget should return the latest stored data

QUERY_REPLY

public static final java.lang.String QUERY_REPLY
Tag to indicate the reply to a QUERY message

UPDATE_AND_QUERY

public static final java.lang.String UPDATE_AND_QUERY
Tag to indicate the widget should get the latest data from the generator and return them

UPDATE_AND_QUERY_REPLY

public static final java.lang.String UPDATE_AND_QUERY_REPLY
Tag to indicate the reply to an UPDATE_AND_QUERY message

QUERY_ATTRIBUTES

public static final java.lang.String QUERY_ATTRIBUTES
Tag to indicate the widget should return its list of attributes

QUERY_ATTRIBUTES_REPLY

public static final java.lang.String QUERY_ATTRIBUTES_REPLY
Tag to indicate the reply to a QUERY_ATTRIBUTES message

QUERY_CALLBACKS

public static final java.lang.String QUERY_CALLBACKS
Tag to indicate the widget should return its list of callbacks

QUERY_CALLBACKS_REPLY

public static final java.lang.String QUERY_CALLBACKS_REPLY
Tag to indicate the reply to a QUERY_CALLBACKS message

QUERY_SERVICES

public static final java.lang.String QUERY_SERVICES
Tag to indicate the widget should return its list of services

QUERY_SERVICES_REPLY

public static final java.lang.String QUERY_SERVICES_REPLY
Tag to indicate the reply to a QUERY_SERVICES message

QUERY_VERSION

public static final java.lang.String QUERY_VERSION
Tag to indicate the widget should return its version number

QUERY_VERSION_REPLY

public static final java.lang.String QUERY_VERSION_REPLY
Tag to indicate the reply to a QUERY_VERSION message

PUT_DATA

public static final java.lang.String PUT_DATA
Tag to indicate the widget should accept the given data

PUT_DATA_REPLY

public static final java.lang.String PUT_DATA_REPLY
Tag to indicate the reply to a PUT_DATA message

UPDATE

public static final java.lang.String UPDATE
Tag to indicate an update is being sent

SPACER

public static final java.lang.String SPACER
Constant for the widget spacer

id

protected java.lang.String id

attributes

protected Attributes attributes

attributeTypes

protected java.util.Hashtable attributeTypes

callbacks

protected Callbacks callbacks

services

protected Services services

CurrentOffset

protected long CurrentOffset

subscribers

public Subscribers subscribers
Object to handle subscriptions to context data
See Also:
Subscribers, Subscriber

storage

public StorageObject storage
Object to keep track of storage
See Also:
StorageObject
Constructor Detail

Widget

public Widget(java.lang.String clientClass,
              java.lang.String serverClass,
              int serverPort,
              java.lang.String encoderClass,
              java.lang.String decoderClass,
              java.lang.String storageClass,
              java.lang.String id)
Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks, and services and setting up the BaseObject info.
Parameters:
clientClass - Class to use for client communications
serverClass - Class to use for server communications
serverPort - Port to use for server communications
encoderClass - Class to use for communications encoding
decoderClass - Class to use for communications decoding
storageClass - Class to use for storage
id - String to use for widget id and persistent storage
See Also:
StorageObject

Widget

public Widget(java.lang.String clientClass,
              java.lang.String serverClass,
              int serverPort,
              java.lang.String encoderClass,
              java.lang.String decoderClass,
              boolean storageFlag,
              java.lang.String id)
Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks, and services and setting up the BaseObject info. This version takes a boolean to indicate whether the default storage class should be used or whether no storage should be provided.
Parameters:
clientClass - Class to use for client communications
serverClass - Class to use for server communications
serverPort - Port to use for server communications
encoderClass - Class to use for communications encoding
decoderClass - Class to use for communications decoding
storageFlag - Flag to determine whether storage should be used or not
id - String to use for widget id and persistent storage
See Also:
StorageObject

Widget

public Widget(int port,
              java.lang.String id,
              boolean storageFlag)
Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services. It takes a port number as a parameter to indicate which port to listen for messages/connections, the id to use for the widget, and a flag to indicate whether storage functionality should be turned on or off.
Parameters:
port - Port to listen to for incoming messages
id - Widget id
storageFlag - Boolean flag to indicate whether storage should be turned on

Widget

public Widget(int port,
              java.lang.String id)
Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services. It takes a port number as a parameter to indicate which port to listen for messages/connections.
Parameters:
port - Port to listen to for incoming messages
id - Widget id

Widget

public Widget(java.lang.String id,
              boolean storageFlag)
Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services. It takes the id to use for the widget, and a flag to indicate whether storage functionality should be turned on or off.
Parameters:
id - Widget id
storageFlag - Boolean flag to indicate whether storage should be turned on

Widget

public Widget(java.lang.String id)
Constructor that sets up internal variables for maintaining the list of widget attributes, callbacks and services. It takes the widget id as a parameter
Parameters:
id - ID of the widget
Method Detail

setAttributes

protected abstract Attributes setAttributes()
Sets the attributes for the widget

setCallbacks

protected abstract Callbacks setCallbacks()
Sets the callbacks for the widget

setServices

protected abstract Services setServices()
Sets the services for the widget

getAttributeType

protected java.lang.String getAttributeType(java.lang.String name)
Returns the attribute value with the given name
Parameters:
name - Name of the attribute to get

setAttribute

protected void setAttribute(java.lang.String name,
                            java.lang.String type)
Sets an attribute
Parameters:
name - Name of the attribute to set
type - Type of the attribute

isAttribute

protected boolean isAttribute(java.lang.String name)
Checks if the given attribute is an attribute of this widget
Parameters:
name - Name of the attribute to check

isCallback

protected boolean isCallback(java.lang.String name)
Checks if the given callback is an callback of this widget
Parameters:
name - Name of the callback to check

notify

public void notify(java.lang.String event,
                   java.lang.Object data)
This is an empty method that should be overridden by objects that subclass from this class. It is intended to be called by generator objects when new data has arrived for the purpose of notifying the widget.
Parameters:
event - Name of the event occurring
data - Object containing the relevant data

updateWidgetInformation

protected void updateWidgetInformation()
This method is called when a remote component sends an UPDATE_AND_QUERY message. It calls the widget's queryGenerator method to get the latest generator info, and then stores it.

queryGenerator

protected abstract AttributeNameValues queryGenerator()
This abstract method is called when the widget wants to get the latest generator info.
Returns:
AttributeNameValues containing the latest generator information

runWidgetMethod

protected DataObject runWidgetMethod(DataObject data,
                                     java.lang.String error)
This is an empty method that should be overridden by objects that subclass from this class. It is called when another component tries to run a method on the widget, but it's not a query.
Parameters:
data - DataObject containing the data for the method
error - String containing the incoming error value
Returns:
DataObject containing the method results

runUserMethod

public DataObject runUserMethod(DataObject data)
This method is meant to handle any internal methods that the baseObject doesn't handle. In particular, this method handles the common details for query requests, update and query requests, and version requests that each widget should provide. If the method is not one of these queries, then it calls runWidgetMethod which each widget should provide.
Overrides:
runUserMethod in class BaseObject
Parameters:
data - DataObject containing the method to run and parameters
Returns:
DataObject containing the results of running the method
See Also:
QUERY, QUERY_VERSION, UPDATE_AND_QUERY

putData

protected DataObject putData(DataObject data,
                             java.lang.String error)
This method puts context data in a widget. It is expected that widgets will get data from a generator. But for some widgets, the generator will not use the context toolkit directly, but may use a web CGI script, for example. For this case, the widget provides this method to collect the data and makes it available to subscribers and for retrieval.
Parameters:
data - DataObject containing the context data to write
error - String containing the incoming error value
Returns:
DataObject containing the results of writing the data

queryCallbacks

protected DataObject queryCallbacks(DataObject query,
                                    java.lang.String error)
This method queries the callbacks of a widget.
Parameters:
query - DataObject containing the query
error - String containing the incoming error value
Returns:
DataObject containing the results of the query

queryAttributes

protected DataObject queryAttributes(DataObject query,
                                     java.lang.String error)
This method queries the attributes of a widget.
Parameters:
query - DataObject containing the query
error - String containing the incoming error value
Returns:
DataObject containing the results of the query

queryServices

protected DataObject queryServices(DataObject query,
                                   java.lang.String error)
This method queries the services of a widget.
Parameters:
query - DataObject containing the query
error - String containing the incoming error value
Returns:
DataObject containing the results of the query

queryWidget

protected DataObject queryWidget(DataObject query,
                                 boolean update,
                                 java.lang.String error)
This method runs a query on a widget, asking for either it's latest acquired data (QUERY) or asking for the widget to acquire and return new data (UPDATE_AND_QUERY)
Parameters:
query - DataObject containing the query request
update - Whether or not to acquire new data
error - String containing the incoming error value
Returns:
DataObject containing the reply to the query

canHandle

protected boolean canHandle(Attributes attributes)
This method checks the list of attributes to ensure that the widget contains these attributes.
Parameters:
attributes - Attributes object containing attributes to check
Returns:
whether the list of attributes is valid

canHandle

protected boolean canHandle(Attributes atts,
                            Conditions conds)
This method checks the list of attributes and conditions to ensure that the widget contains these attributes.
Parameters:
atts - List of attributes to check
conds - List of Conditions to check
Returns:
whether the list of attributes and conditions is valid

canHandle

protected boolean canHandle(AttributeFunctions atts,
                            Conditions conds)
This method checks the list of attributes and conditions to ensure that the widget contains these attributes.
Parameters:
atts - List of attributes to check
conds - List of Conditions to check
Returns:
whether the list of attributes and conditions is valid

canHandle

protected boolean canHandle(AttributeNameValues atts)
This method checks the list of attributes (in an AttributeNameValues object) to ensure that the widget contains these attributes.
Parameters:
atts - List of attributes to check
Returns:
whether the list of attributes and conditions is valid

executeService

protected DataObject executeService(DataObject request,
                                    java.lang.String error)
This method attempts to execute a widget service.
Parameters:
request - DataObject containing the service request
error - String containing the incoming error value
Returns:
DataObject containing the results of the service request

sendToSubscribers

protected void sendToSubscribers(java.lang.String callback,
                                 AttributeNameValues atts)
This method should be called to send data to subscribers when a context widget's callback is triggered. It sends data only to those subscribers that have subscribed to the specified callback.
Parameters:
callbackTag - Context widget callback that was triggered
atts - AttributeNameValues to send to subscribers
data - DataObject version of atts
See Also:
BaseObject.userRequest(DataObject, String, String, int), Subscribers

processCallbackReply

protected void processCallbackReply(DataObject result,
                                    Subscriber sub)
This method should be overriden to process the results of subscription callbacks.
Parameters:
result - DataObject containing the result
sub - Subscriber that returned this reply

addSubscriber

protected DataObject addSubscriber(DataObject sub,
                                   java.lang.String error)
This method adds a subscriber to this object. It calls Subscribers.addSubscriber() if it can add the subscriber. It returns a DataObject containing the reply information, including any error information.
Parameters:
sub - DataObject containing the subscription information
error - String containing the incoming error value
Returns:
DataObject with the reply to the subscription request
See Also:
Subscribers.addSubscriber(String,String,int,String,String,Conditions,Attributes)

removeSubscriber

protected DataObject removeSubscriber(DataObject sub,
                                      java.lang.String error)
This method removes a subscriber to this object. It calls Subscribers.removeSubscriber() if it can remove the subscriber. It returns a DataObject containing the reply information, including any error information.
Parameters:
sub - DataObject containing the subscription information
error - String containing the incoming error value
Returns:
DataObject with the reply to the subscription request
See Also:
Subscribers.removeSubscriber(String, String, String, String, String)

retrieveData

protected DataObject retrieveData(DataObject data,
                                  java.lang.String error)
This method retrieves data from the widget's storage. It returns a DataObject containing the retrieved data information, including any error information.
Parameters:
data - DataObject containing the subscription information
error - String containing the incoming error value
Returns:
DataObject with the reply to the subscription request

store

protected void store(DataObject data)
This stub method stores the data in the given DataObject
Parameters:
data - Data to store
See Also:
StorageObject.store(DataObject)

store

protected void store(AttributeNameValues data)
This stub method stores the data in the given AttributeNameValues object
Parameters:
data - Data to store
See Also:
StorageObject.store(AttributeNameValues)

getNewOffset

protected void getNewOffset()
This method creates a thread that retrieves a global time clock and determines the offset between the local clock and the global clock. It checks this
Returns:
the offset between the global and local clocks
See Also:
OffsetThread

getNewOffsetNoDelay

protected long getNewOffsetNoDelay()
This method retrieves the offset between the local clock and a global clock with no delay.
Returns:
the offset between the global and local clocks
See Also:
OffsetThread

getCurrentTime

protected java.lang.Long getCurrentTime()
This method returns the current time to use as a timestamp
Returns:
the current time, corrected using a global clock offset