1 /* JORTESubscriptionCreate.c */
4 * This code provides conversion between JAVA a C environments.
5 * The C functions are calling here and results are send to JAVA
6 * native functions. It uses the header pregenerated by JAVA
7 * (by command 'javah -jni class_with_native_function')
9 * @author Lukas Pokorny (lukas_pokorny@centrum.cz)
10 * @author CTU FEE Prague - Department of Control Engineering (dce.felk.cvut.cz)
11 * @author Project ORTE - OCERA Real Time Ethernet (www.ocera.org)
12 * @author dedication to Kj
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
32 // library header file's path
34 // pregenerated header
35 #include "jorte/org_ocera_orte_Subscription.h"
36 // enable TEST_STAGE run level
37 #include "jorte/4all.h"
39 #include "jorte/jorte_typedefs_defines.h"
40 #include "jorte/jorte_protos_api.h"
42 /* ****************************************************************** *
44 * ****************************************************************** */
47 recvCallBack(const ORTERecvInfo *info,void *vinstance, void *recvCallBackParam)
52 jclass cls = 0; // local reference!
59 jmethodID mid_callback = 0;
61 // if the subscriber has been destroyed, return
62 if((*(JORTECallbackContext_t**)recvCallBackParam) == 0)
65 JORTECallbackContext_t *callback_cont = *((JORTECallbackContext_t**)recvCallBackParam);
68 printf("\n\n:c: --------------- recvCallBack called.. --------------- \n");
73 // set local variables from struct
74 if(callback_cont->jvm == 0)
77 printf(":!c: jvm = NULL \n");
81 jvm = callback_cont->jvm;
83 (*jvm)->AttachCurrentThread(jvm,
93 printf(":!c: env = NULL \n");
98 // set byte order only if it differs from that currently set
99 if(info->data_endian != callback_cont->cur_endian) {
101 cls = (*env)->FindClass(env, "java/nio/ByteOrder");
104 printf(":!c: cls = NULL \n");
107 if(info->data_endian == BigEndian) {
108 fid = (*env)->GetStaticFieldID(env,
111 "Ljava/nio/ByteOrder;");
112 callback_cont->cur_endian = BigEndian;
115 fid = (*env)->GetStaticFieldID(env,
118 "Ljava/nio/ByteOrder;");
119 callback_cont->cur_endian = LittleEndian;
123 printf(":!c: fid = NULL \n");
126 obj_bo = (*env)->GetStaticObjectField(env, cls, fid);
129 printf(":!c: cls = NULL \n");
133 // set byte order to ByteBuffer
135 cls = (*env)->GetObjectClass(env, callback_cont->obj_buf);
139 printf(":!c: cls = NULL \n");
142 // get methodID - order(ByteOrder)
143 mid = (*env)->GetMethodID(env,
146 "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;");
150 printf(":!c: mid = NULL \n");
155 if((*env)->CallObjectMethod(env,callback_cont->obj_buf,mid,obj_bo) == 0)
158 printf(":!c: set byte order failed.. \n");
163 if(callback_cont->obj == 0)
166 printf(":!c: obj = NULL \n");
171 rinfo = callback_cont->rinfo;
172 obj_msg = callback_cont->msg;
177 printf(":c: env = %#"PRIxPTR", obj_msg = %#"PRIxPTR" \n", (intptr_t)env, (intptr_t)obj_msg);
185 cls = findClass(env, "org.ocera.orte.types.RecvInfo");
189 printf(":!c: cls = NULL \n");
193 // call object constructor
194 mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
198 printf(":!c: constructor failed! \n");
203 rinfo = (*env)->NewObject(env, cls, mid);
207 printf(":!c: rinfo = NULL \n");
211 // create global reference
212 callback_cont->rinfo = (*env)->NewGlobalRef(env, rinfo);
213 if (callback_cont->rinfo == 0)
216 printf(":!c: callback_cont->rinfo = NULL \n");
221 ////////////////////////////////////////////////////
222 // set RecvInfo instance
223 if(setRecvInfo(env,info,callback_cont->rinfo) == 0)
226 printf(":!c: setRecvInfo() failed! \n");
230 ////////////////////////////////////////////////////
231 // control print - only in TEST_STAGE
233 printf(":c: rinfo created :] \n");
234 printf(":c:----- ORTERecvInfo members ------ \n");
235 printf(":c: recvStatus: %#x \n", info->status);
236 printf(":c: senderGuid: hid = %#"PRIx32", aid = %#"PRIx32", oid = %#"PRIx32" \n",
237 info->senderGUID.hid,info->senderGUID.aid,info->senderGUID.oid);
238 printf(":c: topic: %s \n",info->topic);
239 printf(":c: type: %s \n",info->type);
240 printf(":c: localTimeRecv: sec = %"PRId32", fract = %"PRIu32" \n",
241 info->localTimeReceived.seconds,info->localTimeReceived.fraction);
242 printf(":c: remoteTimePub: sec = %"PRId32", fract = %"PRIu32" \n",
243 info->remoteTimePublished.seconds,info->remoteTimePublished.fraction);
244 printf(":c: seqNr: high = %"PRId32", low = %"PRIu32" \n",info->sn.high,info->sn.low);
245 printf(":c:---------------------------------- \n");
247 ////////////////////////////////////////////////////
248 // update MessageData instance
250 cls_msg = (*env)->GetObjectClass(env, obj_msg);
254 printf(":!c: cls_msg = NULL \n");
258 /////////////////////////////////////////////////////
260 mid = (*env)->GetMethodID(env,
267 printf(":!c: mid = NULL \n");
272 (*env)->CallVoidMethod(env,
276 /* *************************** *
277 * call JAVA CallBack method *
278 * *************************** */
280 printf(":c: call JAVA CallBack method \n");
285 cls = (*env)->GetObjectClass(env,callback_cont->obj);
289 printf(":!c: cls = NULL \n");
294 mid = (*env)->GetMethodID(env,
297 "(Lorg/ocera/orte/types/RecvInfo;Lorg/ocera/orte/types/MessageData;)V");
301 printf(":!c: cls = NULL \n");
308 printf(":c: volam callback metodu.. halo jsi tam?? \n");
310 // call object's method
311 (*env)->CallVoidMethod(env,
312 callback_cont->obj, /*obj*/
314 callback_cont->rinfo,
318 // detach current thread
319 if((*jvm)->DetachCurrentThread(jvm) != 0)
320 printf(":c!: DetachCurrentThread failed! \n");
323 printf(":c: ------------ thats all from recvCallBack ------------ \n\n");
328 /* ****************************************************************** *
330 * ****************************************************************** */
331 JNIEXPORT jlong JNICALL
332 Java_org_ocera_orte_Subscription_jORTESubscriptionCreate
335 jlong dhandle, // appDomain handle
336 jint jsmode, // subs mode
337 jint jstype, // subs type
338 jstring jtopic, // subs topic
339 jstring jtname, // subs typeName
340 jobject jinstance, // direct ByteBuffer
341 jint jbyteOrder,// byte order of ByteBuffer
342 jobject obj_msg, // messageData instance
344 jobject jminSeparation,
345 jobject obj_callback,
353 ORTESubscription *s = 0;
355 SubscriptionMode smode;
356 SubscriptionType stype;
358 NtpTime minSeparation;
360 JORTECallbackContext_t *callback_cont;
361 JORTECallbackContext_t **callback_cont_ptr;
363 // standart variables
364 const char *topic = 0;
365 const char *typename = 0;
369 // don't forget use free() funct.!!
370 callback_cont = (JORTECallbackContext_t*)malloc(sizeof(JORTECallbackContext_t));
371 callback_cont_ptr = (JORTECallbackContext_t**)malloc(sizeof(JORTECallbackContext_t*));
372 *callback_cont_ptr = callback_cont;
377 // get direct ByteBuffer pointer from Java
378 buffer = (*env)->GetDirectBufferAddress(env, jinstance);
379 // check obj_callback
380 if (obj_callback == 0)
383 printf(":!c: obj_callback = NULL \n");
388 jint b = (*env)->GetJavaVM(env,&jvm);
392 printf(":!c: getJavaVM() failed! \n");
399 printf(":c: getJavaVM succesfull.. \n");
402 callback_cont->jvm = jvm;
403 callback_cont->cur_endian = (CDR_Endianness) jbyteOrder;
404 // create global references
405 callback_cont->obj = (*env)->NewGlobalRef(env, obj_callback);
407 if (callback_cont->obj == 0)
410 printf(":c: global reference not created! \n");
414 // create global references
415 callback_cont->obj_buf = (*env)->NewGlobalRef(env, jinstance);
417 if (callback_cont->obj_buf == 0)
420 printf(":c: global reference not created! \n");
424 // create global references
425 callback_cont->msg = (*env)->NewGlobalRef(env, obj_msg);
427 if (callback_cont->msg == 0)
430 printf(":c: global reference not created! \n");
434 // init RecvInfo pointer
435 callback_cont->rinfo = 0;
437 cls = (*env)->GetObjectClass(env, obj);
441 printf(":!c: cls = NULL \n");
445 // fieldID - callbackContextHandle
446 fid = (*env)->GetFieldID(env,
448 "callbackContextHandle",
453 printf(":!c: fid = NULL \n");
457 (*env)->SetLongField(env,
460 (jlong) callback_cont_ptr);
462 printf(":c: ORTESubscriptionCreate() calling..\n");
465 d = (ORTEDomain *) dhandle;
469 printf(":!c: d = NULL [bad domain handle] \n");
474 smode = (SubscriptionMode) jsmode;
475 stype = (SubscriptionType) jstype;
476 topic = (*env)->GetStringUTFChars(env, jtopic, 0);
477 typename = (*env)->GetStringUTFChars(env, jtname, 0);
478 deadline = getNtpTime(env, jdeadline);//
479 minSeparation = getNtpTime(env, jminSeparation);//
480 // call ORTE function
481 s = ORTESubscriptionCreate(d,
490 (void*)callback_cont_ptr,
491 (uint32_t) j_multicastIP);
495 printf(":!c: s = NULL [subscription not created] \n");
505 (*env)->ReleaseStringUTFChars(env, jtopic, topic);
506 (*env)->ReleaseStringUTFChars(env, jtname, typename);
507 // returns handle of new created Subscription
508 if(flag_ok == 0) return 0;