java.lang.Object
org.cybergarage.upnp.Service
An UPnP service, described by an SCPD document and reached through its containing device.
Field Summary
Modifier and TypeFieldDescriptionprivate static final StringXML attribute name for configuration ID.private static final StringXML element name for control URL.static final StringXML element name for service nodes.private static final StringXML element name for event subscription URL.static final StringXML element name for major version.static final StringDefault major version value.static final StringXML element name for minor version.static final StringDefault minor version value.private Mutex/////////////////////////////////////////////static final StringRoot node name for service control point documents.static final StringNamespace for service control point documents.private static final StringXML element name for service control point document URL.private static final StringXML element name for service ID.private static final StringXML element name for service type.private Node/////////////////////////////////////////////static final StringXML element name for specification version.private Object/////////////////////////////////////////////Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionvoidAdds an action to this service.voidAdd the StateVariable to the service.
Note: This method should be used to create a dynamic
Device withtout writing any XML that describe the device
.voidaddSubscriber(Subscriber sub) Register a subscriber to receive event notifications for this service.voidAnnounces this service to the network using SSDP.voidAnnounces this service as gone to the network using SSDP.voidclearSID()Discard the subscription ID and zero the timeout, marking the service as no longer subscribed.Finds an action by name in this service.Gets the list of actions provided by this service.intGets the configuration ID for this service.Gets the control URL for this service.Gets the description URL for this service.Gets the device containing this service.private NodeGets the device node containing this service.Gets the event subscription URL for this service.private StringGives the service type as sent in notify messages.private StringBuilds the search target string for notifications.Gets the root device containing this service.private NodeGets the root node of the device description.byte[]Gets the SCPD data for this service as a byte array.private NodeGets the SCPD node for this service, loading it if necessary.private NodegetSCPDNode(File scpdFile) Gets the SCPD node by parsing from a file.private NodegetSCPDNode(URL scpdUrl) Gets the SCPD node by parsing from a URL.Gets the SCPD URL for this service.private ServiceDataParses the service node.Gets the service ID of this service.Gets the XML node representing this service.Builds the state variable table for this service from its SCPD document.Gets the service type of this service.getSID()Get this service's subscription ID.getStateVariable(String name) Looks up a state variable in this service's state table.getSubscriber(String name) Look up a subscriber by its subscription ID.Get the subscribers registered for this service.longGet the subscription timeout in seconds.Get the application object attached to this service.booleanhasSID()Test whether a subscription ID is currently recorded for this service.booleanhasStateVariable(String name) Test whether this service publishes the named state variable.booleanisControlURL(String url) Checks if the given URL matches this service's control URL.booleanisEventSubURL(String url) Checks if the given URL matches this service's event subscription URL.booleanChecks if the given URL matches this service's SCPD URL.booleanTest whether the given name identifies this service, either by its service type or by its service ID.static booleanisServiceNode(Node node) Checks if the given XML node represents a service.booleanTest whether this service currently holds a subscription.private booleanChecks if the given URL matches the reference URL, considering both absolute and relative URLs.booleanLoads service control point description (SCPD) from a file.booleanloadSCPD(InputStream input) Loads service description (SCPD) from an input stream.booleanLoads service control point description (SCPD) from a string.voidlock()Acquires exclusive lock on this service for thread-safe operations.private booleannotify(Subscriber sub, StateVariable stateVar) Sends a state variable's new value to one subscriber.voidnotify(StateVariable stateVar) Send an event for one state variable to every current subscriber, dropping any that have expired first.voidSend an event for every state variable that is flagged to send events.voidDeregister a subscriber so it stops receiving event notifications.booleanserviceSearchResponse(SSDPPacket ssdpPacket) Answer an SSDP M-SEARCH that names this service.voidsetActionListener(ActionListener listener) Register the listener invoked when one of this service's actions is invoked.voidsetControlURL(String value) Sets the control URL for this service.voidsetDescriptionURL(String value) Sets the description URL for this service.voidsetEventSubURL(String value) Sets the event subscription URL for this service.voidsetQueryListener(QueryListener queryListener) Register the listener invoked when any of this service's state variables is queried.voidsetSCPDURL(String value) Sets the SCPD URL for this service.voidsetServiceID(String value) Sets service ID of this service.voidsetServiceType(String value) Sets service type of this service.voidSet this service's subscription ID.voidsetTimeout(long value) Set the subscription timeout.voidsetUserData(Object data) Attach an application object to this service, for retrieval by getUserData.voidunlock()Releases exclusive lock on this service.voidUpdates the configuration ID for this service based on its SCPD XML.
Field Details
ELEM_NAME
XML element name for service nodes.- See Also:
serviceNode
/////////////////////////////////////////////SCPD_ROOTNODE
Root node name for service control point documents.- See Also:
SCPD_ROOTNODE_NS
Namespace for service control point documents.- See Also:
SPEC_VERSION
XML element name for specification version.- See Also:
MAJOR
MAJOR_VALUE
MINOR
MINOR_VALUE
mutex
/////////////////////////////////////////////SERVICE_TYPE
XML element name for service type.- See Also:
SERVICE_ID
XML element name for service ID.- See Also:
CONFIG_ID
XML attribute name for configuration ID.- See Also:
SCPDURL
XML element name for service control point document URL.- See Also:
CONTROL_URL
XML element name for control URL.- See Also:
EVENT_SUB_URL
XML element name for event subscription URL.- See Also:
userData
/////////////////////////////////////////////
Constructor Details
Service
public Service()Creates a new empty Service instance. This constructor creates a service with default SCPD specification version 1.0 and initializes the required XML structure.Service
Creates a Service instance from an existing XML node.- Parameters:
node- XML node representing the service
Method Details
getServiceNode
Gets the XML node representing this service.- Returns:
- the service XML node
lock
public void lock()Acquires exclusive lock on this service for thread-safe operations.unlock
public void unlock()Releases exclusive lock on this service.isServiceNode
Checks if the given XML node represents a service.- Parameters:
node- the XML node to check- Returns:
- true if the node is a service node, false otherwise
getDeviceNode
Gets the device node containing this service.- Returns:
- the device node, or null if not found
getRootNode
getDevice
getRootDevice
setServiceType
Sets service type of this service.- Parameters:
value- service type string to set
getServiceType
Gets the service type of this service.- Returns:
- service type string, or empty string if not set
setServiceID
Sets service ID of this service.- Parameters:
value- service ID string to set
getServiceID
Gets the service ID of this service.- Returns:
- service ID string, or empty string if not set
updateConfigId
public void updateConfigId()Updates the configuration ID for this service based on its SCPD XML.getConfigId
public int getConfigId()Gets the configuration ID for this service.- Returns:
- the configuration ID, or 0 if not set
isURL
setSCPDURL
Sets the SCPD URL for this service.- Parameters:
value- the SCPD URL to set
getSCPDURL
isSCPDURL
Checks if the given URL matches this service's SCPD URL.- Parameters:
url- the URL to check- Returns:
- true if the URL matches the SCPD URL, false otherwise
setControlURL
Sets the control URL for this service.- Parameters:
value- the control URL to set
getControlURL
isControlURL
Checks if the given URL matches this service's control URL.- Parameters:
url- the URL to check- Returns:
- true if the URL matches the control URL, false otherwise
setEventSubURL
Sets the event subscription URL for this service.- Parameters:
value- the event subscription URL to set
getEventSubURL
Gets the event subscription URL for this service.- Returns:
- the event subscription URL
isEventSubURL
Checks if the given URL matches this service's event subscription URL.- Parameters:
url- the URL to check- Returns:
- true if the URL matches the event subscription URL, false otherwise
loadSCPD
Loads service control point description (SCPD) from a string.- Parameters:
scpdStr- the SCPD XML string to load- Returns:
- true if SCPD was loaded successfully, false otherwise
- Throws:
InvalidDescriptionException- if SCPD cannot be parsed
loadSCPD
Loads service control point description (SCPD) from a file.- Parameters:
file- file containing SCPD XML- Returns:
- true if SCPD was loaded successfully, false otherwise
- Throws:
ParserException- if SCPD cannot be parsed
loadSCPD
Loads service description (SCPD) from an input stream.- Parameters:
input- input stream containing SCPD XML- Returns:
- true if SCPD was loaded successfully, false otherwise
- Throws:
ParserException- if SCPD cannot be parsed
setDescriptionURL
Sets the description URL for this service.- Parameters:
value- the description URL to set
getDescriptionURL
Gets the description URL for this service.- Returns:
- the description URL
getSCPDNode
Gets the SCPD node by parsing from a URL.- Parameters:
scpdUrl- the URL to parse SCPD from- Returns:
- the parsed SCPD node
- Throws:
ParserException- if parsing fails
getSCPDNode
Gets the SCPD node by parsing from a file.- Parameters:
scpdFile- the file to parse SCPD from- Returns:
- the parsed SCPD node
- Throws:
ParserException- if parsing fails
getSCPDNode
Gets the SCPD node for this service, loading it if necessary.- Returns:
- the SCPD node, or null if not available
getSCPDData
public byte[] getSCPDData()Gets the SCPD data for this service as a byte array.- Returns:
- the SCPD XML data as bytes, or empty array if not available
getActionList
Gets the list of actions provided by this service.- Returns:
- ActionList containing all actions of this service, empty list if no actions
getAction
addAction
Adds an action to this service.This method sets service reference for all arguments in action and adds action to the service's SCPD structure.
- Parameters:
a- Action to add to this service
getServiceStateTable
Builds the state variable table for this service from its SCPD document.- Returns:
- the state table, empty if the SCPD document declares none
getStateVariable
Looks up a state variable in this service's state table.- Parameters:
name- the state variable name, matched exactly- Returns:
- the state variable, or null if the service declares no such name
hasStateVariable
Test whether this service publishes the named state variable.- Parameters:
name- the state variable name to look for- Returns:
- true if the service declares a state variable with that name
isService
Test whether the given name identifies this service, either by its service type or by its service ID.- Parameters:
name- the service type or service ID to match; a null value returns false- Returns:
- true if name ends with this service's type or its service ID
getServiceData
Parses the service node.- Returns:
- the parsed data of the service node
getNotifyServiceTypeNT
Gives the service type as sent in notify messages.- Returns:
- the service type as sent in notify messages
getNotifyServiceTypeUSN
Builds the search target string for notifications.- Returns:
- the USN string
announce
Announces this service to the network using SSDP.This method sends SSDP NOTIFY messages to announce the service's presence. The announcement includes the service type, unique service identifier, and location of the root device containing this service.
- Parameters:
bindAddr- bind address to send announcement from
byebye
Announces this service as gone to the network using SSDP.- Parameters:
bindAddr- bind address to send the announcement from
serviceSearchResponse
Answer an SSDP M-SEARCH that names this service.- Parameters:
ssdpPacket- the search request, whose ST header selects the target- Returns:
- false if the request carries no ST header, true once any matching search response has been queued
setQueryListener
Register the listener invoked when any of this service's state variables is queried.- Parameters:
queryListener- the listener to attach to every state variable
getSubscriberList
Get the subscribers registered for this service.- Returns:
- the service's subscriber list, empty if it has no subscribers
addSubscriber
Register a subscriber to receive event notifications for this service.- Parameters:
sub- the subscriber to add to the service's subscriber list
removeSubscriber
Deregister a subscriber so it stops receiving event notifications.- Parameters:
sub- the subscriber to remove from the service's subscriber list
getSubscriber
Look up a subscriber by its subscription ID.- Parameters:
name- the subscription ID to match- Returns:
- the matching subscriber, or null if no subscriber carries that SID
notify
Sends a state variable's new value to one subscriber.- Parameters:
sub- the subscriber to notifystateVar- the variable whose value changed- Returns:
- true if the notification was sent
notify
Send an event for one state variable to every current subscriber, dropping any that have expired first.- Parameters:
stateVar- the state variable whose name and value are announced
notifyAllStateVariables
public void notifyAllStateVariables()Send an event for every state variable that is flagged to send events.getSID
Get this service's subscription ID.- Returns:
- the subscription ID assigned to this service's subscriber
setSID
Set this service's subscription ID.- Parameters:
id- the subscription ID to record
clearSID
public void clearSID()Discard the subscription ID and zero the timeout, marking the service as no longer subscribed.hasSID
public boolean hasSID()Test whether a subscription ID is currently recorded for this service.- Returns:
- true if a non-empty subscription ID is set
isSubscribed
public boolean isSubscribed()Test whether this service currently holds a subscription.- Returns:
- true if a subscription ID is set
getTimeout
public long getTimeout()Get the subscription timeout in seconds.- Returns:
- the number of seconds this service's subscription remains valid
setTimeout
public void setTimeout(long value) Set the subscription timeout.- Parameters:
value- the subscription lifetime in seconds; 0 expires it at once
setActionListener
Register the listener invoked when one of this service's actions is invoked.- Parameters:
listener- the listener to attach to every action in the service
addStateVariable
Add the StateVariable to the service.
Note: This method should be used to create a dynamic
Device withtout writing any XML that describe the device
.
Note: that no control for duplicate StateVariable is done.- Parameters:
var- StateVariable that will be added
setUserData
Attach an application object to this service, for retrieval by getUserData.- Parameters:
data- the object to record; may be null
getUserData
Get the application object attached to this service.- Returns:
- the object passed to setUserData, or null if none has been set