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.

72 Chapter 4. Event-driven <strong>Programming</strong> in Practice<br />

Choosing the next phase: Branching<br />

Often there are several potential next phases in sensor-network programs. The<br />

choice of the actual next phase then depends on an input event, the state of a<br />

previous computation, or a combination of both. Also, a single phase may be<br />

reachable through multiple paths, as in the following example.<br />

Start<br />

Surge Example 2<br />

Until now our Surge application ignored a failed sending attempt.<br />

If sending fails, however, the program may decide to store the data<br />

sample in a local buffer (rather then ignoring the failure). The modified<br />

program could then, <strong>for</strong> example, send the stored data sample<br />

in the next round together with the new sample. An updated version<br />

of the program is depicted in Fig. 4.2. In the new version, the<br />

choice of the next phase from SEND depends on the event generated<br />

by the send operation (nack or ack). Likewise, the IDLE phase may<br />

be reached from two phases, namely SEND and STORE. ✷<br />

INIT SAMPLE<br />

init_done sample_done<br />

SEND<br />

nack<br />

ack<br />

STORE<br />

IDLE<br />

store_done<br />

timeout<br />

Figure 4.2: A more sophisticated version of Surge with five program phases. In<br />

this program version a failure while sending the data sample is handled<br />

by buffering the data locally. Note that the control flow branches<br />

from the SEND phase depending on the events nack and ack.<br />

Preemption<br />

In sensor-node programs the occurrence of a certain event often interrupts (we<br />

say it preempts) a sequence of multiple phases in order to start a new phase. That<br />

is, if the event occurs in any phase of a sequence of phases, the control flow of<br />

the program is transfered to the new phase.<br />

Surge Example 3<br />

Let us now consider yet another variation of the basic Surge program,<br />

which is inspired by the modification of Surge as distributed<br />

with TinyOS [125]. In this version we introduce a network-command<br />

interface that allows a remote user to put the node in sleep mode by<br />

sending a sleep message to it. The node then preempts all current<br />

activities and sleeps until it receives a subsequent wakeup message.<br />

This version of Surge is depicted in Fig. 4.3. ✷<br />

The dashed lines in Fig. 4.3 denote the preemption caused by the sleep event.<br />

That is, the control is passed to the SLEEP phase from any of the phases SAMPLE,<br />

SEND, and IDLE on the occurrence of the sleep event.

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

Saved successfully!

Ooh no, something went wrong!