|
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: Comms java to native method call implementations |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <sstream> |
|
19 #include <errno.h> |
|
20 |
|
21 #include "logger.h" |
|
22 #include "javajniutils.h" |
|
23 #include "javacommonutils.h" |
|
24 |
|
25 #include "com_nokia_mj_impl_comms_CommsEndpoint.h" |
|
26 #include "com_nokia_mj_impl_comms_CommsEndpointBase.h" |
|
27 #include "com_nokia_mj_impl_comms_CommsServerEndpoint.h" |
|
28 #include "commsclientendpoint.h" |
|
29 #include "commsserverendpoint.h" |
|
30 #include "commsmessage.h" |
|
31 #include "commscontext.h" |
|
32 |
|
33 using namespace java::comms; |
|
34 using namespace java::util; |
|
35 |
|
36 // CommsEndpointBase methods |
|
37 |
|
38 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1registerlistener( |
|
39 JNIEnv* aEnv, |
|
40 jobject, |
|
41 jint aHandle, |
|
42 jint aModuleId, |
|
43 jobject aListener) |
|
44 { |
|
45 JELOG2(EJavaComms); |
|
46 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
47 return endpoint->registerJavaListener(aModuleId, aListener, aEnv); |
|
48 } |
|
49 |
|
50 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1unregisterlistener( |
|
51 JNIEnv* aEnv, |
|
52 jobject, |
|
53 jint aHandle, |
|
54 jint aModuleId) |
|
55 { |
|
56 JELOG2(EJavaComms); |
|
57 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
58 return endpoint->unregisterJavaListener(aModuleId, 0, aEnv); |
|
59 } |
|
60 |
|
61 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1send( |
|
62 JNIEnv* aEnv, |
|
63 jobject, |
|
64 jint aHandle, |
|
65 jbyteArray aArray) |
|
66 { |
|
67 JELOG2(EJavaComms); |
|
68 int len = aEnv->GetArrayLength(aArray); |
|
69 |
|
70 char* buf = new char[len]; |
|
71 aEnv->GetByteArrayRegion(aArray, 0, len, (jbyte*)buf); |
|
72 ipcMessage_t* msg = reinterpret_cast<ipcMessage_t*>(buf); |
|
73 msg->ipcHeader.length = len; |
|
74 CommsMessage message(msg); |
|
75 delete[] buf; |
|
76 |
|
77 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
78 return endpoint->send(message); |
|
79 } |
|
80 |
|
81 |
|
82 JNIEXPORT jbyteArray JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1sendreceive( |
|
83 JNIEnv* aEnv, |
|
84 jobject, |
|
85 jint aHandle, |
|
86 jbyteArray aArray, |
|
87 jint aTimeout) |
|
88 { |
|
89 JELOG2(EJavaComms); |
|
90 int len = aEnv->GetArrayLength(aArray); |
|
91 |
|
92 char* buf = new char[len]; |
|
93 aEnv->GetByteArrayRegion(aArray, 0, len, (jbyte*)buf); |
|
94 ipcMessage_t* msg = reinterpret_cast<ipcMessage_t*>(buf); |
|
95 msg->ipcHeader.length = len; |
|
96 CommsMessage message(msg); |
|
97 delete[] buf; |
|
98 |
|
99 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
100 if (endpoint) |
|
101 { |
|
102 CommsMessage receivedMessage; |
|
103 |
|
104 int rc = endpoint->sendReceive(message, receivedMessage, aTimeout); |
|
105 if (!rc) |
|
106 { |
|
107 char* arr = receivedMessage.toByteArray(); |
|
108 int len = (reinterpret_cast<ipcMessage_t*>(arr))->ipcHeader.length; |
|
109 jbyteArray bytes = aEnv->NewByteArray(len); |
|
110 aEnv->SetByteArrayRegion(bytes, 0, len, (jbyte*)arr); |
|
111 return bytes; |
|
112 } |
|
113 else |
|
114 { |
|
115 ELOG1(EJavaComms, "Java_com_nokia_mj_impl_comms_CommsEndpointBase__1sendreceive() failed (%d)", rc); |
|
116 std::stringstream msg; |
|
117 msg << "CommsEndpoint.sendReceive() failed, error code=" << rc; |
|
118 |
|
119 if (rc == ETIMEDOUT || rc == EINTR) |
|
120 { |
|
121 JniUtils::throwNewException(aEnv,"java/lang/InterruptedException", msg.str()); |
|
122 } |
|
123 else |
|
124 { |
|
125 JniUtils::throwNewException(aEnv,"java/io/IOException", msg.str()); |
|
126 } |
|
127 } |
|
128 } |
|
129 |
|
130 return 0; |
|
131 } |
|
132 |
|
133 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1registerdefaultlistener( |
|
134 JNIEnv* aEnv, |
|
135 jobject, |
|
136 jint aHandle, |
|
137 jobject aListener) |
|
138 { |
|
139 JELOG2(EJavaComms); |
|
140 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
141 return endpoint->registerDefaultJavaListener(aListener, aEnv); |
|
142 } |
|
143 |
|
144 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1unregisterdefaultlistener( |
|
145 JNIEnv* aEnv, |
|
146 jobject, |
|
147 jint aHandle) |
|
148 { |
|
149 JELOG2(EJavaComms); |
|
150 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
151 return endpoint->unregisterDefaultJavaListener(0, aEnv); |
|
152 } |
|
153 |
|
154 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1release( |
|
155 JNIEnv*, |
|
156 jobject, |
|
157 jint aHandle) |
|
158 { |
|
159 JELOG2(EJavaComms); |
|
160 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
161 delete endpoint; |
|
162 } |
|
163 |
|
164 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_comms_CommsEndpointBase__1detach( |
|
165 JNIEnv*, |
|
166 jobject, |
|
167 jint aHandle) |
|
168 { |
|
169 JELOG2(EJavaComms); |
|
170 CommsEndpoint* endpoint = reinterpret_cast<CommsEndpoint*>(aHandle); |
|
171 endpoint->detachFromVm(); |
|
172 } |
|
173 |
|
174 // CommsEndpoint methods |
|
175 |
|
176 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpoint__1create( |
|
177 JNIEnv* aEnv, |
|
178 jobject) |
|
179 { |
|
180 JELOG2(EJavaComms); |
|
181 CommsClientEndpoint* client = new CommsClientEndpoint(); |
|
182 client->attachToVm(aEnv); |
|
183 return (jint)client; |
|
184 } |
|
185 |
|
186 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpoint__1find( |
|
187 JNIEnv* aEnv, |
|
188 jclass, |
|
189 jstring aName) |
|
190 { |
|
191 JELOG2(EJavaComms); |
|
192 std::wstring name = JniUtils::jstringToWstring(aEnv, aName); |
|
193 CommsClientEndpoint* endpoint = CommsContext::getContext().find(name); |
|
194 |
|
195 jint rc = 0; |
|
196 if (endpoint) |
|
197 { |
|
198 endpoint->attachToVm(aEnv); |
|
199 rc = (jint)endpoint; |
|
200 } |
|
201 else |
|
202 { |
|
203 WLOG1WSTR(EJavaComms, "Find client endpoint failed, no such endpoint=%s", name); |
|
204 } |
|
205 return rc; |
|
206 } |
|
207 |
|
208 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpoint__1connect( |
|
209 JNIEnv* aEnv, |
|
210 jobject, |
|
211 jint aHandle, |
|
212 jint aAddress) |
|
213 { |
|
214 JELOG2(EJavaComms); |
|
215 CommsClientEndpoint* client = reinterpret_cast<CommsClientEndpoint*>(aHandle); |
|
216 client->attachToVm(aEnv); |
|
217 jint rc =client->connect(aAddress); |
|
218 return rc; |
|
219 } |
|
220 |
|
221 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsEndpoint__1disconnect( |
|
222 JNIEnv*, |
|
223 jobject, |
|
224 jint aHandle) |
|
225 { |
|
226 JELOG2(EJavaComms); |
|
227 CommsClientEndpoint* client = reinterpret_cast<CommsClientEndpoint*>(aHandle); |
|
228 jint rc = client->disconnect(); |
|
229 return rc; |
|
230 } |
|
231 |
|
232 // CommsServerEndpoint methods |
|
233 |
|
234 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsServerEndpoint__1create( |
|
235 JNIEnv* aEnv, |
|
236 jobject) |
|
237 { |
|
238 JELOG2(EJavaComms); |
|
239 CommsServerEndpoint* server = new CommsServerEndpoint(); |
|
240 server->attachToVm(aEnv); |
|
241 return (jint)server; |
|
242 } |
|
243 |
|
244 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsServerEndpoint__1find( |
|
245 JNIEnv* aEnv, |
|
246 jclass, |
|
247 jstring aName) |
|
248 { |
|
249 JELOG2(EJavaComms); |
|
250 std::wstring name = JniUtils::jstringToWstring(aEnv, aName); |
|
251 CommsServerEndpoint* server = CommsContext::getContext().findServer(name); |
|
252 jint rc = 0; |
|
253 if (server) |
|
254 { |
|
255 server->attachToVm(aEnv); |
|
256 rc = (jint)server; |
|
257 } |
|
258 else |
|
259 { |
|
260 WLOG1WSTR(EJavaComms, "Find server endpoint failed, no such endpoint=%s", name); |
|
261 } |
|
262 return rc; |
|
263 } |
|
264 |
|
265 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsServerEndpoint__1start( |
|
266 JNIEnv* aEnv, |
|
267 jobject, |
|
268 jint aHandle, |
|
269 jint aAddress) |
|
270 { |
|
271 JELOG2(EJavaComms); |
|
272 CommsServerEndpoint* server = reinterpret_cast<CommsServerEndpoint*>(aHandle); |
|
273 server->attachToVm(aEnv); |
|
274 jint rc = server->start(aAddress); |
|
275 return rc; |
|
276 } |
|
277 |
|
278 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_comms_CommsServerEndpoint__1stop( |
|
279 JNIEnv*, |
|
280 jobject, |
|
281 jint aHandle) |
|
282 { |
|
283 JELOG2(EJavaComms); |
|
284 CommsServerEndpoint* server = reinterpret_cast<CommsServerEndpoint*>(aHandle); |
|
285 jint rc = server->stop(); |
|
286 return rc; |
|
287 } |
|
288 |