21.08.2013 Views

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

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.2. The Anatomy of <strong>Sensor</strong>-Node Programs 73<br />

INIT<br />

init_done<br />

NORMAL_OPERATION<br />

SAMPLE<br />

sample_done (n)ack timeout<br />

SEND<br />

wakeup sleep<br />

SLEEP<br />

IDLE<br />

Figure 4.3: The three program phases SAMPLE, SEND, and IDLE are preempted<br />

by the sleep event.<br />

The execution of the target phase of a preemption takes priority over the preempted<br />

phases. Preemption is basically the same as what we have previously<br />

called branching, only that preemption has a number of source phases (rather<br />

then just one). Preemption is can be considered as handling of exceptions. The<br />

phase to which control is transferred to may then be considered the exception<br />

handler.<br />

Specifying preemption in the event-model requires extensive state management.<br />

The difficulty is two-fold: Firstly, on the occurrence of a preempting event,<br />

the programmer must first deinitialize the phases that are to be preempted. Special<br />

care must be taken if preemption is added during the redesign of a sensornode<br />

program. From our experience, proper deinitialization can be easily <strong>for</strong>gotten.<br />

And secondly, (future) events that are caused by computational operations<br />

per<strong>for</strong>med be<strong>for</strong>e the preemption occurred must be actively ignored. For<br />

example, in our example program the (n)ack event is caused by sending a data<br />

sample. If the send operation has completed, but a sleep event is received next<br />

(i.e., be<strong>for</strong>e the (n)ack event), the (n)ack event is nevertheless generated by the<br />

OS and thus triggers the associated action. In this action, however, it needs to be<br />

ignored. The code fragment in Prog. 4.4 shows a possible implementation of the<br />

preemption in the event-driven model. The handler of the sleep event (lines 1-<br />

14) implements the preemption. The preempted phases are deinitialized within<br />

the switch statement (lines 3-11). The handler of the nack event first checks the<br />

exceptional case when the program is in the SLEEP state (lines 16). Then the<br />

nack event is ignored. If the program was coming from the SEND phase (which<br />

is the regular case), it behaves as without preemption. Clearly, the complexity of<br />

manual state management in the case of preemption is hard to handle <strong>for</strong> programmers.<br />

Even this short program is hard to write and even harder to read. To<br />

deduce a programmers intention from the code is next to impossible.<br />

Structuring Program Phases: Phase Hierarchies<br />

In some cases, it is convenient to think of a set of sequentially composed phases<br />

as a single, higher-level program phase. This implies that there is a hierarchy of<br />

phases, where phases higher in the hierarchy (which we call super-phases) are<br />

composed of one or more sub-phases. Our previous example, Fig. 4.3, is such a<br />

case.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!