|
1 /* |
|
2 * Copyright (c) 2008 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: This class provides container for message. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <string.h> |
|
19 |
|
20 #include "javacommonutils.h" |
|
21 #include "exceptionbase.h" |
|
22 |
|
23 #include "commsmessage.h" |
|
24 #include "logger.h" |
|
25 #include "javauid.h" |
|
26 |
|
27 |
|
28 namespace java |
|
29 { |
|
30 namespace comms |
|
31 { |
|
32 using namespace std; |
|
33 using namespace java::util; |
|
34 |
|
35 |
|
36 OS_EXPORT CommsMessage::CommsMessage() |
|
37 : mStream(ios::in | ios::out | ios::binary), mArray(0) |
|
38 { |
|
39 memset(&mHeader, 0, sizeof(mHeader)); |
|
40 } |
|
41 |
|
42 |
|
43 OS_EXPORT CommsMessage::CommsMessage(const ipcMessage_t* aMessage) |
|
44 : mStream(ios::in | ios::out | ios::binary), mArray(0) |
|
45 { |
|
46 mHeader = aMessage->ipcHeader; |
|
47 |
|
48 mStream.clear(); |
|
49 mStream.str(""); //reset |
|
50 mStream.write(aMessage->data, aMessage->ipcHeader.length-sizeof(aMessage->ipcHeader)); |
|
51 |
|
52 } |
|
53 |
|
54 |
|
55 OS_EXPORT CommsMessage::CommsMessage(const CommsMessage& aMessage) |
|
56 : mStream(ios::in | ios::out | ios::binary), mArray(0) |
|
57 { |
|
58 *this = aMessage; |
|
59 } |
|
60 |
|
61 |
|
62 OS_EXPORT CommsMessage& CommsMessage::operator=(const CommsMessage& aMessage) |
|
63 { |
|
64 if (this != &aMessage) |
|
65 { |
|
66 mHeader = aMessage.mHeader; |
|
67 |
|
68 mStream.clear(); |
|
69 mStream.str(""); //reset |
|
70 mStream << aMessage.toString(); |
|
71 } |
|
72 return *this; |
|
73 } |
|
74 |
|
75 |
|
76 OS_EXPORT CommsMessage::~CommsMessage() |
|
77 { |
|
78 delete[] mArray; |
|
79 } |
|
80 |
|
81 |
|
82 OS_EXPORT void CommsMessage::setMessageId(int aMessageId) |
|
83 { |
|
84 mHeader.messageId = aMessageId; |
|
85 } |
|
86 |
|
87 |
|
88 OS_EXPORT void CommsMessage::setModuleId(int aModuleId) |
|
89 { |
|
90 mHeader.moduleId = aModuleId; |
|
91 } |
|
92 |
|
93 |
|
94 OS_EXPORT void CommsMessage::setReceiver(int aReceiver) |
|
95 { |
|
96 mHeader.receiver = aReceiver; |
|
97 } |
|
98 |
|
99 |
|
100 OS_EXPORT void CommsMessage::setSender(int aSender) |
|
101 { |
|
102 mHeader.sender = aSender; |
|
103 } |
|
104 |
|
105 |
|
106 OS_EXPORT void CommsMessage::setMessageRef(int aMessageRef) |
|
107 { |
|
108 mHeader.messageRef = aMessageRef; |
|
109 } |
|
110 |
|
111 |
|
112 OS_EXPORT int CommsMessage::getMessageId() const |
|
113 { |
|
114 return mHeader.messageId; |
|
115 } |
|
116 |
|
117 |
|
118 OS_EXPORT int CommsMessage::getModuleId() const |
|
119 { |
|
120 return mHeader.moduleId; |
|
121 } |
|
122 |
|
123 |
|
124 OS_EXPORT int CommsMessage::getReceiver() const |
|
125 { |
|
126 return mHeader.receiver; |
|
127 } |
|
128 |
|
129 |
|
130 OS_EXPORT int CommsMessage::getSender() const |
|
131 { |
|
132 return mHeader.sender; |
|
133 } |
|
134 |
|
135 |
|
136 OS_EXPORT bool CommsMessage::hasPermission(const CommsPermission& aPermission) const |
|
137 { |
|
138 return (mHeader.permissions & aPermission) == aPermission; |
|
139 } |
|
140 |
|
141 |
|
142 OS_EXPORT int CommsMessage::getMessageRef() const |
|
143 { |
|
144 return mHeader.messageRef; |
|
145 } |
|
146 |
|
147 |
|
148 OS_EXPORT void CommsMessage::replyTo(const CommsMessage& aMessage) |
|
149 { |
|
150 setModuleId(aMessage.getModuleId()); |
|
151 setMessageId(aMessage.getMessageId()); |
|
152 setMessageRef(aMessage.getMessageRef()); |
|
153 setReceiver(aMessage.getSender()); |
|
154 setSender(aMessage.getReceiver()); |
|
155 } |
|
156 |
|
157 |
|
158 OS_EXPORT CommsMessage& CommsMessage::operator << (const std::string& aStr) |
|
159 { |
|
160 mStream << aStr.size() << " " << aStr; |
|
161 return *this; |
|
162 } |
|
163 |
|
164 |
|
165 OS_EXPORT CommsMessage& CommsMessage::operator << (const std::wstring& aStr) |
|
166 { |
|
167 string str; |
|
168 try |
|
169 { |
|
170 char* utf8 = JavaCommonUtils::wstringToUtf8(aStr); |
|
171 str.assign(utf8); |
|
172 delete[] utf8; |
|
173 } |
|
174 catch (ExceptionBase& e) |
|
175 { |
|
176 ELOG2(EJavaComms, "%s: wstringToUtf8 failed: %s", __PRETTY_FUNCTION__, e.toString().c_str()); |
|
177 } |
|
178 |
|
179 (*this) << str; |
|
180 |
|
181 return *this; |
|
182 } |
|
183 |
|
184 |
|
185 OS_EXPORT CommsMessage& CommsMessage::operator << (const int& aInt) |
|
186 { |
|
187 (*this) << JavaCommonUtils::intToString(aInt); |
|
188 return *this; |
|
189 } |
|
190 |
|
191 |
|
192 OS_EXPORT CommsMessage& CommsMessage::operator << (const long long& aLong) |
|
193 { |
|
194 (*this) << JavaCommonUtils::longLongToString(aLong); |
|
195 return *this; |
|
196 } |
|
197 |
|
198 |
|
199 OS_EXPORT CommsMessage& CommsMessage::operator << (const java::util::Uid& aUid) |
|
200 { |
|
201 (*this) << aUid.toString(); |
|
202 return *this; |
|
203 } |
|
204 |
|
205 |
|
206 OS_EXPORT CommsMessage& CommsMessage::operator >> (std::wstring& aStr) |
|
207 { |
|
208 string str; |
|
209 (*this) >> str; |
|
210 |
|
211 try |
|
212 { |
|
213 aStr = JavaCommonUtils::utf8ToWstring(str.c_str()); |
|
214 } |
|
215 catch (ExceptionBase& e) |
|
216 { |
|
217 ELOG2(EJavaComms, "%s: utf8ToWstring failed: %s", __PRETTY_FUNCTION__, e.toString().c_str()); |
|
218 aStr = L""; |
|
219 } |
|
220 |
|
221 return *this; |
|
222 } |
|
223 |
|
224 |
|
225 OS_EXPORT CommsMessage& CommsMessage::operator >> (std::string& aStr) |
|
226 { |
|
227 aStr.clear(); |
|
228 int length = 0; |
|
229 |
|
230 if (mStream >> length) |
|
231 { |
|
232 //Below is skipped space character. |
|
233 if (!(mStream.get())) |
|
234 { |
|
235 ELOG2(EJavaComms, "ios::badbit - no space after length, invalid stream? - msgId=%d - moduleId=%d", |
|
236 getMessageId(), getModuleId()); |
|
237 mStream.setstate(ios::badbit); |
|
238 return *this; |
|
239 } |
|
240 |
|
241 char* retPtr = new char[length]; |
|
242 if (!(mStream.read(retPtr,length))) |
|
243 { |
|
244 ELOG2(EJavaComms, "ios::badbit - unable read all, invalid stream? - msgId=%d - moduleId=%d", |
|
245 getMessageId(), getModuleId()); |
|
246 mStream.setstate(ios::badbit); |
|
247 delete [] retPtr; |
|
248 return *this; |
|
249 } |
|
250 aStr.append(retPtr, length); |
|
251 delete [] retPtr; |
|
252 } |
|
253 else |
|
254 { |
|
255 LOG2(EJavaComms, EInfo, "ios::badbit - nothing to read - msgId=%d - moduleId=%d", |
|
256 getMessageId(), getModuleId()); |
|
257 mStream.setstate(ios::badbit); |
|
258 } |
|
259 return *this; |
|
260 } |
|
261 |
|
262 |
|
263 OS_EXPORT CommsMessage& CommsMessage::operator >> (int& aInt) |
|
264 { |
|
265 string str; |
|
266 (*this) >> str; |
|
267 try |
|
268 { |
|
269 aInt = JavaCommonUtils::stringToInt(str); |
|
270 } |
|
271 catch (ExceptionBase& e) |
|
272 { |
|
273 aInt = 0; |
|
274 LOG3(EJavaComms, EInfo, "CommsMessage::operator >> (int& aInt) failed: %s - msgId=%d - moduleId=%d", |
|
275 e.toString().c_str(), getMessageId(), getModuleId()); |
|
276 } |
|
277 return *this; |
|
278 } |
|
279 |
|
280 |
|
281 OS_EXPORT CommsMessage& CommsMessage::operator >> (long long& aLong) |
|
282 { |
|
283 string str; |
|
284 (*this) >> str; |
|
285 try |
|
286 { |
|
287 aLong = JavaCommonUtils::stringToLongLong(str); |
|
288 } |
|
289 catch (ExceptionBase& e) |
|
290 { |
|
291 aLong = 0LL; |
|
292 LOG3(EJavaComms, EInfo, "CommsMessage::operator >> (long long& aLong) failed: %s - msgId=%d - moduleId=%d", |
|
293 e.toString().c_str(), getMessageId(), getModuleId()); |
|
294 } |
|
295 return *this; |
|
296 } |
|
297 |
|
298 |
|
299 OS_EXPORT CommsMessage& CommsMessage::operator >> (java::util::Uid& aUid) |
|
300 { |
|
301 std::wstring str; |
|
302 (*this) >> str; |
|
303 java::util::Uid uid(str); |
|
304 aUid = uid; |
|
305 return *this; |
|
306 } |
|
307 |
|
308 |
|
309 OS_EXPORT bool CommsMessage::operator !() const |
|
310 { |
|
311 if ((mStream.fail()) || (mStream.bad())) |
|
312 return true; |
|
313 return false; |
|
314 } |
|
315 |
|
316 |
|
317 OS_EXPORT CommsMessage::operator void*() const |
|
318 { |
|
319 if ((mStream.fail()) || (mStream.bad())) |
|
320 return 0; |
|
321 return const_cast<CommsMessage*>(this); |
|
322 } |
|
323 |
|
324 |
|
325 OS_EXPORT std::string CommsMessage::toString() const |
|
326 { |
|
327 return mStream.str(); |
|
328 } |
|
329 |
|
330 |
|
331 OS_EXPORT char* CommsMessage::toByteArray() |
|
332 { |
|
333 // get size |
|
334 mStream.seekg(0, ios::end); |
|
335 int size = mStream.tellg(); |
|
336 mStream.seekg(0, ios::beg); |
|
337 |
|
338 if (size < 0) |
|
339 { |
|
340 // S60 STL implementation sets error flag on and return size -1 if stream is empty |
|
341 size = 0; |
|
342 mStream.clear(); |
|
343 } |
|
344 |
|
345 delete[] mArray; |
|
346 |
|
347 mArray = new char[size + sizeof(ipcHeader_t)]; |
|
348 ipcMessage_t* msg = reinterpret_cast<ipcMessage_t*>(mArray); |
|
349 |
|
350 // get content |
|
351 mStream.read(msg->data, size); |
|
352 |
|
353 msg->ipcHeader = mHeader; |
|
354 msg->ipcHeader.length = size + sizeof(ipcHeader_t); |
|
355 |
|
356 return mArray; |
|
357 } |
|
358 |
|
359 |
|
360 OS_EXPORT void CommsMessage::begin() |
|
361 { |
|
362 mStream.seekg(0, ios::beg); |
|
363 } |
|
364 |
|
365 |
|
366 OS_EXPORT void CommsMessage::reset() |
|
367 { |
|
368 CommsMessage empty; |
|
369 *this = empty; |
|
370 } |
|
371 |
|
372 } // namespace comms |
|
373 } // namespace java |