|
1 /* |
|
2 * Copyright (c) 2007-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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <iostream> |
|
20 #include <string> |
|
21 #include <vector> |
|
22 #include <unistd.h> |
|
23 |
|
24 #include "logger.h" |
|
25 #include "javacommonutils.h" |
|
26 #include "exceptionbase.h" |
|
27 |
|
28 #include "echoserver.h" |
|
29 |
|
30 using namespace java::comms; |
|
31 using namespace std; |
|
32 |
|
33 EchoServer::EchoServer() |
|
34 { |
|
35 } |
|
36 |
|
37 EchoServer::~EchoServer() |
|
38 { |
|
39 } |
|
40 |
|
41 int EchoServer::start(int addr) |
|
42 { |
|
43 mComms.registerDefaultListener(this); |
|
44 return mComms.start(addr); |
|
45 } |
|
46 |
|
47 int EchoServer::stop() |
|
48 { |
|
49 mComms.unregisterDefaultListener(this); |
|
50 return mComms.stop(); |
|
51 } |
|
52 |
|
53 |
|
54 void EchoServer::processMessage(CommsMessage& aMessage) |
|
55 { |
|
56 /* |
|
57 LOG1(EJavaComms, EInfo, "processMessage, sender = %d", aMessage.getSender()); |
|
58 LOG1(EJavaComms, EInfo, "processMessage, messageId = %d", aMessage.getMessageId()); |
|
59 LOG1(EJavaComms, EInfo, "processMessage, moduleId = %d", aMessage.getModuleId()); |
|
60 LOG1(EJavaComms, EInfo, "processMessage, messageRef= %d", aMessage.getMessageRef()); |
|
61 */ |
|
62 switch (aMessage.getModuleId()) |
|
63 { |
|
64 case MODULE_ID_NO_REPLY: |
|
65 break; |
|
66 |
|
67 case MODULE_ID_SLEEP_1S: |
|
68 case MODULE_ID_SLEEP_2S: |
|
69 case MODULE_ID_SLEEP_5S: |
|
70 usleep(1000*aMessage.getModuleId()); |
|
71 // fall through |
|
72 |
|
73 default: |
|
74 mReply = aMessage; |
|
75 mReply.replyTo(aMessage); |
|
76 mComms.send(mReply); |
|
77 break; |
|
78 } |
|
79 } |
|
80 |
|
81 std::string wstringToUtf8(const std::wstring& str) |
|
82 { |
|
83 string result; |
|
84 try |
|
85 { |
|
86 char* utf8 = java::util::JavaCommonUtils::wstringToUtf8(str); |
|
87 result.assign(utf8); |
|
88 delete[] utf8; |
|
89 } |
|
90 catch (java::util::ExceptionBase& e) |
|
91 { |
|
92 ELOG1(EJavaComms, "wstringToUtf8 failed: %s", e.toString().c_str()); |
|
93 } |
|
94 return result; |
|
95 } |
|
96 |