|
1 // Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "MMExample1states.h" |
|
17 #include <elements/nm_interfaces.h> |
|
18 |
|
19 using namespace Clock; |
|
20 |
|
21 CTimerCallback* CTimerCallback::NewL(Messages::TNodeCtxId aOriginator) |
|
22 { |
|
23 CTimerCallback* t = new (ELeave)CTimerCallback(aOriginator); |
|
24 CleanupStack::PushL(t); |
|
25 t->ConstructL(); |
|
26 CleanupStack::Pop(t); |
|
27 return t; |
|
28 } |
|
29 |
|
30 CTimerCallback::CTimerCallback(Messages::TNodeCtxId aOriginator) |
|
31 : CTimer(EPriorityStandard), iOriginator(aOriginator) |
|
32 { |
|
33 } |
|
34 |
|
35 void CTimerCallback::ConstructL() |
|
36 { |
|
37 CTimer::ConstructL(); |
|
38 CActiveScheduler::Add(this); |
|
39 } |
|
40 |
|
41 void CTimerCallback::RunL() |
|
42 { |
|
43 Messages::TNodeId nodeid = iOriginator; |
|
44 Messages::RNodeInterface::OpenPostMessageClose(nodeid, iOriginator, TGo().CRef()); |
|
45 } |
|
46 |
|
47 MeshMachine::CNodeActivityBase* CTimerActivity::NewL( const MeshMachine::TNodeActivity& aActivitySig, MeshMachine::AMMNodeBase& aNode ) |
|
48 { |
|
49 TUint c = GetNextActivityCountL(aActivitySig,aNode); |
|
50 return new (ELeave) CTimerActivity( aActivitySig, aNode, c); |
|
51 } |
|
52 |
|
53 CTimerActivity::CTimerActivity( const MeshMachine::TNodeActivity& aActivitySig, MeshMachine::AMMNodeBase& aNode, TUint aNextActivityCount ) |
|
54 : CNodeParallelActivityBase(aActivitySig, aNode, aNextActivityCount), iTimer(NULL) |
|
55 { |
|
56 } |
|
57 |
|
58 CTimerActivity::~CTimerActivity() |
|
59 { |
|
60 if (iTimer) |
|
61 { |
|
62 iTimer->Cancel(); |
|
63 } |
|
64 } |
|
65 |
|
66 DEFINE_SMELEMENT(Clock::TKickOffTimer, NetStateMachine::MStateTransition, Clock::TContext) |
|
67 void Clock::TKickOffTimer::DoL() |
|
68 { |
|
69 ASSERT(iContext.iNodeActivity); |
|
70 CTimerActivity* activity = static_cast<CTimerActivity*>(iContext.iNodeActivity); |
|
71 |
|
72 ASSERT(!activity->iTimer); |
|
73 |
|
74 TWait& msg = Messages::message_cast<TWait>(iContext.iMessage); |
|
75 activity->iTimer = CTimerCallback::NewL(Messages::TNodeCtxId(iContext.ActivityId(), iContext.NodeId())); |
|
76 activity->iTimer->After(msg.iValue * 1000000); |
|
77 activity->SetPostedTo(iContext.NodeId()); |
|
78 } |
|
79 |
|
80 using namespace TrafficLightStates; |
|
81 |
|
82 DEFINE_SMELEMENT(TrafficLightStates::TGoGreen, NetStateMachine::MStateTransition, TrafficLightStates::TContext) |
|
83 void TrafficLightStates::TGoGreen::DoL() |
|
84 { |
|
85 iContext.Node().SetColour(CTrafficLight::EGreen); |
|
86 |
|
87 Messages::RNodeInterface* clock = iContext.Node().GetFirstClient<Messages::TDefaultClientMatchPolicy>(Messages::TClientType(EClock)); |
|
88 iContext.iNodeActivity->PostRequestTo(*clock, TWait(10).CRef()); |
|
89 } |
|
90 |
|
91 DEFINE_SMELEMENT(TrafficLightStates::TGoAmber, NetStateMachine::MStateTransition, TrafficLightStates::TContext) |
|
92 void TrafficLightStates::TGoAmber::DoL() |
|
93 { |
|
94 iContext.Node().SetColour(CTrafficLight::EAmber); |
|
95 |
|
96 Messages::RNodeInterface* clock = iContext.Node().GetFirstClient<Messages::TDefaultClientMatchPolicy>(Messages::TClientType(EClock)); |
|
97 iContext.iNodeActivity->PostRequestTo(*clock, TWait(3).CRef()); |
|
98 } |
|
99 |
|
100 DEFINE_SMELEMENT(TrafficLightStates::TGoRedAndSendGoneRed, NetStateMachine::MStateTransition, TrafficLightStates::TContext) |
|
101 void TrafficLightStates::TGoRedAndSendGoneRed::DoL() |
|
102 { |
|
103 iContext.Node().SetColour(CTrafficLight::ERed); |
|
104 |
|
105 iContext.iNodeActivity->PostToOriginators(TGoneRed()); |
|
106 } |
|
107 |
|
108 using namespace ControllerStates; |
|
109 |
|
110 DEFINE_SMELEMENT(ControllerStates::TAwaitingAllRed, NetStateMachine::MState, ControllerStates::TContext) |
|
111 TBool ControllerStates::TAwaitingAllRed::Accept() |
|
112 { |
|
113 if (!iContext.iMessage.IsMessage<TGoneRed>()) |
|
114 { |
|
115 return EFalse; |
|
116 } |
|
117 |
|
118 if (iContext.iPeer) |
|
119 { |
|
120 iContext.iPeer->ClearFlags(EGreenFlag); |
|
121 } |
|
122 |
|
123 if (iContext.Node().CountClients<Messages::TFlagsOnlyClientMatchPolicy>(Messages::TClientType(0, EGreenFlag))) |
|
124 { |
|
125 iContext.iMessage.ClearMessageId(); |
|
126 return EFalse; |
|
127 } |
|
128 else |
|
129 { |
|
130 return ETrue; |
|
131 } |
|
132 |
|
133 return EFalse; |
|
134 } |
|
135 |
|
136 DEFINE_SMELEMENT(ControllerStates::TEastAndWestGreen, NetStateMachine::MStateTransition, ControllerStates::TContext) |
|
137 void ControllerStates::TEastAndWestGreen::DoL() |
|
138 { |
|
139 Messages::TNodeCtxId from(iContext.ActivityId(), iContext.NodeId()); |
|
140 TGoGreen msg; |
|
141 |
|
142 iContext.Node().PostToClients<Messages::TDefaultClientMatchPolicy>(from, msg, Messages::TClientType(ETrafficLight, EWest), Messages::TClientType::NullType(), EGreenFlag); |
|
143 iContext.Node().PostToClients<Messages::TDefaultClientMatchPolicy>(from, msg, Messages::TClientType(ETrafficLight, EEast), Messages::TClientType::NullType(), EGreenFlag); |
|
144 } |
|
145 |
|
146 DEFINE_SMELEMENT(ControllerStates::TNorthAndSouthGreen, NetStateMachine::MStateTransition, ControllerStates::TContext) |
|
147 void ControllerStates::TNorthAndSouthGreen::DoL() |
|
148 { |
|
149 Messages::TNodeCtxId from(iContext.ActivityId(), iContext.NodeId()); |
|
150 TGoGreen msg; |
|
151 |
|
152 ASSERT(!from.IsNull()); |
|
153 |
|
154 iContext.Node().PostToClients<Messages::TDefaultClientMatchPolicy>(from, msg, Messages::TClientType(ETrafficLight, ENorth), Messages::TClientType::NullType(), EGreenFlag); |
|
155 iContext.Node().PostToClients<Messages::TDefaultClientMatchPolicy>(from, msg, Messages::TClientType(ETrafficLight, ESouth), Messages::TClientType::NullType(), EGreenFlag); |
|
156 } |
|
157 |