A State-Based Programming Model for Wireless Sensor Networks
A State-Based Programming Model for Wireless Sensor Networks
A State-Based Programming Model for Wireless Sensor Networks
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
7.2. Modular and Well-Structured Program Design 125<br />
Program 7.2: OSM code fragment implementing the transition from FOLLOWER<br />
to MEMBER upon a join event. The code also includes the timer<br />
initialization of the target state (line 6).<br />
1 state FOLLOWER {<br />
2 join_ev / op7() -> MEMBER;<br />
3 // [...]<br />
4 }<br />
5 state MEMBER {<br />
6 onEntry: / reset_timer( wait_receive() );<br />
7 // [...]<br />
8 }<br />
Comparison<br />
There are two main differences between the event-driven and the state-based<br />
programming model regarding program structure. Firstly, the order of structuring<br />
devices—states and events—is reversed. In the event-driven model, the<br />
first-level structuring device is events, or rather event handlers. In the event<br />
handlers, control flow is then manually multiplexed (with switch statements)<br />
according to the (manually coded) program state. In the state-based model of<br />
OSM, on the other hand, programs are essentially constructed from hierarchically<br />
structured states, in which the control flow is multiplexed according to<br />
events. Allowing programmers to specify their programs in terms of freely arrangeable<br />
hierarchical states offers more freedom and a better way of structuring,<br />
rather than being confined to a few predetermined event handlers. OSM<br />
program code can be divided into multiple functional units, each implemented<br />
in a state or state hierarchy. Because of the clearly defined interfaces of states,<br />
their implementation can be easily replaced by another implementation with<br />
the same interface. In general, using state machines is a more natural way to<br />
describe programs (see Sect. 4.2).<br />
The second difference between both models is that program state in OSM is a<br />
first-class abstraction and state management can thus be automated. The highlevel<br />
state notation is much more concise compared to the code in the eventdriven<br />
model, which is lacking such an abstraction and thus state has to be managed<br />
manually. From the high-level description of the program’s state structure,<br />
code <strong>for</strong> multiplexing the control flow is generated automatically and transparently<br />
<strong>for</strong> the programmer. The programmer is saved from having to manually<br />
code the control flow. The amount of code that can be saved is significant, as we<br />
will show <strong>for</strong> our EnviroTrack example.<br />
The code structure of the EnviroTrack group-management implementation in<br />
NesC (see Appendix B.1) is clearly dominated by manual stack management<br />
necessitated by the event-driven programming model. As can be seen from<br />
Tab. 7.1, out of the 426 code lines of the original NesC implementation, 187 lines<br />
(i.e., about 44%) are dedicated to manual state management. The remaining<br />
239 lines mainly contain the actual program logic (but also some declarations<br />
and interface declarations). That is, out of 9 lines of code, 4 lines are required