Title: Communicating with Devices
1Communicating with Devices
Prasun Dewan
Department of Computer Science University of
North Carolina dewan_at_unc.edu
2Steps to UPnP Networking
5 Presentation
4 Eventing
3 Control
- 0 Control point and device get addresses
- 1 Control point finds interesting device
- 2 Control point learns about device capabilities
- 3 Control point invokes actions on device
- 4 Control point listens to state changes of
device - 5 Control point controls device and/or views
device status using HTML UI
2 Description
1 Discovery
0 Addressing
3Example Service
lt?xml version"1.0" ?gt ltscpd xmlns"urnschemas-u
pnp-orgservice-1-0"gt ltspecVersiongt
ltmajorgt1lt/majorgt ltminorgt0lt/minorgt
lt/specVersiongt
4Example Action List
ltactionListgt ltactiongt
ltnamegtPowerOnlt/namegt lt/actiongt
ltactiongt ltnamegtSetChannellt/namegt
ltargumentListgt ltargumentgt
ltnamegtChannellt/namegt ltrelatedStateVariable
gtChannellt/relatedStateVariablegt
ltdirectiongtinlt/directiongt
lt/argumentgt
lt/argumentListgt lt/actiongt
. ltactionListgt
5Example Variables
ltserviceStateTablegt ltstateVariable
sendEvents"yes"gt
ltnamegtPowerlt/namegt
ltdataTypegtBooleanlt/dataTypegt
ltdefaultValuegt0lt/defaultValuegt
lt/stateVariablegt ltstateVariable
sendEvents"yes"gt
ltnamegtChannellt/namegt
ltdataTypegti4lt/dataTypegt
ltallowedValueRangegt
ltminimumgt1lt/minimumgt
ltmaximumgt100lt/maximumgt
ltstepgt1lt/stepgt
lt/allowedValueRangegt
ltdefaultValuegt1lt/defaultValuegt
lt/stateVariablegt .. lt/serviceStateTablegt
6Example Server Code
Object processMessages (Message message) switch
(message.kind) case GET_VAR_REQUEST switch
((GetVarMessage) message).varName) case
Channel message.result getChannel()
case ACTION_REQUEST actionMessage
(ActionMessage) message. switch
(actionMessage.actionName) case setChannel
setChannel(parseString(actionMessage.params0))
int channel int getChannel () return
curentChannel void setChannel (int newChannel)
channel newChannel
UPnP.announce(Channel, toString(getChannel())
...
7High Programming Cost
- Device developer must
- define the XML-based external representation of
the device - translate the client operation-invocation and
variable-access requests to local
operation-invocations - announce variable change notifications
- Exporting functionality requires more code than
implementing it in example Intel device - not counting UI code
- Cost proportional to complexity of type.
- Reason for not allowing complex types?
8High Maintenance Cost
- Information repeated multiple times
- operation implementation code
- action list
- state variable table
- message processing code
- event announcement code
- All repetitions must be changed
- tedious
- error-prone
- Channel -gt Current Channel requires 9 changes!
9Changed Action List
ltactionListgt ltactiongt
ltnamegtPowerOnlt/namegt lt/actiongt
ltactiongt ltnamegtSetCurrentChannellt/namegt
ltargumentListgt
ltargumentgt ltnamegtCurrentChannellt/namegt
ltrelatedStateVariabl
egtCurrentChannellt/relatedStateVariablegt
ltdirectiongtinlt/directiongt
lt/argumentgt
lt/argumentListgt lt/actiongt
. ltactionListgt
10Changed State Variable Table
ltserviceStateTablegt ltstateVariable
sendEvents"yes"gt
ltnamegtPowerlt/namegt
ltdataTypegtBooleanlt/dataTypegt
ltdefaultValuegt0lt/defaultValuegt
lt/stateVariablegt ltstateVariable
sendEvents"yes"gt
ltnamegtCurrentChannellt/namegt
ltdataTypegti4lt/dataTypegt
ltallowedValueRangegt
ltminimumgt1lt/minimumgt
ltmaximumgt100lt/maximumgt
ltstepgt1lt/stepgt
lt/allowedValueRangegt
ltdefaultValuegt1lt/defaultValuegt
lt/stateVariablegt .. lt/serviceStateTablegt
11Changed Server Code
Object processMessages (Message message) switch
(message.kind) case GET_VAR_REQUEST switch
((GetVarMessage) message).varName) case
CurrentChannel message.result
getCurrentChannel() case ACTION_REQUEST
actionMessage (ActionMessage) message.
switch (actionMessage.actionName) case
setCurrentChannel setCurrentChannel(parseString
(actionMessage.params0)) int
channel int getCurrentChannel () return
channel void setCurrentChannel (int newChannel)
channel newChannel
UPnP.announce(CurrentChannel,
toString(getCurrentChannel()) ...
12Ideal Developer Effort
int channel int getChannel () return
channel void setChannel (int newChannel)
channel newChannel ...
13No Programmer-Defined Types
- XML defines 24 data types
- ui1, ui2, ui4, i1, i2, i4, int
- r4, r8, number, fixed.14.4, float
- char, string
- date, dateTime, dateTime.tz, time, time.tz
- boolean
- bin.base64, bin.hex
- uri
- uuid
14No Programmer-Defined Types
- Only supports predefined types
- state variables
- action parameters
- No way to model dynamic composites
15Generating Action List
External Description
Device Code
Actions
Generator
16Generating Action Descriptions
int channel int getChannel () return
channel void setChannel (int newChannel)
channel newChannel
- Mainly syntactic issue
- Static approach
- precompiler
- Dynamic approach
- reflection
- vcr.getClass().getMethods()
- method.getName()
- method.getParameterTypes()
- Done in ICrafter (Stanford)
- How to get related state variable?
ltaction listgt ltactiongt ltnamegtSetCurrentChanne
llt/namegt ltargumentListgt
ltargumentgt ltnamegtNewChannellt/namegt
ltrelatedStateVariablegt
CurrentChannel
lt/relatedStateVariablegt
ltdirectiongtinlt/directiongt
lt/argumentgt lt/argumentListgt
lt/actiongt . lt/actionListgt
17Why variables?
- Clients may view device as state manager
- Because their user has this view.
- VCR user interface
- May compose state
- aggregating sensors
- More abstract to associate events with variable
changes rather than method invocations - Channel changed vs.SetChannel called
- Similarly access and concurrency control state
specific
18Generating External Representation
External Description
Actions
Device class
Generator
Variables
- Problem in general unsolvable
- Need help from programmer
19Generating Variable Descriptions
public int channel int getChannel () return
channel void setChannel (int newChannel)
channel newChannel
- Basic idea
- Object state stored in internal vars.
- Make them public.
- Drawbacks
- Violates encapsulation
- Changing internal representation changes clients
- Not all state may be stored
- How to get
- Related state vars
- Min, max range
- How to generate events
- public var could be changed directly
ltserviceStateTablegt ltstateVariable
sendEvents"yes"gt
ltnamegtChannellt/namegt
ltdataTypegti4lt/dataTypegt
ltallowedValueRangegt
ltminimumgt1lt/minimumgt
ltmaximumgt100lt/maximumgt
ltstepgt1lt/stepgt
lt/allowedValueRangegt
ltdefaultValuegt1lt/defaultValuegt
lt/stateVariablegt .. lt/serviceStateTablegt
20Interface-based extraction
- Abstract data type helps by exposing logical
structure using standard system-defined interface
(TreeNode) - Approach used in Swing table widget
- Equivalent to writing external description
- extra programming effort
- extra maintenance effort
- no type checking (everything TreeNode)
- Cannot generate types of variables
- must use common interfaces and associated object
files
public class VCR implements TreeNode int
channel int getChannel () return channel
void setChannel (int newChannel)
channel newChannel
public Vector getVarNames() public TreeNode
getVar(String fieldName) public void
setVar(String fieldName, val TreeNode)
21Pattern-based Approach
int channel int getChannel () return
channel void setChannel (int newChannel)
channel newChannel
- Can use relationships between operation names and
signatures to - derive external variables (properties)
- channel int
- related state variable
- setChannel changes channel
- Relationships defined by Java beans
22Simple Properties in Java
- Defined by a pair of getter/setter methods
- String getTitle()
- void setTitle(String newTitle)
- define a title property of type String.
- In general,
- ltPropertyTypegt getltPropertyNamegt()
- void setltPropertyNamegt(ltPropertyTypegt)
- define a simple property named ltPropertyNamegt of
type ltPropertyTypegt with read/write semantics.
23Role of JavaBeans Properties
- Properties are discrete named attributes of a
Java Bean that can affect its appearance, or its
behavior. - typical properties
- font
- color
- size
24Properties as Record Fields
int channel int getChannel () return
channel void setChannel (int newChannel)
channel newChannel
- Main idea
- consider properties as elements of the logical
structure of the object. - record fields
- Problems
- Forces conventions
- Channel() instead of getChannel()?
- Legacy code
- Min, max range?
ltserviceStateTablegt ltstateVariable
sendEvents"yes"gt
ltnamegtChannellt/namegt
ltdataTypegti4lt/dataTypegt
ltallowedValueRangegt
ltminimumgt1lt/minimumgt
ltmaximumgt100lt/maximumgt
ltstepgt1lt/stepgt
lt/allowedValueRangegt
ltdefaultValuegt1lt/defaultValuegt
lt/stateVariablegt .. lt/serviceStateTablegt
25Property Specification
- Approach
- separate property specification and
interpretation - Mechanism
- introduce a pattern language for property
specification
26Pattern specifications
Pattern specification
Property Extractor
Property Definitions
Device Operations
- Approach
- separate property specification and
interpretation - Mechanism
- introduce a pattern language for property
specification
27Examples
Literals
Free Variables Unified
- Specification 1
- String getTitle()
- void setTitle(String newTitle)
- define a title property of type String.
- Specification 2
- String title()
- void setTitle(String newTitle)
- define a title property of type String.
- Grammar?
- Methods as sentences
- Property name and types not known
- Must be extracted
- Unification
- Methods as facts
- Property names and types as unified variables
28Property Specification Language
- Canonical representation of method signatures
- ltreturn_typegt method_name(arg1_type, ,argN_type)
- An example specification simple properties
- 1. Method pattern specifications
- getter lt.GetType.gt get.PropName.()
- setter ltvoidgt set.PropName.(.SetType.)
- pattern variables GetType, PropName, SetType
- example matches
- getter String getTitle()
- GetType String,PropName Title
- setter void setTitle(String)
- SetType String, PropName Title
29Java Bean Specification with Constraints
property type simple access methods
getter, setter getter lt.GetType.gt
get.PropName.() setter ltvoidgt
set.PropName.(.SetType.) constraints
getter.PropName setter.PropName
getter.GetType setter.SetType name
getter.PropName end
30Examples
- Specification 1
- String getTitle()
- void setTitle(String newTitle)
- define a title property of type String.
- Specification 2
- String title()
- void setTitle(String newTitle)
- define a title property of type String.
31Alternative Bean Specification
property type simple access methods
getter, setter getter lt.GetType.gt.PropName.()
setter ltvoidgt set.PropName.(.SetType.)
constraints getter.PropName setter.PropName get
ter.GetType setter.SetType name
getter.PropName end
32Properties as Record Fields
int channel int getChannel () return
channel void setChannel (int newChannel)
channel newChannel
- Allows
- Channel() instead of getChannel()
- Legacy code
- Issues
- Min, max range?
ltserviceStateTablegt ltstateVariable
sendEvents"yes"gt
ltnamegtChannellt/namegt
ltdataTypegti4lt/dataTypegt
ltallowedValueRangegt
ltminimumgt1lt/minimumgt
ltmaximumgt100lt/maximumgt
ltstepgt1lt/stepgt
lt/allowedValueRangegt
ltdefaultValuegt1lt/defaultValuegt
lt/stateVariablegt .. lt/serviceStateTablegt
33Min, Max Extensions
Property type simple access methods min,
max min ltintgt getMin.PropName.() max ltintgt
getMax.PropName.() step ltintgt
getStep.PropName.() constraints
getter.PropNamemin.PropName max.PropName end
34Alternative Min, Max Extensions
- Constraints on variable declarations
property type simple access fields min,
max, step min ltfinalgt ltintgt MIN_.PropName. max
ltfinalgt ltintgt MAX_.PropName. step ltfinalgt
ltintgt STEP_.PropName. constraints
min.PropNamemax.PropName step.PropName
getter.PropName end
35No Programmer-Defined Types
- Only supports predefined types
- state variables
- action parameters
- No way to model dynamic composites
36Two issues
- Representation of programmer-defined types in XML
- Seems to be syntactic issue
- Generating external representation
- Static (Record) structures addressed
- JavaBeans defines field properties
- Language allows convention definitions
- Dynamic structures?
37Indexed Properties in Java
- An indexed property is
- a simple property of an array type
- Section getSection()
- void setSection( Section section)
- with two additional methods
- Section getSection( int index)
- void setSection( Section s, int index)
- Emulates array semantics in procedural languages
38Understand Java Vector
- Cannot support programmer-defined dynamic list
class VCR void power() int
getCurrentChannel() void setCurrentChannel(in
t newVal) Vector getSettings() void
setSettings(Vector newVal)
39Example Programmer-defined List
class VCR void power() int
getCurrentChannel() void setCurrentChannel(in
t newVal) int size() Setting
elementAt(int index) void removeElement(Setti
ng newVal) void addElement (Setting newVal)
void setElementAt(Setting newVal,
int index)
40Sequence properties
insert ltvoidgt insert.Prop.at( int,
.InsType.) remove ltvoidgt remove.Prop.at(
int) lookup lt.GetType.gt .Prop.At( int) set
ltvoidgt set.Prop.At( int, .SetType.) count ltintgt
size() property type sequence access methods
insert, remove, lookup, count, set
constraints insert.Prop remove.Prop
lookup.Prop insert.InsType lookup.SetType
lookup.GetType name insert.Prop element
type insert.InsType
public int size() public int settingAt(int
index) public void setSettingAt(int index,
Setting newVal) public void insertSettingAt(int
index, Setting newVal)
41Programmer-Defined Types in External
Representation
- Concrete Constructors
- Pascal enumeration
- Subrange
- Hashtable
- Array
- Sequence (variable size list)
- Record
- Not abstract types
- Appliance object is abstract type
- Must deconstruct object so that variables are
known, that is, must map it to concrete type
42Pattern specifications
Pattern specification
Property Extractor
Property Definitions
Device Operations
Programmer must conform to specified pattern
43Conforming to Patterns
- Makes code understandable to humans
- Also makes code understandable to system tools
- Bean builder
- Property Extractor
- Unlike Bean framework, the patterns chosen by
users - System adapts to application interfaces rather
than vice versa - More flexible than the alternative of
system-defined interfaces - Pattern specification without pattern variables
is simply an interface
44Sequence interface
insert ltvoidgt insertElementat( int,
Object) remove ltvoidgt removeElementAt(
int) lookup Object elementAt( int) set ltvoidgt
setElementAt( int, Object) count ltintgt
size() property type sequence access methods
insert, remove, lookup, count, set name
vector element type Object
- Fixes method names
- Fixes element type to Object
- not Setting
- no type checking
public int size() public int elementAt(int
index) public void setElementAt(int index,
Object newVal) public void insertElementAt(int
index, Object newVal) public void
insertElementAt(int index, Object newVal)
45Pattern Variables Allowing Multiple Methods
Names/Element Types
insert ltvoidgt insert.Prop.at( int,
.InsType.) remove ltvoidgt remove.Prop.at(
int) lookup lt.GetType.gt .Prop.At( int) set
ltvoidgt set.Prop.At( int, .SetType.) count ltintgt
size() property type sequence access methods
insert, remove, lookup, count, set
constraints insert.Prop remove.Prop
lookup.Prop insert.InsType remove.SetType
lookup.GetType name insert.Prop element
type insert.InsType
public int size() public int elementAt(int
index) public void setElementAt(int index,
Object newVal) public void insertElementAt(int
index, Object newVal) public void
removeElementAt(int index, Object newVal)
public int size() public int settingAt(int
index) public void setSettingAt(int index,
Setting newVal) public void insertSettingAt(int
index, Setting newVal) public void
removeElementAt(int index)
46Alternative Sequence properties with same access
methods
insert ltvoidgt insert.Prop.at( int,
.InsType.) remove ltvoidgt remove.Prop.at(
int) index lt.GetType.gt .Prop.At( int) set
ltvoidgt set.Prop.At( int, .SetType.) count ltintgt
count() property type sequence access methods
insert, remove, index, count, set constraints
insert.Prop remove.Prop index.Prop
insert.InsType remove.SetType
lookup.GetType name insert.Prop element type
insert.InsType
public int count() public int settingAt(int
index) public void setSettingAt(int index,
Setting newVal) public void insertSettingAt(int
index, Setting newVal) public void
removeElementAt(int index)
47Alternative Sequence properties with different
access methods
insert ltvoidgt insert.Prop.at( int,
.InsType.) remove ltvoidgt remove.Prop.at(
int) iterate Enumeration elements() set
ltvoidgt set.Prop.At( int, .SetType.) property
type sequence access methods insert, remove,
iterate, set constraints insert.Prop
remove.Prop lookup.Prop insert.InsType
remove.SetType lookup.GetType name
insert.Prop element type insert.InsType
public Enumeration elements() public void
setSettingAt(int index, Setting newVal) public
void insertSettingAt(int index, Setting
newVal) public void removeElementAt(int index)
48Reducing High Programming Cost
- Device developer must
- define the XML-based external representation of
the device - system does it automatically, mapping operations
to external representation - translate the client operation-invocation and
variable-access requests to local
operation-invocations - based on its knowledge of the mapping it can
translate automatically - announce variable change notifications
- system does successive object diffs to announce
events automatically
49Generating Events
- Subscription Manager
- Separate from device
- Allows clients to register interests in device
- Forwards announcements to them
- Differ assumes subscription manager exists
- Takes successive snapshots
- When?
- How?
Device Object
Differ
Subscription Manager
register interest
forward event
Client
50When Snapshots Taken?
- Client requests
- user refreshed
- wants the differences rather than complete values
- Some client has taken an action on device
- informs some global object about it
- other clients can now be notified
- Timer-based
- differ polls
- when upto date information not crucial
- Device requests
- Differ.announce() vs.
- UPnP.announce(Channel, toString(getChannel())
51How Snapshots Compared?
- Operations diff(s1, s2)
- for each property p of s1 and s2
- diffs diff (s1.p1, s2.p2)
diff(o1,o2) returns a sequence of operations that
when applied to o1 take it to o2.
52How properties found?
property type sequence access methods
getter, setter ...
- Property extraction based on pattern
specifications. - But pattern specifications defined and
interpreted by user. - matchedProperty.getMethod(getter).invoke
(params) - getter is user-defined
- Also multiple access methods possible per
property type - indexing and iteration for sequences
- Need a standard device-independent interface for
each property constructor
53Device Independent Interface
- Unix file operations for all devices
- do not model structured objects
- UPnP
- standard operation to read any variable
- no standard operation to write variable
- client must determine device-specific write
operation via relatedStateVariable clause - no programmer-defined types
- Need standard operations for each property
- say indexing for sequence
- how mapped to device-specific methods?
54Mapping Approaches
- Global table/registry
- Standard OS approach
- may not have one to one mapping from
device-independent to device-dependent - indexing to iteration
- Mapping per device?
- Additional effort on device programmer
- Per-pattern mapping
- done by property handlers
- apply to multiple device classes
55Property Handlers
property type sequence access methods
elements, ... ... handlers lookup
IteratorToIndexing ...
- Specified in pattern definition
- Implement device-indep interface
interface Indexing Object elementAt(Object
target, MatchedProperty mp, int index
class IteratorToIndexing implements Indexing
... Object elementAt(Object target,
MatchedProperty mp, int index Enumeration
elements mp.getMethod( elements")).invoke(targe
t,
null) for (i 0 i lt index
i) elements.nextElement() return
elements.nextElement()
56Three Levels of Indirection
- Level 0
- Client calls operation directly
- device.elementAt(index)
- bound to specific names
- Level 1
- Client calls pattern-specific access methods
- mp.getMethod(index).invoke(device, index)
- lookup may be bound to elementAt() or
settingAt() - Level 2
- Client calls property handler
- mp.getClass(lookup).newInstance().invoke (
device, mp, index) - Independent of access methods
- indexing
- iteration
57Three Levels of Indirection
mp.getPropertyHandler (lookup). invoke(device,
mp, index)
mp.getAccessMethod(index). invoke(device,
index)
mp.getAccessMethod(iterate). invoke(device, )
device1.elementAt (index)
device2.settingAt(index)
device3.iterate ()
58Late Binding vs. Programmability/Efficiency
- The later the binding
- the more levels of indirection
- the more awkward the programming
- the less efficient the program
- Choose the minimum amount of indirection for the
lateness desired - Device-independent interface meant really for
system functions such as diff - may be useful to clients
59Vassils thesis
- Reduces programming cost
- No cost of exporting functionality to distributed
clients - Like RPC, but accommodates late binding as UPnP
does - dynamic service discovery
- Reduces maintenance cost
- No duplication of information as it is all
automatically generated. - Supports predefined and programmer-defined types
- Makes reduction of programming and maintenance
costs harder
60Reducing High Prog/Maintenance Cost
- Information manually repeated multiple times
- operation implementation code
- action list
- state variable table
- message processing code
- event announcement code
- Channel -gt Current Channel requires 9 changes!
- getChannel -gt getCurrentChannel
- setChannel -gt setCurrentChannel
2
61Architecture
Device Operations
Access Specification
Event Generator
Property Specification
External Description Generator
Device Handler
Subscription Manager
Access Controller
Differ
Property Extractor
Inter-Process Communication
Device-Independent Interface
Reflection