03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

This encapsulation is important, as it ensures we cannot inadvertently change state at an<br />

inappropriate time.<br />

An initial state change occurs every time the task represented by the state machine is<br />

invoked. This may be through an external request (a 'send packet' request, for example) or<br />

just by opening the state machine. This can then cause a whole sequence of successive<br />

asynchronous requests, each with a corresponding state change. While these asynchronous<br />

operations are taking place, the task is 'active', meaning that no further external requests<br />

can be serviced until we're finished with this one. Finally, when no more asynchronous<br />

requests are required, the task returns to 'inactive' and the state machine is able to accept<br />

further client requests.<br />

For every state change, there are two phases, as shown in Figure 20.2. First, we issue a<br />

request to enter the new state – which in turn will issue an asynchronous request – and<br />

second, we complete the entry to the state, when the asynchronous request completes. At<br />

this point, there may be another state change – and further asynchronous request(s) – or the<br />

task may just complete. The latter case signifies the completion of the external request, and<br />

the machine moves back to its inactive mode waiting for another request.<br />

Figure 20.2<br />

The actual code to change state is as follows:<br />

void CGdpStateMachine::ChangeState(CGdpStateMachine::TState*<br />

aNextState)<br />

{<br />

// Enter aNextState, and make it our current state.<br />

__ASSERT_ALWAYS(!IsActive(),<br />

GdpUtil::Panic(GdpUtil::EStateMachineStateError));<br />

TInt err;<br />

while(aNextState)<br />

{<br />

// State change required.<br />

iState = aNextState;

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

Saved successfully!

Ooh no, something went wrong!