|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Queue the message |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <mpxlog.h> |
|
19 #ifdef _DEBUG |
|
20 #include <mpxmessagegeneraldefs.h> |
|
21 #endif |
|
22 #include "mpxmessagequeue.h" |
|
23 |
|
24 // ============================ PRIVATE CLASSES ============================== |
|
25 NONSHARABLE_CLASS(CMPXMessageItem) : public CBase |
|
26 { |
|
27 public: |
|
28 static CMPXMessageItem* NewL(const CMPXMessage* aMsg, TInt aError); |
|
29 ~CMPXMessageItem(); |
|
30 /** |
|
31 * message object |
|
32 * |
|
33 * @return message object |
|
34 */ |
|
35 CMPXMessage* Message() |
|
36 { |
|
37 return iMsg; |
|
38 } |
|
39 /** |
|
40 * Error code associated with the message |
|
41 * |
|
42 * @return the error code |
|
43 */ |
|
44 TInt Error() |
|
45 { |
|
46 return iError; |
|
47 } |
|
48 /** |
|
49 * Offset to the task queue |
|
50 * |
|
51 * @return the offset |
|
52 */ |
|
53 static TInt Offset() |
|
54 { |
|
55 return _FOFF(CMPXMessageItem,iLink); |
|
56 } |
|
57 private: |
|
58 CMPXMessageItem(TInt aError); |
|
59 void ConstructL(const CMPXMessage* aMsg); |
|
60 private: |
|
61 TSglQueLink iLink; |
|
62 CMPXMessage* iMsg; |
|
63 TInt iError; |
|
64 }; |
|
65 |
|
66 CMPXMessageItem* CMPXMessageItem::NewL(const CMPXMessage* aMsg, TInt aError) |
|
67 { |
|
68 CMPXMessageItem* self = new ( ELeave ) CMPXMessageItem(aError); |
|
69 CleanupStack::PushL( self ); |
|
70 self->ConstructL(aMsg); |
|
71 CleanupStack::Pop(self); |
|
72 return self; |
|
73 } |
|
74 |
|
75 CMPXMessageItem::CMPXMessageItem(TInt aError) |
|
76 : iError(aError) |
|
77 { |
|
78 } |
|
79 |
|
80 CMPXMessageItem::~CMPXMessageItem() |
|
81 { |
|
82 MPX_DEBUG2("-->CMPXMessageItem::~CMPXMessageItem 0x%08x", this); |
|
83 if (iMsg) |
|
84 { |
|
85 MPX_DEBUG3("CMPXMessageItem::~CMPXMessageItem msg 0x%08x, msgHandle 0x%08x", |
|
86 iMsg, iMsg->Data()); |
|
87 |
|
88 delete iMsg; |
|
89 iMsg = NULL; |
|
90 } |
|
91 MPX_DEBUG2("<--CMPXMessageItem::~CMPXMessageItem 0x%08x", this); |
|
92 } |
|
93 |
|
94 void CMPXMessageItem::ConstructL(const CMPXMessage* aMsg) |
|
95 { |
|
96 MPX_FUNC_EX("CMPXMessageItem::ConstructL"); |
|
97 iMsg = aMsg ? CMPXMessage::NewL(*aMsg) : NULL; |
|
98 } |
|
99 |
|
100 // ============================ MEMBER FUNCTIONS ============================== |
|
101 |
|
102 // ---------------------------------------------------------------------------- |
|
103 // Two-phased constructor. |
|
104 // ---------------------------------------------------------------------------- |
|
105 // |
|
106 EXPORT_C CMPXMessageQueue* CMPXMessageQueue::NewL() |
|
107 { |
|
108 CMPXMessageQueue* self = new ( ELeave ) CMPXMessageQueue(); |
|
109 CleanupStack::PushL( self ); |
|
110 self->ConstructL(); |
|
111 CleanupStack::Pop(self); |
|
112 return self; |
|
113 } |
|
114 |
|
115 // ---------------------------------------------------------------------------- |
|
116 // C++ constructor. |
|
117 // ---------------------------------------------------------------------------- |
|
118 // |
|
119 CMPXMessageQueue::CMPXMessageQueue() |
|
120 : iMsgs(CMPXMessageItem::Offset()), |
|
121 iFailure(KErrNone) |
|
122 { |
|
123 } |
|
124 |
|
125 // ---------------------------------------------------------------------------- |
|
126 // Second-phase constructor. |
|
127 // ---------------------------------------------------------------------------- |
|
128 // |
|
129 void CMPXMessageQueue::ConstructL() |
|
130 { |
|
131 MPX_FUNC_EX("CMPXMessageQueue::ConstructL"); |
|
132 } |
|
133 |
|
134 // ---------------------------------------------------------------------------- |
|
135 // Destructor |
|
136 // ---------------------------------------------------------------------------- |
|
137 // |
|
138 EXPORT_C CMPXMessageQueue::~CMPXMessageQueue() |
|
139 { |
|
140 MPX_FUNC_EX("CMPXMessageQueue::~CMPXMessageQueue()"); |
|
141 Reset(); |
|
142 } |
|
143 |
|
144 // ---------------------------------------------------------------------------- |
|
145 // Send next message request from client |
|
146 // ---------------------------------------------------------------------------- |
|
147 // |
|
148 EXPORT_C void CMPXMessageQueue::SendNext(const RMessage2& aMsgRequest) |
|
149 { |
|
150 MPX_FUNC_EX("CMPXMessageQueue::SendNext()"); |
|
151 MPX_ASSERT(iMsgRequest.IsNull()); |
|
152 iMsgRequest = aMsgRequest; |
|
153 if (!iMsgs.IsEmpty()) |
|
154 { |
|
155 Send(); |
|
156 } |
|
157 } |
|
158 |
|
159 // ---------------------------------------------------------------------------- |
|
160 // Adds a message into the queue and sends the message if slot is available |
|
161 // ---------------------------------------------------------------------------- |
|
162 // |
|
163 EXPORT_C void CMPXMessageQueue::Add(const CMPXMessage* aMessage, TInt aError) |
|
164 { |
|
165 // aMessage should not be NULL and aError is KErrNone |
|
166 MPX_ASSERT(aMessage || aError); |
|
167 MPX_FUNC_EX("CMPXMessageQueue::Add()"); |
|
168 CMPXMessageItem* item(NULL); |
|
169 TRAPD(err, item = CMPXMessageItem::NewL(aMessage, aError)); |
|
170 if (KErrNone==err) |
|
171 { |
|
172 iMsgs.AddLast(*item); |
|
173 #ifdef _DEBUG |
|
174 MPX_DEBUG3("CMPXMessageQueue::Add 0x%08x, items %d", this, ++iCount); |
|
175 #endif |
|
176 }//else failed to create message item. |
|
177 else |
|
178 { |
|
179 MPX_DEBUG2("CMPXMessageQueue::Add Failed to create message item %d", err); |
|
180 } |
|
181 |
|
182 if (KErrNone!=err && KErrNone==iFailure) |
|
183 { // Set the failure code |
|
184 iFailure = err; |
|
185 } |
|
186 |
|
187 if (!iMsgRequest.IsNull()) |
|
188 { // outstanding request |
|
189 Send(); |
|
190 } // else client has not finished current message yet |
|
191 } |
|
192 |
|
193 // ---------------------------------------------------------------------------- |
|
194 // Reset message queue |
|
195 // ---------------------------------------------------------------------------- |
|
196 // |
|
197 EXPORT_C void CMPXMessageQueue::Reset() |
|
198 { |
|
199 MPX_FUNC_EX("CMPXMessageQueue::Reset()"); |
|
200 if (!iMsgRequest.IsNull()) |
|
201 { |
|
202 iMsgRequest.Complete(KErrCancel); |
|
203 } |
|
204 delete iMsgSent; |
|
205 iMsgSent = NULL; |
|
206 TSglQueIter<CMPXMessageItem> iter(iMsgs); |
|
207 CMPXMessageItem* msgItem=NULL; |
|
208 while ((msgItem=iter++) != NULL) // Compiler on warning while(msgItem==iter++) |
|
209 { |
|
210 delete msgItem; |
|
211 } |
|
212 iMsgs.Reset(); |
|
213 } |
|
214 |
|
215 // ---------------------------------------------------------------------------- |
|
216 // Send a message |
|
217 // ---------------------------------------------------------------------------- |
|
218 // |
|
219 void CMPXMessageQueue::Send() |
|
220 { |
|
221 MPX_DEBUG3("-->CMPXMessageQueue::Send() 0x%08x, iMsgSent 0x%08x", |
|
222 this, iMsgSent); |
|
223 delete iMsgSent; |
|
224 iMsgSent = NULL; |
|
225 TInt data(0); |
|
226 TInt err(KErrNone); |
|
227 if (iFailure) |
|
228 { |
|
229 err=iFailure; |
|
230 iFailure = KErrNone; // Reset failure code |
|
231 } |
|
232 else |
|
233 { |
|
234 MPX_ASSERT(!iMsgs.IsEmpty()); |
|
235 iMsgSent = iMsgs.First(); |
|
236 iMsgs.Remove(*iMsgSent); |
|
237 #ifdef _DEBUG |
|
238 MPX_DEBUG4("CMPXMessageQueue::Send 0x%08x, msg 0x%08x, items %d", |
|
239 this, iMsgSent->Message(), --iCount); |
|
240 #endif |
|
241 if (iMsgSent->Message()) |
|
242 { |
|
243 data = iMsgSent->Message()->Data(); |
|
244 MPX_ASSERT(data>0); |
|
245 #ifdef _DEBUG |
|
246 CMPXMessage* msg = iMsgSent->Message(); |
|
247 TMPXMessageId* pId = msg->Value<TMPXMessageId>(KMPXMessageGeneralId); |
|
248 if (pId) |
|
249 { |
|
250 TInt id = static_cast<TInt>(*pId); |
|
251 MPX_DEBUG5("CMPXMessageQueue::Send msg item 0x%08x, msg 0x%08x, msgHandle 0x%08x, msgId 0x%08x", |
|
252 iMsgSent, iMsgSent->Message(), data, id); |
|
253 if (id==KMPXMessageGeneral) |
|
254 { |
|
255 MPX_ASSERT(msg->IsSupported(KMPXMessageGeneralEvent)); |
|
256 TInt* pEvent = msg->Value<TInt>(KMPXMessageGeneralEvent); |
|
257 MPX_ASSERT(msg->IsSupported(KMPXMessageGeneralType)); |
|
258 TInt* pType = msg->Value<TInt>(KMPXMessageGeneralType); |
|
259 MPX_ASSERT(msg->IsSupported(KMPXMessageGeneralData)); |
|
260 TInt* pData = msg->Value<TInt>(KMPXMessageGeneralData); |
|
261 if (pEvent && pType && pData) |
|
262 { |
|
263 MPX_DEBUG5("CMPXMessageQueue::Send general msg item 0x%08x, event %d, type %d, data %d", |
|
264 iMsgSent, *pEvent, *pType, *pData); |
|
265 } |
|
266 else |
|
267 { |
|
268 MPX_DEBUG1("CMPXMessageQueue::Send OOM"); |
|
269 } |
|
270 } |
|
271 } |
|
272 else |
|
273 { |
|
274 MPX_DEBUG1("CMPXMessageQueue::Send OOM"); |
|
275 } |
|
276 #endif |
|
277 } // else NULL message |
|
278 else // else NULL message |
|
279 { |
|
280 MPX_ASSERT(iMsgSent->Error()); |
|
281 MPX_DEBUG3("CMPXMessageQueue::Send NULL message 0x%08x, err %d", |
|
282 this, iMsgSent->Error()); |
|
283 } |
|
284 err = iMsgSent->Error(); |
|
285 } |
|
286 |
|
287 TPckgC<TInt> dataPkg(data); |
|
288 TPckgC<TInt> errPkg(err); |
|
289 TInt ret = (iMsgRequest.Write(0, dataPkg)); |
|
290 if (!ret) |
|
291 { |
|
292 ret = iMsgRequest.Write(1, errPkg); |
|
293 } |
|
294 #ifdef _DEBUG |
|
295 if (ret) |
|
296 { |
|
297 MPX_DEBUG1("CMPXMessageQueue::Send Failed to write data"); |
|
298 } |
|
299 #endif |
|
300 iMsgRequest.Complete(ret); |
|
301 MPX_DEBUG5("<--CMPXMessageQueue::Send() 0x%08x, sentMsgHandle 0x%08x, sentError %d, sentRet %d", |
|
302 this, data, err, ret); |
|
303 } |
|
304 |
|
305 // End of file |