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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.1. Limitations of Event-Driven <strong>Programming</strong> 65<br />

management code, which obscures the program logic and is an additional source<br />

of error.<br />

In the previous Program 4.1, <strong>for</strong> example, replies from remote sensors should<br />

only be regarded until the timeout expires and thus the timeout action is invoked.<br />

After the timeout event, no more changes to sum and num should be<br />

made (even though this is not critical in the concrete example). To achieve this<br />

behavior, the timeout action needs to communicate with the radio-message action<br />

so that no more replies should be regarded. Manual state management is<br />

highlighted in the program code. The desired behavior is en<strong>for</strong>ced by introducing<br />

a (global) boolean flag sampling_active (line 3), which is set to indicate<br />

the state of the aggregation operation. The flag is used in all three functions<br />

(lines 6, 12, and 17). In the message action, the program checks whether the<br />

timeout has occurred already and thus remote temperature values should no<br />

longer regarded (line 12).<br />

In general, programmers using the event-driven programming model must<br />

manually manage the program state in order to be able to decide in which context<br />

an action has been invoked. <strong>Based</strong> on that state in<strong>for</strong>mation programmers<br />

must multiplex the program’s control flow to the appropriate part of the action.<br />

We call this manual state management. Coding the flow control manually requires<br />

operating on state that is shared between multiple functions (such as the<br />

flag sampling_active in our example). Again, this state needs to be managed<br />

manually by the programmers. A general pattern <strong>for</strong> a well-structured implementation<br />

of context-sensitive event handling is shown in Program 4.2.<br />

Accidental Concurrency<br />

Manually managing the program state may lead to accidentally (re)starting<br />

an event cascade, which we call accidental concurrency. In our example,<br />

the initialization function init_remote_average() and the two actions<br />

(message_hdl() and timeout_hdl()) implement a single logical operation<br />

that runs <strong>for</strong> some period of time (i.e., the aggregation phase). The parts of that<br />

operation belong inherently together and it is implicitly expected that the entire<br />

phase has been completed be<strong>for</strong>e it is started anew. However, our example<br />

is prone to accidental concurrency. The event cascade implementing the aggregation<br />

phase could be started anew with a call to init_remote_average()<br />

(perhaps from an action handling incoming network packets <strong>for</strong> re-tasking the<br />

node). Restarting the aggregation phase while a previously started aggregation<br />

phase is still running will lead to unexpected behavior and thus impair the reliability<br />

of the program. In our example, the global variables sum and num will<br />

be reset to zero, yielding an incorrect result <strong>for</strong> the previous instantiation of the<br />

remote-average operation. The topic of accidential concurrency is closly related<br />

to reentrant functions in multithreaded systems. Just like functions, most event<br />

cascades are not reentrant when they rely on global variables to store “private”<br />

state. In event-driven programming, however, global variables need to be used<br />

often, as explained in the previous section.<br />

Accidential concurrency is not limited to multiple instances of the same cascade<br />

of events. Some locical operations (i.e., event cascades) may be mutually<br />

exclusive or may need to be executed in a particular order. Events that occur

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

Saved successfully!

Ooh no, something went wrong!